core/database: Replace std::map with a glib hash table

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-05 14:19:33 -05:00
parent 9dd9d7d8f7
commit 4a4529b158
7 changed files with 15 additions and 11 deletions

View File

@ -17,7 +17,7 @@ db_entry :: ~db_entry() {}
static void __dbe_free(struct database *db, struct db_entry *dbe)
{
if (dbe) {
db->db_keys.erase(dbe->dbe_key);
g_hash_table_remove(db->db_keys, dbe->dbe_key);
g_free(dbe->dbe_key);
g_ptr_array_index(db->db_entries, dbe->dbe_index) = NULL;
@ -59,7 +59,7 @@ static void __dbe_setup(struct database *db, unsigned int index)
if (dbe) {
dbe->dbe_index = index;
dbe->dbe_key = db->db_ops->dbe_key(dbe);
db->db_keys[dbe->dbe_key] = index;
g_hash_table_insert(db->db_keys, dbe->dbe_key, dbe);
db->db_size++;
if (db->db_ops->dbe_setup)
db->db_ops->dbe_setup(dbe);
@ -83,6 +83,7 @@ void db_init(struct database *db, const char *filepath, bool autosave,
db->db_size = 0;
db->db_autosave = autosave;
db->db_entries = g_ptr_array_new();
db->db_keys = g_hash_table_new(g_str_hash, g_str_equal);
file_init(&db->db_file, filepath, 0);
}
@ -94,7 +95,9 @@ void db_deinit(struct database *db)
__dbe_free(db, dbe);
g_ptr_array_free(db->db_entries, true);
g_hash_table_destroy(db->db_keys);
db->db_entries = NULL;
db->db_keys = NULL;
}
void db_save(struct database *db)
@ -180,11 +183,7 @@ struct db_entry *db_at(struct database *db, unsigned int index)
struct db_entry *db_get(struct database *db, const gchar *key)
{
std::map<const std::string, unsigned int>::iterator it;
it = db->db_keys.find(key);
if (it == db->db_keys.end())
return NULL;
return DB_ENTRY_AT(db, it->second);
return (struct db_entry *)g_hash_table_lookup(db->db_keys, key);
}
struct db_entry *db_find(struct database *db, const gchar *key)

View File

@ -8,8 +8,6 @@ extern "C" {
#include <core/file.h>
}
#include <map>
/**
* The DatabaseEntry class is the base class for storing
* generic data inside a Database.
@ -67,9 +65,9 @@ struct database {
bool db_autosave; /* True if the database saves when changed. */
struct file db_file; /* The database's associated file object. */
GPtrArray *db_entries; /* The database's backing array. */
GHashTable *db_keys; /* The database's mapping of key -> value. */
const struct db_ops *db_ops; /* The database's operations vector. */
std::map<const std::string, unsigned int> db_keys;
};

View File

@ -5,6 +5,7 @@
#define OCARINA_CORE_TAGS_ALBUM_H
#include <core/database.h>
#include <string>
/**
* The Album tag is used to store the name and year of albums

View File

@ -5,6 +5,7 @@
#define OCARINA_CORE_TAGS_ARTIST_H
#include <core/database.h>
#include <string>
/**
* The Artist tag is used to store the name of artists added

View File

@ -5,6 +5,7 @@
#define OCARINA_CORE_TAGS_GENRE_H
#include <core/database.h>
#include <string>
/**
* The Genre tag is used to store the name of genres added

View File

@ -5,6 +5,7 @@
#define OCARINA_CORE_TAGS_LIBRARY_H
#include <core/database.h>
#include <string>
/**
* The Library tag is used to store a single directory added

View File

@ -108,7 +108,7 @@ static void test_init()
/* Check initial sizes. */
test_equal(db.db_entries->len, 0);
test_equal(db.db_keys.size(), 0);
test_equal(g_hash_table_size(db.db_keys), 0);
test_equal(db.db_size, 0);
test_equal(db.db_autosave, false);
test_equal(db.db_file.f_version, 0);
@ -256,6 +256,7 @@ static void test_save_load()
db_deinit(&db2);
db2.db_entries = g_ptr_array_new();
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
test_setup_count = 0;
db_load(&db2);
test_equal(db2.db_size, N / 2);
@ -276,12 +277,14 @@ static void test_save_load()
db_deinit(&db2);
db2.db_entries = g_ptr_array_new();
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
db_load(&db2);
test_equal(db2.db_size, N / 2);
db_save(&db1);
db_deinit(&db2);
db2.db_entries = g_ptr_array_new();
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
db_load(&db2);
i = 1;