Commit 348d0c94 authored by Max Kellermann's avatar Max Kellermann

Stats: lazy initialization

Ask the DatabasePlugin for stats when the first client requests them, not at startup.
parent e9ba5fca
...@@ -137,8 +137,6 @@ DatabaseGlobalOpen(Error &error) ...@@ -137,8 +137,6 @@ DatabaseGlobalOpen(Error &error)
db_is_open = true; db_is_open = true;
stats_update();
return true; return true;
} }
......
...@@ -39,6 +39,12 @@ static unsigned start_time; ...@@ -39,6 +39,12 @@ static unsigned start_time;
static DatabaseStats stats; static DatabaseStats stats;
enum class StatsValidity : uint8_t {
INVALID, VALID, FAILED,
};
static StatsValidity stats_validity = StatsValidity::INVALID;
void stats_global_init(void) void stats_global_init(void)
{ {
#ifndef WIN32 #ifndef WIN32
...@@ -46,21 +52,39 @@ void stats_global_init(void) ...@@ -46,21 +52,39 @@ void stats_global_init(void)
#endif #endif
} }
void stats_update(void) void
stats_invalidate()
{ {
assert(GetDatabase() != nullptr); 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); const DatabaseSelection selection("", true);
if (GetDatabase()->GetStats(selection, stats2, error)) { if (GetDatabase()->GetStats(selection, stats, error)) {
stats = stats2; stats_validity = StatsValidity::VALID;
return true;
} else { } else {
LogError(error); LogError(error);
stats.Clear(); stats_validity = StatsValidity::FAILED;
return true;
} }
} }
...@@ -74,7 +98,10 @@ db_stats_print(Client &client) ...@@ -74,7 +98,10 @@ db_stats_print(Client &client)
database plugin */ database plugin */
/* TODO: move this into the "proxy" database plugin as /* TODO: move this into the "proxy" database plugin as
an "idle" handler */ an "idle" handler */
stats_update(); stats_invalidate();
if (!stats_update())
return;
client_printf(client, client_printf(client,
"artists: %u\n" "artists: %u\n"
......
...@@ -24,7 +24,8 @@ class Client; ...@@ -24,7 +24,8 @@ class Client;
void stats_global_init(void); void stats_global_init(void);
void stats_update(void); void
stats_invalidate();
void void
stats_print(Client &client); stats_print(Client &client);
......
...@@ -163,7 +163,7 @@ static void update_finished_event(void) ...@@ -163,7 +163,7 @@ static void update_finished_event(void)
} else { } else {
progress = UPDATE_PROGRESS_IDLE; progress = UPDATE_PROGRESS_IDLE;
stats_update(); stats_invalidate();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment