changeset 3082:847a0ed92654 db-changes

new extension for database plugin SDK: createInstance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Jan 2019 13:52:34 +0100
parents 2e5970ddcfeb
children 683d572424b6
files OrthancServer/IDatabaseWrapper.h OrthancServer/SQLiteDatabaseWrapper.h OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.h OrthancServer/ServerIndex.cpp Plugins/Engine/OrthancPluginDatabase.cpp Plugins/Engine/OrthancPluginDatabase.h Plugins/Include/orthanc/OrthancCDatabasePlugin.h
diffstat 10 files changed, 95 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/IDatabaseWrapper.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/IDatabaseWrapper.h	Fri Jan 04 13:52:34 2019 +0100
@@ -228,15 +228,14 @@
                                       ResourceType queryLevel,
                                       size_t limit) = 0;
 
-    // Returns "true" iff. the instance already exists *and*
-    // "overwrite" is "false". If "false" is returned, the content of
-    // "result" is undefined, but "instanceId" must be properly set.
+    // Returns "true" iff. the instance already exists. If "false" is
+    // returned, the content of "result" is undefined, but
+    // "instanceId" must be properly set.
     virtual bool CreateInstance(CreateInstanceResult& result, /* out */
                                 int64_t& instanceId,          /* out */
                                 const std::string& patient,
                                 const std::string& study,
                                 const std::string& series,
-                                const std::string& instance,
-                                bool overwrite) = 0;
+                                const std::string& instance) = 0;
   };
 }
--- a/OrthancServer/SQLiteDatabaseWrapper.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/SQLiteDatabaseWrapper.h	Fri Jan 04 13:52:34 2019 +0100
@@ -329,11 +329,10 @@
                                 const std::string& patient,
                                 const std::string& study,
                                 const std::string& series,
-                                const std::string& instance,
-                                bool overwrite)
+                                const std::string& instance)
     {
-      return ICompatibilityCreateInstance::Apply(
-        result, instanceId, *this, *this, patient, study, series, instance, overwrite);
+      return ICompatibilityCreateInstance::Apply
+        (result, instanceId, *this, *this, patient, study, series, instance);
     }
   };
 }
--- a/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp	Fri Jan 04 13:52:34 2019 +0100
@@ -58,11 +58,10 @@
       const std::string& patient,
       const std::string& study,
       const std::string& series,
-      const std::string& instance,
-      bool overwrite)
+      const std::string& instance)
     {
       return ICompatibilityCreateInstance::Apply
-        (result, instanceId, *this, *this, patient, study, series, instance, overwrite);
+        (result, instanceId, *this, *this, patient, study, series, instance);
     }
   }
 }
--- a/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h	Fri Jan 04 13:52:34 2019 +0100
@@ -61,8 +61,7 @@
                                   const std::string& patient,
                                   const std::string& study,
                                   const std::string& series,
-                                  const std::string& instance,
-                                  bool overwrite)
+                                  const std::string& instance)
         ORTHANC_OVERRIDE;
 
       virtual void GetAllInternalIds(std::list<int64_t>& target,
--- a/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp	Fri Jan 04 13:52:34 2019 +0100
@@ -47,8 +47,7 @@
                                              const std::string& hashPatient,
                                              const std::string& hashStudy,
                                              const std::string& hashSeries,
-                                             const std::string& hashInstance,
-                                             bool overwrite)
+                                             const std::string& hashInstance)
     {
       {
         ResourceType type;
@@ -56,20 +55,10 @@
         
         if (database.LookupResource(tmp, type, hashInstance))
         {
+          // The instance already exists
           assert(type == ResourceType_Instance);
-
-          if (overwrite)
-          {
-            // Overwrite the old instance
-            LOG(INFO) << "Overwriting instance: " << hashInstance;
-            database.DeleteResource(tmp);
-          }
-          else
-          {
-            // Do nothing if the instance already exists
-            instanceId = tmp;
-            return false;
-          }
+          instanceId = tmp;
+          return false;
         }
       }
 
--- a/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.h	Fri Jan 04 13:52:34 2019 +0100
@@ -55,8 +55,7 @@
                         const std::string& patient,
                         const std::string& study,
                         const std::string& series,
-                        const std::string& instance,
-                        bool overwrite);
+                        const std::string& instance);
     };
   }
 }
--- a/OrthancServer/ServerIndex.cpp	Thu Jan 03 20:03:35 2019 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Jan 04 13:52:34 2019 +0100
@@ -720,11 +720,29 @@
 
       // Check whether this instance is already stored
       if (!db_.CreateInstance(status, instanceId, hashPatient,
-                              hashStudy, hashSeries, hashInstance, overwrite_))
+                              hashStudy, hashSeries, hashInstance))
       {
-        // Do nothing if the instance already exists and overwriting is disabled
-        db_.GetAllMetadata(instanceMetadata, instanceId);
-        return StoreStatus_AlreadyStored;
+        // The instance already exists
+        
+        if (overwrite_)
+        {
+          // Overwrite the old instance
+          LOG(INFO) << "Overwriting instance: " << hashInstance;
+          db_.DeleteResource(instanceId);
+
+          // Re-create the instance, now that the old one is removed
+          if (!db_.CreateInstance(status, instanceId, hashPatient,
+                                  hashStudy, hashSeries, hashInstance))
+          {
+            throw OrthancException(ErrorCode_InternalError);
+          }
+        }
+        else
+        {
+          // Do nothing if the instance already exists and overwriting is disabled
+          db_.GetAllMetadata(instanceMetadata, instanceId);
+          return StoreStatus_AlreadyStored;
+        }
       }
 
 
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Thu Jan 03 20:03:35 2019 +0100
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Jan 04 13:52:34 2019 +0100
@@ -1195,13 +1195,39 @@
     const std::string& patient,
     const std::string& study,
     const std::string& series,
-    const std::string& instance,
-    bool overwrite)
+    const std::string& instance)
   {
-    // TODO optimized version
+    if (extensions_.createInstance == NULL)
+    {
+      // Fallback to compatibility mode
+      return CompatibilityDatabaseWrapper::CreateInstance
+        (result, instanceId, patient, study, series, instance);
+    }
+    else
+    {
+      OrthancPluginCreateInstanceResult output;
+      memset(&output, 0, sizeof(output));
+
+      CheckSuccess(extensions_.createInstance(&output, payload_, patient.c_str(),
+                                              study.c_str(), series.c_str(), instance.c_str()));
 
-    return CompatibilityDatabaseWrapper::CreateInstance(
-      result, instanceId, patient, study, series, instance, overwrite);
+      instanceId = output.instanceId;
+      
+      if (output.isNewInstance)
+      {
+        result.isNewPatient_ = output.isNewPatient;
+        result.isNewStudy_ = output.isNewStudy;
+        result.isNewSeries_ = output.isNewSeries;
+        result.patientId_ = output.patientId;
+        result.studyId_ = output.studyId;
+        result.seriesId_ = output.seriesId;
+        return true;
+      }
+      else
+      {
+        return false;
+      }
+    }
   }
 
 
--- a/Plugins/Engine/OrthancPluginDatabase.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/Plugins/Engine/OrthancPluginDatabase.h	Fri Jan 04 13:52:34 2019 +0100
@@ -321,8 +321,7 @@
                                 const std::string& patient,
                                 const std::string& study,
                                 const std::string& series,
-                                const std::string& instance,
-                                bool overwrite)
+                                const std::string& instance)
       ORTHANC_OVERRIDE;
 
     // From the "CompatibilityDatabaseWrapper" interface
--- a/Plugins/Include/orthanc/OrthancCDatabasePlugin.h	Thu Jan 03 20:03:35 2019 +0100
+++ b/Plugins/Include/orthanc/OrthancCDatabasePlugin.h	Fri Jan 04 13:52:34 2019 +0100
@@ -134,12 +134,27 @@
     const char* const*           values;
   } OrthancPluginDatabaseConstraint;
 
-  typedef struct
+  typedef struct   /* New in Orthanc 1.5.2 */
   {
     const char*  resourceId;
     const char*  someInstanceId;  /* Can be NULL if not requested */
   } OrthancPluginMatchingResource;
 
+  typedef struct   /* New in Orthanc 1.5.2 */
+  {
+    /* Mandatory field */
+    uint8_t  isNewInstance;
+    int64_t  instanceId;
+
+    /* The following fields must only be set if "isNewInstance" is "true" */
+    uint8_t  isNewPatient;
+    uint8_t  isNewStudy;
+    uint8_t  isNewSeries;
+    int64_t  patientId;
+    int64_t  studyId;
+    int64_t  seriesId;
+  } OrthancPluginCreateInstanceResult;
+
 
   typedef struct
   {
@@ -744,7 +759,16 @@
       OrthancPluginResourceType queryLevel,
       uint32_t limit,
       uint8_t requestSomeInstance);
-    
+
+    OrthancPluginErrorCode  (*createInstance) (
+      /* output */
+      OrthancPluginCreateInstanceResult* output,
+      /* inputs */
+      void* payload,
+      const char* hashPatient,
+      const char* hashStudy,
+      const char* hashSeries,
+      const char* hashInstance);
    } OrthancPluginDatabaseExtensions;
 
 /*<! @endcond */