From 879c2ef35011b6fbb7bc8755c2b3c4f5c3cc506c Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Mon, 8 Jun 2015 23:07:56 +0200 Subject: [PATCH] Avoid cache regeneration triggering during db_query_run (in case the query is long running, like db_files_update_songalbumid might be) --- src/cache.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cache.h | 6 +++++ src/db.c | 5 ++++ 3 files changed, 84 insertions(+) diff --git a/src/cache.c b/src/cache.c index ca3abce5..a34b7e15 100644 --- a/src/cache.c +++ b/src/cache.c @@ -103,6 +103,7 @@ struct stash // After being triggered wait 60 seconds before rebuilding cache static struct timeval g_wait = { 60, 0 }; +static int g_suspended; // The user may configure a threshold (in msec), and queries slower than // that will have their reply cached @@ -936,6 +937,30 @@ cache_daap_update_timer(struct cache_command *cmd) return 0; } +static int +cache_daap_suspend_timer(struct cache_command *cmd) +{ + if (!g_cacheev) + return -1; + + g_suspended = evtimer_pending(g_cacheev, NULL); + if (g_suspended) + evtimer_del(g_cacheev); + + return 0; +} + +static int +cache_daap_resume_timer(struct cache_command *cmd) +{ + if (!g_cacheev) + return -1; + + if (g_suspended) + evtimer_add(g_cacheev, &g_wait); + + return 0; +} /* * Updates cached timestamps to current time for all cache entries for the given path, if the file was not modfied @@ -1404,6 +1429,54 @@ cache_daap_trigger(void) nonblock_command(cmd); } +void +cache_daap_suspend(void) +{ + struct cache_command *cmd; + + if (!g_initialized) + return; + + cmd = (struct cache_command *)malloc(sizeof(struct cache_command)); + if (!cmd) + { + DPRINTF(E_LOG, L_CACHE, "Could not allocate cache_command\n"); + return; + } + + memset(cmd, 0, sizeof(struct cache_command)); + + cmd->nonblock = 1; + + cmd->func = cache_daap_suspend_timer; + + nonblock_command(cmd); +} + +void +cache_daap_resume(void) +{ + struct cache_command *cmd; + + if (!g_initialized) + return; + + cmd = (struct cache_command *)malloc(sizeof(struct cache_command)); + if (!cmd) + { + DPRINTF(E_LOG, L_CACHE, "Could not allocate cache_command\n"); + return; + } + + memset(cmd, 0, sizeof(struct cache_command)); + + cmd->nonblock = 1; + + cmd->func = cache_daap_resume_timer; + + nonblock_command(cmd); +} + int cache_daap_get(const char *query, struct evbuffer *evbuf) { diff --git a/src/cache.h b/src/cache.h index 7b573bd7..cd342a5e 100644 --- a/src/cache.h +++ b/src/cache.h @@ -15,6 +15,12 @@ void cache_daap_trigger(void); +void +cache_daap_suspend(void); + +void +cache_daap_resume(void); + int cache_daap_get(const char *query, struct evbuffer *evbuf); diff --git a/src/db.c b/src/db.c index fcf6556b..b5bc446e 100644 --- a/src/db.c +++ b/src/db.c @@ -1589,6 +1589,9 @@ db_query_run(char *query, int free, int cache_update) DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); + /* If the query will be long running we don't want the cache to start regenerating */ + cache_daap_suspend(); + ret = db_exec(query, &errmsg); if (ret != SQLITE_OK) DPRINTF(E_LOG, L_DB, "Error '%s' while runnning '%s'\n", errmsg, query); @@ -1600,6 +1603,8 @@ db_query_run(char *query, int free, int cache_update) if (cache_update) cache_daap_trigger(); + else + cache_daap_resume(); return ((ret != SQLITE_OK) ? -1 : 0); }