core/containers/database: Add a function for loading databases when idle
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
2ab0c6957b
commit
0bcfa06bfb
|
@ -2,6 +2,7 @@
|
||||||
* Copyright 2013 (c) Anna Schumaker.
|
* Copyright 2013 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
#include <core/containers/database.h>
|
#include <core/containers/database.h>
|
||||||
|
#include <core/idle.h>
|
||||||
|
|
||||||
#define DB_ENTRY_AT(db, index) \
|
#define DB_ENTRY_AT(db, index) \
|
||||||
(struct db_entry *)g_ptr_array_index(db->db_entries, 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);
|
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 *db_insert(struct database *db, const gchar *key)
|
||||||
{
|
{
|
||||||
struct db_entry *item = NULL;
|
struct db_entry *item = NULL;
|
||||||
|
|
|
@ -106,6 +106,9 @@ void db_autosave(struct database *);
|
||||||
/* Called to read the database from disk. */
|
/* Called to read the database from disk. */
|
||||||
void db_load(struct database *);
|
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. */
|
/* Returns the size of the backing std::vector. */
|
||||||
unsigned int db_actual_size(const struct database *);
|
unsigned int db_actual_size(const struct database *);
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
* Test a Database
|
* Test a Database
|
||||||
*/
|
*/
|
||||||
#include <core/containers/database.h>
|
#include <core/containers/database.h>
|
||||||
|
#include <core/idle.h>
|
||||||
#include <core/string.h>
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
|
||||||
|
@ -251,6 +252,7 @@ static void test_save_load()
|
||||||
g_free(key);
|
g_free(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Load using the standard db_load() function */
|
||||||
i = 0;
|
i = 0;
|
||||||
db_load(&db2);
|
db_load(&db2);
|
||||||
test_equal(db2.db_size, N);
|
test_equal(db2.db_size, N);
|
||||||
|
@ -264,6 +266,7 @@ static void test_save_load()
|
||||||
for (i = 0; i < N; i += 2)
|
for (i = 0; i < N; i += 2)
|
||||||
db_remove(&db1, db_at(&db1, i));
|
db_remove(&db1, db_at(&db1, i));
|
||||||
|
|
||||||
|
/* Use db_load() again */
|
||||||
db_deinit(&db2);
|
db_deinit(&db2);
|
||||||
db2.db_entries = g_ptr_array_new();
|
db2.db_entries = g_ptr_array_new();
|
||||||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
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)
|
for (i = N; i < (2 * N); i += 2)
|
||||||
db_remove(&db1, db_at(&db1, i));
|
db_remove(&db1, db_at(&db1, i));
|
||||||
|
|
||||||
|
/* Use db_load_idle() this time */
|
||||||
db_deinit(&db2);
|
db_deinit(&db2);
|
||||||
db2.db_entries = g_ptr_array_new();
|
db2.db_entries = g_ptr_array_new();
|
||||||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
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);
|
test_equal(db2.db_size, N / 2);
|
||||||
|
|
||||||
|
/* Use db_load_idle() again */
|
||||||
db_save(&db1);
|
db_save(&db1);
|
||||||
db_deinit(&db2);
|
db_deinit(&db2);
|
||||||
db2.db_entries = g_ptr_array_new();
|
db2.db_entries = g_ptr_array_new();
|
||||||
db2.db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
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(db2.db_size, N);
|
||||||
test_equal(db_actual_size(&db2), 2 * N);
|
test_equal(db_actual_size(&db2), 2 * N);
|
||||||
|
|
||||||
|
i = 1;
|
||||||
db_for_each(dbe, next, &db2) {
|
db_for_each(dbe, next, &db2) {
|
||||||
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
test_loop_equal(INT_ENTRY(dbe)->ie_val, i, i);
|
||||||
i += 2;
|
i += 2;
|
||||||
|
|
Loading…
Reference in New Issue