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 <anna@ocarinaproject.net>
This commit is contained in:
Anna Schumaker 2014-03-22 10:01:14 -04:00 committed by Anna Schumaker
parent bfefd1761a
commit 89fd79e079
7 changed files with 28 additions and 31 deletions

8
DESIGN
View File

@ -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.

1
TODO
View File

@ -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

View File

@ -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();

View File

@ -72,25 +72,21 @@ void Database<T> :: load()
}
template <class T>
unsigned int Database<T> :: insert(T val)
typename Database<T>::iterator Database<T> :: 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 <class T>

View File

@ -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();

View File

@ -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)));

View File

@ -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++;