changeset 5852:ea547160f27e find-refactoring

StatelessDatabaseOperations: reimplementing LookupAttachment()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 29 Oct 2024 15:57:53 +0000
parents 6a4e47163b3c
children 65f8c6dfba50
files OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/Database/FindResponse.cpp OrthancServer/Sources/Database/FindResponse.h OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.h OrthancServer/Sources/OrthancWebDav.cpp OrthancServer/Sources/ResourceFinder.cpp OrthancServer/UnitTestsSources/ServerIndexTests.cpp
diffstat 12 files changed, 81 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -1665,9 +1665,14 @@
             target->AddLabel(source.labels(i));
           }
 
+          if (source.attachments().size() != source.attachments_revisions().size())
+          {
+            throw OrthancException(ErrorCode_DatabasePlugin);
+          }
+
           for (int i = 0; i < source.attachments().size(); i++)
           {
-            target->AddAttachment(Convert(source.attachments(i)));
+            target->AddAttachment(Convert(source.attachments(i)), source.attachments_revisions(i));
           }
 
           Convert(*target, ResourceType_Patient, source.patient_content());
--- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Tue Oct 29 15:57:53 2024 +0000
@@ -961,6 +961,7 @@
     string one_instance_public_id = 13;
     repeated Metadata one_instance_metadata = 14;
     repeated FileInfo one_instance_attachments = 15;
+    repeated int64 attachments_revisions = 16;
   }
 }
 
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -497,7 +497,7 @@
           if (transaction_.LookupAttachment(info, revision, internalId, *it) &&
               info.GetContentType() == *it)
           {
-            resource->AddAttachment(info);
+            resource->AddAttachment(info, revision);
           }
           else
           {
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -455,11 +455,14 @@
   }
 
 
-  void FindResponse::Resource::AddAttachment(const FileInfo& attachment)
+  void FindResponse::Resource::AddAttachment(const FileInfo& attachment,
+                                             int64_t revision)
   {
-    if (attachments_.find(attachment.GetContentType()) == attachments_.end())
+    if (attachments_.find(attachment.GetContentType()) == attachments_.end() &&
+        revisions_.find(attachment.GetContentType()) == revisions_.end())
     {
       attachments_[attachment.GetContentType()] = attachment;
+      revisions_[attachment.GetContentType()] = revision;
     }
     else
     {
@@ -468,17 +471,36 @@
   }
 
 
-  bool FindResponse::Resource::LookupAttachment(FileInfo& target, FileContentType type) const
+  bool FindResponse::Resource::LookupAttachment(FileInfo& target,
+                                                int64_t& revision,
+                                                FileContentType type) const
   {
     std::map<FileContentType, FileInfo>::const_iterator it = attachments_.find(type);
+    std::map<FileContentType, int64_t>::const_iterator it2 = revisions_.find(type);
+
     if (it != attachments_.end())
     {
-      target = it->second;
-      return true;
+      if (it2 != revisions_.end())
+      {
+        target = it->second;
+        revision = it2->second;
+        return true;
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
     else
     {
-      return false;
+      if (it2 == revisions_.end())
+      {
+        return false;
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
   }
 
--- a/OrthancServer/Sources/Database/FindResponse.h	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/FindResponse.h	Tue Oct 29 15:57:53 2024 +0000
@@ -123,6 +123,7 @@
       ChildrenInformation                   childrenInstancesInformation_;
       std::set<std::string>                 labels_;
       std::map<FileContentType, FileInfo>   attachments_;
+      std::map<FileContentType, int64_t>    revisions_;
       bool                                  hasOneInstanceMetadataAndAttachments_;
       std::string                           oneInstancePublicId_;
       std::map<MetadataType, std::string>   oneInstanceMetadata_;
@@ -263,9 +264,11 @@
         return labels_;
       }
 
-      void AddAttachment(const FileInfo& attachment);
+      void AddAttachment(const FileInfo& attachment,
+                         int64_t revision);
 
       bool LookupAttachment(FileInfo& target,
+                            int64_t& revision,
                             FileContentType type) const;
 
       const std::map<FileContentType, FileInfo>& GetAttachments() const
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -987,7 +987,7 @@
                           s.ColumnInt64(C8_BIG_INT_1), s.ColumnString(C4_STRING_2),
                           static_cast<CompressionType>(s.ColumnInt(C7_INT_2)),
                           s.ColumnInt64(C9_BIG_INT_2), s.ColumnString(C5_STRING_3));
-            res.AddAttachment(file);
+            res.AddAttachment(file, 0 /* TODO - REVISIONS */);
           }; break;
 
           case QUERY_MAIN_DICOM_TAGS:
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h	Tue Oct 29 15:57:53 2024 +0000
@@ -102,8 +102,8 @@
 
     virtual bool HasIntegratedFind() const ORTHANC_OVERRIDE
     {
-      return true;   // => This uses specialized SQL commands
-      //return false;   // => This uses Compatibility/GenericFind
+      //return true;   // => This uses specialized SQL commands
+      return false;   // => This uses Compatibility/GenericFind
     }
 
     /**
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -599,7 +599,14 @@
     }
     else if (response.GetSize() == 1)
     {
-      target = response.GetResourceByIndex(0).GetMetadata(level);
+      if (response.GetResourceByIndex(0).GetLevel() != level)
+      {
+        throw OrthancException(ErrorCode_DatabasePlugin);
+      }
+      else
+      {
+        target = response.GetResourceByIndex(0).GetMetadata(level);
+      }
     }
     else
     {
@@ -611,43 +618,35 @@
   bool StatelessDatabaseOperations::LookupAttachment(FileInfo& attachment,
                                                      int64_t& revision,
                                                      ResourceType level,
-                                                     const std::string& instancePublicId,
+                                                     const std::string& publicId,
                                                      FileContentType contentType)
   {
-    class Operations : public ReadOnlyOperationsT6<bool&, FileInfo&, int64_t&, const std::string&, FileContentType, ResourceType>
+    FindRequest request(level);
+    request.SetOrthancId(level, publicId);
+    request.SetRetrieveAttachments(true);
+
+    FindResponse response;
+    ExecuteFind(response, request);
+
+    if (response.GetSize() == 0)
     {
-    public:
-      virtual void ApplyTuple(ReadOnlyTransaction& transaction,
-                              const Tuple& tuple) ORTHANC_OVERRIDE
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+    else if (response.GetSize() == 1)
+    {
+      if (response.GetResourceByIndex(0).GetLevel() != level)
       {
-        int64_t internalId;
-        ResourceType type;
-        if (!transaction.LookupResource(internalId, type, tuple.get<3>()))
-        {
-          throw OrthancException(ErrorCode_UnknownResource);
-        }
-
-        if (type != tuple.get<5>())
-        {
-          throw OrthancException(ErrorCode_DatabasePlugin);
-        }
-
-        if (transaction.LookupAttachment(tuple.get<1>(), tuple.get<2>(), internalId, tuple.get<4>()))
-        {
-          assert(tuple.get<1>().GetContentType() == tuple.get<4>());
-          tuple.get<0>() = true;
-        }
-        else
-        {
-          tuple.get<0>() = false;
-        }
+        throw OrthancException(ErrorCode_DatabasePlugin);
+      }
+      else
+      {
+        return response.GetResourceByIndex(0).LookupAttachment(attachment, revision, contentType);
       }
-    };
-
-    bool found;
-    Operations operations;
-    operations.Apply(*this, found, attachment, revision, instancePublicId, contentType, level);
-    return found;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_DatabasePlugin);
+    }
   }
 
 
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Tue Oct 29 15:57:53 2024 +0000
@@ -541,7 +541,7 @@
     bool LookupAttachment(FileInfo& attachment,
                           int64_t& revision,
                           ResourceType level,
-                          const std::string& instancePublicId,
+                          const std::string& publicId,
                           FileContentType contentType);
 
     void GetChanges(Json::Value& target,
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -145,7 +145,8 @@
         if (resource.GetLevel() == ResourceType_Instance)
         {
           FileInfo info;
-          if (resource.LookupAttachment(info, FileContentType_Dicom))
+          int64_t revision;
+          if (resource.LookupAttachment(info, revision, FileContentType_Dicom))
           {
             std::unique_ptr<File> f(new File(uid + ".dcm"));
             f->SetMimeType(MimeType_Dicom);
--- a/OrthancServer/Sources/ResourceFinder.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/Sources/ResourceFinder.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -326,7 +326,8 @@
         if (responseContent_ & ResponseContentFlags_AttachmentsLegacy)
         {
           FileInfo info;
-          if (resource.LookupAttachment(info, FileContentType_Dicom))
+          int64_t revision;
+          if (resource.LookupAttachment(info, revision, FileContentType_Dicom))
           {
             target["FileSize"] = static_cast<Json::UInt64>(info.GetUncompressedSize());
             target["FileUuid"] = info.GetUuid();
--- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Tue Oct 29 15:21:15 2024 +0000
+++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Tue Oct 29 15:57:53 2024 +0000
@@ -869,7 +869,7 @@
     }
 
     FileInfo dicom1, pixelData1;
-    int64_t revision;
+    int64_t revision = -1;
     ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom1, revision, ResourceType_Instance, id, FileContentType_Dicom));
     ASSERT_EQ(0, revision);
     revision = -1;