From 1dfd27090e0572e8b69c0ce13a598647f3c9e9e3 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sun, 12 Jun 2011 11:06:37 +0200 Subject: [PATCH] Speedup startup rescan --- src/db.c | 33 +++++++++++++++++++-------------- src/db.h | 6 +++--- src/filescanner.c | 5 +++-- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/db.c b/src/db.c index b4b3ac20..17082b85 100644 --- a/src/db.c +++ b/src/db.c @@ -1571,14 +1571,14 @@ db_file_inc_playcount(int id) } void -db_file_ping(char *path) +db_file_ping(int id) { -#define Q_TMPL "UPDATE files SET db_timestamp = %" PRIi64 ", disabled = 0 WHERE path = '%q';" +#define Q_TMPL "UPDATE files SET db_timestamp = %" PRIi64 ", disabled = 0 WHERE id = %d;" char *query; char *errmsg; int ret; - query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), path); + query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), id); if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); @@ -1590,7 +1590,7 @@ db_file_ping(char *path) ret = db_exec(query, &errmsg); if (ret != SQLITE_OK) - DPRINTF(E_LOG, L_DB, "Error pinging file '%s': %s\n", path, errmsg); + DPRINTF(E_LOG, L_DB, "Error pinging file ID %d: %s\n", id, errmsg); sqlite3_free(errmsg); sqlite3_free(query); @@ -1795,21 +1795,22 @@ db_file_id_byurl(char *url) #undef Q_TMPL } -time_t -db_file_stamp_bypath(char *path) +void +db_file_stamp_bypath(char *path, time_t *stamp, int *id) { -#define Q_TMPL "SELECT db_timestamp FROM files WHERE path = '%q';" +#define Q_TMPL "SELECT id, db_timestamp FROM files WHERE path = '%q';" char *query; sqlite3_stmt *stmt; - time_t stamp; int ret; + *stamp = 0; + query = sqlite3_mprintf(Q_TMPL, path); if (!query) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); - return 0; + return; } DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); @@ -1820,7 +1821,7 @@ db_file_stamp_bypath(char *path) DPRINTF(E_LOG, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl)); sqlite3_free(query); - return 0; + return; } ret = db_blocking_step(stmt); @@ -1833,10 +1834,11 @@ db_file_stamp_bypath(char *path) sqlite3_finalize(stmt); sqlite3_free(query); - return 0; + return; } - stamp = (time_t)sqlite3_column_int64(stmt, 0); + *id = sqlite3_column_int(stmt, 0); + *stamp = (time_t)sqlite3_column_int64(stmt, 1); #ifdef DB_PROFILE while (db_blocking_step(stmt) == SQLITE_ROW) @@ -1846,8 +1848,6 @@ db_file_stamp_bypath(char *path) sqlite3_finalize(stmt); sqlite3_free(query); - return stamp; - #undef Q_TMPL } @@ -3966,6 +3966,9 @@ db_perthread_deinit(void) " path VARCHAR(4096) NOT NULL" \ ");" +#define I_RESCAN \ + "CREATE INDEX IF NOT EXISTS idx_rescan ON files(path, db_timestamp);" + #define I_FILEPATH \ "CREATE INDEX IF NOT EXISTS idx_filepath ON playlistitems(filepath ASC);" @@ -4032,6 +4035,8 @@ static const struct db_init_query db_init_queries[] = { T_SPEAKERS, "create table speakers" }, { T_INOTIFY, "create table inotify" }, + { I_RESCAN, "create rescan index" }, + { I_FILEPATH, "create file path index" }, { I_PLITEMID, "create playlist id index" }, { I_PAIRING, "create pairing guid index" }, diff --git a/src/db.h b/src/db.h index 5e4bba89..b3db97ec 100644 --- a/src/db.h +++ b/src/db.h @@ -326,7 +326,7 @@ void db_file_inc_playcount(int id); void -db_file_ping(char *path); +db_file_ping(int id); char * db_file_path_byid(int id); @@ -343,8 +343,8 @@ db_file_id_byfile(char *filename); int db_file_id_byurl(char *url); -time_t -db_file_stamp_bypath(char *path); +void +db_file_stamp_bypath(char *path, time_t *stamp, int *id); struct media_file_info * db_file_fetch_byid(int id); diff --git a/src/filescanner.c b/src/filescanner.c index 30ce89aa..4c865a0a 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -301,13 +301,14 @@ process_media_file(char *file, time_t mtime, off_t size, int compilation) char *filename; char *ext; time_t stamp; + int id; int ret; - stamp = db_file_stamp_bypath(file); + db_file_stamp_bypath(file, &stamp, &id); if (stamp >= mtime) { - db_file_ping(file); + db_file_ping(id); return; }