core/tags/track: Add support for re-keying the track database

This lets up update track keys to prevent duplicates showing up during a
library path update.

Fixes #93: Library sometimes has duplicated tracks
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-26 15:40:54 -04:00
parent 811509ff80
commit eb8a23f2ff
5 changed files with 32 additions and 1 deletions

View File

@ -2,6 +2,7 @@
- Add a switch to disable GUI tests
- Don't save and restore the "Last Played" tracks column width
- Save databases if they have been upgraded
- Don't duplicate tracks in the library after a defrag
6.5.0-rc:
- Convert to CMake/CTest

View File

@ -42,6 +42,8 @@ bool tags_defragment(void *data)
bool library = library_db_defrag();
bool track = track_db_defrag();
if (library)
track_db_rekey();
if (album || library || track)
track_db_commit();
return track;

View File

@ -210,6 +210,25 @@ bool track_db_defrag()
return db_defrag(&track_db);
}
void track_db_rekey()
{
struct db_entry *dbe, *next;
struct track *track;
unsigned int lib;
gchar *path;
db_for_each(dbe, next, &track_db) {
track = TRACK(dbe);
sscanf(track->tr_path, "%u/%m[^\n]", &lib, &path);
if (lib != library_index(track->tr_library)) {
track->tr_path = __track_key(track->tr_library, path);
db_rekey(&track_db, dbe);
} else
g_free(path);
}
}
const struct database *track_db_get()
{
return &track_db;

View File

@ -72,6 +72,9 @@ void track_db_commit();
/* Called to defragment the track database. */
bool track_db_defrag();
/* Called to update track database keys. */
void track_db_rekey();
/* Called to access the track database. */
const struct database *track_db_get();

View File

@ -275,7 +275,13 @@ static void __test_track_db_subprocess()
g_assert_cmpuint(track_db_count_plays(), ==, 0);
g_assert_cmpuint(track_db_average_plays(), ==, 0);
track_remove_all(library);
/* Rekey the database */
track_get(1)->tr_library = library_find("tests/Music2");
track_db_rekey();
g_assert_cmpstr(track_get(1)->tr_path, ==, "1/Hyrule Symphony/01 - Title Theme.ogg");
track_remove_all(library_get(0));
track_remove_all(library_get(1));
g_assert_cmpuint(track_db_get()->db_size, ==, 0);
}