diff --git a/core/tags/library.cpp b/core/tags/library.cpp index 5e6a7d31..9bbe3895 100644 --- a/core/tags/library.cpp +++ b/core/tags/library.cpp @@ -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() diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 4e61aaa2..67dccfc7 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -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(); } diff --git a/include/core/database.h b/include/core/database.h index 6db300c5..2c6dc7b9 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -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 *); template T *db_insert(struct database *, T *); +/* Called to remove an item from the database. */ +template +void db_remove(struct database *, T *); #include "database.hpp" diff --git a/include/core/database.hpp b/include/core/database.hpp index bde68367..e81c1270 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -94,17 +94,17 @@ T *db_insert(struct database *db, T *item) } template -void database :: remove(unsigned int index) +void db_remove(struct database *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 diff --git a/tests/core/database.cpp b/tests/core/database.cpp index ad88fbff..26f59825 100644 --- a/tests/core/database.cpp +++ b/tests/core/database.cpp @@ -73,7 +73,7 @@ static void test_stress(unsigned int N) database db("stress.db", false); database::iterator it; std::vector 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("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("save_load.db", false); db_load(&db1);