From 3d3c27190b9c004c2a8b8edcfbc93cb475e67d38 Mon Sep 17 00:00:00 2001 From: chme Date: Wed, 14 Dec 2016 19:28:27 +0100 Subject: [PATCH 1/3] [db] Add function to cleanup the 'groups' table (artists and albums) Allows removing of item no longer referenced from the files table --- src/db.c | 38 ++++++++++++++++++++++++++++++++++++++ src/db.h | 3 +++ 2 files changed, 41 insertions(+) diff --git a/src/db.c b/src/db.c index 60e13ad7..5ff3c1b9 100644 --- a/src/db.c +++ b/src/db.c @@ -3385,6 +3385,44 @@ db_groups_clear(void) return db_query_run("DELETE FROM groups;", 0, 1); } +/* + * Remove album and artist entries in the groups table that are not longer referenced from the files table + */ +int +db_groups_cleanup() +{ +#define Q_TMPL_ALBUM "DELETE FROM groups WHERE type = 1 AND NOT persistentid IN (SELECT songalbumid from files WHERE disabled = 0);" +#define Q_TMPL_ARTIST "DELETE FROM groups WHERE type = 2 AND NOT persistentid IN (SELECT songartistid from files WHERE disabled = 0);" + + int ret; + + db_transaction_begin(); + + ret = db_query_run(Q_TMPL_ALBUM, 0, 0); + if (ret < 0) + { + db_transaction_rollback(); + return -1; + } + + DPRINTF(E_DBG, L_DB, "Removed album group-entries: %d\n", sqlite3_changes(hdl)); + + ret = db_query_run(Q_TMPL_ARTIST, 0, 0); + if (ret < 0) + { + db_transaction_rollback(); + return -1; + } + + DPRINTF(E_DBG, L_DB, "Removed artist group-entries: %d\n", sqlite3_changes(hdl)); + db_transaction_end(); + + return 0; + +#undef Q_TMPL_ALBUM +#undef Q_TMPL_ARTIST +} + static enum group_type db_group_type_bypersistentid(int64_t persistentid) { diff --git a/src/db.h b/src/db.h index 92837deb..ca99fd15 100644 --- a/src/db.h +++ b/src/db.h @@ -617,6 +617,9 @@ db_pl_enable_bycookie(uint32_t cookie, char *path); int db_groups_clear(void); +int +db_groups_cleanup(); + int db_group_persistentid_byid(int id, int64_t *persistentid); From f5fe1e665a8495c1bb85cbff7da749320f2b4cfb Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 17 Dec 2016 07:38:00 +0100 Subject: [PATCH 2/3] [filescanner] Remove rebuilding of persistent ids (artist, album) The hash is not portable, therefor this removes the ability to build the database on a different machine than the one running forked-daapd. It also removes the clearing of the groups table and replaces it with a cleanup call (removes unreferenced entries from the groups table). --- src/filescanner.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/filescanner.c b/src/filescanner.c index d8e93e2d..b44f3366 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -1221,13 +1221,18 @@ bulk_scan(int flags) } else { + db_transaction_begin(); /* Protect spotify from the imminent purge if rescanning */ db_file_ping_bymatch("spotify:", 0); db_pl_ping_bymatch("spotify:", 0); DPRINTF(E_DBG, L_SCAN, "Purging old database content\n"); db_purge_cruft(start); + db_groups_cleanup(); db_queue_cleanup(); + + db_transaction_end(); + cache_artwork_purge_cruft(start); DPRINTF(E_LOG, L_SCAN, "Bulk library scan completed in %.f sec\n", difftime(end, start)); @@ -1278,14 +1283,6 @@ filescanner(void *arg) pthread_exit(NULL); } - ret = db_groups_clear(); - if (ret < 0) - { - DPRINTF(E_LOG, L_SCAN, "Error: could not clear old groups from DB\n"); - - pthread_exit(NULL); - } - // Only clear the queue if enabled (default) in config clear_queue_on_stop_disabled = cfg_getbool(cfg_getsec(cfg, "mpd"), "clear_queue_on_stop_disable"); if (!clear_queue_on_stop_disabled) @@ -1299,13 +1296,6 @@ filescanner(void *arg) } } - /* Recompute all songartistids and songalbumids, in case the SQLite DB got transferred - * to a different host; the hash is not portable. - * It will also rebuild the groups we just cleared. - */ - db_files_update_songartistid(); - db_files_update_songalbumid(); - if (cfg_getbool(cfg_getsec(cfg, "library"), "filescan_disable")) bulk_scan(F_SCAN_BULK | F_SCAN_FAST); else From 7856498951111eb33de115db1590a43df6182cde Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 17 Dec 2016 15:43:39 +0100 Subject: [PATCH 3/3] [db] Remove unused functions db_files_update_songartistid, db_files_update_songalbumid --- src/db.c | 12 ------------ src/db.h | 6 ------ 2 files changed, 18 deletions(-) diff --git a/src/db.c b/src/db.c index 5ff3c1b9..784ae209 100644 --- a/src/db.c +++ b/src/db.c @@ -2024,18 +2024,6 @@ db_files_get_count_bymatch(char *path) #undef Q_TMPL } -void -db_files_update_songartistid(void) -{ - db_query_run("UPDATE files SET songartistid = daap_songalbumid(LOWER(album_artist), '');", 0, 1); -} - -void -db_files_update_songalbumid(void) -{ - db_query_run("UPDATE files SET songalbumid = daap_songalbumid(LOWER(album_artist), LOWER(album));", 0, 1); -} - void db_file_inc_playcount(int id) { diff --git a/src/db.h b/src/db.h index ca99fd15..ab0e53a5 100644 --- a/src/db.h +++ b/src/db.h @@ -498,12 +498,6 @@ db_files_get_album_count(void); int db_files_get_count_bymatch(char *path); -void -db_files_update_songartistid(void); - -void -db_files_update_songalbumid(void); - void db_file_inc_playcount(int id);