core/database: Add file versioning to databases

I plan to change the file format of some tags, so add versioning so we
can change things at different times.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-12 08:02:18 -04:00
parent 2f098a2af6
commit 8d9139aea5
13 changed files with 20 additions and 19 deletions

View File

@ -71,14 +71,14 @@ static void __dbe_write(struct database *db, struct db_entry *dbe)
}
void db_init(struct database *db, const char *filepath, bool autosave,
const struct db_ops *ops)
const struct db_ops *ops, unsigned int fmin, unsigned int fcur)
{
db->db_ops = ops;
db->db_size = 0;
db->db_autosave = autosave;
db->db_entries = g_ptr_array_new();
db->db_keys = g_hash_table_new(g_str_hash, g_str_equal);
file_init(&db->db_file, filepath, 0, 0);
file_init(&db->db_file, filepath, fcur, fmin);
}
void db_deinit(struct database *db)

View File

@ -239,7 +239,7 @@ static const struct db_ops album_ops = {
void album_db_init()
{
db_init(&album_db, "album.db", true, &album_ops);
db_init(&album_db, "album.db", true, &album_ops, 0, 0);
db_load_idle(&album_db);
}

View File

@ -60,7 +60,7 @@ static const struct db_ops artist_ops = {
void artist_db_init()
{
db_init(&artist_db, "artist.db", true, &artist_ops);
db_init(&artist_db, "artist.db", true, &artist_ops, 0, 0);
db_load_idle(&artist_db);
}

View File

@ -58,7 +58,7 @@ static const struct db_ops genre_ops = {
void genre_db_init()
{
db_init(&genre_db, "genre.db", true, &genre_ops);
db_init(&genre_db, "genre.db", true, &genre_ops, 0, 0);
db_load_idle(&genre_db);
}

View File

@ -62,7 +62,7 @@ static const struct db_ops library_ops = {
void library_db_init()
{
db_init(&library_db, "library.db", true, &library_ops);
db_init(&library_db, "library.db", true, &library_ops, 0, 0);
db_load_idle(&library_db);
}

View File

@ -184,7 +184,7 @@ static const struct db_ops track_ops = {
void track_db_init()
{
db_init(&track_db, "track.db", false, &track_ops);
db_init(&track_db, "track.db", false, &track_ops, 0, 0);
db_load_idle(&track_db);
}

View File

@ -77,11 +77,11 @@ struct database {
const struct db_ops *db_ops; /* The database's operations vector. */
};
#define DB_INIT(fname, autosave, ops) \
#define DB_INIT(fname, autosave, ops, fmin, fcur) \
{ \
.db_size = 0, \
.db_autosave = autosave, \
.db_file = FILE_INIT(fname, 0, 0), \
.db_file = FILE_INIT(fname, fcur, fmin), \
.db_entries = g_ptr_array_new(), \
.db_keys = g_hash_table_new(g_str_hash, g_str_equal), \
.db_ops = ops, \
@ -92,7 +92,8 @@ struct database {
* Initialize a database using filepath as a location on disk to store data
* and autosave as a hint for if this database should be automatically saved.
*/
void db_init(struct database *, const char *, bool, const struct db_ops *);
void db_init(struct database *, const char *, bool, const struct db_ops *,
unsigned int, unsigned int);
/* Called to prevent memory leaks by freeing all remaining database entries. */
void db_deinit(struct database *);

View File

@ -107,7 +107,7 @@ static void test_db_entry()
static void test_init()
{
struct database db = DB_INIT("init.db", false, &int_ops);
struct database db = DB_INIT("init.db", false, &int_ops, 0, 0);
/* Check initial sizes. */
g_assert_cmpuint(db.db_entries->len, ==, 0);
@ -123,7 +123,7 @@ static void test_init()
static void test_database(gconstpointer arg)
{
unsigned int N = GPOINTER_TO_UINT(arg);
struct database db = DB_INIT("stress.db", false, &int_ops);;
struct database db = DB_INIT("stress.db", false, &int_ops, 0, 0);
struct db_entry *dbe, *next;
struct int_entry rmv;
unsigned int i;
@ -228,8 +228,8 @@ static void test_database(gconstpointer arg)
static void test_save_load()
{
struct database db1 = DB_INIT("save_load.db", true, &int_ops);
struct database db2 = DB_INIT("save_load.db", false, &int_ops);
struct database db1 = DB_INIT("save_load.db", true, &int_ops, 0, 0);
struct database db2 = DB_INIT("save_load.db", false, &int_ops, 0, 0);
struct db_entry *dbe, *next;
const unsigned int N = 10;
unsigned int i;

View File

@ -97,7 +97,7 @@ static void test_album_db()
g_assert(album_get(0) == album);
g_assert_null(album_get(1));
db_init(&album_db, "album.db", false, album_ops);
db_init(&album_db, "album.db", false, album_ops, 0, 0);
db_load(&album_db);
g_assert_cmpuint(album_db.db_size, ==, 1);

View File

@ -102,7 +102,7 @@ static void test_artist_db()
g_assert(artist_get(0) == artist);
g_assert_null(artist_get(1));
db_init(&artist_db, "artist.db", false, artist_ops);
db_init(&artist_db, "artist.db", false, artist_ops, 0, 0);
db_load(&artist_db);
g_assert_cmpuint(artist_db.db_size, ==, 1);

View File

@ -92,7 +92,7 @@ static void test_genere_db()
g_assert(genre_get(0) == genre);
g_assert_null(genre_get(1));
db_init(&genre_db, "genre.db", false, genre_ops);
db_init(&genre_db, "genre.db", false, genre_ops, 0, 0);
db_load(&genre_db);
g_assert_cmpint(genre_db.db_size, ==, 1);

View File

@ -88,7 +88,7 @@ static void test_library_db()
g_assert(library_get(0) == library);
g_assert_null(library_get(1));
db_init(&library_db, "library.db", false, library_ops);
db_init(&library_db, "library.db", false, library_ops, 0, 0);
db_load(&library_db);
g_assert_cmpuint(library_db.db_size, ==, 1);
g_assert_cmpuint(db_actual_size(&library_db), ==, 1);

View File

@ -229,7 +229,7 @@ static void __test_track_db_subprocess()
g_assert_cmpuint(track_db_get()->db_size, ==, 1);
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
db_init(&track_db, "track.db", false, track_ops);
db_init(&track_db, "track.db", false, track_ops, 0, 0);
db_load(&track_db);
g_assert_cmpuint(track_db.db_size, ==, 0);