core/database: Move db_{save|autosave}() out of the database struct

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-18 10:11:24 -04:00
parent 8486fc6111
commit 68c953f186
7 changed files with 29 additions and 33 deletions

View File

@ -80,7 +80,7 @@ void Index :: insert(const std::string &key, unsigned int value)
it = database :: insert(IndexEntry(key));
it->insert(value);
autosave();
::db_autosave(this);
}
void Index :: remove(const std::string &key, unsigned int value)
@ -91,5 +91,5 @@ void Index :: remove(const std::string &key, unsigned int value)
return;
it->remove(value);
autosave();
::db_autosave(this);
}

View File

@ -48,7 +48,7 @@ const bool Library :: enabled()
void Library :: set_enabled(bool enabled)
{
_enabled = enabled;
library_db.save();
db_save(&library_db);
}
const unsigned int Library :: size()

View File

@ -181,5 +181,5 @@ unsigned int tags :: track_size()
void tags :: commit_track_db()
{
track_db.save();
db_save(&track_db);
}

View File

@ -107,18 +107,6 @@ struct database {
*/
~database();
/**
* Called to save the Database to disk.
*/
void save();
/**
* Called to save the Database to disk ONLY if Database::_autosave
* was set to True.
*/
void autosave();
/**
* Called to add a new item to the Database. Upon successful insertion,
@ -185,6 +173,14 @@ struct database {
};
/* Called to write the database to disk. */
template <class T>
void db_save(struct database<T> *);
/* Save the database to disk iff database->db_autosave is set to True */
template <class T>
void db_autosave(struct database<T> *);
/* Called to read the database from disk. */
template <class T>
void db_load(struct database<T> *);

View File

@ -23,30 +23,30 @@ database<T> :: ~database()
}
template <class T>
void database<T> :: save()
void db_save(struct database<T> *db)
{
if (file_open(&db_file, OPEN_WRITE) == false)
if (file_open(&db->db_file, OPEN_WRITE) == false)
return;
file_writef(&db_file, "%u\n", db_actual_size(this));
for (unsigned int i = 0; i < db_entries.size(); i++) {
if (db_entries[i] == NULL)
file_writef(&db_file, "%d\n", false);
file_writef(&db->db_file, "%u\n", db_actual_size(db));
for (unsigned int i = 0; i < db_actual_size(db); i++) {
if (db->db_entries[i] == NULL)
file_writef(&db->db_file, "%d\n", false);
else {
file_writef(&db_file, "%d ", true);
db_entries[i]->write(db_file);
file_writef(&db_file, "\n");
file_writef(&db->db_file, "%d ", true);
db->db_entries[i]->write(db->db_file);
file_writef(&db->db_file, "\n");
}
}
file_close(&db_file);
file_close(&db->db_file);
}
template <class T>
void database<T> :: autosave()
void db_autosave(struct database<T> *db)
{
if (db_autosave == true)
save();
if (db->db_autosave == true)
db_save(db);
}
template <class T>
@ -92,7 +92,7 @@ T *database<T> :: insert(const T &item)
db_keys[t->primary_key()] = t->index();
db_size++;
autosave();
::db_autosave(this);
return t;
}
@ -107,7 +107,7 @@ void database<T> :: remove(unsigned int index)
delete db_entries[index];
db_entries[index] = NULL;
db_size--;
autosave();
::db_autosave(this);
}
template <class T>

View File

@ -189,7 +189,7 @@ static void test_save_load()
db_load(&db1);
test_equal(db1.db_size, N / 2);
db2.save();
db_save(&db2);
db1 = database<struct int_entry>("save_load.db", false);
db_load(&db1);

View File

@ -80,7 +80,7 @@ static void test_save_load(unsigned int n)
db_load(&idx2);
test_equal(idx2.db_size, (unsigned)0);
INDEX->save();
db_save(INDEX);
db_load(&idx2);
test_equal(idx2.db_size, INDEX->db_size);