Purge old files and playlists after bulk scan

This commit is contained in:
Julien BLACHE 2009-06-07 20:49:13 +02:00
parent 4e3b29f502
commit 7314dd21c7
3 changed files with 66 additions and 0 deletions

View File

@ -283,6 +283,60 @@ free_pli(struct playlist_info *pli, int content_only)
free(pli);
}
void
db_purge_cruft(time_t ref)
{
char *errmsg;
int i;
int ret;
char *queries[4] = { NULL, NULL, NULL, NULL };
char *queries_tmpl[4] =
{
"DELETE FROM playlistitems WHERE playlistid IN (SELECT id FROM playlists WHERE id <> 1 AND db_timestamp < %" PRIi64 ");",
"DELETE FROM playlists WHERE id <> 1 AND db_timestamp < %" PRIi64 ";",
"DELETE FROM playlistitems WHERE songid IN (SELECT id FROM songs WHERE db_timestamp < %" PRIi64 ");",
"DELETE FROM songs WHERE db_timestamp < %" PRIi64 ";"
};
if (sizeof(queries) != sizeof(queries_tmpl))
{
DPRINTF(E_LOG, L_DB, "db_purge_cruft(): queries out of sync with queries_tmpl\n");
return;
}
for (i = 0; i < (sizeof(queries_tmpl) / sizeof(queries_tmpl[0])); i++)
{
queries[i] = sqlite3_mprintf(queries_tmpl[i], (int64_t)ref);
if (!queries[i])
{
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
goto purge_fail;
}
}
for (i = 0; i < (sizeof(queries) / sizeof(queries[0])); i++)
{
DPRINTF(E_DBG, L_DB, "Running purge query '%s'\n", queries[i]);
ret = sqlite3_exec(hdl, queries[i], NULL, NULL, &errmsg);
if (ret != SQLITE_OK)
{
DPRINTF(E_LOG, L_DB, "Purge query %d error: %s\n", i, errmsg);
sqlite3_free(errmsg);
}
else
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
}
purge_fail:
for (i = 0; i < (sizeof(queries) / sizeof(queries[0])); i++)
{
sqlite3_free(queries[i]);
}
}
/* Queries */
static int

View File

@ -2,6 +2,8 @@
#ifndef __DB_H__
#define __DB_H__
#include <time.h>
#include <stdint.h>
#include <sqlite3.h>
@ -181,6 +183,9 @@ free_mfi(struct media_file_info *mfi, int content_only);
void
free_pli(struct playlist_info *pli, int content_only);
void
db_purge_cruft(time_t ref);
/* Queries */
int
db_query_start(struct query_params *qp);

View File

@ -29,6 +29,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -608,9 +609,12 @@ bulk_scan(void)
int nlib;
int ndirs;
char *path;
time_t start;
int i;
int j;
start = time(NULL);
playlists = NULL;
dirstack = NULL;
@ -638,6 +642,9 @@ bulk_scan(void)
if (dirstack)
DPRINTF(E_LOG, L_SCAN, "WARNING: unhandled leftover directories\n");
DPRINTF(E_DBG, L_SCAN, "Purging old database content\n");
db_purge_cruft(start);
}