changeset 3084:195ba4cbac3f db-changes

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Jan 2019 16:42:55 +0100
parents 683d572424b6
children c829758b9ca0
files CMakeLists.txt OrthancServer/SQLiteDatabaseWrapper.h OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h OrthancServer/Search/Compatibility/DatabaseLookup.cpp OrthancServer/Search/Compatibility/DatabaseLookup.h OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.h OrthancServer/Search/Compatibility/ICreateInstance.cpp OrthancServer/Search/Compatibility/ICreateInstance.h OrthancServer/Search/Compatibility/ISetResourcesContent.h OrthancServer/Search/Compatibility/SetOfResources.cpp OrthancServer/Search/Compatibility/SetOfResources.h Plugins/Engine/OrthancPluginDatabase.cpp Plugins/Engine/OrthancPluginDatabase.h
diffstat 15 files changed, 307 insertions(+), 304 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Jan 04 15:52:19 2019 +0100
+++ b/CMakeLists.txt	Fri Jan 04 16:42:55 2019 +0100
@@ -73,7 +73,7 @@
   OrthancServer/SQLiteDatabaseWrapper.cpp
   OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp
   OrthancServer/Search/Compatibility/DatabaseLookup.cpp
-  OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp
+  OrthancServer/Search/Compatibility/ICreateInstance.cpp
   OrthancServer/Search/Compatibility/SetOfResources.cpp
   OrthancServer/Search/DatabaseConstraint.cpp
   OrthancServer/Search/DatabaseLookup.cpp
--- a/OrthancServer/SQLiteDatabaseWrapper.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/SQLiteDatabaseWrapper.h	Fri Jan 04 16:42:55 2019 +0100
@@ -36,7 +36,7 @@
 #include "IDatabaseWrapper.h"
 
 #include "../Core/SQLite/Connection.h"
-#include "Search/Compatibility/ICompatibilityCreateInstance.h"
+#include "Search/Compatibility/ICreateInstance.h"
 #include "Search/Compatibility/ISetResourcesContent.h"
 #include "ServerToolbox.h"
 
@@ -53,7 +53,8 @@
    * exclusion MUST be implemented at a higher level.
    **/
   class SQLiteDatabaseWrapper :
-    public Compatibility::ICompatibilityCreateInstance,
+    public IDatabaseWrapper,
+    public Compatibility::ICreateInstance,
     public Compatibility::ISetResourcesContent
   {
   private:
@@ -331,10 +332,15 @@
                                 const std::string& patient,
                                 const std::string& study,
                                 const std::string& series,
-                                const std::string& instance)
+                                const std::string& instance)  ORTHANC_OVERRIDE
     {
-      return ICompatibilityCreateInstance::Apply
-        (result, instanceId, *this, *this, patient, study, series, instance);
+      return ICreateInstance::Apply
+        (*this, *this, result, instanceId, patient, study, series, instance);
+    }
+
+    virtual void SetResourcesContent(const Orthanc::ResourcesContent& content)  ORTHANC_OVERRIDE
+    {
+      ISetResourcesContent::Apply(*this, content);
     }
   };
 }
--- a/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.cpp	Fri Jan 04 16:42:55 2019 +0100
@@ -40,28 +40,17 @@
 {
   namespace Compatibility
   {
-    void CompatibilityDatabaseWrapper::ApplyLookupResources(
+    void CompatibilityDatabaseWrapper::Apply(
+      IDatabaseWrapper& database,
+      CompatibilityDatabaseWrapper& compatibility,
       std::list<std::string>& resourcesId,
       std::list<std::string>* instancesId,
       const std::vector<DatabaseConstraint>& lookup,
       ResourceType queryLevel,
       size_t limit)
     {
-      Compatibility::DatabaseLookup compat(*this);
+      Compatibility::DatabaseLookup compat(database, compatibility);
       compat.ApplyLookupResources(resourcesId, instancesId, lookup, queryLevel, limit);
     }
-    
-
-    bool CompatibilityDatabaseWrapper::CreateInstance(
-      IDatabaseWrapper::CreateInstanceResult& result,
-      int64_t& instanceId,
-      const std::string& patient,
-      const std::string& study,
-      const std::string& series,
-      const std::string& instance)
-    {
-      return ICompatibilityCreateInstance::Apply
-        (result, instanceId, *this, *this, patient, study, series, instance);
-    }
   }
 }
--- a/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h	Fri Jan 04 16:42:55 2019 +0100
@@ -33,9 +33,7 @@
 
 #pragma once
 
-#include "../../ServerToolbox.h"
-#include "ICompatibilityCreateInstance.h"
-#include "ISetResourcesContent.h"
+#include "../../IDatabaseWrapper.h"
 
 namespace Orthanc
 {
@@ -46,26 +44,13 @@
      * that were used in Orthanc <= 1.5.1, and that have been removed
      * during the optimization of the database engine.
      **/
-    class CompatibilityDatabaseWrapper :
-      public ICompatibilityCreateInstance,
-      public ISetResourcesContent
+    class CompatibilityDatabaseWrapper : public boost::noncopyable
     {     
     public:
-      virtual void ApplyLookupResources(std::list<std::string>& resourcesId,
-                                        std::list<std::string>* instancesId,
-                                        const std::vector<DatabaseConstraint>& lookup,
-                                        ResourceType queryLevel,
-                                        size_t limit)
-        ORTHANC_OVERRIDE;
-
-      virtual bool CreateInstance(CreateInstanceResult& result,
-                                  int64_t& instanceId,
-                                  const std::string& patient,
-                                  const std::string& study,
-                                  const std::string& series,
-                                  const std::string& instance)
-        ORTHANC_OVERRIDE;
-
+      virtual ~CompatibilityDatabaseWrapper()
+      {
+      }
+      
       virtual void GetAllInternalIds(std::list<int64_t>& target,
                                      ResourceType resourceType) = 0;
       
@@ -80,6 +65,14 @@
                                          const DicomTag& tag,
                                          const std::string& start,
                                          const std::string& end) = 0;
+
+      static void Apply(IDatabaseWrapper& database,
+                        CompatibilityDatabaseWrapper& compatibility,
+                        std::list<std::string>& resourcesId,
+                        std::list<std::string>* instancesId,
+                        const std::vector<DatabaseConstraint>& lookup,
+                        ResourceType queryLevel,
+                        size_t limit);
     };
   }
 }
--- a/OrthancServer/Search/Compatibility/DatabaseLookup.cpp	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/DatabaseLookup.cpp	Fri Jan 04 16:42:55 2019 +0100
@@ -92,7 +92,7 @@
     
     
     static void ApplyIdentifierConstraint(SetOfResources& candidates,
-                                          CompatibilityDatabaseWrapper& database,
+                                          CompatibilityDatabaseWrapper& compatibility,
                                           const DatabaseConstraint& constraint,
                                           ResourceType level)
     {
@@ -101,23 +101,23 @@
       switch (constraint.GetConstraintType())
       {
         case ConstraintType_Equal:
-          database.LookupIdentifier(matches, level, constraint.GetTag(),
+          compatibility.LookupIdentifier(matches, level, constraint.GetTag(),
                                     IdentifierConstraintType_Equal, constraint.GetSingleValue());
           break;
           
         case ConstraintType_SmallerOrEqual:
-          database.LookupIdentifier(matches, level, constraint.GetTag(),
+          compatibility.LookupIdentifier(matches, level, constraint.GetTag(),
                                     IdentifierConstraintType_SmallerOrEqual, constraint.GetSingleValue());
           break;
           
         case ConstraintType_GreaterOrEqual:
-          database.LookupIdentifier(matches, level, constraint.GetTag(),
+          compatibility.LookupIdentifier(matches, level, constraint.GetTag(),
                                     IdentifierConstraintType_GreaterOrEqual, constraint.GetSingleValue());
 
           break;
           
         case ConstraintType_Wildcard:
-          database.LookupIdentifier(matches, level, constraint.GetTag(),
+          compatibility.LookupIdentifier(matches, level, constraint.GetTag(),
                                     IdentifierConstraintType_Wildcard, constraint.GetSingleValue());
 
           break;
@@ -126,7 +126,7 @@
           for (size_t i = 0; i < constraint.GetValuesCount(); i++)
           {
             std::list<int64_t> tmp;
-            database.LookupIdentifier(tmp, level, constraint.GetTag(),
+            compatibility.LookupIdentifier(tmp, level, constraint.GetTag(),
                                       IdentifierConstraintType_Wildcard, constraint.GetValue(i));
             matches.splice(matches.end(), tmp);
           }
@@ -142,7 +142,7 @@
 
     
     static void ApplyIdentifierRange(SetOfResources& candidates,
-                                     CompatibilityDatabaseWrapper& database,
+                                     CompatibilityDatabaseWrapper& compatibility,
                                      const DatabaseConstraint& smaller,
                                      const DatabaseConstraint& greater,
                                      ResourceType level)
@@ -153,14 +153,15 @@
              ServerToolbox::IsIdentifier(smaller.GetTag(), level));
 
       std::list<int64_t> matches;
-      database.LookupIdentifierRange(matches, level, smaller.GetTag(),
+      compatibility.LookupIdentifierRange(matches, level, smaller.GetTag(),
                                      greater.GetSingleValue(), smaller.GetSingleValue());
       candidates.Intersect(matches);
     }
 
     
     static void ApplyLevel(SetOfResources& candidates,
-                           CompatibilityDatabaseWrapper& database,
+                           IDatabaseWrapper& database,
+                           CompatibilityDatabaseWrapper& compatibility,
                            const std::vector<DatabaseConstraint>& lookup,
                            ResourceType level)
     {
@@ -220,7 +221,7 @@
             greater != NULL)
         {
           // There is a range constraint: Apply it, as it is more efficient
-          ApplyIdentifierRange(candidates, database, *smaller, *greater, level);
+          ApplyIdentifierRange(candidates, compatibility, *smaller, *greater, level);
         }
         else
         {
@@ -235,7 +236,7 @@
           if (*it2 != smaller &&
               *it2 != greater)
           {
-            ApplyIdentifierConstraint(candidates, database, **it2, level);
+            ApplyIdentifierConstraint(candidates, compatibility, **it2, level);
           }
         }
       }
@@ -257,7 +258,7 @@
         }
 
         std::list<int64_t>  source;
-        candidates.Flatten(source);
+        candidates.Flatten(compatibility, source);
         candidates.Clear();
 
         std::list<int64_t>  filtered;
@@ -289,16 +290,16 @@
     }
 
 
-    static std::string GetOneInstance(IDatabaseWrapper& database,
+    static std::string GetOneInstance(IDatabaseWrapper& compatibility,
                                       int64_t resource,
                                       ResourceType level)
     {
       for (int i = level; i < ResourceType_Instance; i++)
       {
-        assert(database.GetResourceType(resource) == static_cast<ResourceType>(i));
+        assert(compatibility.GetResourceType(resource) == static_cast<ResourceType>(i));
 
         std::list<int64_t> children;
-        database.GetChildrenInternalId(children, resource);
+        compatibility.GetChildrenInternalId(children, resource);
           
         if (children.empty())
         {
@@ -308,7 +309,7 @@
         resource = children.front();
       }
 
-      return database.GetPublicId(resource);
+      return compatibility.GetPublicId(resource);
     }
                            
 
@@ -350,7 +351,7 @@
 
       for (int level = upperLevel; level <= lowerLevel; level++)
       {
-        ApplyLevel(candidates, database_, lookup, static_cast<ResourceType>(level));
+        ApplyLevel(candidates, database_, compatibility_, lookup, static_cast<ResourceType>(level));
 
         if (level != lowerLevel)
         {
@@ -359,7 +360,7 @@
       }
 
       std::list<int64_t> resources;
-      candidates.Flatten(resources);
+      candidates.Flatten(compatibility_, resources);
 
       // Climb up, up to queryLevel
 
--- a/OrthancServer/Search/Compatibility/DatabaseLookup.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/DatabaseLookup.h	Fri Jan 04 16:42:55 2019 +0100
@@ -33,6 +33,7 @@
 
 #pragma once
 
+#include "../../IDatabaseWrapper.h"
 #include "CompatibilityDatabaseWrapper.h"
 
 namespace Orthanc
@@ -42,11 +43,14 @@
     class DatabaseLookup : public boost::noncopyable
     {
     private:
-      CompatibilityDatabaseWrapper&  database_;
+      IDatabaseWrapper&              database_;
+      CompatibilityDatabaseWrapper&  compatibility_;
 
     public:
-      DatabaseLookup(CompatibilityDatabaseWrapper& database) :
-        database_(database)
+      DatabaseLookup(IDatabaseWrapper&  database,
+                     CompatibilityDatabaseWrapper& compatibility) :
+        database_(database),
+        compatibility_(compatibility)
       {
       }
 
--- a/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.cpp	Fri Jan 04 15:52:19 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,156 +0,0 @@
-/**
- * Orthanc - A Lightweight, RESTful DICOM Store
- * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
- * Department, University Hospital of Liege, Belgium
- * Copyright (C) 2017-2019 Osimis S.A., Belgium
- *
- * This program is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * In addition, as a special exception, the copyright holders of this
- * program give permission to link the code of its release with the
- * OpenSSL project's "OpenSSL" library (or with modified versions of it
- * that use the same license as the "OpenSSL" library), and distribute
- * the linked executables. You must obey the GNU General Public License
- * in all respects for all of the code used other than "OpenSSL". If you
- * modify file(s) with this exception, you may extend this exception to
- * your version of the file(s), but you are not obligated to do so. If
- * you do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source files
- * in the program, then also delete it here.
- * 
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- **/
-
-
-#include "../../PrecompiledHeadersServer.h"
-#include "ICompatibilityCreateInstance.h"
-
-#include "../../../Core/OrthancException.h"
-
-namespace Orthanc
-{
-  namespace Compatibility
-  {
-    bool ICompatibilityCreateInstance::Apply(IDatabaseWrapper::CreateInstanceResult& result,
-                                             int64_t& instanceId,
-                                             ICompatibilityCreateInstance& compatibility,
-                                             IDatabaseWrapper& database,
-                                             const std::string& hashPatient,
-                                             const std::string& hashStudy,
-                                             const std::string& hashSeries,
-                                             const std::string& hashInstance)
-    {
-      {
-        ResourceType type;
-        int64_t tmp;
-        
-        if (database.LookupResource(tmp, type, hashInstance))
-        {
-          // The instance already exists
-          assert(type == ResourceType_Instance);
-          instanceId = tmp;
-          return false;
-        }
-      }
-
-      instanceId = compatibility.CreateResource(hashInstance, ResourceType_Instance);
-
-      result.isNewPatient_ = false;
-      result.isNewStudy_ = false;
-      result.isNewSeries_ = false;
-      result.patientId_ = -1;
-      result.studyId_ = -1;
-      result.seriesId_ = -1;
-      
-      // Detect up to which level the patient/study/series/instance
-      // hierarchy must be created
-
-      {
-        ResourceType dummy;
-
-        if (database.LookupResource(result.seriesId_, dummy, hashSeries))
-        {
-          assert(dummy == ResourceType_Series);
-          // The patient, the study and the series already exist
-
-          bool ok = (database.LookupResource(result.patientId_, dummy, hashPatient) &&
-                     database.LookupResource(result.studyId_, dummy, hashStudy));
-          assert(ok);
-        }
-        else if (database.LookupResource(result.studyId_, dummy, hashStudy))
-        {
-          assert(dummy == ResourceType_Study);
-
-          // New series: The patient and the study already exist
-          result.isNewSeries_ = true;
-
-          bool ok = database.LookupResource(result.patientId_, dummy, hashPatient);
-          assert(ok);
-        }
-        else if (database.LookupResource(result.patientId_, dummy, hashPatient))
-        {
-          assert(dummy == ResourceType_Patient);
-
-          // New study and series: The patient already exist
-          result.isNewStudy_ = true;
-          result.isNewSeries_ = true;
-        }
-        else
-        {
-          // New patient, study and series: Nothing exists
-          result.isNewPatient_ = true;
-          result.isNewStudy_ = true;
-          result.isNewSeries_ = true;
-        }
-      }
-
-      // Create the series if needed
-      if (result.isNewSeries_)
-      {
-        result.seriesId_ = compatibility.CreateResource(hashSeries, ResourceType_Series);
-      }
-
-      // Create the study if needed
-      if (result.isNewStudy_)
-      {
-        result.studyId_ = compatibility.CreateResource(hashStudy, ResourceType_Study);
-      }
-
-      // Create the patient if needed
-      if (result.isNewPatient_)
-      {
-        result.patientId_ = compatibility.CreateResource(hashPatient, ResourceType_Patient);
-      }
-
-      // Create the parent-to-child links
-      compatibility.AttachChild(result.seriesId_, instanceId);
-
-      if (result.isNewSeries_)
-      {
-        compatibility.AttachChild(result.studyId_, result.seriesId_);
-      }
-
-      if (result.isNewStudy_)
-      {
-        compatibility.AttachChild(result.patientId_, result.studyId_);
-      }
-
-      // Sanity checks
-      assert(result.patientId_ != -1);
-      assert(result.studyId_ != -1);
-      assert(result.seriesId_ != -1);
-      assert(instanceId != -1);
-
-      return true;
-    }
-  }
-}
--- a/OrthancServer/Search/Compatibility/ICompatibilityCreateInstance.h	Fri Jan 04 15:52:19 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,65 +0,0 @@
-/**
- * Orthanc - A Lightweight, RESTful DICOM Store
- * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
- * Department, University Hospital of Liege, Belgium
- * Copyright (C) 2017-2019 Osimis S.A., Belgium
- *
- * This program is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * In addition, as a special exception, the copyright holders of this
- * program give permission to link the code of its release with the
- * OpenSSL project's "OpenSSL" library (or with modified versions of it
- * that use the same license as the "OpenSSL" library), and distribute
- * the linked executables. You must obey the GNU General Public License
- * in all respects for all of the code used other than "OpenSSL". If you
- * modify file(s) with this exception, you may extend this exception to
- * your version of the file(s), but you are not obligated to do so. If
- * you do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source files
- * in the program, then also delete it here.
- * 
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- **/
-
-
-#pragma once
-
-#include "../../IDatabaseWrapper.h"
-
-namespace Orthanc
-{
-  namespace Compatibility
-  {
-    class ICompatibilityCreateInstance : public boost::noncopyable
-    {
-    public:
-      virtual ~ICompatibilityCreateInstance()
-      {
-      }
-      
-      virtual int64_t CreateResource(const std::string& publicId,
-                                     ResourceType type) = 0;
-
-      virtual void AttachChild(int64_t parent,
-                               int64_t child) = 0;
-      
-      static bool Apply(IDatabaseWrapper::CreateInstanceResult& result,
-                        int64_t& instanceId,
-                        ICompatibilityCreateInstance& compatibility,
-                        IDatabaseWrapper& database,
-                        const std::string& patient,
-                        const std::string& study,
-                        const std::string& series,
-                        const std::string& instance);
-    };
-  }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Search/Compatibility/ICreateInstance.cpp	Fri Jan 04 16:42:55 2019 +0100
@@ -0,0 +1,156 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2019 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../../PrecompiledHeadersServer.h"
+#include "ICreateInstance.h"
+
+#include "../../../Core/OrthancException.h"
+
+namespace Orthanc
+{
+  namespace Compatibility
+  {
+    bool ICreateInstance::Apply(ICreateInstance& compatibility,
+                                IDatabaseWrapper& database,
+                                IDatabaseWrapper::CreateInstanceResult& result,
+                                int64_t& instanceId,
+                                const std::string& hashPatient,
+                                const std::string& hashStudy,
+                                const std::string& hashSeries,
+                                const std::string& hashInstance)
+    {
+      {
+        ResourceType type;
+        int64_t tmp;
+        
+        if (database.LookupResource(tmp, type, hashInstance))
+        {
+          // The instance already exists
+          assert(type == ResourceType_Instance);
+          instanceId = tmp;
+          return false;
+        }
+      }
+
+      instanceId = compatibility.CreateResource(hashInstance, ResourceType_Instance);
+
+      result.isNewPatient_ = false;
+      result.isNewStudy_ = false;
+      result.isNewSeries_ = false;
+      result.patientId_ = -1;
+      result.studyId_ = -1;
+      result.seriesId_ = -1;
+      
+      // Detect up to which level the patient/study/series/instance
+      // hierarchy must be created
+
+      {
+        ResourceType dummy;
+
+        if (database.LookupResource(result.seriesId_, dummy, hashSeries))
+        {
+          assert(dummy == ResourceType_Series);
+          // The patient, the study and the series already exist
+
+          bool ok = (database.LookupResource(result.patientId_, dummy, hashPatient) &&
+                     database.LookupResource(result.studyId_, dummy, hashStudy));
+          assert(ok);
+        }
+        else if (database.LookupResource(result.studyId_, dummy, hashStudy))
+        {
+          assert(dummy == ResourceType_Study);
+
+          // New series: The patient and the study already exist
+          result.isNewSeries_ = true;
+
+          bool ok = database.LookupResource(result.patientId_, dummy, hashPatient);
+          assert(ok);
+        }
+        else if (database.LookupResource(result.patientId_, dummy, hashPatient))
+        {
+          assert(dummy == ResourceType_Patient);
+
+          // New study and series: The patient already exist
+          result.isNewStudy_ = true;
+          result.isNewSeries_ = true;
+        }
+        else
+        {
+          // New patient, study and series: Nothing exists
+          result.isNewPatient_ = true;
+          result.isNewStudy_ = true;
+          result.isNewSeries_ = true;
+        }
+      }
+
+      // Create the series if needed
+      if (result.isNewSeries_)
+      {
+        result.seriesId_ = compatibility.CreateResource(hashSeries, ResourceType_Series);
+      }
+
+      // Create the study if needed
+      if (result.isNewStudy_)
+      {
+        result.studyId_ = compatibility.CreateResource(hashStudy, ResourceType_Study);
+      }
+
+      // Create the patient if needed
+      if (result.isNewPatient_)
+      {
+        result.patientId_ = compatibility.CreateResource(hashPatient, ResourceType_Patient);
+      }
+
+      // Create the parent-to-child links
+      compatibility.AttachChild(result.seriesId_, instanceId);
+
+      if (result.isNewSeries_)
+      {
+        compatibility.AttachChild(result.studyId_, result.seriesId_);
+      }
+
+      if (result.isNewStudy_)
+      {
+        compatibility.AttachChild(result.patientId_, result.studyId_);
+      }
+
+      // Sanity checks
+      assert(result.patientId_ != -1);
+      assert(result.studyId_ != -1);
+      assert(result.seriesId_ != -1);
+      assert(instanceId != -1);
+
+      return true;
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Search/Compatibility/ICreateInstance.h	Fri Jan 04 16:42:55 2019 +0100
@@ -0,0 +1,61 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2019 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../../IDatabaseWrapper.h"
+
+namespace Orthanc
+{
+  namespace Compatibility
+  {
+    class ICreateInstance : public boost::noncopyable
+    {
+    public:
+      virtual int64_t CreateResource(const std::string& publicId,
+                                     ResourceType type) = 0;
+
+      virtual void AttachChild(int64_t parent,
+                               int64_t child) = 0;
+      
+      static bool Apply(ICreateInstance& compatibility,
+                        IDatabaseWrapper& database,
+                        IDatabaseWrapper::CreateInstanceResult& result,
+                        int64_t& instanceId,
+                        const std::string& patient,
+                        const std::string& study,
+                        const std::string& series,
+                        const std::string& instance);
+    };
+  }
+}
--- a/OrthancServer/Search/Compatibility/ISetResourcesContent.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/ISetResourcesContent.h	Fri Jan 04 16:42:55 2019 +0100
@@ -33,26 +33,19 @@
 
 #pragma once
 
-#include "../../IDatabaseWrapper.h"
 #include "../../ServerToolbox.h"
 
 namespace Orthanc
 {
   namespace Compatibility
   {
-    class ISetResourcesContent : public IDatabaseWrapper
+    class ISetResourcesContent : public boost::noncopyable
     {
     public:
       virtual ~ISetResourcesContent()
       {
       }
       
-      virtual void SetResourcesContent(const ResourcesContent& content)
-        ORTHANC_OVERRIDE
-      {
-        content.Store(*this);
-      }
-
       virtual void SetMainDicomTag(int64_t id,
                                    const DicomTag& tag,
                                    const std::string& value) = 0;
@@ -64,6 +57,12 @@
       virtual void SetMetadata(int64_t id,
                                MetadataType type,
                                const std::string& value) = 0;
+
+      static void Apply(ISetResourcesContent& that,
+                        const ResourcesContent& content)
+      {
+        content.Store(that);
+      }
     };
   }
 }
--- a/OrthancServer/Search/Compatibility/SetOfResources.cpp	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/SetOfResources.cpp	Fri Jan 04 16:42:55 2019 +0100
@@ -138,14 +138,15 @@
     }
 
 
-    void SetOfResources::Flatten(std::list<int64_t>& result)
+    void SetOfResources::Flatten(CompatibilityDatabaseWrapper& compatibility,
+                                 std::list<int64_t>& result)
     {
       result.clear();
       
       if (resources_.get() == NULL)
       {
         // All the resources of this level are part of the filter
-        database_.GetAllInternalIds(result, level_);
+        compatibility.GetAllInternalIds(result, level_);
       }
       else
       {
--- a/OrthancServer/Search/Compatibility/SetOfResources.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/OrthancServer/Search/Compatibility/SetOfResources.h	Fri Jan 04 16:42:55 2019 +0100
@@ -33,6 +33,7 @@
 
 #pragma once
 
+#include "../../IDatabaseWrapper.h"
 #include "CompatibilityDatabaseWrapper.h"
 
 #include <set>
@@ -47,12 +48,12 @@
     private:
       typedef std::set<int64_t>  Resources;
 
-      CompatibilityDatabaseWrapper&  database_;
-      ResourceType                   level_;
-      std::auto_ptr<Resources>       resources_;
+      IDatabaseWrapper&         database_;
+      ResourceType              level_;
+      std::auto_ptr<Resources>  resources_;
     
     public:
-      SetOfResources(CompatibilityDatabaseWrapper& database,
+      SetOfResources(IDatabaseWrapper& database,
                      ResourceType level) : 
         database_(database),
         level_(level)
@@ -68,7 +69,8 @@
 
       void GoDown();
 
-      void Flatten(std::list<int64_t>& result);
+      void Flatten(CompatibilityDatabaseWrapper& compatibility,
+                   std::list<int64_t>& result);
 
       void Flatten(std::list<std::string>& result);
 
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Jan 04 15:52:19 2019 +0100
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Jan 04 16:42:55 2019 +0100
@@ -1161,8 +1161,8 @@
     if (extensions_.lookupResources == NULL)
     {
       // Fallback to compatibility mode
-      CompatibilityDatabaseWrapper::ApplyLookupResources
-        (resourcesId, instancesId, lookup, queryLevel, limit);
+      CompatibilityDatabaseWrapper::Apply(*this, *this, resourcesId, instancesId,
+                                          lookup, queryLevel, limit);
     }
     else
     {
@@ -1200,8 +1200,8 @@
     if (extensions_.createInstance == NULL)
     {
       // Fallback to compatibility mode
-      return CompatibilityDatabaseWrapper::CreateInstance
-        (result, instanceId, patient, study, series, instance);
+      return ICreateInstance::Apply
+        (*this, *this, result, instanceId, patient, study, series, instance);
     }
     else
     {
--- a/Plugins/Engine/OrthancPluginDatabase.h	Fri Jan 04 15:52:19 2019 +0100
+++ b/Plugins/Engine/OrthancPluginDatabase.h	Fri Jan 04 16:42:55 2019 +0100
@@ -37,12 +37,18 @@
 
 #include "../../Core/SharedLibrary.h"
 #include "../../OrthancServer/Search/Compatibility/CompatibilityDatabaseWrapper.h"
+#include "../../OrthancServer/Search/Compatibility/ISetResourcesContent.h"
+#include "../../OrthancServer/Search/Compatibility/ICreateInstance.h"
 #include "../Include/orthanc/OrthancCDatabasePlugin.h"
 #include "PluginsErrorDictionary.h"
 
 namespace Orthanc
 {
-  class OrthancPluginDatabase : public Compatibility::CompatibilityDatabaseWrapper
+  class OrthancPluginDatabase :
+    public IDatabaseWrapper,
+    public Compatibility::CompatibilityDatabaseWrapper,
+    public Compatibility::ISetResourcesContent,
+    public Compatibility::ICreateInstance
   {
   private:
     class Transaction;
@@ -344,6 +350,12 @@
                                        const std::string& start,
                                        const std::string& end)
       ORTHANC_OVERRIDE;
+
+    virtual void SetResourcesContent(const Orthanc::ResourcesContent& content)
+      ORTHANC_OVERRIDE
+    {
+      ISetResourcesContent::Apply(*this, content);
+    }
   };
 }