tagdb: Add most functions

I implemented:

- add_track()
- remove_track()
- add_library()
- remove_library()
- lookup()
- get_track_db()
- get_library_db()

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2014-03-23 14:42:01 -04:00 committed by Anna Schumaker
parent 5d97c84d10
commit e2560f3134
12 changed files with 170 additions and 9 deletions

14
DESIGN
View File

@ -577,22 +577,24 @@ Tag Database:
last played variables.
Library *tagdb :: add_library(const std::string &filepath);
Add a new path to library_db and return its library_id.
Add a new path to library_db. Return a pointer to the new path
or return NULL if the path is already in the database.
void tagdb :: remove_track(unsigned int track_id);
Remove the track with id equal to track_id from the track_db.
void tagdb :: remove_library(unsigned int library_id);
Remove the library with id equal to library_id from the
library_db.
Remove all tracks associated with this library from the
track_db, then remove this library from the library_db.
Track *tagdb :: lookup(unsigned int track_id);
Look up the track_id in the track database.
Look up the track_id in the track database. Return NULL if
there is no matching track.
const Database<Track> &tagdb :: get_track_db();
Database<Track> &tagdb :: get_track_db();
Return a reference to the track_db.
const Database<Library> &tagdb :: get_library_db();
Database<Library> &tagdb :: get_library_db();
Return a reference to the library_db.

View File

@ -105,9 +105,16 @@ public:
};
namespace tagdb {
namespace tagdb
{
Track *add_track(const std::string &, Library *);
Library *add_library(const std::string &);
void remove_track(unsigned int);
void remove_library(unsigned int);
Track *lookup(unsigned int);
Database<Track> &get_track_db();
Database<Library> &get_library_db();
}

View File

@ -12,6 +12,7 @@ Database<Artist> artist_db("artist.db", true);
Database<Album> album_db("album.db", true);
Database<Genre> genre_db("genre.db", true);
Database<Library> library_db("library.db", true);
Database<Track> track_db("track.db", false);
/**
@ -193,8 +194,53 @@ bool Track :: less_than(Track *rhs, sort_t field)
*
*/
Library *tagdb :: add_library(const std::string &filepath)
Track *tagdb :: add_track(const std::string &filepath, Library *library)
{
Database<Library>::iterator it = library_db.insert(Library(filepath));
unsigned int size = track_db.size();
Database<Track>::iterator it = track_db.insert(Track(filepath, library));
if (size == track_db.size())
return NULL;
return &(*it);
}
Library *tagdb :: add_library(const std::string &filepath)
{
unsigned int size = library_db.size();
Database<Library>::iterator it = library_db.insert(Library(filepath));
if (size == library_db.size())
return NULL;
return &(*it);
}
void tagdb :: remove_track(unsigned int track_id)
{
track_db.remove(track_id);
}
void tagdb :: remove_library(unsigned int library_id)
{
Database<Track>::iterator it;
for (it = track_db.begin(); it != track_db.end(); it++) {
if (it->library->id == library_id)
track_db.remove(it->id);
}
library_db.remove(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);
}
Database<Track> &tagdb :: get_track_db()
{
return track_db;
}
Database<Library> &tagdb :: get_library_db()
{
return library_db;
}

Binary file not shown.

BIN
tests/Music/10.ogg Normal file

Binary file not shown.

BIN
tests/Music/15.ogg Normal file

Binary file not shown.

BIN
tests/Music/60.ogg Normal file

Binary file not shown.

BIN
tests/Music/600.ogg Normal file

Binary file not shown.

BIN
tests/Music/666.ogg Normal file

Binary file not shown.

BIN
tests/Music/90.ogg Normal file

Binary file not shown.

101
tests/src/tagdb.cpp Normal file
View File

@ -0,0 +1,101 @@
/*
* Copyright 2014 (c) Anna Schumaker.
* Test a DatabaseEntry
*/
#include <tags.h>
#include <print.h>
#include <stdlib.h>
unsigned int test_num = 0;
void test_results(bool success, unsigned int line)
{
print(" %u: ", test_num);
if (success)
print("Success!\n");
else {
print("FAILED (%u) =(\n", line);
exit(1);
}
test_num++;
}
void test_library_size(unsigned int expected, unsigned int line)
{
test_results(tagdb :: get_library_db().size() == expected, line);
}
void test_track_size(unsigned int expected, unsigned int line)
{
test_results(tagdb :: get_track_db().size() == expected, line);
}
int main(int argc, char **argv)
{
/**
* Initial library checks
*/
Library *library = tagdb :: add_library("Music");
test_results(library->root_path == "Music", __LINE__);
test_library_size(1, __LINE__);
Library *library_null = tagdb :: add_library("Music");
test_results(library_null == NULL, __LINE__);
test_library_size(1, __LINE__);
unsigned int id = library->id;
tagdb :: remove_library(id);
test_library_size(0, __LINE__);
tagdb :: remove_library(id);
test_library_size(0, __LINE__);
/**
* Test adding / removing tracks
*/
library = tagdb :: add_library("Music");
Track *track = tagdb :: add_track("Music/1.ogg", library);
test_track_size(1, __LINE__);
Track *track_null = tagdb :: add_track("Music/1.ogg", library);
test_track_size(1, __LINE__);
test_results(track->path() == "Music/1.ogg", __LINE__);
test_results(track_null == NULL, __LINE__);
id = track->id;
tagdb :: remove_track(id);
test_track_size(0, __LINE__);
tagdb :: remove_track(id);
test_track_size(0, __LINE__);
/**
* 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);
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(8) == NULL, __LINE__);
tagdb :: remove_library(library->id);
test_track_size(0, __LINE__);
return 0;
}

View File

@ -18,3 +18,8 @@ echo
test_tag "Library" "-l"
echo
test_tag "Track" "-t"
echo
new_test "Test TagDB"
./src/tagdb.run