# HG changeset patch # User Sebastien Jodogne # Date 1617268573 -7200 # Node ID c8e06b41feeca6fedf7438123388dc99492c697e # Parent 73cc85f3d9c15387e82284908fff271d64cd19ec refactoring registration/finalization of index backend diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/DatabaseBackendAdapterV2.cpp --- a/Framework/Plugins/DatabaseBackendAdapterV2.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/DatabaseBackendAdapterV2.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -1437,8 +1437,22 @@ } - void DatabaseBackendAdapterV2::Register(IDatabaseBackend& backend) + static std::unique_ptr backend_; + + void DatabaseBackendAdapterV2::Register(IDatabaseBackend* backend) { + if (backend == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + + if (backend_.get() != NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + backend_.reset(backend); + OrthancPluginDatabaseBackend params; memset(¶ms, 0, sizeof(params)); @@ -1515,7 +1529,7 @@ extensions.getLastChangeIndex = GetLastChangeIndex; extensions.tagMostRecentPatient = TagMostRecentPatient; - if (backend.HasCreateInstance()) + if (backend_->HasCreateInstance()) { extensions.createInstance = CreateInstance; // Fast creation of resources } @@ -1530,7 +1544,7 @@ # endif #endif - OrthancPluginContext* context = backend.GetContext(); + OrthancPluginContext* context = backend_->GetContext(); if (performanceWarning) { @@ -1550,12 +1564,18 @@ } OrthancPluginDatabaseContext* database = - OrthancPluginRegisterDatabaseBackendV2(context, ¶ms, &extensions, &backend); + OrthancPluginRegisterDatabaseBackendV2(context, ¶ms, &extensions, backend_.get()); if (database == NULL) { throw std::runtime_error("Unable to register the database backend"); } - backend.SetOutputFactory(new Factory(context, database)); + backend_->SetOutputFactory(new Factory(context, database)); + } + + + void DatabaseBackendAdapterV2::Finalize() + { + backend_.reset(NULL); } } diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/DatabaseBackendAdapterV2.h --- a/Framework/Plugins/DatabaseBackendAdapterV2.h Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/DatabaseBackendAdapterV2.h Thu Apr 01 11:16:13 2021 +0200 @@ -70,14 +70,8 @@ virtual IDatabaseBackendOutput* CreateOutput() ORTHANC_OVERRIDE; }; + static void Register(IDatabaseBackend* backend); - /** - * Register a custom database back-end written in C++. - * - * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param backend Your custom database engine. - **/ - - static void Register(IDatabaseBackend& backend); + static void Finalize(); }; } diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/DatabaseBackendAdapterV3.cpp --- a/Framework/Plugins/DatabaseBackendAdapterV3.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/DatabaseBackendAdapterV3.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -1755,8 +1755,22 @@ } - void DatabaseBackendAdapterV3::Register(IndexBackend& database) + static std::unique_ptr backend_; + + void DatabaseBackendAdapterV3::Register(IndexBackend* backend) { + if (backend == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + + if (backend_.get() != NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + backend_.reset(backend); + OrthancPluginDatabaseBackendV3 params; memset(¶ms, 0, sizeof(params)); @@ -1829,14 +1843,20 @@ params.setProtectedPatient = SetProtectedPatient; params.setResourcesContent = SetResourcesContent; - OrthancPluginContext* context = database.GetContext(); + OrthancPluginContext* context = backend_->GetContext(); - if (OrthancPluginRegisterDatabaseBackendV3(context, ¶ms, sizeof(params), &database) != OrthancPluginErrorCode_Success) + if (OrthancPluginRegisterDatabaseBackendV3(context, ¶ms, sizeof(params), backend_.get()) != OrthancPluginErrorCode_Success) { throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Unable to register the database backend"); } - database.SetOutputFactory(new Factory); + backend_->SetOutputFactory(new Factory); + } + + + void DatabaseBackendAdapterV3::Finalize() + { + backend_.reset(NULL); } } diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/DatabaseBackendAdapterV3.h --- a/Framework/Plugins/DatabaseBackendAdapterV3.h Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/DatabaseBackendAdapterV3.h Thu Apr 01 11:16:13 2021 +0200 @@ -56,7 +56,9 @@ virtual IDatabaseBackendOutput* CreateOutput() ORTHANC_OVERRIDE; }; - static void Register(IndexBackend& backend); + static void Register(IndexBackend* backend); + + static void Finalize(); }; } diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/IndexBackend.cpp --- a/Framework/Plugins/IndexBackend.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/IndexBackend.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -2221,13 +2221,18 @@ } - void IndexBackend::Register(IndexBackend& backend) + void IndexBackend::Register(IndexBackend* backend) { + if (backend == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + bool hasLoadedV3 = false; #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) - if (OrthancPluginCheckVersionAdvanced(backend.GetContext(), 1, 9, 2) == 1) + if (OrthancPluginCheckVersionAdvanced(backend->GetContext(), 1, 9, 2) == 1) { OrthancDatabases::DatabaseBackendAdapterV3::Register(backend); hasLoadedV3 = true; @@ -2241,4 +2246,16 @@ OrthancDatabases::DatabaseBackendAdapterV2::Register(backend); } } + + + void IndexBackend::Finalize() + { + OrthancDatabases::DatabaseBackendAdapterV2::Finalize(); + +#if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 +# if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) + OrthancDatabases::DatabaseBackendAdapterV3::Finalize(); +# endif +#endif + } } diff -r 73cc85f3d9c1 -r c8e06b41feec Framework/Plugins/IndexBackend.h --- a/Framework/Plugins/IndexBackend.h Tue Mar 30 10:40:34 2021 +0200 +++ b/Framework/Plugins/IndexBackend.h Thu Apr 01 11:16:13 2021 +0200 @@ -357,6 +357,8 @@ const char* hashSeries, const char* hashInstance); - static void Register(IndexBackend& backend); + static void Register(IndexBackend* backend); + + static void Finalize(); }; } diff -r 73cc85f3d9c1 -r c8e06b41feec MySQL/Plugins/IndexPlugin.cpp --- a/MySQL/Plugins/IndexPlugin.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/MySQL/Plugins/IndexPlugin.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -23,13 +23,10 @@ #include "../../Framework/MySQL/MySQLDatabase.h" #include "../../Framework/Plugins/PluginInitialization.h" -#include // For std::unique_ptr<> #include #include #include -static std::unique_ptr backend_; - extern "C" { @@ -66,12 +63,7 @@ try { OrthancDatabases::MySQLParameters parameters(mysql, configuration); - - /* Create the database back-end */ - backend_.reset(new OrthancDatabases::MySQLIndex(context, parameters)); - - /* Register the MySQL index into Orthanc */ - OrthancDatabases::IndexBackend::Register(*backend_); + OrthancDatabases::IndexBackend::Register(new OrthancDatabases::MySQLIndex(context, parameters)); } catch (Orthanc::OrthancException& e) { @@ -91,8 +83,8 @@ ORTHANC_PLUGINS_API void OrthancPluginFinalize() { LOG(WARNING) << "MySQL index is finalizing"; + OrthancDatabases::IndexBackend::Finalize(); - backend_.reset(NULL); OrthancDatabases::MySQLDatabase::GlobalFinalization(); Orthanc::HttpClient::GlobalFinalize(); Orthanc::Toolbox::FinalizeOpenSsl(); diff -r 73cc85f3d9c1 -r c8e06b41feec PostgreSQL/Plugins/IndexPlugin.cpp --- a/PostgreSQL/Plugins/IndexPlugin.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/PostgreSQL/Plugins/IndexPlugin.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -22,11 +22,8 @@ #include "PostgreSQLIndex.h" #include "../../Framework/Plugins/PluginInitialization.h" -#include // For std::unique_ptr<> #include -static std::unique_ptr backend_; - extern "C" { @@ -60,12 +57,7 @@ try { OrthancDatabases::PostgreSQLParameters parameters(postgresql); - - /* Create the database back-end */ - backend_.reset(new OrthancDatabases::PostgreSQLIndex(context, parameters)); - - /* Register the PostgreSQL index into Orthanc */ - OrthancDatabases::IndexBackend::Register(*backend_); + OrthancDatabases::IndexBackend::Register(new OrthancDatabases::PostgreSQLIndex(context, parameters)); } catch (Orthanc::OrthancException& e) { @@ -85,7 +77,7 @@ ORTHANC_PLUGINS_API void OrthancPluginFinalize() { LOG(WARNING) << "PostgreSQL index is finalizing"; - backend_.reset(NULL); + OrthancDatabases::IndexBackend::Finalize(); } diff -r 73cc85f3d9c1 -r c8e06b41feec SQLite/Plugins/IndexPlugin.cpp --- a/SQLite/Plugins/IndexPlugin.cpp Tue Mar 30 10:40:34 2021 +0200 +++ b/SQLite/Plugins/IndexPlugin.cpp Thu Apr 01 11:16:13 2021 +0200 @@ -20,14 +20,10 @@ #include "SQLiteIndex.h" -#include "../../Framework/Plugins/DatabaseBackendAdapterV3.h" #include "../../Framework/Plugins/PluginInitialization.h" -#include // For std::unique_ptr<> #include -static std::unique_ptr backend_; - extern "C" { @@ -62,11 +58,9 @@ try { - /* Create the database back-end */ - backend_.reset(new OrthancDatabases::SQLiteIndex(context, "index.db")); // TODO parameter - /* Register the SQLite index into Orthanc */ - OrthancDatabases::IndexBackend::Register(*backend_); + OrthancDatabases::IndexBackend::Register( + new OrthancDatabases::SQLiteIndex(context, "index.db")); // TODO parameter } catch (Orthanc::OrthancException& e) { @@ -86,7 +80,7 @@ ORTHANC_PLUGINS_API void OrthancPluginFinalize() { LOG(WARNING) << "SQLite index is finalizing"; - backend_.reset(NULL); + OrthancDatabases::IndexBackend::Finalize(); }