diff --git a/core/tags.cpp b/core/tags.cpp index d46d66e5..f9ef9ba8 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -12,7 +12,6 @@ #include #include -Database album_db("album.db", true); Database genre_db("genre.db", true); Database library_db("library.db", true); Database track_db("track.db", false); @@ -58,7 +57,7 @@ void Track :: read(File &f) library = library_db.at(library_id); artist = tags :: get_artist(artist_id); - album = album_db.at(album_id); + album = tags :: get_album(album_id); genre = genre_db.at(genre_id); title_lower = filter :: add(title, index()); @@ -120,7 +119,7 @@ bool Track :: tag() audio = ref.audioProperties(); artist = tags :: get_artist(format_tag(tag->artist())); - album = find_or_insert(Album(format_tag(tag->album()), tag->year()), album_db); + album = tags :: get_album(format_tag(tag->album()), tag->year()); genre = find_or_insert(Genre(format_tag(tag->genre())), genre_db); track = tag->track(); length = audio->length(); @@ -220,7 +219,6 @@ int Track :: less_than(Track *rhs, sort_t field) void tagdb :: init() { tags :: init(); - album_db.load(); genre_db.load(); library_db.load(); track_db.load(); diff --git a/core/tags/album.cpp b/core/tags/album.cpp index da2b6812..ef54ac84 100644 --- a/core/tags/album.cpp +++ b/core/tags/album.cpp @@ -5,6 +5,17 @@ #include #include + +static Database album_db("album.db", true); + +static const std::string make_key(const std::string &name, unsigned int year) +{ + std::stringstream ss; + ss << year << "/" << name; + return ss.str(); +} + + Album :: Album() : GenericTag(), _year(0) {} Album :: Album(const std::string &name, unsigned int year) @@ -14,9 +25,7 @@ Album :: Album(const std::string &name, unsigned int year) const std::string Album :: primary_key() const { - std::stringstream ss; - ss << _year << "/" << GenericTag :: primary_key(); - return ss.str(); + return make_key(GenericTag :: primary_key(), _year); } void Album :: read(File &file) @@ -35,3 +44,22 @@ unsigned int Album :: year() { return _year; } + + +void tags :: init_album_db() +{ + album_db.load(); +} + +Album *tags :: get_album(const std::string &name, unsigned int year) +{ + Album *ret = album_db.find(make_key(name, year)); + if (ret) + return ret; + return album_db.insert(Album(name, year)); +} + +Album *tags ::get_album(const unsigned int index) +{ + return album_db.at(index); +} diff --git a/core/tags/tags.cpp b/core/tags/tags.cpp index 77109f07..70f070e1 100644 --- a/core/tags/tags.cpp +++ b/core/tags/tags.cpp @@ -2,10 +2,12 @@ * @file * Copyright 2014 (c) Anna Schumaker. */ +#include #include #include void tags :: init() { + tags :: init_album_db(); tags :: init_artist_db(); } diff --git a/include/core/tags/album.h b/include/core/tags/album.h index ffae9485..19498095 100644 --- a/include/core/tags/album.h +++ b/include/core/tags/album.h @@ -64,4 +64,31 @@ public: unsigned int year(); }; +namespace tags +{ + + /** Called to read the album_db from disk. */ + void init_album_db(); + + /** + * Called to look up an Album tag by name and year. If no + * existing tag is found a new one will be created and + * returned to the caller. + * + * @param name The name of the album. + * @param year The year of the album. + * @return A matching Album tag. + */ + Album *get_album(const std::string &, unsigned int); + + /** + * Called to look up an Album tag by tag index. + * + * @param index The index of the Album tag. + * @return A matching Album tag or NULL. + */ + Album *get_album(const unsigned int); + +} /* Namespace: tags */ + #endif /* OCARINA_CORE_TAGS_ALBUM_H */ diff --git a/tests/core/tags/album.cpp b/tests/core/tags/album.cpp index 8bdfedd7..a5b32220 100644 --- a/tests/core/tags/album.cpp +++ b/tests/core/tags/album.cpp @@ -35,8 +35,27 @@ static void test_album_tag() test_equal(album.primary_key(), (std::string)"1998/Hyrule Symphony"); } +static void test_album_tag_lookup() +{ + Database album_db("album.db", false); + Album *album = tags :: get_album("Hyrule Symphony", 1998); + + test_equal(album->name(), (std::string)"Hyrule Symphony"); + test_equal(album->lowercase(), (std::string)"hyrule symphony"); + test_equal(album->year(), (unsigned int)1998); + test_equal(album->primary_key(), (std::string)"1998/Hyrule Symphony"); + + test_equal(tags :: get_album("Hyrule Symphony", 1998), album); + test_equal(tags :: get_album(0), album); + test_equal(tags :: get_album(1), (Album *)NULL); + + album_db.load(); + test_equal(album_db.size(), (unsigned)1); +} + int main(int argc, char **argv) { run_test("Album Tag Test", test_album_tag); + run_test("Album Tag Lookup Test", test_album_tag_lookup); return 0; }