core/database: Introduce the dbe_alloc() database operation

For allocating new database items from a given key.  Tracks do not need
this function since we have special handling for creation and insertion.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-02 16:59:55 -05:00
parent ab1572ed34
commit 049156a523
18 changed files with 79 additions and 69 deletions

View File

@ -9,17 +9,18 @@ index_entry :: index_entry()
ie_set = SET_INIT();
}
index_entry :: index_entry(const std::string &key)
: ie_key(key)
{
ie_set = SET_INIT();
}
index_entry :: ~index_entry()
{
set_deinit(&ie_set);
}
static struct db_entry *index_alloc(const gchar *key)
{
struct index_entry *ent = new struct index_entry;
ent->ie_key = key;
return ent;
}
const std::string index_entry :: primary_key() const
{
return ie_key;
@ -43,6 +44,7 @@ void index_entry :: read(file &file)
static const struct db_ops index_ops = {
index_alloc,
};

View File

@ -36,9 +36,11 @@ static void __album_from_key(struct album *album, const gchar *key)
album :: album() : al_year(0), al_name("") {}
album :: album(const std::string &key)
static struct db_entry *album_alloc(const gchar *key)
{
__album_from_key(this, key.c_str());
struct album *album = new struct album;
__album_from_key(album, key);
return album;
}
const std::string album :: primary_key() const
@ -60,6 +62,7 @@ void album :: write(file &file)
static const struct db_ops album_ops = {
album_alloc,
};

View File

@ -12,14 +12,16 @@ static database<struct artist> artist_db;
artist :: artist() {}
artist :: artist(const std::string &name)
static db_entry *artist_alloc(const gchar *name)
{
gchar *lower = string_lowercase(name.c_str());
struct artist *artist = new struct artist;
gchar *lower = string_lowercase(name);
ar_name = name;
ar_lower = lower;
artist->ar_name = name;
artist->ar_lower = lower;
g_free(lower);
return artist;
}
const std::string artist :: primary_key() const
@ -46,6 +48,7 @@ void artist :: write(file &file)
static const struct db_ops artist_ops = {
artist_alloc,
};

View File

@ -11,14 +11,16 @@ static database<struct genre> genre_db;
genre :: genre() {}
genre :: genre(const std::string &name)
static struct db_entry *genre_alloc(const gchar *name)
{
gchar *lower = string_lowercase(name.c_str());
struct genre *genre = new struct genre;
gchar *lower = string_lowercase(name);
ge_name = name;
ge_lower = lower;
genre->ge_name = name;
genre->ge_lower = lower;
g_free(lower);
return genre;
}
const std::string genre :: primary_key() const
@ -45,6 +47,7 @@ void genre :: write(file &file)
static const struct db_ops genre_ops = {
genre_alloc,
};

View File

@ -12,9 +12,13 @@ library :: library()
{
}
library :: library(const std::string &path)
: li_size(0), li_enabled(true), li_path(path)
static struct db_entry *library_alloc(const gchar *path)
{
struct library *library = new struct library;
library->li_size = 0;
library->li_enabled = true;
library->li_path = path;
return library;
}
const std::string library :: primary_key() const
@ -42,6 +46,7 @@ void library :: write(file &file)
static const struct db_ops library_ops = {
library_alloc,
};

View File

@ -20,7 +20,6 @@ struct db_entry {
unsigned int dbe_index; /* The db_entry's position in the database. */
db_entry(); /**< Initialize _index to 0. */
db_entry(const std::string);
virtual ~db_entry() = 0; /**< Virtual destructor */
/**
@ -52,6 +51,8 @@ struct db_entry {
struct db_ops {
/* Allocate a new struct db_entry from a given key. */
struct db_entry *(*dbe_alloc)(const gchar *);
};

View File

@ -167,6 +167,7 @@ T *db_find(struct database<T> *db, const gchar *key)
T *dbe = db_get(db, key);
if (dbe)
return dbe;
return db_insert(db, new T(key));
dbe = (T *)db->db_ops->dbe_alloc(key);
return db_insert(db, dbe);
}
#endif /* OCARINA_DATABASE_HPP */

View File

@ -21,16 +21,8 @@ struct index_entry : public db_entry {
struct set ie_set;
index_entry(); /**< Create an empty IndexEntry. */
/**
* Create an IndexEntry with a specific key.
*
* @param key The key associated with this IndexEntry.
*/
index_entry(const std::string &);
~index_entry();
/**
* Access the key stored by this IndexEntry.
*

View File

@ -24,11 +24,6 @@ struct album : public db_entry {
album(); /**< Album tag constructor */
/**
* Album tag constructor
*/
album(const std::string &);
/**
* The album's primary key is the concatenation of year
* and name, allowing for multiple albums with the same

View File

@ -16,13 +16,6 @@ struct artist : public db_entry {
artist(); /**< Artist tag constructor. */
/**
* Artist tag constructor
*
* @param name Artist name.
*/
artist(const std::string &);
const std::string primary_key() const;
void read(file &);
void write(file &);

View File

@ -17,13 +17,6 @@ public:
genre(); /**< Genre tag constructor. */
/**
* Genre tag constructor.
*
* @param name Genre name.
*/
genre(const std::string &);
const std::string primary_key() const;
void read(file &);
void write(file &);

View File

@ -24,13 +24,6 @@ struct library : public db_entry {
library(); /**< Library tag constructor. */
/**
* Library tag constructor.
*
* @param path Path to the library directory on disk.
*/
library(const std::string &);
/**
* Called to access the library tag's primary key.
*

View File

@ -17,7 +17,6 @@ struct int_entry : public db_entry {
int_entry() : ie_val(0) {};
int_entry(unsigned int v) : ie_val(v) {};
int_entry(const std::string &k) { sscanf(k.c_str(), "%u", &ie_val); }
const std::string primary_key() const
{
@ -31,16 +30,25 @@ struct int_entry : public db_entry {
void read(file &f) { file_readf(&f, "%u", &ie_val); }
};
static struct db_entry *int_alloc(const gchar *key)
{
struct int_entry *ent = new int_entry;
sscanf(key, "%u", &ent->ie_val);
return ent;
}
static const struct db_ops int_ops = {
int_alloc,
};
static void test_db_entry()
{
struct int_entry *ent = new struct int_entry(1);
struct int_entry *ent;
struct file f;
ent = (struct int_entry *)int_ops.dbe_alloc("1");
test_equal(ent->dbe_index, 0);
test_equal(ent->ie_val, 1);
test_equal(ent->primary_key(), "1");

View File

@ -8,11 +8,12 @@
static void test_entry()
{
index_entry *ie = new index_entry("Link");
struct set_iter it;
index_entry *ie;
unsigned int i;
struct file f;
ie = (index_entry *)test_index_ops()->dbe_alloc("Link");
test_equal(ie->primary_key(), "Link");
set_insert(&ie->ie_set, 0);
set_insert(&ie->ie_set, 1);

View File

@ -22,9 +22,11 @@ static void test_verify_hyrule(struct album *album)
static void test_album()
{
struct album *album = new struct album("1998 Hyrule Symphony");
const struct db_ops *album_ops = test_album_ops();
struct album *album;
file f;
album = (struct album *)album_ops->dbe_alloc("1998 Hyrule Symphony");
test_verify_hyrule(album);
file_init(&f, "album_tag", 0);
@ -49,8 +51,11 @@ static void test_album()
static void test_album_compare()
{
struct album *twilight = new struct album("2006 Twilight Princess");
struct album *skyward = new struct album("2011 skyward sword");
const struct db_ops *album_ops = test_album_ops();
struct album *twilight, *skyward;
twilight = (struct album *)album_ops->dbe_alloc("2006 Twilight Princess");
skyward = (struct album *)album_ops->dbe_alloc("2011 skyward sword");
test_equal(album_compare(twilight, twilight), 0);
test_equal(album_compare(twilight, skyward), 1);

View File

@ -20,10 +20,12 @@ static void test_verify_koji(struct artist *artist)
static void test_artist()
{
struct artist *artist = new struct artist("Koji Kondo");
const struct db_ops *artist_ops = test_artist_ops();
struct artist *artist;
unsigned int i;
file f;
artist = (struct artist *)artist_ops->dbe_alloc("Koji Kondo");
test_verify_koji(artist);
file_init(&f, "artist_tag", 0);
@ -50,8 +52,11 @@ static void test_artist()
static void test_artist_compare()
{
struct artist *koji = new struct artist("Koji Kondo");
struct artist *hajime = new struct artist("hajime wakai");
const struct db_ops *artist_ops = test_artist_ops();
struct artist *koji, *hajime;
koji = (struct artist *)artist_ops->dbe_alloc("Koji Kondo");
hajime = (struct artist *)artist_ops->dbe_alloc("hajime wakai");
test_equal(artist_compare(koji, koji), 0);
test_equal(artist_compare(koji, hajime), 1);

View File

@ -20,10 +20,12 @@ static void test_verify_vg(struct genre *genre)
static void test_genre()
{
struct genre *genre = new struct genre("Video Game Music");
const struct db_ops *genre_ops = test_genre_ops();
struct genre *genre;
unsigned int i;
file f;
genre = (struct genre *)genre_ops->dbe_alloc("Video Game Music");
test_verify_vg(genre);
file_init(&f, "genre_tag", 0);
@ -50,8 +52,11 @@ static void test_genre()
static void test_genre_compare()
{
struct genre *video = new struct genre("Video Game Music");
struct genre *game = new struct genre("game music");
const struct db_ops *genre_ops = test_genre_ops();
struct genre *video, *game;
video = (struct genre *)genre_ops->dbe_alloc("Video Game Music");
game = (struct genre *)genre_ops->dbe_alloc("game music");
test_equal(genre_compare(video, video), 0);
test_equal(genre_compare(video, game), 1);

View File

@ -27,11 +27,13 @@ static void test_verify_link(struct library *library)
static void test_library()
{
struct library *link = new struct library("/home/Link/Music");
struct library *zelda = new struct library("/home/Zelda/Music");
struct library *library = new struct library();
const struct db_ops *library_ops = test_library_ops();
struct library *link, *zelda, *library = new struct library();
file f;
link = (struct library *)library_ops->dbe_alloc("/home/Link/Music");
zelda = (struct library *)library_ops->dbe_alloc("/home/Zelda/Music");
library_set_enabled(link, false);
test_verify_link(link);
test_verify_zelda(zelda);