core/tags/track: Reduce amount of saved data

Artist and genre information are already saved by the album tag.  In
addition, we can save a single date stamp instead of (year, day, month)
triplets.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-12 15:52:00 -04:00
parent 0cefd158d9
commit 71ee59ae22
3 changed files with 52 additions and 36 deletions

View File

@ -7,7 +7,8 @@
#include <glib.h>
#include <taglib/tag_c.h>
#define TRACK_DB_MIN 0 /* Ocarina 6.0 */
#define TRACK_DB_CUR 1 /* Ocarina 6.5 */
static struct database track_db;
static unsigned int unplayed_count = 0;
static unsigned int play_count = 0;
@ -117,22 +118,16 @@ static gchar *track_key(struct db_entry *dbe)
return TRACK(dbe)->tr_path;
}
static struct db_entry *track_read(struct file *file)
static void track_read_v0(struct file *file, struct track *track)
{
unsigned int library_id, artist_id, album_id, genre_id;
unsigned int artist_id, album_id, genre_id;
struct artist *artist;
struct genre *genre;
struct album *album;
struct track *track = __track_alloc();
file_readf(file, "%u %u %u %u %hu", &library_id, &artist_id, &album_id,
&genre_id, &track->tr_track);
file_readf(file, "%u %u %u %hu", &artist_id, &album_id, &genre_id,
&track->tr_track);
date_read(file, &track->tr_date);
file_readf(file, "%hu %hu", &track->tr_count, &track->tr_length);
play_count += track->tr_count;
if (track->tr_count == 0)
unplayed_count++;
album = album_get(album_id);
artist = artist_get(artist_id);
@ -141,16 +136,39 @@ static struct db_entry *track_read(struct file *file)
if (album->al_artist != artist || album->al_genre != genre)
album = album_find(artist, genre, album->al_name, album->al_year);
track->tr_album = album;
track->tr_artist = artist;
track->tr_genre = genre;
track->tr_album = album;
}
static struct db_entry *track_read(struct file *file)
{
struct track *track = __track_alloc();
unsigned int library_id, album_id;
file_readf(file, "%u", &library_id);
track->tr_library = library_get(library_id);
if (file_version(file) == 0)
track_read_v0(file, track);
else {
file_readf(file, "%u %hu", &album_id, &track->tr_track);
track->tr_album = album_get(album_id);
date_read_stamp(file, &track->tr_date);
}
file_readf(file, "%hu %hu", &track->tr_count, &track->tr_length);
play_count += track->tr_count;
if (track->tr_count == 0)
unplayed_count++;
track->tr_title = file_readl(file);
track->tr_artist = track->tr_album->al_artist;
track->tr_genre = track->tr_album->al_genre;
track->tr_tokens = g_str_tokenize_and_fold(track->tr_title, NULL,
&track->tr_alts);
track->tr_path = __track_key(track->tr_library, file_readl(file));
return &track->tr_dbe;
}
static void track_write(struct file *file, struct db_entry *dbe)
@ -158,16 +176,14 @@ static void track_write(struct file *file, struct db_entry *dbe)
struct track *track = TRACK(dbe);
gchar *path = __track_path(track);
file_writef(file, "%u %u %u %u %u ", library_index(track->tr_library),
artist_index(track->tr_artist),
album_index(track->tr_album),
genre_index(track->tr_genre),
track->tr_track);
date_write(file, &track->tr_date);
file_writef(file, " %hu %hu %s\n%s\n", track->tr_count,
track->tr_length,
track->tr_title,
path);
file_writef(file, "%u %u %hu ", library_index(track->tr_library),
album_index(track->tr_album),
track->tr_track);
date_write_stamp(file, &track->tr_date);
file_writef(file, " %hu %hu %s\n%s", track->tr_count,
track->tr_length,
track->tr_title,
path);
g_free(path);
}
@ -185,7 +201,8 @@ static const struct db_ops track_ops = {
void track_db_init()
{
db_init(&track_db, "track.db", false, &track_ops, 0, 0);
db_init(&track_db, "track.db", false, &track_ops, TRACK_DB_MIN,
TRACK_DB_CUR);
db_load_idle(&track_db);
}

View File

@ -7,14 +7,13 @@
* When writing a Track tag to disk, write as many fields as
* possible on one line before spilling over to a second:
*
* library number count
* | artist | year | length
* | | album| | month | | title
* | | | genre | | day| | | path
* | | | | | | | | | | | |
* ... 0 1 2 1 12 2015 10 15 13 232 Ocarina Medley |
* Hyrule Symphony/12 - Ocarina Medley.mp3 <---
* ... 0 1 2 1 13 2015 10 15 10 288 Legend of Zelda Medley
* library datestamp
* | album | count title
* | | number | | length | path
* | | | | | | | |
* ... 0 2 12 3741780495 13 232 Ocarina Medley |
* Hyrule Symphony/12 - Ocarina Medley.mp3 <---
* ... 0 2 13 3741780495 10 288 Legend of Zelda Medley
* Hyrule Symphony/13 - Legend of Zelda Medly.mp3
*/
#ifndef OCARINA_CORE_TAGS_TRACK_H

View File

@ -78,7 +78,7 @@ static void test_track()
struct file f;
gchar *date;
file_init(&f, "track_tag", 0, 0);
file_init(&f, "track_tag", 1, 1);
date = string_tm2str(now);
library = library_find("tests/Music");
@ -94,7 +94,7 @@ static void test_track()
g_assert_false(track_match_token(track, "hyrule"));
file_open(&f, OPEN_WRITE);
file_writef(&f, "0 0 0 0 0 0 0 0 0 0 \n");
file_writef(&f, "0 0 0 0 0 0 \n");
file_writef(&f, "Hyrule Symphony/00 - No Track.ogg\n");
track_ops->dbe_write(&f, &track->tr_dbe);
file_close(&f);
@ -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, 0, 0);
db_init(&track_db, "track.db", false, track_ops, 1, 1);
db_load(&track_db);
g_assert_cmpuint(track_db.db_size, ==, 0);