core/database: Change db_insert() to insert-by-key

This means that databases completely manage when memory is allocated.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-08 13:09:10 -05:00
parent bd1de2262d
commit 746c55ac56
4 changed files with 22 additions and 11 deletions

View File

@ -128,8 +128,13 @@ void db_load(struct database *db)
file_close(&db->db_file);
}
struct db_entry *db_insert(struct database *db, struct db_entry *item)
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);
if (item) {
g_ptr_array_add(db->db_entries, item);
__dbe_setup(db, db_actual_size(db) - 1);
@ -184,6 +189,5 @@ struct db_entry *db_find(struct database *db, const gchar *key)
struct db_entry *dbe = db_get(db, key);
if (dbe)
return dbe;
dbe = db->db_ops->dbe_alloc(key);
return db_insert(db, dbe);
return db_insert(db, key);
}

View File

@ -179,7 +179,7 @@ struct track *track_add(struct library *library, const std::string &filepath)
struct track *track = NULL;
if (!db_get(&track_db, key))
track = TRACK(db_insert(&track_db, track_alloc(key)));
track = TRACK(db_insert(&track_db, key));
g_free(key);
return track;

View File

@ -114,7 +114,7 @@ unsigned int db_actual_size(const struct database *);
* Called to add a new item to the database. The caller MUST check if the
* database already contains a similar item before calling this function.
*/
struct db_entry *db_insert(struct database *, struct db_entry *);
struct db_entry *db_insert(struct database *, const gchar *);
/* Called to remove an item from the database. */
void db_remove(struct database *, struct db_entry *);

View File

@ -29,7 +29,7 @@ static struct int_entry *__int_alloc(unsigned int val)
static struct db_entry *int_alloc(const gchar *key)
{
unsigned int val;
sscanf(key, "%u",&val);
sscanf(key, "%u", &val);
return &__int_alloc(val)->ie_dbe;
}
@ -140,7 +140,7 @@ static void test_stress(unsigned int N)
/* db_insert() */
for (i = 0; i < N; i++) {
key = g_strdup_printf("%u", i);
dbe = db_insert(&db, &__int_alloc(i)->ie_dbe);
dbe = db_insert(&db, key);
test_loop_not_equal((void *)dbe, NULL, i);
test_loop_equal(dbe->dbe_index, i, i);
test_loop_equal(dbe->dbe_key, key, i);
@ -242,10 +242,14 @@ static void test_save_load()
struct db_entry *dbe, *next;
const unsigned int N = 10;
unsigned int i;
gchar *key;
/* 10 items should "autosave" when inserted */
for (i = 0; i < N; i++)
db_insert(&db1, &__int_alloc(i)->ie_dbe);
for (i = 0; i < N; i++) {
key = g_strdup_printf("%u", i);
db_insert(&db1, key);
g_free(key);
}
i = 0;
db_load(&db2);
@ -276,8 +280,11 @@ static void test_save_load()
/* Manually turn autosave off. */
db1.db_autosave = false;
for (i = N; i < (2 * N); i++)
db_insert(&db1, &__int_alloc(i)->ie_dbe);
for (i = N; i < (2 * N); i++) {
key = g_strdup_printf("%u", i);
db_insert(&db1, key);
g_free(key);
}
for (i = N; i < (2 * N); i += 2)
db_remove(&db1, db_at(&db1, i));