changeset 262:b0c65094b299

adding support for revisions in attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Apr 2021 14:46:42 +0200
parents 34e2b93a7ac1
children 29d2b76516f6
files Framework/Plugins/DatabaseBackendAdapterV3.cpp Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h Framework/Plugins/IndexUnitTests.h SQLite/Plugins/PrepareIndex.sql
diffstat 5 files changed, 105 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Mon Apr 19 18:47:50 2021 +0200
+++ b/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Tue Apr 20 14:46:42 2021 +0200
@@ -2009,6 +2009,7 @@
     params.rollback = Rollback;
     params.commit = Commit;
 
+    params.addAttachment = AddAttachment;
     params.clearChanges = ClearChanges;
     params.clearExportedResources = ClearExportedResources;
     params.clearMainDicomTags = ClearMainDicomTags;
@@ -2048,7 +2049,6 @@
     params.lookupResources = LookupResources;
     params.selectPatientToRecycle = SelectPatientToRecycle;
     params.selectPatientToRecycle2 = SelectPatientToRecycle2;
-    params.setAttachment = AddAttachment;
     params.setGlobalProperty = SetGlobalProperty;
     params.setMetadata = SetMetadata;
     params.setProtectedPatient = SetProtectedPatient;
--- a/Framework/Plugins/IndexBackend.cpp	Mon Apr 19 18:47:50 2021 +0200
+++ b/Framework/Plugins/IndexBackend.cpp	Tue Apr 20 14:46:42 2021 +0200
@@ -58,8 +58,8 @@
   }
 
   
-  int64_t IndexBackend::ReadInteger64(const DatabaseManager::StatementBase& statement,
-                                      size_t field)
+  static int64_t ReadInteger64(const DatabaseManager::StatementBase& statement,
+                               size_t field)
   {
     if (statement.IsDone())
     {
@@ -80,8 +80,8 @@
   }
 
 
-  int32_t IndexBackend::ReadInteger32(const DatabaseManager::StatementBase& statement,
-                                      size_t field)
+  static int32_t ReadInteger32(const DatabaseManager::StatementBase& statement,
+                               size_t field)
   {
     if (statement.IsDone())
     {
@@ -102,8 +102,8 @@
   }
 
     
-  std::string IndexBackend::ReadString(const DatabaseManager::StatementBase& statement,
-                                       size_t field)
+  static std::string ReadString(const DatabaseManager::StatementBase& statement,
+                                size_t field)
   {
     const IValue& value = statement.GetResultField(field);
 
@@ -123,9 +123,9 @@
 
     
   template <typename T>
-  void IndexBackend::ReadListOfIntegers(std::list<T>& target,
-                                        DatabaseManager::CachedStatement& statement,
-                                        const Dictionary& args)
+  static void ReadListOfIntegers(std::list<T>& target,
+                                 DatabaseManager::CachedStatement& statement,
+                                 const Dictionary& args)
   {
     statement.Execute(args);
       
@@ -149,9 +149,9 @@
   }
 
     
-  void IndexBackend::ReadListOfStrings(std::list<std::string>& target,
-                                       DatabaseManager::CachedStatement& statement,
-                                       const Dictionary& args)
+  static void ReadListOfStrings(std::list<std::string>& target,
+                                DatabaseManager::CachedStatement& statement,
+                                const Dictionary& args)
   {
     statement.Execute(args);
 
@@ -349,19 +349,12 @@
     }
   }
 
-  
-  void IndexBackend::AddAttachment(DatabaseManager& manager,
+
+  static void ExecuteAddAttachment(DatabaseManager::CachedStatement& statement,
+                                   Dictionary& args,
                                    int64_t id,
-                                   const OrthancPluginAttachment& attachment,
-                                   int64_t revision)
+                                   const OrthancPluginAttachment& attachment)
   {
-    // TODO - REVISIONS
-    
-    DatabaseManager::CachedStatement statement(
-      STATEMENT_FROM_HERE, manager,
-      "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, "
-      "${compressed}, ${uncompressed}, ${compression}, ${hash}, ${hash-compressed})");
-
     statement.SetParameterType("id", ValueType_Integer64);
     statement.SetParameterType("type", ValueType_Integer64);
     statement.SetParameterType("uuid", ValueType_Utf8String);
@@ -371,7 +364,6 @@
     statement.SetParameterType("hash", ValueType_Utf8String);
     statement.SetParameterType("hash-compressed", ValueType_Utf8String);
 
-    Dictionary args;
     args.SetIntegerValue("id", id);
     args.SetIntegerValue("type", attachment.contentType);
     args.SetUtf8Value("uuid", attachment.uuid);
@@ -380,10 +372,42 @@
     args.SetIntegerValue("compression", attachment.compressionType);
     args.SetUtf8Value("hash", attachment.uncompressedHash);
     args.SetUtf8Value("hash-compressed", attachment.compressedHash);
-    
+
     statement.Execute(args);
   }
 
+  
+  void IndexBackend::AddAttachment(DatabaseManager& manager,
+                                   int64_t id,
+                                   const OrthancPluginAttachment& attachment,
+                                   int64_t revision)
+  {
+    if (HasRevisionsSupport())
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager,
+        "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, ${compressed}, "
+        "${uncompressed}, ${compression}, ${hash}, ${hash-compressed}, ${revision})");
+
+      Dictionary args;
+
+      statement.SetParameterType("revision", ValueType_Integer64);
+      args.SetIntegerValue("revision", revision);
+      
+      ExecuteAddAttachment(statement, args, id, attachment);
+    }
+    else
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager,
+        "INSERT INTO AttachedFiles VALUES(${id}, ${type}, ${uuid}, ${compressed}, "
+        "${uncompressed}, ${compression}, ${hash}, ${hash-compressed})");
+
+      Dictionary args;
+      ExecuteAddAttachment(statement, args, id, attachment);
+    }
+  }
+
     
   void IndexBackend::AttachChild(DatabaseManager& manager,
                                  int64_t parent,
@@ -1026,19 +1050,12 @@
     statement.Execute(args);
   }
 
-    
-  /* Use GetOutput().AnswerAttachment() */
-  bool IndexBackend::LookupAttachment(IDatabaseBackendOutput& output,
-                                      int64_t& revision /*out*/,
-                                      DatabaseManager& manager,
+
+  static bool ExecuteLookupAttachment(DatabaseManager::CachedStatement& statement,
+                                      IDatabaseBackendOutput& output,
                                       int64_t id,
                                       int32_t contentType)
   {
-    DatabaseManager::CachedStatement statement(
-      STATEMENT_FROM_HERE, manager,
-      "SELECT uuid, uncompressedSize, compressionType, compressedSize, "
-      "uncompressedHash, compressedHash FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
-
     statement.SetReadOnly(true);
     statement.SetParameterType("id", ValueType_Integer64);
     statement.SetParameterType("type", ValueType_Integer64);
@@ -1062,12 +1079,48 @@
                               ReadInteger32(statement, 2),
                               ReadInteger64(statement, 3),
                               ReadString(statement, 5));
-
-      revision = 0;  // TODO - REVISIONS
-
       return true;
     }
   }
+                                      
+  
+    
+  /* Use GetOutput().AnswerAttachment() */
+  bool IndexBackend::LookupAttachment(IDatabaseBackendOutput& output,
+                                      int64_t& revision /*out*/,
+                                      DatabaseManager& manager,
+                                      int64_t id,
+                                      int32_t contentType)
+  {
+    if (HasRevisionsSupport())
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager,
+        "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, "
+        "compressedHash, revision FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
+      
+      if (ExecuteLookupAttachment(statement, output, id, contentType))
+      {
+        revision = ReadInteger64(statement, 6);
+        return true;
+      }
+      else
+      {
+        return false;
+      }
+    }
+    else
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager,
+        "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, "
+        "compressedHash FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
+      
+      revision = 0;
+
+      return ExecuteLookupAttachment(statement, output, id, contentType);
+    }
+  }
 
 
   static bool ReadGlobalProperty(std::string& target,
--- a/Framework/Plugins/IndexBackend.h	Mon Apr 19 18:47:50 2021 +0200
+++ b/Framework/Plugins/IndexBackend.h	Tue Apr 20 14:46:42 2021 +0200
@@ -45,24 +45,6 @@
     std::unique_ptr<IDatabaseBackendOutput::IFactory>  outputFactory_;
     
   protected:
-    static int64_t ReadInteger64(const DatabaseManager::StatementBase& statement,
-                                 size_t field);
-
-    static int32_t ReadInteger32(const DatabaseManager::StatementBase& statement,
-                                 size_t field);
-    
-    static std::string ReadString(const DatabaseManager::StatementBase& statement,
-                                  size_t field);
-    
-    template <typename T>
-    static void ReadListOfIntegers(std::list<T>& target,
-                                   DatabaseManager::CachedStatement& statement,
-                                   const Dictionary& args);
-    
-    static void ReadListOfStrings(std::list<std::string>& target,
-                                  DatabaseManager::CachedStatement& statement,
-                                  const Dictionary& args);
-
     void ClearDeletedFiles(DatabaseManager& manager);
 
     void ClearDeletedResources(DatabaseManager& manager);
--- a/Framework/Plugins/IndexUnitTests.h	Mon Apr 19 18:47:50 2021 +0200
+++ b/Framework/Plugins/IndexUnitTests.h	Tue Apr 20 14:46:42 2021 +0200
@@ -375,7 +375,12 @@
   expectedAttachment->compressedSize = 42;
   expectedAttachment->compressedHash = "md5_1";
   ASSERT_TRUE(db.LookupAttachment(*output, revision, *manager, a, Orthanc::FileContentType_Dicom));
-  ASSERT_EQ(0, revision);  // TODO - REVISIONS
+
+#if ORTHANC_ENABLE_SQLITE == 1
+  ASSERT_EQ(42, revision);  // Only SQLite implements revisions so far
+#else
+  ASSERT_EQ(0, revision);
+#endif
 
   expectedAttachment.reset(new OrthancPluginAttachment);
   expectedAttachment->uuid = "uuid2";
@@ -387,7 +392,12 @@
   expectedAttachment->compressedHash = "md5_2";
   revision = -1;
   ASSERT_TRUE(db.LookupAttachment(*output, revision, *manager, a, Orthanc::FileContentType_DicomAsJson));
-  ASSERT_EQ(0, revision);  // TODO - REVISIONS
+
+#if ORTHANC_ENABLE_SQLITE == 1
+  ASSERT_EQ(43, revision);  // Only SQLite implements revisions so far
+#else
+  ASSERT_EQ(0, revision);
+#endif
 
   db.ListAvailableAttachments(fc, *manager, b);
   ASSERT_EQ(0u, fc.size());
--- a/SQLite/Plugins/PrepareIndex.sql	Mon Apr 19 18:47:50 2021 +0200
+++ b/SQLite/Plugins/PrepareIndex.sql	Tue Apr 20 14:46:42 2021 +0200
@@ -43,6 +43,7 @@
        compressionType INTEGER,
        uncompressedHash TEXT,
        compressedHash TEXT,
+       revision INTEGER,
        PRIMARY KEY(id, fileType)
        );