Speedup startup rescan

This commit is contained in:
Julien BLACHE 2011-06-12 11:06:37 +02:00
parent 96c367f556
commit 1dfd27090e
3 changed files with 25 additions and 19 deletions

View File

@ -1571,14 +1571,14 @@ db_file_inc_playcount(int id)
} }
void 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 *query;
char *errmsg; char *errmsg;
int ret; int ret;
query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), path); query = sqlite3_mprintf(Q_TMPL, (int64_t)time(NULL), id);
if (!query) if (!query)
{ {
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); 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); ret = db_exec(query, &errmsg);
if (ret != SQLITE_OK) 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(errmsg);
sqlite3_free(query); sqlite3_free(query);
@ -1795,21 +1795,22 @@ db_file_id_byurl(char *url)
#undef Q_TMPL #undef Q_TMPL
} }
time_t void
db_file_stamp_bypath(char *path) 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; char *query;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
time_t stamp;
int ret; int ret;
*stamp = 0;
query = sqlite3_mprintf(Q_TMPL, path); query = sqlite3_mprintf(Q_TMPL, path);
if (!query) if (!query)
{ {
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); 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); 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)); DPRINTF(E_LOG, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl));
sqlite3_free(query); sqlite3_free(query);
return 0; return;
} }
ret = db_blocking_step(stmt); ret = db_blocking_step(stmt);
@ -1833,10 +1834,11 @@ db_file_stamp_bypath(char *path)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
sqlite3_free(query); 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 #ifdef DB_PROFILE
while (db_blocking_step(stmt) == SQLITE_ROW) while (db_blocking_step(stmt) == SQLITE_ROW)
@ -1846,8 +1848,6 @@ db_file_stamp_bypath(char *path)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
sqlite3_free(query); sqlite3_free(query);
return stamp;
#undef Q_TMPL #undef Q_TMPL
} }
@ -3966,6 +3966,9 @@ db_perthread_deinit(void)
" path VARCHAR(4096) NOT NULL" \ " path VARCHAR(4096) NOT NULL" \
");" ");"
#define I_RESCAN \
"CREATE INDEX IF NOT EXISTS idx_rescan ON files(path, db_timestamp);"
#define I_FILEPATH \ #define I_FILEPATH \
"CREATE INDEX IF NOT EXISTS idx_filepath ON playlistitems(filepath ASC);" "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_SPEAKERS, "create table speakers" },
{ T_INOTIFY, "create table inotify" }, { T_INOTIFY, "create table inotify" },
{ I_RESCAN, "create rescan index" },
{ I_FILEPATH, "create file path index" }, { I_FILEPATH, "create file path index" },
{ I_PLITEMID, "create playlist id index" }, { I_PLITEMID, "create playlist id index" },
{ I_PAIRING, "create pairing guid index" }, { I_PAIRING, "create pairing guid index" },

View File

@ -326,7 +326,7 @@ void
db_file_inc_playcount(int id); db_file_inc_playcount(int id);
void void
db_file_ping(char *path); db_file_ping(int id);
char * char *
db_file_path_byid(int id); db_file_path_byid(int id);
@ -343,8 +343,8 @@ db_file_id_byfile(char *filename);
int int
db_file_id_byurl(char *url); db_file_id_byurl(char *url);
time_t void
db_file_stamp_bypath(char *path); db_file_stamp_bypath(char *path, time_t *stamp, int *id);
struct media_file_info * struct media_file_info *
db_file_fetch_byid(int id); db_file_fetch_byid(int id);

View File

@ -301,13 +301,14 @@ process_media_file(char *file, time_t mtime, off_t size, int compilation)
char *filename; char *filename;
char *ext; char *ext;
time_t stamp; time_t stamp;
int id;
int ret; int ret;
stamp = db_file_stamp_bypath(file); db_file_stamp_bypath(file, &stamp, &id);
if (stamp >= mtime) if (stamp >= mtime)
{ {
db_file_ping(file); db_file_ping(id);
return; return;
} }