diff OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp @ 5455:176bc05f85f4 pg-transactions

DB: new Capabilities class to manage future new methods from DB plugins + Added IncrementGlobalProperty
author Alain Mazy <am@osimis.io>
date Thu, 07 Dec 2023 12:04:11 +0100
parents fafdb6179829
children dceed5e3d6a9
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed Dec 06 14:20:33 2023 +0100
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Thu Dec 07 12:04:11 2023 +0100
@@ -229,7 +229,7 @@
                             bool isSingleResource,
                             int64_t resource)
     {
-      if (database_.HasLabelsSupport())
+      if (database_.GetDatabaseCapabilities().HasLabelsSupport())
       {
         DatabasePluginMessages::TransactionRequest request;
         request.mutable_list_labels()->set_single_resource(isSingleResource);
@@ -305,6 +305,10 @@
       }
     }
 
+    virtual const IDatabaseWrapper::Capabilities& GetDatabaseCapabilities() const ORTHANC_OVERRIDE
+    {
+      return database_.GetDatabaseCapabilities();
+    }
 
     void* GetTransactionObject()
     {
@@ -772,6 +776,21 @@
       }
     }
 
+
+    virtual int64_t IncrementGlobalProperty(GlobalProperty property,
+                                            int64_t increment,
+                                            bool shared) ORTHANC_OVERRIDE
+    {
+      DatabasePluginMessages::TransactionRequest request;
+      request.mutable_increment_global_property()->set_server_id(shared ? "" : database_.GetServerIdentifier());
+      request.mutable_increment_global_property()->set_property(property);
+      request.mutable_increment_global_property()->set_increment(increment);
+
+      DatabasePluginMessages::TransactionResponse response;
+      ExecuteTransaction(response, DatabasePluginMessages::OPERATION_INCREMENT_GLOBAL_PROPERTY, request);
+
+      return response.increment_global_property().new_value();
+    }
     
     virtual bool LookupMetadata(std::string& target,
                                 int64_t& revision,
@@ -948,7 +967,7 @@
                                       LabelsConstraint labelsConstraint,
                                       uint32_t limit) ORTHANC_OVERRIDE
     {
-      if (!database_.HasLabelsSupport() &&
+      if (!database_.GetDatabaseCapabilities().HasLabelsSupport() &&
           !labels.empty())
       {
         throw OrthancException(ErrorCode_InternalError);
@@ -1197,7 +1216,7 @@
     virtual void AddLabel(int64_t resource,
                           const std::string& label) ORTHANC_OVERRIDE
     {
-      if (database_.HasLabelsSupport())
+      if (database_.GetDatabaseCapabilities().HasLabelsSupport())
       {
         DatabasePluginMessages::TransactionRequest request;
         request.mutable_add_label()->set_id(resource);
@@ -1216,7 +1235,7 @@
     virtual void RemoveLabel(int64_t resource,
                              const std::string& label) ORTHANC_OVERRIDE
     {
-      if (database_.HasLabelsSupport())
+      if (database_.GetDatabaseCapabilities().HasLabelsSupport())
       {
         DatabasePluginMessages::TransactionRequest request;
         request.mutable_remove_label()->set_id(resource);
@@ -1256,9 +1275,7 @@
     serverIdentifier_(serverIdentifier),
     open_(false),
     databaseVersion_(0),
-    hasFlushToDisk_(false),
-    hasRevisionsSupport_(false),
-    hasLabelsSupport_(false)
+    dbCapabilities_(false, false, false, false) // updated in Open()
   {
     CLOG(INFO, PLUGINS) << "Identifier of this Orthanc server for the global properties "
                         << "of the custom database: \"" << serverIdentifier << "\"";
@@ -1325,10 +1342,13 @@
       DatabasePluginMessages::DatabaseRequest request;
       DatabasePluginMessages::DatabaseResponse response;
       ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_GET_SYSTEM_INFORMATION, request);
-      databaseVersion_ = response.get_system_information().database_version();
-      hasFlushToDisk_ = response.get_system_information().supports_flush_to_disk();
-      hasRevisionsSupport_ = response.get_system_information().supports_revisions();
-      hasLabelsSupport_ = response.get_system_information().supports_labels();
+      
+      const ::Orthanc::DatabasePluginMessages::GetSystemInformation_Response& systemInfo = response.get_system_information();
+      databaseVersion_ = systemInfo.database_version();
+      dbCapabilities_.hasFlushToDisk_ = systemInfo.supports_flush_to_disk();
+      dbCapabilities_.hasRevisionsSupport_ = systemInfo.supports_revisions();
+      dbCapabilities_.hasLabelsSupport_ = systemInfo.supports_labels();
+      dbCapabilities_.hasAtomicIncrementGlobalProperty_ = systemInfo.supports_increment_global_property();
     }
 
     open_ = true;
@@ -1350,23 +1370,11 @@
   }
   
 
-  bool OrthancPluginDatabaseV4::HasFlushToDisk() const
-  {
-    if (!open_)
-    {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);
-    }
-    else
-    {
-      return hasFlushToDisk_;
-    }
-  }
-
 
   void OrthancPluginDatabaseV4::FlushToDisk()
   {
     if (!open_ ||
-        !hasFlushToDisk_)
+        !GetDatabaseCapabilities().HasFlushToDisk())
     {
       throw OrthancException(ErrorCode_BadSequenceOfCalls);
     }
@@ -1438,8 +1446,8 @@
     }
   }
 
-  
-  bool OrthancPluginDatabaseV4::HasRevisionsSupport() const
+
+  const IDatabaseWrapper::Capabilities& OrthancPluginDatabaseV4::GetDatabaseCapabilities() const
   {
     if (!open_)
     {
@@ -1447,20 +1455,9 @@
     }
     else
     {
-      return hasRevisionsSupport_;
+      return dbCapabilities_;
     }
   }
 
-  
-  bool OrthancPluginDatabaseV4::HasLabelsSupport() const
-  {
-    if (!open_)
-    {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);
-    }
-    else
-    {
-      return hasLabelsSupport_;
-    }
-  }
+
 }