core/containers/database: Add a function for loading databases when idle

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-02-26 09:35:28 -05:00
parent 2ab0c6957b
commit 0bcfa06bfb
3 changed files with 23 additions and 3 deletions

View File

@ -2,6 +2,7 @@
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/containers/database.h>
#include <core/idle.h>
#define DB_ENTRY_AT(db, index) \
(struct db_entry *)g_ptr_array_index(db->db_entries, index)
@ -128,6 +129,11 @@ void db_load(struct database *db)
file_close(&db->db_file);
}
void db_load_idle(struct database *db)
{
idle_schedule((void (*)(void *))db_load, db);
}
struct db_entry *db_insert(struct database *db, const gchar *key)
{
struct db_entry *item = NULL;

View File

@ -106,6 +106,9 @@ void db_autosave(struct database *);
/* Called to read the database from disk. */
void db_load(struct database *);
/* Called to read the database from disk, but only when idle. */
void db_load_idle(struct database *);
/* Returns the size of the backing std::vector. */
unsigned int db_actual_size(const struct database *);

View File

@ -3,6 +3,7 @@
* Test a Database
*/
#include <core/containers/database.h>
#include <core/idle.h>
#include <core/string.h>
#include <tests/test.h>
@ -251,6 +252,7 @@ static void test_save_load()
g_free(key);
}
/* Load using the standard db_load() function */
i = 0;
db_load(&db2);
test_equal(db2.db_size, N);
@ -264,6 +266,7 @@ static void test_save_load()
for (i = 0; i < N; i += 2)
db_remove(&db1, db_at(&db1, i));
/* Use db_load() again */
db_deinit(&db2);
db2.db_entries = g_ptr_array_new();
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
@ -288,21 +291,29 @@ static void test_save_load()
for (i = N; i < (2 * N); i += 2)
db_remove(&db1, db_at(&db1, i));
/* Use db_load_idle() this time */
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);
db_load_idle(&db2);
test_equal(db2.db_size, 0);
while (idle_run_task()) {};
test_equal(db2.db_size, N / 2);
/* Use db_load_idle() again */
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;
db_load_idle(&db2);
test_equal(db2.db_size, 0);
while (idle_run_task()) {};
test_equal(db2.db_size, N);
test_equal(db_actual_size(&db2), 2 * N);
i = 1;
db_for_each(dbe, next, &db2) {
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
i += 2;