changeset 195:53bd9022c58b

Support of "OrthancPluginRegisterStorageArea2()" from Orthanc SDK 1.9.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 08 Jan 2021 18:54:20 +0100
parents a51ce147dbe0
children 92fd27ccc60f
files Framework/Plugins/StorageAreaBuffer.cpp Framework/Plugins/StorageAreaBuffer.h Framework/Plugins/StorageBackend.cpp MySQL/NEWS MySQL/UnitTests/UnitTestsMain.cpp PostgreSQL/NEWS PostgreSQL/UnitTests/PostgreSQLTests.cpp
diffstat 7 files changed, 165 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/StorageAreaBuffer.cpp	Fri Jan 08 14:40:03 2021 +0100
+++ b/Framework/Plugins/StorageAreaBuffer.cpp	Fri Jan 08 18:54:20 2021 +0100
@@ -29,7 +29,108 @@
 
 namespace OrthancDatabases
 {
-  StorageAreaBuffer::StorageAreaBuffer() :
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
+  /**
+   * If Orthanc SDK >= 1.9.0
+   **/
+  
+  StorageAreaBuffer::StorageAreaBuffer(OrthancPluginContext* context) :
+    context_(context)
+  {
+    buffer_.data = NULL;
+    buffer_.size = 0;
+  }
+
+  
+  void StorageAreaBuffer::Clear()
+  {
+    if (buffer_.data != NULL)
+    {
+      if (context_ == NULL)  // Are we running the unit tests?
+      {
+        free(buffer_.data);
+      }
+      else
+      {
+        OrthancPluginFreeMemoryBuffer64(context_, &buffer_);
+      }
+      
+      buffer_.data = NULL;
+      buffer_.size = 0;
+    }
+  }
+
+
+  int64_t StorageAreaBuffer::GetSize() const
+  {
+    return buffer_.size;
+  }
+
+  
+  const void* StorageAreaBuffer::GetData() const
+  {
+    return buffer_.data;
+  }
+
+
+  void StorageAreaBuffer::Assign(const std::string& content)
+  {
+    Clear();
+
+    if (context_ == NULL)  // Are we running the unit tests?
+    {
+      buffer_.size = static_cast<uint64_t>(content.size());
+
+      if (content.empty())
+      {
+        buffer_.data = NULL;
+      }
+      else
+      {
+        buffer_.data = malloc(content.size());
+      }
+    }
+    else
+    {
+      if (OrthancPluginCreateMemoryBuffer64(context_, &buffer_, static_cast<uint64_t>(content.size())) !=
+          OrthancPluginErrorCode_Success)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
+      }
+    }
+
+    if (!content.empty())
+    {
+      memcpy(buffer_.data, content.c_str(), content.size());
+    }
+  }
+
+
+  void StorageAreaBuffer::Move(OrthancPluginMemoryBuffer64* target)
+  {
+    *target = buffer_;
+    buffer_.data = NULL;
+    buffer_.size = 0;
+  }
+
+
+  void StorageAreaBuffer::ToString(std::string& target)
+  {
+    target.resize(buffer_.size);
+
+    if (buffer_.size != 0)
+    {
+      memcpy(&target[0], buffer_.data, buffer_.size);
+    }
+  }
+  
+
+#else
+  /**
+   * If Orthanc SDK <= 1.8.2
+   **/
+  
+  StorageAreaBuffer::StorageAreaBuffer(OrthancPluginContext* /* not used in this flavor */) :
     data_(NULL),
     size_(0)
   {
@@ -47,6 +148,18 @@
   }
 
 
+  int64_t StorageAreaBuffer::GetSize() const
+  {
+    return size_;
+  }
+
+  
+  const void* StorageAreaBuffer::GetData() const
+  {
+    return data_;
+  }
+
+
   void StorageAreaBuffer::Assign(const std::string& content)
   {
     Clear();
@@ -95,4 +208,5 @@
       memcpy(&target[0], data_, size_);
     }
   }
+#endif
 }
--- a/Framework/Plugins/StorageAreaBuffer.h	Fri Jan 08 14:40:03 2021 +0100
+++ b/Framework/Plugins/StorageAreaBuffer.h	Fri Jan 08 18:54:20 2021 +0100
@@ -21,22 +21,28 @@
 
 #pragma once
 
-#include <orthanc/OrthancCPlugin.h>
+#include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
 
 #include <boost/noncopyable.hpp>
 #include <string>
 
 
+
 namespace OrthancDatabases
 {
   class StorageAreaBuffer : public boost::noncopyable
   {
   private:
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
+    OrthancPluginContext*        context_;
+    OrthancPluginMemoryBuffer64  buffer_;
+#else
     void*    data_;
     int64_t  size_;
+#endif
 
   public:
-    StorageAreaBuffer();
+    StorageAreaBuffer(OrthancPluginContext* context);
 
     ~StorageAreaBuffer()
     {
@@ -47,17 +53,15 @@
 
     void Assign(const std::string& content);
 
-    int64_t GetSize() const
-    {
-      return size_;
-    }
+    int64_t GetSize() const;
+
+    const void* GetData() const;
 
-    const void* GetData() const
-    {
-      return data_;
-    }
-
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
+    void Move(OrthancPluginMemoryBuffer64* target);
+#else
     void* ReleaseData();
+#endif
 
     void ToString(std::string& target);
   };
--- a/Framework/Plugins/StorageBackend.cpp	Fri Jan 08 14:40:03 2021 +0100
+++ b/Framework/Plugins/StorageBackend.cpp	Fri Jan 08 18:54:20 2021 +0100
@@ -170,6 +170,28 @@
   }
 
 
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
+  static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64 *target,
+                                                 const char* uuid,
+                                                 OrthancPluginContentType type)
+  {
+    try
+    {
+      StorageAreaBuffer buffer(context_);
+
+      {
+        DatabaseManager::Transaction transaction(backend_->GetManager());
+        backend_->Read(buffer, transaction, uuid, type);
+        transaction.Commit();
+      }
+
+      buffer.Move(target);
+      
+      return OrthancPluginErrorCode_Success;
+    }
+    ORTHANC_PLUGINS_DATABASE_CATCH;
+  }
+#else
   static OrthancPluginErrorCode StorageRead(void** content,
                                             int64_t* size,
                                             const char* uuid,
@@ -177,7 +199,7 @@
   {
     try
     {
-      StorageAreaBuffer buffer;
+      StorageAreaBuffer buffer(context_);
 
       {
         DatabaseManager::Transaction transaction(backend_->GetManager());
@@ -192,6 +214,7 @@
     }
     ORTHANC_PLUGINS_DATABASE_CATCH;
   }
+#endif
 
 
   static OrthancPluginErrorCode StorageRemove(const char* uuid,
@@ -229,7 +252,12 @@
       backend_.reset(backend);
       backend_->GetManager().Open();
 
+#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
+      OrthancPluginRegisterStorageArea2(context_, StorageCreate, StorageReadWhole,
+                                        NULL /* TODO - StorageReadRange */, StorageRemove);
+#else
       OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove);
+#endif
     }
   }
 
--- a/MySQL/NEWS	Fri Jan 08 14:40:03 2021 +0100
+++ b/MySQL/NEWS	Fri Jan 08 18:54:20 2021 +0100
@@ -1,6 +1,8 @@
 Pending changes in the mainline
 ===============================
 
+* Support of "OrthancPluginRegisterStorageArea2()" from Orthanc SDK 1.9.0
+
 
 Release 3.0 (2020-12-16)
 ========================
--- a/MySQL/UnitTests/UnitTestsMain.cpp	Fri Jan 08 14:40:03 2021 +0100
+++ b/MySQL/UnitTests/UnitTestsMain.cpp	Fri Jan 08 18:54:20 2021 +0100
@@ -163,7 +163,7 @@
       storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown);
     }
 
-    OrthancDatabases::StorageAreaBuffer buffer;
+    OrthancDatabases::StorageAreaBuffer buffer(NULL /* we are running unit tests */);
     ASSERT_THROW(storageArea.Read(buffer, transaction, "nope", OrthancPluginContentType_Unknown), 
                  Orthanc::OrthancException);
   
--- a/PostgreSQL/NEWS	Fri Jan 08 14:40:03 2021 +0100
+++ b/PostgreSQL/NEWS	Fri Jan 08 18:54:20 2021 +0100
@@ -1,6 +1,8 @@
 Pending changes in the mainline
 ===============================
 
+* Support of "OrthancPluginRegisterStorageArea2()" from Orthanc SDK 1.9.0
+
 
 Release 3.3 (2020-12-14)
 ========================
--- a/PostgreSQL/UnitTests/PostgreSQLTests.cpp	Fri Jan 08 14:40:03 2021 +0100
+++ b/PostgreSQL/UnitTests/PostgreSQLTests.cpp	Fri Jan 08 18:54:20 2021 +0100
@@ -356,7 +356,7 @@
       storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown);
     }
 
-    StorageAreaBuffer buffer;
+    StorageAreaBuffer buffer(NULL /* we are running unit tests */);
     ASSERT_THROW(storageArea.Read(buffer, transaction, "nope", OrthancPluginContentType_Unknown), 
                  Orthanc::OrthancException);