core/database: Pass database index when allocating dbes

This is useful for user playlists so they can store their playlist-id
directly, letting us remove the entire playlist_get_id() function.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-12 15:27:02 -04:00
parent 9b4a1785a1
commit 58cd43c330
14 changed files with 42 additions and 42 deletions

View File

@ -40,7 +40,7 @@ static struct db_entry *__dbe_read(struct database *db, unsigned int index)
file_readf(&db->db_file, "%d", &valid);
if (valid)
dbe = db->db_ops->dbe_read(&db->db_file);
dbe = db->db_ops->dbe_read(&db->db_file, index);
g_ptr_array_index(db->db_entries, index) = dbe;
return dbe;
@ -137,7 +137,7 @@ struct db_entry *db_insert(struct database *db, const gchar *key)
struct db_entry *item = NULL;
if (key)
item = db->db_ops->dbe_alloc(key);
item = db->db_ops->dbe_alloc(key, db_actual_size(db));
if (item) {
g_ptr_array_add(db->db_entries, item);

View File

@ -18,7 +18,7 @@ static struct user_playlist *__user_db_alloc(gchar *name)
return playlist;
}
static struct db_entry *user_db_alloc(const gchar *name)
static struct db_entry *user_db_alloc(const gchar *name, unsigned int index)
{
return &__user_db_alloc(g_strdup(name))->pl_dbe;
}
@ -35,7 +35,7 @@ static gchar *user_db_key(struct db_entry *dbe)
return g_strdup(USER_PLAYLIST(dbe)->pl_playlist.pl_name);
}
static struct db_entry *user_db_read(struct file *file)
static struct db_entry *user_db_read(struct file *file, unsigned int index)
{
gchar *name = file_readl(file);
struct user_playlist *playlist = __user_db_alloc(name);

View File

@ -203,7 +203,7 @@ static struct db_entry *__album_alloc_v0(const gchar *key)
return &__album_alloc(NULL, NULL, name, year)->al_dbe;
}
static struct db_entry *album_alloc(const gchar *key)
static struct db_entry *album_alloc(const gchar *key, unsigned int index)
{
unsigned int artist_id, genre_id, year, n;
gchar *name;
@ -241,7 +241,7 @@ static struct album *__album_parse_v0(gchar *line)
return __album_alloc(NULL, NULL, name, year);
}
static struct db_entry *album_read(struct file *file)
static struct db_entry *album_read(struct file *file, unsigned int index)
{
unsigned int year, artist_id, genre_id, n;
struct album *album;

View File

@ -20,7 +20,7 @@ static struct artist *__artist_alloc(gchar *name)
}
static struct db_entry *artist_alloc(const gchar *name)
static struct db_entry *artist_alloc(const gchar *name, unsigned int index)
{
return &__artist_alloc(g_strdup(name))->ar_dbe;
}
@ -37,7 +37,7 @@ static gchar *artist_key(struct db_entry *dbe)
return ARTIST(dbe)->ar_name;
}
struct db_entry *artist_read(struct file *file)
struct db_entry *artist_read(struct file *file, unsigned int index)
{
return &__artist_alloc(file_readl(file))->ar_dbe;
}

View File

@ -18,7 +18,7 @@ static struct genre *__genre_alloc(gchar *name)
return genre;
}
static struct db_entry *genre_alloc(const gchar *name)
static struct db_entry *genre_alloc(const gchar *name, unsigned int index)
{
return &__genre_alloc(g_strdup(name))->ge_dbe;
}
@ -35,7 +35,7 @@ static gchar *genre_key(struct db_entry *dbe)
return GENRE(dbe)->ge_name;
}
static struct db_entry *genre_read(struct file *file)
static struct db_entry *genre_read(struct file *file, unsigned int index)
{
return &__genre_alloc(file_readl(file))->ge_dbe;
}

View File

@ -17,7 +17,7 @@ static struct library *__library_alloc(gchar *path)
return library;
}
static struct db_entry *library_alloc(const gchar *path)
static struct db_entry *library_alloc(const gchar *path, unsigned int index)
{
return &__library_alloc(g_strdup(path))->li_dbe;
}
@ -32,7 +32,7 @@ static gchar *library_key(struct db_entry *dbe)
return LIBRARY(dbe)->li_path;
}
static struct db_entry *library_read(struct file *file)
static struct db_entry *library_read(struct file *file, unsigned int index)
{
int enabled;
gchar *path;

View File

@ -42,7 +42,7 @@ static struct track *__track_alloc()
}
struct db_entry *track_alloc(const gchar *key)
struct db_entry *track_alloc(const gchar *key, unsigned int index)
{
const TagLib_AudioProperties *audio;
struct library *library;
@ -132,7 +132,7 @@ static void track_read_v0(struct file *file, struct track *track)
track->tr_album = album;
}
static struct db_entry *track_read(struct file *file)
static struct db_entry *track_read(struct file *file, unsigned int index)
{
struct track *track = __track_alloc();
unsigned int library_id, album_id;

View File

@ -48,7 +48,7 @@ static inline void *DBE_DATA(struct db_entry *dbe)
struct db_ops {
/* Allocate a new struct db_entry from a given key. */
struct db_entry *(*dbe_alloc)(const gchar *);
struct db_entry *(*dbe_alloc)(const gchar *, unsigned int);
/* Free a struct db_entry. */
void (*dbe_free)(struct db_entry *);
@ -57,7 +57,7 @@ struct db_ops {
gchar *(*dbe_key)(struct db_entry *);
/* Read a single struct db_entry from disk. */
struct db_entry *(*dbe_read)(struct file *);
struct db_entry *(*dbe_read)(struct file *, unsigned int);
/* Write a single struct db_entry to disk. */
void (*dbe_write)(struct file *, struct db_entry *);

View File

@ -26,7 +26,7 @@ static struct int_entry *__int_alloc(unsigned int val)
return ent;
}
static struct db_entry *int_alloc(const gchar *key)
static struct db_entry *int_alloc(const gchar *key, unsigned int index)
{
unsigned int val;
sscanf(key, "%u", &val);
@ -44,7 +44,7 @@ static gchar *int_key(struct db_entry *dbe)
return g_strdup_printf("%u", INT_ENTRY(dbe)->ie_val);
}
static struct db_entry *int_read(struct file *f)
static struct db_entry *int_read(struct file *f, unsigned int index)
{
unsigned int val;
file_readf(f, "%u", &val);
@ -70,7 +70,7 @@ static void test_db_entry()
struct int_entry *ent;
struct file f;
ent = INT_ENTRY(int_ops.dbe_alloc("1"));
ent = INT_ENTRY(int_ops.dbe_alloc("1", 0));
g_assert_cmpuint(ent->ie_dbe.dbe_index, ==, 0);
g_assert(ent->ie_dbe.dbe_data == ent);
g_assert_cmpuint(ent->ie_val, ==, 1);
@ -85,7 +85,7 @@ static void test_db_entry()
g_assert_cmpuint(test_free_count, ==, 1);
file_open(&f, OPEN_READ);
ent = INT_ENTRY(int_ops.dbe_read(&f));
ent = INT_ENTRY(int_ops.dbe_read(&f, 0));
file_close(&f);
g_assert(ent->ie_dbe.dbe_data == ent);

View File

@ -52,7 +52,7 @@ static void test_album()
koji = artist_find("Koji Kondo");
genre = genre_find("Video Game Music");
album = ALBUM(album_ops->dbe_alloc("0/0/1998/Hyrule Symphony"));
album = ALBUM(album_ops->dbe_alloc("0/0/1998/Hyrule Symphony", 0));
test_verify_hyrule(album, koji, genre);
g_assert_true( album_match_token(album, "hyrule"));
g_assert_true( album_match_token(album, "symphony"));
@ -66,11 +66,11 @@ static void test_album()
album_ops->dbe_free(&album->al_dbe);
file_open(&f, OPEN_READ);
album = ALBUM(album_ops->dbe_read(&f));
album = ALBUM(album_ops->dbe_read(&f, 0));
test_verify_empty(album);
album_ops->dbe_free(&album->al_dbe);
album = ALBUM(album_ops->dbe_read(&f));
album = ALBUM(album_ops->dbe_read(&f, 0));
file_close(&f);
test_verify_hyrule(album, koji, genre);
album_ops->dbe_free(&album->al_dbe);
@ -81,8 +81,8 @@ static void test_album_compare()
const struct db_ops *album_ops = test_album_ops();
struct album *twilight, *skyward;
twilight = ALBUM(album_ops->dbe_alloc("2006/Twilight Princess"));
skyward = ALBUM(album_ops->dbe_alloc("2011/skyward sword"));
twilight = ALBUM(album_ops->dbe_alloc("2006/Twilight Princess", 0));
skyward = ALBUM(album_ops->dbe_alloc("2011/skyward sword", 0));
g_assert_cmpint(album_compare(twilight, twilight), ==, 0);
g_assert_cmpint(album_compare(twilight, skyward), ==, 1);

View File

@ -33,7 +33,7 @@ static void test_artist()
unsigned int i;
struct file f;
artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo"));
artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0));
test_verify_koji(artist);
g_assert_true( artist_match_token(artist, "koji"));
@ -50,13 +50,13 @@ static void test_artist()
file_open(&f, OPEN_READ);
file_readf(&f, "%u", &i);
artist = ARTIST(artist_ops->dbe_read(&f));
artist = ARTIST(artist_ops->dbe_read(&f, 0));
test_verify_empty(artist);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);
file_readf(&f, "%u", &i);
artist = ARTIST(artist_ops->dbe_read(&f));
artist = ARTIST(artist_ops->dbe_read(&f, 0));
file_close(&f);
test_verify_koji(artist);
g_free(artist->ar_name);
@ -68,8 +68,8 @@ static void test_artist_compare()
const struct db_ops *artist_ops = test_artist_ops();
struct artist *koji, *hajime;
koji = ARTIST(artist_ops->dbe_alloc("Koji Kondo"));
hajime = ARTIST(artist_ops->dbe_alloc("hajime wakai"));
koji = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0));
hajime = ARTIST(artist_ops->dbe_alloc("hajime wakai", 0));
g_assert_cmpint(artist_compare(koji, koji), ==, 0);
g_assert_cmpint(artist_compare(koji, hajime), ==, 1);

View File

@ -32,7 +32,7 @@ static void test_genre()
unsigned int i;
struct file f;
genre = GENRE(genre_ops->dbe_alloc("Video Game Music"));
genre = GENRE(genre_ops->dbe_alloc("Video Game Music", 0));
test_verify_vg(genre);
g_assert_true( genre_match_token(genre, "video"));
g_assert_true( genre_match_token(genre, "music"));
@ -48,13 +48,13 @@ static void test_genre()
file_open(&f, OPEN_READ);
file_readf(&f, "%u", &i);
genre = GENRE(genre_ops->dbe_read(&f));
genre = GENRE(genre_ops->dbe_read(&f, 0));
test_verify_empty(genre);
g_free(genre->ge_name);
genre_ops->dbe_free(&genre->ge_dbe);
file_readf(&f, "%u", &i);
genre = GENRE(genre_ops->dbe_read(&f));
genre = GENRE(genre_ops->dbe_read(&f, 0));
file_close(&f);
test_verify_vg(genre);
g_free(genre->ge_name);
@ -66,8 +66,8 @@ static void test_genre_compare()
const struct db_ops *genre_ops = test_genre_ops();
struct genre *video, *game;
video = GENRE(genre_ops->dbe_alloc("Video Game Music"));
game = GENRE(genre_ops->dbe_alloc("game music"));
video = GENRE(genre_ops->dbe_alloc("Video Game Music", 0));
game = GENRE(genre_ops->dbe_alloc("game music", 0));
g_assert_cmpint(genre_compare(video, video), ==, 0);
g_assert_cmpint(genre_compare(video, game), ==, 1);

View File

@ -24,8 +24,8 @@ static void test_library()
struct library *link, *zelda, *library;
struct file f;
link = LIBRARY(library_ops->dbe_alloc("/home/Link/Music"));
zelda = LIBRARY(library_ops->dbe_alloc("/home/Zelda/Music"));
link = LIBRARY(library_ops->dbe_alloc("/home/Link/Music", 0));
zelda = LIBRARY(library_ops->dbe_alloc("/home/Zelda/Music", 0));
test_verify_link(link);
test_verify_zelda(zelda);
@ -38,14 +38,14 @@ static void test_library()
file_close(&f);
file_open(&f, OPEN_READ);
library = LIBRARY(library_ops->dbe_read(&f));
library = LIBRARY(library_ops->dbe_read(&f, 0));
test_verify_link(library);
g_assert_cmpstr_free(library_file(library, "navi.mp3"), ==,
"/home/Link/Music/navi.mp3");
g_free(library->li_path);
library_ops->dbe_free(&library->li_dbe);
library = LIBRARY(library_ops->dbe_read(&f));
library = LIBRARY(library_ops->dbe_read(&f, 0));
file_close(&f);
test_verify_zelda(library);
g_assert_cmpstr_free(library_file(library, "impa.ogg"), ==,

View File

@ -12,7 +12,7 @@
static struct track *test_alloc(const gchar *key)
{
const struct db_ops *track_ops = test_track_ops();
struct db_entry *dbe = track_ops->dbe_alloc(key);
struct db_entry *dbe = track_ops->dbe_alloc(key, 0);
g_assert_nonnull(dbe);
return TRACK(dbe);
}
@ -98,14 +98,14 @@ static void test_track()
track_ops->dbe_free(&track->tr_dbe);
file_open(&f, OPEN_READ);
track = TRACK(track_ops->dbe_read(&f));
track = TRACK(track_ops->dbe_read(&f, 0));
track->tr_dbe.dbe_index = 0;
test_verify_notrack(track);
g_free(track->tr_path);
track_ops->dbe_free(&track->tr_dbe);
track = TRACK(track_ops->dbe_read(&f));
track = TRACK(track_ops->dbe_read(&f, 0));
track->tr_dbe.dbe_index = 0;
test_verify_track(track);
file_close(&f);