diff options
author | Max Kellermann <max@duempel.org> | 2014-01-10 23:40:05 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-10 23:42:46 +0100 |
commit | 348d0c944e92e8dfb361190b97a168f27a7333b8 (patch) | |
tree | b0b807712792435f02bf752e2e34e7d15184fb14 /src | |
parent | e9ba5fcaf319654ce685cd329414fcf2591c9c95 (diff) | |
download | mpd-348d0c944e92e8dfb361190b97a168f27a7333b8.tar.gz mpd-348d0c944e92e8dfb361190b97a168f27a7333b8.tar.xz mpd-348d0c944e92e8dfb361190b97a168f27a7333b8.zip |
Stats: lazy initialization
Ask the DatabasePlugin for stats when the first client requests them,
not at startup.
Diffstat (limited to 'src')
-rw-r--r-- | src/DatabaseGlue.cxx | 2 | ||||
-rw-r--r-- | src/Stats.cxx | 41 | ||||
-rw-r--r-- | src/Stats.hxx | 3 | ||||
-rw-r--r-- | src/UpdateGlue.cxx | 2 |
4 files changed, 37 insertions, 11 deletions
diff --git a/src/DatabaseGlue.cxx b/src/DatabaseGlue.cxx index bc99baa4f..f64b31738 100644 --- a/src/DatabaseGlue.cxx +++ b/src/DatabaseGlue.cxx @@ -137,8 +137,6 @@ DatabaseGlobalOpen(Error &error) db_is_open = true; - stats_update(); - return true; } diff --git a/src/Stats.cxx b/src/Stats.cxx index e22f1a995..1764516ef 100644 --- a/src/Stats.cxx +++ b/src/Stats.cxx @@ -39,6 +39,12 @@ static unsigned start_time; static DatabaseStats stats; +enum class StatsValidity : uint8_t { + INVALID, VALID, FAILED, +}; + +static StatsValidity stats_validity = StatsValidity::INVALID; + void stats_global_init(void) { #ifndef WIN32 @@ -46,21 +52,39 @@ void stats_global_init(void) #endif } -void stats_update(void) +void +stats_invalidate() { assert(GetDatabase() != nullptr); - Error error; + stats_validity = StatsValidity::INVALID; +} + +static bool +stats_update() +{ + switch (stats_validity) { + case StatsValidity::INVALID: + break; + + case StatsValidity::VALID: + return true; + + case StatsValidity::FAILED: + return false; + } - DatabaseStats stats2; + Error error; const DatabaseSelection selection("", true); - if (GetDatabase()->GetStats(selection, stats2, error)) { - stats = stats2; + if (GetDatabase()->GetStats(selection, stats, error)) { + stats_validity = StatsValidity::VALID; + return true; } else { LogError(error); - stats.Clear(); + stats_validity = StatsValidity::FAILED; + return true; } } @@ -74,7 +98,10 @@ db_stats_print(Client &client) database plugin */ /* TODO: move this into the "proxy" database plugin as an "idle" handler */ - stats_update(); + stats_invalidate(); + + if (!stats_update()) + return; client_printf(client, "artists: %u\n" diff --git a/src/Stats.hxx b/src/Stats.hxx index 63b96c4d6..e11be0d68 100644 --- a/src/Stats.hxx +++ b/src/Stats.hxx @@ -24,7 +24,8 @@ class Client; void stats_global_init(void); -void stats_update(void); +void +stats_invalidate(); void stats_print(Client &client); diff --git a/src/UpdateGlue.cxx b/src/UpdateGlue.cxx index 12ea126a9..52a5802ce 100644 --- a/src/UpdateGlue.cxx +++ b/src/UpdateGlue.cxx @@ -163,7 +163,7 @@ static void update_finished_event(void) } else { progress = UPDATE_PROGRESS_IDLE; - stats_update(); + stats_invalidate(); } } |