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:
parent
2f098a2af6
commit
8d9139aea5
|
@ -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,
|
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_ops = ops;
|
||||||
db->db_size = 0;
|
db->db_size = 0;
|
||||||
db->db_autosave = autosave;
|
db->db_autosave = autosave;
|
||||||
db->db_entries = g_ptr_array_new();
|
db->db_entries = g_ptr_array_new();
|
||||||
db->db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
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)
|
void db_deinit(struct database *db)
|
||||||
|
|
|
@ -239,7 +239,7 @@ static const struct db_ops album_ops = {
|
||||||
|
|
||||||
void album_db_init()
|
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);
|
db_load_idle(&album_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ static const struct db_ops artist_ops = {
|
||||||
|
|
||||||
void artist_db_init()
|
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);
|
db_load_idle(&artist_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ static const struct db_ops genre_ops = {
|
||||||
|
|
||||||
void genre_db_init()
|
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);
|
db_load_idle(&genre_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ static const struct db_ops library_ops = {
|
||||||
|
|
||||||
void library_db_init()
|
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);
|
db_load_idle(&library_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -184,7 +184,7 @@ static const struct db_ops track_ops = {
|
||||||
|
|
||||||
void track_db_init()
|
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);
|
db_load_idle(&track_db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,11 +77,11 @@ struct database {
|
||||||
const struct db_ops *db_ops; /* The database's operations vector. */
|
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_size = 0, \
|
||||||
.db_autosave = autosave, \
|
.db_autosave = autosave, \
|
||||||
.db_file = FILE_INIT(fname, 0, 0), \
|
.db_file = FILE_INIT(fname, fcur, fmin), \
|
||||||
.db_entries = g_ptr_array_new(), \
|
.db_entries = g_ptr_array_new(), \
|
||||||
.db_keys = g_hash_table_new(g_str_hash, g_str_equal), \
|
.db_keys = g_hash_table_new(g_str_hash, g_str_equal), \
|
||||||
.db_ops = ops, \
|
.db_ops = ops, \
|
||||||
|
@ -92,7 +92,8 @@ struct database {
|
||||||
* Initialize a database using filepath as a location on disk to store data
|
* 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.
|
* 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. */
|
/* Called to prevent memory leaks by freeing all remaining database entries. */
|
||||||
void db_deinit(struct database *);
|
void db_deinit(struct database *);
|
||||||
|
|
|
@ -107,7 +107,7 @@ static void test_db_entry()
|
||||||
|
|
||||||
static void test_init()
|
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. */
|
/* Check initial sizes. */
|
||||||
g_assert_cmpuint(db.db_entries->len, ==, 0);
|
g_assert_cmpuint(db.db_entries->len, ==, 0);
|
||||||
|
@ -123,7 +123,7 @@ static void test_init()
|
||||||
static void test_database(gconstpointer arg)
|
static void test_database(gconstpointer arg)
|
||||||
{
|
{
|
||||||
unsigned int N = GPOINTER_TO_UINT(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 db_entry *dbe, *next;
|
||||||
struct int_entry rmv;
|
struct int_entry rmv;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
@ -228,8 +228,8 @@ static void test_database(gconstpointer arg)
|
||||||
|
|
||||||
static void test_save_load()
|
static void test_save_load()
|
||||||
{
|
{
|
||||||
struct database db1 = DB_INIT("save_load.db", true, &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);
|
struct database db2 = DB_INIT("save_load.db", false, &int_ops, 0, 0);
|
||||||
struct db_entry *dbe, *next;
|
struct db_entry *dbe, *next;
|
||||||
const unsigned int N = 10;
|
const unsigned int N = 10;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
|
@ -97,7 +97,7 @@ static void test_album_db()
|
||||||
g_assert(album_get(0) == album);
|
g_assert(album_get(0) == album);
|
||||||
g_assert_null(album_get(1));
|
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);
|
db_load(&album_db);
|
||||||
g_assert_cmpuint(album_db.db_size, ==, 1);
|
g_assert_cmpuint(album_db.db_size, ==, 1);
|
||||||
|
|
||||||
|
|
|
@ -102,7 +102,7 @@ static void test_artist_db()
|
||||||
g_assert(artist_get(0) == artist);
|
g_assert(artist_get(0) == artist);
|
||||||
g_assert_null(artist_get(1));
|
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);
|
db_load(&artist_db);
|
||||||
g_assert_cmpuint(artist_db.db_size, ==, 1);
|
g_assert_cmpuint(artist_db.db_size, ==, 1);
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ static void test_genere_db()
|
||||||
g_assert(genre_get(0) == genre);
|
g_assert(genre_get(0) == genre);
|
||||||
g_assert_null(genre_get(1));
|
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);
|
db_load(&genre_db);
|
||||||
g_assert_cmpint(genre_db.db_size, ==, 1);
|
g_assert_cmpint(genre_db.db_size, ==, 1);
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ static void test_library_db()
|
||||||
g_assert(library_get(0) == library);
|
g_assert(library_get(0) == library);
|
||||||
g_assert_null(library_get(1));
|
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);
|
db_load(&library_db);
|
||||||
g_assert_cmpuint(library_db.db_size, ==, 1);
|
g_assert_cmpuint(library_db.db_size, ==, 1);
|
||||||
g_assert_cmpuint(db_actual_size(&library_db), ==, 1);
|
g_assert_cmpuint(db_actual_size(&library_db), ==, 1);
|
||||||
|
|
|
@ -229,7 +229,7 @@ static void __test_track_db_subprocess()
|
||||||
g_assert_cmpuint(track_db_get()->db_size, ==, 1);
|
g_assert_cmpuint(track_db_get()->db_size, ==, 1);
|
||||||
g_assert_cmpuint(track_db_count_unplayed(), ==, 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);
|
db_load(&track_db);
|
||||||
g_assert_cmpuint(track_db.db_size, ==, 0);
|
g_assert_cmpuint(track_db.db_size, ==, 0);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue