database: Return pointers for insert() at() and find()
This gives me the item directly, which is usually what I want when I call these functions. Save iterators for iterating. Signed-off-by: Anna Schumaker <anna@ocarinaproject.net>
This commit is contained in:
parent
e2560f3134
commit
38990748bb
20
DESIGN
20
DESIGN
|
@ -280,7 +280,7 @@ Database:
|
|||
void save();
|
||||
void load();
|
||||
|
||||
iterator insert(T);
|
||||
T *insert(T);
|
||||
void remove(unsigned int);
|
||||
unsigned int size();
|
||||
unsigned int actual_size();
|
||||
|
@ -289,8 +289,8 @@ Database:
|
|||
iterator end();
|
||||
iterator next(iterator &);
|
||||
|
||||
iterator at(unsigned int);
|
||||
iterator find(const std::string &);
|
||||
T *at(unsigned int);
|
||||
T *find(const std::string &);
|
||||
};
|
||||
|
||||
- File format:
|
||||
|
@ -311,7 +311,7 @@ Database:
|
|||
void Database :: load();
|
||||
Load the database from disk.
|
||||
|
||||
iterator Database :: insert(T &item);
|
||||
T *Database :: insert(T &item);
|
||||
Look up the item in the _keys map.
|
||||
If we find an item with the same key:
|
||||
- Return an iterator to the found item.
|
||||
|
@ -350,17 +350,17 @@ Database:
|
|||
Return the next DatabaseEntry with valid == true or _db.end()
|
||||
if there are no valid entries left.
|
||||
|
||||
iterator Database :: at(unsigned int i);
|
||||
T *Database :: at(unsigned int i);
|
||||
If _db[i].valid == true:
|
||||
Return an iterator pointing to _db[i];
|
||||
Return a pointer to _db[i];
|
||||
Otherwise:
|
||||
Return _db.end();
|
||||
Return NULL;
|
||||
|
||||
iterator Database :: find(const std::string &key);
|
||||
T *Database :: find(const std::string &key);
|
||||
If key is in the _keys map:
|
||||
Return an iterator pointing to the corresponding entry.
|
||||
Return a pointer to the corresponding entry.
|
||||
Otherwise:
|
||||
Return _db.end();
|
||||
Return NULL;
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ public:
|
|||
void autosave();
|
||||
void load();
|
||||
|
||||
iterator insert(T);
|
||||
T *insert(T);
|
||||
void remove(unsigned int);
|
||||
unsigned int size();
|
||||
unsigned int actual_size();
|
||||
|
@ -52,8 +52,8 @@ public:
|
|||
iterator end();
|
||||
iterator next(iterator &);
|
||||
|
||||
iterator at(unsigned int);
|
||||
iterator find(const std::string &);
|
||||
T *at(unsigned int);
|
||||
T *find(const std::string &);
|
||||
};
|
||||
|
||||
#include "database.hpp"
|
||||
|
|
|
@ -72,21 +72,21 @@ void Database<T> :: load()
|
|||
}
|
||||
|
||||
template <class T>
|
||||
typename Database<T>::iterator Database<T> :: insert(T val)
|
||||
T *Database<T> :: insert(T val)
|
||||
{
|
||||
iterator it = find(val.primary_key());
|
||||
T *t = find(val.primary_key());
|
||||
|
||||
if (it != end())
|
||||
return it;
|
||||
if (t != NULL)
|
||||
return t;
|
||||
|
||||
it = _db.insert(_db.end(), val);
|
||||
iterator it = _db.insert(_db.end(), val);
|
||||
it->valid = true;
|
||||
it->id = it - _db.begin();
|
||||
|
||||
_keys[it->primary_key()] = it->id;
|
||||
_size++;
|
||||
autosave();
|
||||
return it;
|
||||
return &_db[it->id];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
@ -142,23 +142,23 @@ typename Database<T>::iterator Database<T> :: next(iterator &it)
|
|||
}
|
||||
|
||||
template <class T>
|
||||
typename Database<T>::iterator Database<T> :: at(unsigned int id)
|
||||
T *Database<T> :: at(unsigned int id)
|
||||
{
|
||||
if (id >= actual_size())
|
||||
return end();
|
||||
return NULL;
|
||||
|
||||
if (_db[id].valid == false)
|
||||
return end();
|
||||
return _db.begin() + id;
|
||||
return NULL;
|
||||
return &_db[id];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
typename Database<T>::iterator Database<T> :: find(const std::string &key)
|
||||
T *Database<T> :: find(const std::string &key)
|
||||
{
|
||||
std::map<const std::string, unsigned int>::iterator it;
|
||||
it = _keys.find(key);
|
||||
if (it == _keys.end())
|
||||
return end();
|
||||
return _db.begin() + it->second;
|
||||
return NULL;
|
||||
return &_db[it->second];
|
||||
}
|
||||
#endif /* OCARINA_DATABASE_HPP */
|
||||
|
|
|
@ -93,7 +93,7 @@ std::string filter :: add(const std::string &text, unsigned int track_id)
|
|||
|
||||
static void find_intersection(std::string &text, std::set<unsigned int> &res)
|
||||
{
|
||||
Index::iterator it = filter_index.find(text);
|
||||
IndexEntry *it = filter_index.find(text);
|
||||
std::set<unsigned int> tmp;
|
||||
|
||||
set_intersection(it->values.begin(), it->values.end(),
|
||||
|
|
|
@ -51,8 +51,8 @@ Index :: Index(const std::string &filepath, bool autosave)
|
|||
|
||||
void Index :: insert(const std::string &key, unsigned int val)
|
||||
{
|
||||
iterator it = find(key);
|
||||
if (it == end())
|
||||
IndexEntry *it = find(key);
|
||||
if (it == NULL)
|
||||
it = Database :: insert(IndexEntry(key));
|
||||
|
||||
it->insert(val);
|
||||
|
@ -61,9 +61,9 @@ void Index :: insert(const std::string &key, unsigned int val)
|
|||
|
||||
void Index :: remove(const std::string &key, unsigned int val)
|
||||
{
|
||||
iterator it = find(key);
|
||||
IndexEntry *it = find(key);
|
||||
|
||||
if (it == end())
|
||||
if (it == NULL)
|
||||
return;
|
||||
|
||||
it->remove(val);
|
||||
|
|
|
@ -305,7 +305,7 @@ static bool process_path(unsigned int lib_id, const std :: string &dir,
|
|||
scan.path = path;
|
||||
idle :: schedule (do_scan_path, scan);
|
||||
} else {
|
||||
if (track_db.find(path) == track_db.end()) {
|
||||
if (track_db.find(path) == NULL) {
|
||||
read_tags(lib_id, path);
|
||||
changed = true;
|
||||
}
|
||||
|
@ -435,7 +435,7 @@ static void do_import_library(std::string &s)
|
|||
f >> id >> enabled >> next_id >> size;
|
||||
|
||||
/* Assign this path a new id */
|
||||
if (library_db.find(path) != library_db.end()) {
|
||||
if (library_db.find(path) != NULL) {
|
||||
print("Library already contains path: %s, skipping\n", path.c_str());
|
||||
return;
|
||||
}
|
||||
|
@ -487,7 +487,7 @@ void library :: add_path(const std::string &dir)
|
|||
unsigned int id;
|
||||
if (g_file_test(dir.c_str(), G_FILE_TEST_IS_DIR) == false)
|
||||
throw -E_INVAL;
|
||||
if (library_db.find(dir) != library_db.end())
|
||||
if (library_db.find(dir) != NULL)
|
||||
return;
|
||||
|
||||
id = library_db.insert(library :: Library(dir, true))->id;
|
||||
|
|
|
@ -85,8 +85,8 @@ void playlist :: select(const std::string &name)
|
|||
const std::set<unsigned int> &playlist :: get_tracks(const std::string &name)
|
||||
{
|
||||
if ((name == "Banned") || (name == "Favorites")) {
|
||||
Database<IndexEntry>::iterator it = playlist_db.find(name);
|
||||
if (it != playlist_db.end())
|
||||
IndexEntry *it = playlist_db.find(name);
|
||||
if (it != NULL)
|
||||
return it->values;
|
||||
return empty_set;
|
||||
}
|
||||
|
|
13
lib/tags.cpp
13
lib/tags.cpp
|
@ -197,19 +197,19 @@ bool Track :: less_than(Track *rhs, sort_t field)
|
|||
Track *tagdb :: add_track(const std::string &filepath, Library *library)
|
||||
{
|
||||
unsigned int size = track_db.size();
|
||||
Database<Track>::iterator it = track_db.insert(Track(filepath, library));
|
||||
Track *track = track_db.insert(Track(filepath, library));
|
||||
if (size == track_db.size())
|
||||
return NULL;
|
||||
return &(*it);
|
||||
return track;
|
||||
}
|
||||
|
||||
Library *tagdb :: add_library(const std::string &filepath)
|
||||
{
|
||||
unsigned int size = library_db.size();
|
||||
Database<Library>::iterator it = library_db.insert(Library(filepath));
|
||||
Library *library = library_db.insert(Library(filepath));
|
||||
if (size == library_db.size())
|
||||
return NULL;
|
||||
return &(*it);
|
||||
return library;
|
||||
}
|
||||
|
||||
void tagdb :: remove_track(unsigned int track_id)
|
||||
|
@ -229,10 +229,7 @@ void tagdb :: remove_library(unsigned int library_id)
|
|||
|
||||
Track *tagdb :: lookup(unsigned int track_id)
|
||||
{
|
||||
Database<Track>::iterator it = track_db.at(track_id);
|
||||
if (it == track_db.end())
|
||||
return NULL;
|
||||
return &(*it);
|
||||
return track_db.at(track_id);
|
||||
}
|
||||
|
||||
Database<Track> &tagdb :: get_track_db()
|
||||
|
|
|
@ -156,10 +156,10 @@ int main(int argc, char **argv)
|
|||
* 8. Test access by id
|
||||
*/
|
||||
for (unsigned int i = 0; i < n + 10; i++) {
|
||||
Database<IntEntry>::iterator it = db.at(i);
|
||||
if (((i % 2) == 0) && (it != db.end()))
|
||||
IntEntry *it = db.at(i);
|
||||
if (((i % 2) == 0) && (it != NULL))
|
||||
test_results(false, __LINE__);
|
||||
if ((i >= n) && (it != db.end()))
|
||||
if ((i >= n) && (it != NULL))
|
||||
test_results(false, __LINE__);
|
||||
}
|
||||
test_results(true, __LINE__);
|
||||
|
|
|
@ -46,8 +46,8 @@ int main(int argc, char **argv)
|
|||
* 0: Test inserting when there is no key
|
||||
*/
|
||||
index.insert("a", 0);
|
||||
Index :: iterator it = index.find("a");
|
||||
if (it == index.end())
|
||||
IndexEntry *it = index.find("a");
|
||||
if (it == NULL)
|
||||
test_results(false, __LINE__);
|
||||
if (it->values.size() == 0)
|
||||
test_results(false, __LINE__);
|
||||
|
|
|
@ -74,23 +74,23 @@ int main(int argc, char **argv)
|
|||
/**
|
||||
* Test adding and removing multiple tracks
|
||||
*/
|
||||
tagdb :: add_track("Music/1.ogg", library);
|
||||
tagdb :: add_track("Music/10.ogg", library);
|
||||
tagdb :: add_track("Music/15.ogg", library);
|
||||
tagdb :: add_track("Music/60.ogg", library);
|
||||
tagdb :: add_track("Music/90.ogg", library);
|
||||
tagdb :: add_track("Music/600.ogg", library);
|
||||
tagdb :: add_track("Music/666.ogg", library);
|
||||
Track *track1 = tagdb :: add_track("Music/1.ogg", library);
|
||||
Track *track2 = tagdb :: add_track("Music/10.ogg", library);
|
||||
Track *track3 = tagdb :: add_track("Music/15.ogg", library);
|
||||
Track *track4 = tagdb :: add_track("Music/60.ogg", library);
|
||||
Track *track5 = tagdb :: add_track("Music/90.ogg", library);
|
||||
Track *track6 = tagdb :: add_track("Music/600.ogg", library);
|
||||
Track *track7 = tagdb :: add_track("Music/666.ogg", library);
|
||||
test_track_size(7, __LINE__);
|
||||
|
||||
test_results(tagdb :: lookup(0) == NULL, __LINE__);
|
||||
test_results(tagdb :: lookup(1)->id == 1, __LINE__);
|
||||
test_results(tagdb :: lookup(2)->id == 2, __LINE__);
|
||||
test_results(tagdb :: lookup(3)->id == 3, __LINE__);
|
||||
test_results(tagdb :: lookup(4)->id == 4, __LINE__);
|
||||
test_results(tagdb :: lookup(5)->id == 5, __LINE__);
|
||||
test_results(tagdb :: lookup(6)->id == 6, __LINE__);
|
||||
test_results(tagdb :: lookup(7)->id == 7, __LINE__);
|
||||
test_results(tagdb :: lookup(1) == track1, __LINE__);
|
||||
test_results(tagdb :: lookup(2) == track2, __LINE__);
|
||||
test_results(tagdb :: lookup(3) == track3, __LINE__);
|
||||
test_results(tagdb :: lookup(4) == track4, __LINE__);
|
||||
test_results(tagdb :: lookup(5) == track5, __LINE__);
|
||||
test_results(tagdb :: lookup(6) == track6, __LINE__);
|
||||
test_results(tagdb :: lookup(7) == track7, __LINE__);
|
||||
test_results(tagdb :: lookup(8) == NULL, __LINE__);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue