core/database: Add a db_rekey() function

Sometimes database entry keys change, so this gives us a way to update
them in the database keys hash table.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-26 14:57:36 -04:00
parent cf4eedb592
commit 811509ff80
3 changed files with 28 additions and 0 deletions

View File

@ -182,6 +182,18 @@ bool db_defrag(struct database *db)
return true;
}
void db_rekey(struct database *db, struct db_entry *dbe)
{
if (dbe == NULL)
return;
g_hash_table_remove(db->db_keys, dbe->dbe_key);
g_free(dbe->dbe_key);
dbe->dbe_key = db->db_ops->dbe_key(dbe);
g_hash_table_insert(db->db_keys, dbe->dbe_key, dbe);
}
unsigned int db_actual_size(const struct database *db)
{
if (db->db_entries)

View File

@ -124,6 +124,9 @@ void db_remove(struct database *, struct db_entry *);
*/
bool db_defrag(struct database *);
/* Called to change the key of a database entry. */
void db_rekey(struct database *, struct db_entry *);
/* Returns the database item at the requested index. */
struct db_entry *db_at(const struct database *, unsigned int);

View File

@ -221,6 +221,19 @@ static void test_database(gconstpointer arg)
}
g_assert_false(db_defrag(&db));
/* db_rekey() */
i = 0;
db_for_each(dbe, next, &db) {
INT_ENTRY(dbe)->ie_val = i;
db_rekey(&db, dbe);
key = g_strdup_printf("%u", i);
g_assert_cmpstr(dbe->dbe_key, ==, key);
g_assert_true(db_get(&db, key) == dbe);
g_free(key);
i++;
}
db_deinit(&db);
g_assert_cmpuint(db.db_size, ==, 0);
g_assert_cmpuint(db_actual_size(&db), ==, 0);