core/database: Move db_remove() out of the database struct

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-20 08:05:13 -04:00
parent 9c358539fe
commit 929279259d
5 changed files with 22 additions and 31 deletions

View File

@ -88,7 +88,7 @@ Library *tags :: get_library(const unsigned int index)
void tags :: remove_library(Library *library)
{
if (library)
library_db.remove(library->index());
db_remove(&library_db, library);
}
unsigned int tags :: library_size()

View File

@ -167,7 +167,7 @@ Track *tags :: get_track(const unsigned int index)
void tags :: remove_track(Track *track)
{
track_db.remove(track->index());
db_remove(&track_db, track);
}
void tags :: remove_library_tracks(Library *library)
@ -176,7 +176,7 @@ void tags :: remove_library_tracks(Library *library)
for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) {
if ((*it)->library() == library)
track_db.remove((*it)->index());
db_remove(&track_db, *it);
}
tags :: commit_track_db();
}

View File

@ -108,18 +108,6 @@ struct database {
~database();
/**
* Called to remove an item from the Database. This function will:
* -# Delete memory allocated for the DatabaseEntry
* -# Set Database::_db[index] = NULL
* -# Decrement Database::_size
* -# Call Database::autosave() to commit changes
*
* @param index The index of the item that should be removed.
*/
void remove(unsigned int);
/**
* @return An iterator pointing to the first valid entry in the Database.
* @return Return Database::end() if there are no valid entries.
@ -180,6 +168,9 @@ unsigned int db_actual_size(const struct database<T> *);
template <class T>
T *db_insert(struct database<T> *, T *);
/* Called to remove an item from the database. */
template <class T>
void db_remove(struct database<T> *, T *);
#include "database.hpp"

View File

@ -94,17 +94,17 @@ T *db_insert(struct database<T> *db, T *item)
}
template <class T>
void database<T> :: remove(unsigned int index)
void db_remove(struct database<T> *db, T *item)
{
if (index >= db_actual_size(this))
if (item == NULL)
return;
if (db_entries[index] == NULL)
if (db->at(item->index()) != item)
return;
db_keys.erase(db_entries[index]->primary_key());
delete db_entries[index];
db_entries[index] = NULL;
db_size--;
::db_autosave(this);
db->db_entries[item->index()] = NULL;
db->db_keys.erase(item->primary_key());
delete item;
db->db_size--;
db_autosave(db);
}
template <class T>

View File

@ -73,7 +73,7 @@ static void test_stress(unsigned int N)
database<struct int_entry> db("stress.db", false);
database<struct int_entry>::iterator it;
std::vector<struct int_entry *> ptrs;
struct int_entry *dbe;
struct int_entry *dbe, rmv(42);
unsigned int i;
gchar *key;
@ -114,16 +114,16 @@ static void test_stress(unsigned int N)
test_equal(db.find(key), NULL);
g_free(key);
/* database.remove(): Even indices only! */
for (i = 0; i < N; i += 2) {
/* db_remove(): Even indices only! */
for (i = 0; i <= (N + 2); i += 2) {
key = g_strdup_printf("%u", i);
db.remove(i);
db.remove(i);
dbe = db.find(key);
db_remove(&db, dbe);
test_loop_equal(db.at(i), NULL, i);
test_loop_equal(db.find(key), NULL, i);
g_free(key);
} test_loop_passed();
db.remove(N);
db_remove(&db, &rmv);
test_equal(db.db_size, N / 2);
test_equal(db_actual_size(&db), N);
@ -165,7 +165,7 @@ static void test_save_load()
/* Removing 5 items, should also trigger autosaving. */
for (i = 0; i < N; i += 2)
db1.remove(i);
db_remove(&db1, db1.at(i));
db2 = database<struct int_entry>("save_load.db", false);
db_load(&db2);
@ -184,7 +184,7 @@ static void test_save_load()
for (i = N; i < (2 * N); i++)
db_insert(&db2, new int_entry(i));
for (i = N; i < (2 * N); i += 2)
db2.remove(i);
db_remove(&db2, db2.at(i));
db1 = database<struct int_entry>("save_load.db", false);
db_load(&db1);