db/Interface: remove IsPlugin(), use `dynamic_cast` instead

This commit is contained in:
Max Kellermann 2018-11-19 19:19:20 +01:00
parent ed9ece5ea3
commit bda77ffc5b
5 changed files with 15 additions and 30 deletions

View File

@ -216,17 +216,17 @@ glue_db_init_and_load(const ConfigData &config)
std::throw_with_nested(std::runtime_error("Failed to open database plugin")); std::throw_with_nested(std::runtime_error("Failed to open database plugin"));
} }
if (!instance->database->IsPlugin(simple_db_plugin)) auto *db = dynamic_cast<SimpleDatabase *>(instance->database);
if (db == nullptr)
return true; return true;
SimpleDatabase &db = *(SimpleDatabase *)instance->database;
instance->update = new UpdateService(config, instance->update = new UpdateService(config,
instance->event_loop, db, instance->event_loop, *db,
static_cast<CompositeStorage &>(*instance->storage), static_cast<CompositeStorage &>(*instance->storage),
*instance); *instance);
/* run database update after daemonization? */ /* run database update after daemonization? */
return db.FileExists(); return db->FileExists();
} }
static bool static bool

View File

@ -209,12 +209,9 @@ handle_mount(Client &client, Request args, Response &r)
instance.EmitIdle(IDLE_MOUNT); instance.EmitIdle(IDLE_MOUNT);
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
Database *_db = instance.database; if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) {
if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
SimpleDatabase &db = *(SimpleDatabase *)_db;
try { try {
db.Mount(local_uri, remote_uri); db->Mount(local_uri, remote_uri);
} catch (...) { } catch (...) {
composite.Unmount(local_uri); composite.Unmount(local_uri);
throw; throw;
@ -256,11 +253,8 @@ handle_unmount(Client &client, Request args, Response &r)
destroy here */ destroy here */
instance.update->CancelMount(local_uri); instance.update->CancelMount(local_uri);
Database *_db = instance.database; if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) {
if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) { if (db->Unmount(local_uri))
SimpleDatabase &db = *(SimpleDatabase *)_db;
if (db.Unmount(local_uri))
// TODO: call Instance::OnDatabaseModified()? // TODO: call Instance::OnDatabaseModified()?
instance.EmitIdle(IDLE_DATABASE); instance.EmitIdle(IDLE_DATABASE);
} }

View File

@ -52,10 +52,6 @@ public:
return plugin; return plugin;
} }
bool IsPlugin(const DatabasePlugin &other) const noexcept {
return &plugin == &other;
}
/** /**
* Open the database. Read it into memory if applicable. * Open the database. Read it into memory if applicable.
* *

View File

@ -94,11 +94,9 @@ UpdateService::CancelMount(const char *uri)
cancel_current = next.IsDefined() && next.storage == storage2; cancel_current = next.IsDefined() && next.storage == storage2;
} }
Database &_db2 = *lr.directory->mounted_database; if (auto *db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database)) {
if (_db2.IsPlugin(simple_db_plugin)) { queue.Erase(*db2);
SimpleDatabase &db2 = static_cast<SimpleDatabase &>(_db2); cancel_current |= next.IsDefined() && next.db == db2;
queue.Erase(db2);
cancel_current |= next.IsDefined() && next.db == &db2;
} }
if (cancel_current && walk != nullptr) { if (cancel_current && walk != nullptr) {
@ -190,12 +188,10 @@ UpdateService::Enqueue(const char *path, bool discard)
/* follow the mountpoint, update the mounted /* follow the mountpoint, update the mounted
database */ database */
Database &_db2 = *lr.directory->mounted_database; db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database);
if (!_db2.IsPlugin(simple_db_plugin)) if (db2 == nullptr)
throw std::runtime_error("Cannot update this type of database"); throw std::runtime_error("Cannot update this type of database");
db2 = static_cast<SimpleDatabase *>(&_db2);
if (lr.uri == nullptr) { if (lr.uri == nullptr) {
storage2 = storage.GetMount(path); storage2 = storage.GetMount(path);
path = ""; path = "";

View File

@ -106,10 +106,9 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
return true; return true;
} }
Database *db = instance.database; if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) {
if (db != nullptr && db->IsPlugin(simple_db_plugin)) {
try { try {
((SimpleDatabase *)db)->Mount(uri.c_str(), url.c_str()); db->Mount(uri.c_str(), url.c_str());
} catch (...) { } catch (...) {
FormatError(std::current_exception(), FormatError(std::current_exception(),
"Failed to restore mount to %s", "Failed to restore mount to %s",