DatabasePlugin: add method GetUpdateStamp()

Refactor SimpleDatabase::GetLastModified() to be generic for all
plugins.  Remove the SimpleDatabase assumption from db_stats_print(),
allowing it to be implemented by all database plugins.
This commit is contained in:
Max Kellermann
2013-11-22 00:35:29 +01:00
parent 099a2cb586
commit c064e8d62f
6 changed files with 26 additions and 26 deletions

View File

@@ -148,12 +148,12 @@ DatabaseGlobalOpen(Error &error)
return true; return true;
} }
time_t bool
db_get_mtime(void) db_exists()
{ {
assert(db != nullptr); assert(db != nullptr);
assert(db_is_open); assert(db_is_open);
assert(db_is_simple()); assert(db_is_simple());
return ((SimpleDatabase *)db)->GetLastModified(); return ((SimpleDatabase *)db)->GetUpdateStamp() > 0;
} }

View File

@@ -30,6 +30,8 @@
#include "tag/TagType.h" #include "tag/TagType.h"
#include "Compiler.h" #include "Compiler.h"
#include <time.h>
struct config_param; struct config_param;
struct DatabaseSelection; struct DatabaseSelection;
struct db_visitor; struct db_visitor;
@@ -132,6 +134,13 @@ public:
virtual bool GetStats(const DatabaseSelection &selection, virtual bool GetStats(const DatabaseSelection &selection,
DatabaseStats &stats, DatabaseStats &stats,
Error &error) const = 0; Error &error) const = 0;
/**
* Returns the time stamp of the last database update.
* Returns 0 if that is not not known/available.
*/
gcc_pure
virtual time_t GetUpdateStamp() const = 0;
}; };
struct DatabasePlugin { struct DatabasePlugin {

View File

@@ -62,25 +62,13 @@ db_get_directory(const char *name);
bool bool
db_save(Error &error); db_save(Error &error);
/**
* May only be used if db_is_simple() returns true.
*/
gcc_pure
time_t
db_get_mtime(void);
/** /**
* Returns true if there is a valid database file on the disk. * Returns true if there is a valid database file on the disk.
* *
* May only be used if db_is_simple() returns true. * May only be used if db_is_simple() returns true.
*/ */
gcc_pure gcc_pure
static inline bool bool
db_exists(void) db_exists();
{
/* mtime is set only if the database file was loaded or saved
successfully */
return db_get_mtime() > 0;
}
#endif #endif

View File

@@ -83,10 +83,11 @@ db_stats_print(Client &client)
stats.song_count, stats.song_count,
stats.total_duration); stats.total_duration);
if (db_is_simple()) const time_t update_stamp = GetDatabase()->GetUpdateStamp();
if (update_stamp > 0)
client_printf(client, client_printf(client,
"db_update: %lu\n", "db_update: %lu\n",
(unsigned long)db_get_mtime()); (unsigned long)update_stamp);
} }
void void

View File

@@ -71,6 +71,11 @@ public:
DatabaseStats &stats, DatabaseStats &stats,
Error &error) const override; Error &error) const override;
virtual time_t GetUpdateStamp() const override {
// TODO: implement
return 0;
}
private: private:
bool Configure(const config_param &param, Error &error); bool Configure(const config_param &param, Error &error);

View File

@@ -26,8 +26,6 @@
#include <cassert> #include <cassert>
#include <time.h>
struct Directory; struct Directory;
class SimpleDatabase : public Database { class SimpleDatabase : public Database {
@@ -55,11 +53,6 @@ public:
bool Save(Error &error); bool Save(Error &error);
gcc_pure
time_t GetLastModified() const {
return mtime;
}
static Database *Create(const config_param &param, static Database *Create(const config_param &param,
Error &error); Error &error);
@@ -85,6 +78,10 @@ public:
DatabaseStats &stats, DatabaseStats &stats,
Error &error) const override; Error &error) const override;
virtual time_t GetUpdateStamp() const override {
return mtime;
}
protected: protected:
bool Configure(const config_param &param, Error &error); bool Configure(const config_param &param, Error &error);