core/database: Introduce the dbe_write() database operation

For writing database entries to disk.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-22 15:54:21 -04:00
parent 2da33f1c75
commit f51852f62e
21 changed files with 65 additions and 76 deletions

View File

@ -28,10 +28,11 @@ const std::string index_entry :: primary_key() const
return ie_key;
}
void index_entry :: write(file &file)
static void index_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%s\n" , ie_key.c_str());
set_write(&file, &ie_set);
struct index_entry *ent = (struct index_entry *)dbe;
file_writef(file, "%s\n" , ent->ie_key.c_str());
set_write(file, &ent->ie_set);
}
static struct db_entry *index_read(struct file *file)
@ -52,6 +53,7 @@ static const struct db_ops index_ops = {
index_free,
index_read,
NULL,
index_write,
};

View File

@ -61,9 +61,10 @@ static struct db_entry *album_read(struct file *file)
return album;
}
void album :: write(file &file)
static void album_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%u %s", al_year, al_name.c_str());
struct album *album = (struct album *)dbe;
file_writef(file, "%u %s", album->al_year, album->al_name.c_str());
}
@ -72,6 +73,7 @@ static const struct db_ops album_ops = {
album_free,
album_read,
NULL,
album_write,
};

View File

@ -42,9 +42,10 @@ struct db_entry *artist_read(struct file *file)
return artist;
}
void artist :: write(file &file)
static void artist_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%s", ar_name.c_str());
struct artist *artist = (struct artist *)dbe;
file_writef(file, "%s", artist->ar_name.c_str());
}
@ -53,6 +54,7 @@ static const struct db_ops artist_ops = {
artist_free,
artist_read,
NULL,
artist_write,
};

View File

@ -41,9 +41,10 @@ static struct db_entry *genre_read(struct file *file)
return genre;
}
void genre :: write(file &file)
static void genre_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%s", ge_name.c_str());
struct genre *genre = (struct genre *)dbe;
file_writef(file, "%s", genre->ge_name.c_str());
}
@ -52,6 +53,7 @@ static const struct db_ops genre_ops = {
genre_free,
genre_read,
NULL,
genre_write,
};

View File

@ -47,9 +47,10 @@ static struct db_entry *library_read(struct file *file)
return library;
}
void library :: write(file &file)
static void library_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%d %s", li_enabled, li_path.c_str());
struct library *library = (struct library *)dbe;
file_writef(file, "%d %s", library->li_enabled, library->li_path.c_str());
}
@ -58,6 +59,7 @@ static const struct db_ops library_ops = {
library_free,
library_read,
NULL,
library_write,
};

View File

@ -128,14 +128,19 @@ static struct db_entry *track_read(struct file *file)
return track;
}
void track :: write(file &file)
static void track_write(struct file *file, struct db_entry *dbe)
{
file_writef(&file, "%u %u %u %u %u ", tr_library->dbe_index,
tr_artist->dbe_index, tr_album->dbe_index,
tr_genre->dbe_index, tr_track);
date_write(&file, &tr_date);
file_writef(&file, " %u %u %s\n%s\n", tr_count, tr_length,
tr_title.c_str(), tr_path.c_str());
struct track *track = (struct track *)dbe;
file_writef(file, "%u %u %u %u %u ", track->tr_library->dbe_index,
track->tr_artist->dbe_index,
track->tr_album->dbe_index,
track->tr_genre->dbe_index,
track->tr_track);
date_write(file, &track->tr_date);
file_writef(file, " %u %u %s\n%s\n", track->tr_count,
track->tr_length,
track->tr_title.c_str(),
track->tr_path.c_str());
}
@ -144,6 +149,7 @@ static const struct db_ops track_ops = {
track_free,
track_read,
track_setup,
track_write,
};

View File

@ -31,14 +31,6 @@ struct db_entry {
* @return A unique string identifying a DatabaseEntry instance.
*/
virtual const std::string primary_key() const = 0;
/**
* This function is called by the Database to write a specific
* DatabaseEntry instance to disk.
*
* @param file File to use when writing data.
*/
virtual void write(file &) = 0;
};
@ -54,6 +46,9 @@ struct db_ops {
/* Set up a struct db_entry after adding to the database. */
void (*dbe_setup)(struct db_entry *);
/* Write a single struct db_entry to disk. */
void (*dbe_write)(struct file *, struct db_entry *);
};

View File

@ -46,6 +46,17 @@ static inline void __dbe_setup(struct database<T> *db, unsigned int index)
}
}
template <class T>
static inline void __dbe_write(struct database<T> *db, struct db_entry *dbe)
{
if (dbe) {
file_writef(&db->db_file, "%u ", true);
db->db_ops->dbe_write(&db->db_file, dbe);
} else
file_writef(&db->db_file, "%u", false);
file_writef(&db->db_file, "\n");
}
template <class T>
void db_init(struct database<T> *db, const char *filepath, bool autosave,
const struct db_ops *ops)
@ -71,15 +82,8 @@ void db_save(struct database<T> *db)
return;
file_writef(&db->db_file, "%u\n", db_actual_size(db));
for (unsigned int i = 0; i < db_actual_size(db); i++) {
if (db->db_entries[i] == NULL)
file_writef(&db->db_file, "%d\n", false);
else {
file_writef(&db->db_file, "%d ", true);
db->db_entries[i]->write(db->db_file);
file_writef(&db->db_file, "\n");
}
}
for (unsigned int i = 0; i < db_actual_size(db); i++)
__dbe_write(db, db->db_entries[i]);
file_close(&db->db_file);
}

View File

@ -28,13 +28,6 @@ struct index_entry : public db_entry {
* @return IndexEntry::_key.
*/
const std::string primary_key() const;
/**
* Write an IndexEntry to file.
*
* @param file The file to use when writing data.
*/
void write(file &);
};

View File

@ -32,13 +32,6 @@ struct album : public db_entry {
* @return Album::_year / GenericTag::primary_key() (Example: "1998/Hyrule Symphony")
*/
const std::string primary_key() const;
/**
* Write album information to file.
*
* @param file The file to write to.
*/
void write(file &);
};

View File

@ -17,7 +17,6 @@ struct artist : public db_entry {
artist(); /**< Artist tag constructor. */
const std::string primary_key() const;
void write(file &);
};

View File

@ -18,7 +18,6 @@ public:
genre(); /**< Genre tag constructor. */
const std::string primary_key() const;
void write(file &);
};

View File

@ -30,13 +30,6 @@ struct library : public db_entry {
* @return Library::_path.
*/
const std::string primary_key() const;
/**
* Write library information to file.
*
* @param file The file to write to.
*/
void write(file &);
};

View File

@ -44,14 +44,6 @@ struct track : public db_entry {
* @return Track::_library->index() / Track::_path.
*/
const std::string primary_key() const;
/**
* Write track data to file.
*
* @param file The file to write to.
*/
void write(file &);
};

View File

@ -25,8 +25,6 @@ struct int_entry : public db_entry {
g_free(g_val);
return res;
}
void write(file &f) { file_writef(&f, "%u", ie_val); }
};
static unsigned int test_free_count = 0;
@ -57,11 +55,18 @@ static void int_setup(struct db_entry *dbe)
test_setup_count++;
}
static void int_write(struct file *file, struct db_entry *dbe)
{
struct int_entry *ent = (struct int_entry *)dbe;
file_writef(file, "%u", ent->ie_val);
}
static const struct db_ops int_ops = {
int_alloc,
int_free,
int_read,
int_setup,
int_write,
};
@ -77,7 +82,7 @@ static void test_db_entry()
file_init(&f, "test_db_entry", 0);
file_open(&f, OPEN_WRITE);
ent->write(f);
int_ops.dbe_write(&f, ent);
file_close(&f);
int_ops.dbe_free(ent);

View File

@ -29,7 +29,7 @@ static void test_entry()
file_init(&f, "index_entry", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "Zelda\n0 \n");
ie->write(f);
index_ops->dbe_write(&f, ie);
file_close(&f);
index_ops->dbe_free(ie);

View File

@ -32,7 +32,7 @@ static void test_album()
file_init(&f, "album_tag", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "0 \n");
album->write(f);
album_ops->dbe_write(&f, album);
file_close(&f);
album_ops->dbe_free(album);

View File

@ -31,7 +31,7 @@ static void test_artist()
file_init(&f, "artist_tag", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "1 \n1 ");
artist->write(f);
artist_ops->dbe_write(&f, artist);
file_close(&f);
artist_ops->dbe_free(artist);

View File

@ -31,7 +31,7 @@ static void test_genre()
file_init(&f, "genre_tag", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "1 \n1 ");
genre->write(f);
genre_ops->dbe_write(&f, genre);
file_close(&f);
genre_ops->dbe_free(genre);

View File

@ -36,9 +36,9 @@ static void test_library()
file_init(&f, "library_tag", 0);
file_open(&f, OPEN_WRITE);
link->write(f);
library_ops->dbe_write(&f, link);
file_writef(&f, "\n");
zelda->write(f);
library_ops->dbe_write(&f, zelda);
file_close(&f);
file_open(&f, OPEN_READ);

View File

@ -101,7 +101,7 @@ static void test_track()
file_open(&f, OPEN_WRITE);
file_writef(&f, "0 0 0 0 0 0 0 0 0 0 \n");
file_writef(&f, "Hyrule Symphony/00 - No Track.ogg\n");
track->write(f);
track_ops->dbe_write(&f, track);
file_close(&f);
track_ops->dbe_free(track);