changeset 222:c8e06b41feec

refactoring registration/finalization of index backend
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Apr 2021 11:16:13 +0200
parents 73cc85f3d9c1
children af049cd66661
files Framework/Plugins/DatabaseBackendAdapterV2.cpp Framework/Plugins/DatabaseBackendAdapterV2.h Framework/Plugins/DatabaseBackendAdapterV3.cpp Framework/Plugins/DatabaseBackendAdapterV3.h Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h MySQL/Plugins/IndexPlugin.cpp PostgreSQL/Plugins/IndexPlugin.cpp SQLite/Plugins/IndexPlugin.cpp
diffstat 9 files changed, 83 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- 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<IDatabaseBackend> 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(&params, 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, &params, &extensions, &backend);
+      OrthancPluginRegisterDatabaseBackendV2(context, &params, &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);
   }
 }
--- 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();
   };
 }
--- 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<IndexBackend> 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(&params, 0, sizeof(params));
 
@@ -1829,14 +1843,20 @@
     params.setProtectedPatient = SetProtectedPatient;
     params.setResourcesContent = SetResourcesContent;
 
-    OrthancPluginContext* context = database.GetContext();
+    OrthancPluginContext* context = backend_->GetContext();
  
-    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, sizeof(params), &database) != OrthancPluginErrorCode_Success)
+    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, 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);
   }
 }
 
--- 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();
   };
 }
 
--- 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
+  }
 }
--- 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();
   };
 }
--- 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 <Compatibility.h>  // For std::unique_ptr<>
 #include <HttpClient.h>
 #include <Logging.h>
 #include <Toolbox.h>
 
-static std::unique_ptr<OrthancDatabases::MySQLIndex> 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();
--- 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 <Compatibility.h>  // For std::unique_ptr<>
 #include <Logging.h>
 
-static std::unique_ptr<OrthancDatabases::PostgreSQLIndex> 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();
   }
 
 
--- 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 <Compatibility.h>  // For std::unique_ptr<>
 #include <Logging.h>
 
-static std::unique_ptr<OrthancDatabases::SQLiteIndex> 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();
   }