From 89fd79e07910d0aef7c4944519843e2a0ee2c639 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 22 Mar 2014 10:01:14 -0400 Subject: [PATCH] Database: Improve on the insert() return value Rather than returning an index into the database, instead return an iterator pointing at the item we just inserted. Signed-off-by: Anna Schumaker --- DESIGN | 8 ++++---- TODO | 1 + include/database.h | 2 +- include/database.hpp | 20 ++++++++------------ lib/index.cpp | 2 +- lib/library.cpp | 20 ++++++++++---------- tests/src/database.cpp | 6 +++--- 7 files changed, 28 insertions(+), 31 deletions(-) diff --git a/DESIGN b/DESIGN index 65a00db5..bb852fb6 100644 --- a/DESIGN +++ b/DESIGN @@ -280,7 +280,7 @@ Database: void save(); void load(); - unsigned int insert(T); + iterator insert(T); void remove(unsigned int); unsigned int size(); unsigned int actual_size(); @@ -311,10 +311,10 @@ Database: void Database :: load(); Load the database from disk. - unsigned int Database :: insert(T &item); + iterator Database :: insert(T &item); Look up the item in the _keys map. If we find an item with the same key: - - Return the index of the item to the caller. + - Return an iterator to the found item. Otherwise: - Add the new item to the end of the _db. - Add the new item to _keys. @@ -322,7 +322,7 @@ Database: - Set item.id to the index of the new item. _ Increment _size. - If autosave == true: save(). - - Return item.id. + - Return an iterator to the new item. unsigned int Database :: remove(); - Remove the item from the _keys map. diff --git a/TODO b/TODO index 073916f7..3798032e 100644 --- a/TODO +++ b/TODO @@ -96,3 +96,4 @@ Future work: - Investigate "Bulk insert" callbacks for performance - Initialize PQs with multiple flags - Prefill Song structures in each library :: Track + - Use "friend" class for valid and id flags of a DatabaseEntry diff --git a/include/database.h b/include/database.h index a0c5f55d..850cf82d 100644 --- a/include/database.h +++ b/include/database.h @@ -43,7 +43,7 @@ public: void autosave(); void load(); - unsigned int insert(T); + iterator insert(T); void remove(unsigned int); unsigned int size(); unsigned int actual_size(); diff --git a/include/database.hpp b/include/database.hpp index fad06e44..84e1e8b5 100644 --- a/include/database.hpp +++ b/include/database.hpp @@ -72,25 +72,21 @@ void Database :: load() } template -unsigned int Database :: insert(T val) +typename Database::iterator Database :: insert(T val) { - unsigned int id; iterator it = find(val.primary_key()); if (it != end()) - return it - _db.begin(); + return it; - /* - * Check primary key stuff here - */ - id = _db.size(); - _db.push_back(val); - _keys[val.primary_key()] = id; - _db[id].valid = true; - _db[id].id = id; + it = _db.insert(_db.end(), val); + it->valid = true; + it->id = it - _db.begin(); + + _keys[it->primary_key()] = it->id; _size++; autosave(); - return id; + return it; } template diff --git a/lib/index.cpp b/lib/index.cpp index d1842b0d..e67cf877 100644 --- a/lib/index.cpp +++ b/lib/index.cpp @@ -53,7 +53,7 @@ void Index :: insert(const std::string &key, unsigned int val) { iterator it = find(key); if (it == end()) - it = at(Database :: insert(IndexEntry(key))); + it = Database :: insert(IndexEntry(key)); it->insert(val); autosave(); diff --git a/lib/library.cpp b/lib/library.cpp index 21486822..bd3f251a 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -276,11 +276,11 @@ static void read_tags(unsigned int lib_id, const std :: string &path) audio = ref.audioProperties(); - artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, tag)); - album_id = album_db.insert(library :: Album(tag, artist_id)); - genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, tag)); + artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, tag))->id; + album_id = album_db.insert(library :: Album(tag, artist_id))->id; + genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, tag))->id; track_id = track_db.insert(library :: Track(tag, audio, lib_id, - artist_id, album_id, genre_id, path)); + artist_id, album_id, genre_id, path))->id; if (track_db.at(track_id)->valid == false) return; @@ -400,11 +400,11 @@ static void do_import_track(File &f, unsigned int lib_id) f >> tmp >> tmp >>tmp >> banned; /* bitrate, sample, channels, banned */ f.getline(); /* get rest of line */ - artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, artist)); - album_id = album_db.insert(library :: Album(album, year, artist_id)); - genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, genre)); + artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, artist))->id; + album_id = album_db.insert(library :: Album(album, year, artist_id))->id; + genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, genre))->id; track_id = track_db.insert(library :: Track(&data, lib_id, artist_id, - album_id, genre_id)); + album_id, genre_id))->id; library_db.at(lib_id)->size++; filter::add(artist_db.at(artist_id)->name, track_id); @@ -440,7 +440,7 @@ static void do_import_library(std::string &s) return; } print("Adding path: %s\n", path.c_str()); - id = library_db.insert(library :: Library(path, enabled)); + id = library_db.insert(library :: Library(path, enabled))->id; get_callbacks()->on_library_add(id, &(*library_db.at(id))); library_db.save(); @@ -490,7 +490,7 @@ void library :: add_path(const std::string &dir) if (library_db.find(dir) != library_db.end()) return; - id = library_db.insert(library :: Library(dir, true)); + id = library_db.insert(library :: Library(dir, true))->id; library_db.save(); get_callbacks()->on_library_add(id, &(*library_db.at(id))); diff --git a/tests/src/database.cpp b/tests/src/database.cpp index b99bfb84..4d32a46b 100644 --- a/tests/src/database.cpp +++ b/tests/src/database.cpp @@ -85,7 +85,7 @@ int main(int argc, char **argv) * 1: Test insertion */ for (unsigned int i = 0; i < n; i++) { - if (db.insert(IntEntry(i)) != i) + if (db.insert(IntEntry(i))->id != i) test_results(false, __LINE__); } test_results(true, __LINE__); @@ -103,7 +103,7 @@ int main(int argc, char **argv) * 3: Test inserting ... again. */ for (unsigned int i = 0; i < n; i++) { - if (db.insert(IntEntry(i)) != i) + if (db.insert(IntEntry(i))->id != i) test_results(false, __LINE__); } test_results(true, __LINE__); @@ -169,7 +169,7 @@ int main(int argc, char **argv) * 9. Test inserting once again */ for (unsigned int i = 0; i < n; i++) { - index = db.insert(i); + index = db.insert(i)->id; if ((i % 2) == 0) { size++; actual++;