mirror of
https://github.com/owntone/owntone-server.git
synced 2025-04-14 08:16:17 -04:00
Wrap bulk scan in a db transaction (credit @chme)
This commit is contained in:
parent
44c3dba3d9
commit
a5b2fbc0fe
38
src/db.c
38
src/db.c
@ -850,6 +850,44 @@ db_get_count(char *query)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Transactions */
|
||||||
|
void
|
||||||
|
db_transaction_begin(void)
|
||||||
|
{
|
||||||
|
char *query = "BEGIN TRANSACTION;";
|
||||||
|
char *errmsg;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
|
|
||||||
|
ret = db_exec(query, &errmsg);
|
||||||
|
if (ret != SQLITE_OK)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "SQL error running '%s': %s\n", query, errmsg);
|
||||||
|
|
||||||
|
sqlite3_free(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
db_transaction_end(void)
|
||||||
|
{
|
||||||
|
char *query = "END TRANSACTION;";
|
||||||
|
char *errmsg;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query);
|
||||||
|
|
||||||
|
ret = db_exec(query, &errmsg);
|
||||||
|
if (ret != SQLITE_OK)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_DB, "SQL error running '%s': %s\n", query, errmsg);
|
||||||
|
|
||||||
|
sqlite3_free(errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Queries */
|
/* Queries */
|
||||||
static int
|
static int
|
||||||
db_build_query_index_clause(struct query_params *qp, char **i)
|
db_build_query_index_clause(struct query_params *qp, char **i)
|
||||||
|
7
src/db.h
7
src/db.h
@ -318,6 +318,13 @@ db_purge_cruft(time_t ref);
|
|||||||
void
|
void
|
||||||
db_purge_all(void);
|
db_purge_all(void);
|
||||||
|
|
||||||
|
/* Transactions */
|
||||||
|
void
|
||||||
|
db_transaction_begin(void);
|
||||||
|
|
||||||
|
void
|
||||||
|
db_transaction_end(void);
|
||||||
|
|
||||||
/* Queries */
|
/* Queries */
|
||||||
int
|
int
|
||||||
db_query_start(struct query_params *qp);
|
db_query_start(struct query_params *qp);
|
||||||
|
@ -97,6 +97,9 @@ static pthread_t tid_scan;
|
|||||||
static struct deferred_pl *playlists;
|
static struct deferred_pl *playlists;
|
||||||
static struct stacked_dir *dirstack;
|
static struct stacked_dir *dirstack;
|
||||||
|
|
||||||
|
/* Count of files scanned during a bulk scan */
|
||||||
|
static int counter;
|
||||||
|
|
||||||
/* Forward */
|
/* Forward */
|
||||||
static void
|
static void
|
||||||
bulk_scan(int flags);
|
bulk_scan(int flags);
|
||||||
@ -694,6 +697,16 @@ process_file(char *file, time_t mtime, off_t size, int type, int flags)
|
|||||||
|
|
||||||
/* Not any kind of special file, so let's see if it's a media file */
|
/* Not any kind of special file, so let's see if it's a media file */
|
||||||
filescanner_process_media(file, mtime, size, type, NULL);
|
filescanner_process_media(file, mtime, size, type, NULL);
|
||||||
|
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
/* When in bulk mode, split transaction in pieces of 200 */
|
||||||
|
if ((flags & F_SCAN_BULK) && (counter % 200 == 0))
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_SCAN, "Scanned %d files...\n", counter);
|
||||||
|
db_transaction_end();
|
||||||
|
db_transaction_begin();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Thread: scan */
|
/* Thread: scan */
|
||||||
@ -931,6 +944,7 @@ bulk_scan(int flags)
|
|||||||
char *path;
|
char *path;
|
||||||
char *deref;
|
char *deref;
|
||||||
time_t start;
|
time_t start;
|
||||||
|
time_t end;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
start = time(NULL);
|
start = time(NULL);
|
||||||
@ -960,7 +974,10 @@ bulk_scan(int flags)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
counter = 0;
|
||||||
|
db_transaction_begin();
|
||||||
process_directories(deref, flags);
|
process_directories(deref, flags);
|
||||||
|
db_transaction_end();
|
||||||
|
|
||||||
free(deref);
|
free(deref);
|
||||||
|
|
||||||
@ -977,14 +994,18 @@ bulk_scan(int flags)
|
|||||||
if (dirstack)
|
if (dirstack)
|
||||||
DPRINTF(E_LOG, L_SCAN, "WARNING: unhandled leftover directories\n");
|
DPRINTF(E_LOG, L_SCAN, "WARNING: unhandled leftover directories\n");
|
||||||
|
|
||||||
|
end = time(NULL);
|
||||||
|
|
||||||
if (flags & F_SCAN_FAST)
|
if (flags & F_SCAN_FAST)
|
||||||
DPRINTF(E_LOG, L_SCAN, "Bulk library scan complete (with file scan disabled)\n");
|
{
|
||||||
|
DPRINTF(E_LOG, L_SCAN, "Bulk library scan completed in %.f sec (with file scan disabled)\n", difftime(end, start));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DPRINTF(E_DBG, L_SCAN, "Purging old database content\n");
|
DPRINTF(E_DBG, L_SCAN, "Purging old database content\n");
|
||||||
db_purge_cruft(start);
|
db_purge_cruft(start);
|
||||||
|
|
||||||
DPRINTF(E_LOG, L_SCAN, "Bulk library scan complete\n");
|
DPRINTF(E_LOG, L_SCAN, "Bulk library scan completed in %.f sec\n", difftime(end, start));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user