mirror of
https://github.com/owntone/owntone-server.git
synced 2025-03-31 01:33:44 -04:00
Purge old files and playlists after bulk scan
This commit is contained in:
parent
4e3b29f502
commit
7314dd21c7
54
src/db.c
54
src/db.c
@ -283,6 +283,60 @@ free_pli(struct playlist_info *pli, int content_only)
|
|||||||
free(pli);
|
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 */
|
/* Queries */
|
||||||
static int
|
static int
|
||||||
|
5
src/db.h
5
src/db.h
@ -2,6 +2,8 @@
|
|||||||
#ifndef __DB_H__
|
#ifndef __DB_H__
|
||||||
#define __DB_H__
|
#define __DB_H__
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
@ -181,6 +183,9 @@ free_mfi(struct media_file_info *mfi, int content_only);
|
|||||||
void
|
void
|
||||||
free_pli(struct playlist_info *pli, int content_only);
|
free_pli(struct playlist_info *pli, int content_only);
|
||||||
|
|
||||||
|
void
|
||||||
|
db_purge_cruft(time_t ref);
|
||||||
|
|
||||||
/* Queries */
|
/* Queries */
|
||||||
int
|
int
|
||||||
db_query_start(struct query_params *qp);
|
db_query_start(struct query_params *qp);
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -608,9 +609,12 @@ bulk_scan(void)
|
|||||||
int nlib;
|
int nlib;
|
||||||
int ndirs;
|
int ndirs;
|
||||||
char *path;
|
char *path;
|
||||||
|
time_t start;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
|
start = time(NULL);
|
||||||
|
|
||||||
playlists = NULL;
|
playlists = NULL;
|
||||||
dirstack = NULL;
|
dirstack = NULL;
|
||||||
|
|
||||||
@ -638,6 +642,9 @@ bulk_scan(void)
|
|||||||
|
|
||||||
if (dirstack)
|
if (dirstack)
|
||||||
DPRINTF(E_LOG, L_SCAN, "WARNING: unhandled leftover directories\n");
|
DPRINTF(E_LOG, L_SCAN, "WARNING: unhandled leftover directories\n");
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_SCAN, "Purging old database content\n");
|
||||||
|
db_purge_cruft(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user