From 0ded6d63bf105ad0034f6e51ed540420939e8549 Mon Sep 17 00:00:00 2001 From: chme Date: Wed, 30 Jan 2019 08:36:34 +0100 Subject: [PATCH] [db/jsonapi] New admin value for Last-Modified in JSON API header This adds a new timestamp value "db_modified" into the admin db table. In addition to the existing "db_update" admin value, this value is also updated if rating, play-/skip-count or seek changes for a media_info_file (files db table). This should improve the caching behavior in clients of the JSON API (especially the player web interface) in refreshing its data if some of this values changes. --- src/db.c | 52 +++++++++++++++++++++++++++++---------------- src/db.h | 1 + src/httpd_jsonapi.c | 6 +++--- src/library.c | 5 ++++- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/db.c b/src/db.c index d0b21f47..ec5ea732 100644 --- a/src/db.c +++ b/src/db.c @@ -2540,6 +2540,7 @@ db_file_inc_playcount(int id) " rating = CAST(((play_count + 1.0) / (play_count + skip_count + 2.0) * 100 * 0.75) + ((rating + ((100.0 - rating) / 2.0)) * 0.25) AS INT)" \ " WHERE id = %d;" char *query; + int ret; if (db_rating_updates) query = sqlite3_mprintf(Q_TMPL_WITH_RATING, (int64_t)time(NULL), id); @@ -2552,7 +2553,9 @@ db_file_inc_playcount(int id) return; } - db_query_run(query, 1, 0); + ret = db_query_run(query, 1, 0); + if (ret == 0) + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) time(NULL)); #undef Q_TMPL #undef Q_TMPL_WITH_RATING } @@ -2568,6 +2571,7 @@ db_file_inc_skipcount(int id) " rating = CAST(((play_count + 1.0) / (play_count + skip_count + 2.0) * 100 * 0.75) + ((rating - (rating / 2.0)) * 0.25) AS INT)" \ " WHERE id = %d;" char *query; + int ret; if (db_rating_updates) query = sqlite3_mprintf(Q_TMPL_WITH_RATING, (int64_t)time(NULL), id); @@ -2580,7 +2584,9 @@ db_file_inc_skipcount(int id) return; } - db_query_run(query, 1, 0); + ret = db_query_run(query, 1, 0); + if (ret == 0) + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) time(NULL)); #undef Q_TMPL #undef Q_TMPL_WITH_RATING } @@ -2590,6 +2596,7 @@ db_file_reset_playskip_count(int id) { #define Q_TMPL "UPDATE files SET play_count = 0, skip_count = 0, time_played = 0, time_skipped = 0 WHERE id = %d;" char *query; + int ret; query = sqlite3_mprintf(Q_TMPL, id); if (!query) @@ -2599,7 +2606,9 @@ db_file_reset_playskip_count(int id) return; } - db_query_run(query, 1, 0); + ret = db_query_run(query, 1, 0); + if (ret == 0) + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) time(NULL)); #undef Q_TMPL } @@ -3058,6 +3067,7 @@ db_file_seek_update(int id, uint32_t seek) { #define Q_TMPL "UPDATE files SET seek = %d WHERE id = %d;" char *query; + int ret; if (id == 0) return; @@ -3070,25 +3080,37 @@ db_file_seek_update(int id, uint32_t seek) return; } - db_query_run(query, 1, 0); + ret = db_query_run(query, 1, 0); + if (ret == 0) + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) time(NULL)); #undef Q_TMPL } +static int +db_file_rating_update(char *query) +{ + int ret; + + ret = db_query_run(query, 1, 0); + + if (ret == 0) + { + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) time(NULL)); + listener_notify(LISTENER_RATING); + } + + return ((ret < 0) ? -1 : sqlite3_changes(hdl)); +} + int db_file_rating_update_byid(uint32_t id, uint32_t rating) { #define Q_TMPL "UPDATE files SET rating = %d WHERE id = %d;" char *query; - int ret; query = sqlite3_mprintf(Q_TMPL, rating, id); - ret = db_query_run(query, 1, 0); - - if (ret == 0) - listener_notify(LISTENER_RATING); - - return ((ret < 0) ? -1 : sqlite3_changes(hdl)); + return db_file_rating_update(query); #undef Q_TMPL } @@ -3097,16 +3119,10 @@ db_file_rating_update_byvirtualpath(const char *virtual_path, uint32_t rating) { #define Q_TMPL "UPDATE files SET rating = %d WHERE virtual_path = %Q;" char *query; - int ret; query = sqlite3_mprintf(Q_TMPL, rating, virtual_path); - ret = db_query_run(query, 1, 0); - - if (ret == 0) - listener_notify(LISTENER_RATING); - - return ((ret < 0) ? -1 : sqlite3_changes(hdl)); + return db_file_rating_update(query); #undef Q_TMPL } diff --git a/src/db.h b/src/db.h index d28dfac6..c8e4602a 100644 --- a/src/db.h +++ b/src/db.h @@ -71,6 +71,7 @@ enum query_type { #define DB_ADMIN_SCHEMA_VERSION "schema_version" #define DB_ADMIN_QUEUE_VERSION "queue_version" #define DB_ADMIN_DB_UPDATE "db_update" +#define DB_ADMIN_DB_MODIFIED "db_modified" #define DB_ADMIN_START_TIME "start_time" #define DB_ADMIN_LASTFM_SESSION_KEY "lastfm_sk" #define DB_ADMIN_SPOTIFY_REFRESH_TOKEN "spotify_refresh_token" diff --git a/src/httpd_jsonapi.c b/src/httpd_jsonapi.c index cac40a5e..f640acc2 100644 --- a/src/httpd_jsonapi.c +++ b/src/httpd_jsonapi.c @@ -2590,7 +2590,7 @@ jsonapi_reply_library_album_tracks(struct httpd_request *hreq) int total; int ret = 0; - db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_UPDATE); + db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_MODIFIED); if (db_update && httpd_request_not_modified_since(hreq->req, &db_update)) return HTTP_NOTMODIFIED; @@ -2644,7 +2644,7 @@ jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq) json_object *reply = NULL; int ret = 0; - db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_UPDATE); + db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_MODIFIED); if (db_update && httpd_request_not_modified_since(hreq->req, &db_update)) return HTTP_NOTMODIFIED; @@ -2836,7 +2836,7 @@ jsonapi_reply_library_playlist_tracks(struct httpd_request *hreq) int total; int ret = 0; - db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_UPDATE); + db_update = (time_t) db_admin_getint64(DB_ADMIN_DB_MODIFIED); if (db_update && httpd_request_not_modified_since(hreq->req, &db_update)) return HTTP_NOTMODIFIED; diff --git a/src/library.c b/src/library.c index dac3bfb6..de038c34 100644 --- a/src/library.c +++ b/src/library.c @@ -98,6 +98,7 @@ static short deferred_update_events; static bool handle_deferred_update_notifications(void) { + time_t update_time; bool ret = (deferred_update_notifications > 0); if (ret) @@ -105,7 +106,9 @@ handle_deferred_update_notifications(void) DPRINTF(E_DBG, L_LIB, "Database changed (%d changes)\n", deferred_update_notifications); deferred_update_notifications = 0; - db_admin_setint64(DB_ADMIN_DB_UPDATE, (int64_t) time(NULL)); + update_time = time(NULL); + db_admin_setint64(DB_ADMIN_DB_UPDATE, (int64_t) update_time); + db_admin_setint64(DB_ADMIN_DB_MODIFIED, (int64_t) update_time); } return ret;