changeset 5850:1980354c8113 find-refactoring

add level of interest to StatelessDatabaseOperations::LookupAttachment()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 29 Oct 2024 15:03:32 +0000
parents 0f4345cbe558
children 6a4e47163b3c
files OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.h OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/OrthancWebDav.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/UnitTestsSources/ServerIndexTests.cpp
diffstat 8 files changed, 81 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -610,10 +610,11 @@
 
   bool StatelessDatabaseOperations::LookupAttachment(FileInfo& attachment,
                                                      int64_t& revision,
+                                                     ResourceType level,
                                                      const std::string& instancePublicId,
                                                      FileContentType contentType)
   {
-    class Operations : public ReadOnlyOperationsT5<bool&, FileInfo&, int64_t&, const std::string&, FileContentType>
+    class Operations : public ReadOnlyOperationsT6<bool&, FileInfo&, int64_t&, const std::string&, FileContentType, ResourceType>
     {
     public:
       virtual void ApplyTuple(ReadOnlyTransaction& transaction,
@@ -625,7 +626,13 @@
         {
           throw OrthancException(ErrorCode_UnknownResource);
         }
-        else if (transaction.LookupAttachment(tuple.get<1>(), tuple.get<2>(), internalId, tuple.get<4>()))
+
+        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;
@@ -639,7 +646,7 @@
 
     bool found;
     Operations operations;
-    operations.Apply(*this, found, attachment, revision, instancePublicId, contentType);
+    operations.Apply(*this, found, attachment, revision, instancePublicId, contentType, level);
     return found;
   }
 
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Tue Oct 29 15:03:32 2024 +0000
@@ -540,6 +540,7 @@
 
     bool LookupAttachment(FileInfo& attachment,
                           int64_t& revision,
+                          ResourceType level,
                           const std::string& instancePublicId,
                           FileContentType contentType);
 
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -399,7 +399,7 @@
     else
     {
       // return the attachment without any transcoding
-      context.AnswerAttachment(call.GetOutput(), publicId, FileContentType_Dicom);
+      context.AnswerAttachment(call.GetOutput(), ResourceType_Instance, publicId, FileContentType_Dicom);
     }
   }
 
@@ -1673,11 +1673,17 @@
 
   // Handling of metadata -----------------------------------------------------
 
-  static void CheckValidResourceType(const RestApiCall& call)
+  static ResourceType GetResourceTypeFromUri(const RestApiCall& call)
   {
     assert(!call.GetFullUri().empty());
     const std::string resourceType = call.GetFullUri() [0];
-    StringToResourceType(resourceType.c_str());
+    return StringToResourceType(resourceType.c_str());
+  }
+
+
+  static void CheckValidResourceType(const RestApiCall& call)
+  {
+    GetResourceTypeFromUri(call);
   }
 
 
@@ -2197,6 +2203,7 @@
 
   
   static bool GetAttachmentInfo(FileInfo& info,
+                                ResourceType level,
                                 RestApiGetCall& call)
   {
     CheckValidResourceType(call);
@@ -2206,7 +2213,7 @@
     FileContentType contentType = StringToContentType(name);
 
     int64_t revision;
-    if (OrthancRestApi::GetIndex(call).LookupAttachment(info, revision, publicId, contentType))
+    if (OrthancRestApi::GetIndex(call).LookupAttachment(info, revision, level, publicId, contentType))
     {
       SetAttachmentETag(call.GetOutput(), revision, info);  // New in Orthanc 1.9.2
 
@@ -2233,6 +2240,8 @@
 
   static void GetAttachmentOperations(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
       ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
@@ -2248,7 +2257,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       Json::Value operations = Json::arrayValue;
 
@@ -2287,9 +2296,10 @@
   template <int uncompress>
   static void GetAttachmentData(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       call.GetDocumentation()
         .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
@@ -2312,13 +2322,13 @@
     FileContentType type = StringToContentType(call.GetUriComponent("name", ""));
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       // NB: "SetAttachmentETag()" is already invoked by "GetAttachmentInfo()"
 
       if (uncompress)
       {
-        context.AnswerAttachment(call.GetOutput(), publicId, type);
+        context.AnswerAttachment(call.GetOutput(), t, publicId, type);
       }
       else
       {
@@ -2326,7 +2336,7 @@
         std::string content;
         std::string attachmentId;
         int64_t revision;
-        context.ReadAttachment(content, revision, attachmentId, publicId, type, false, true /* skipCache when you absolutely need the compressed data */);
+        context.ReadAttachment(content, revision, attachmentId, t, publicId, type, false, true /* skipCache when you absolutely need the compressed data */);
 
         int64_t userRevision;
         std::string userMD5;
@@ -2347,9 +2357,10 @@
 
   static void GetAttachmentSize(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2361,7 +2372,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MimeType_PlainText);
     }
@@ -2369,9 +2380,10 @@
 
   static void GetAttachmentInfo(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2384,7 +2396,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       Json::Value result = Json::objectValue;    
       result["Uuid"] = info.GetUuid();
@@ -2400,9 +2412,10 @@
 
   static void GetAttachmentCompressedSize(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2415,7 +2428,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MimeType_PlainText);
     }
@@ -2424,9 +2437,10 @@
 
   static void GetAttachmentMD5(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2438,7 +2452,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call) &&
+    if (GetAttachmentInfo(info, t, call) &&
         info.GetUncompressedMD5() != "")
     {
       call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), MimeType_PlainText);
@@ -2448,9 +2462,10 @@
 
   static void GetAttachmentCompressedMD5(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2463,7 +2478,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call) &&
+    if (GetAttachmentInfo(info, t, call) &&
         info.GetCompressedMD5() != "")
     {
       call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), MimeType_PlainText);
@@ -2473,9 +2488,10 @@
 
   static void VerifyAttachment(RestApiPostCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       call.GetDocumentation()
         .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
@@ -2496,7 +2512,7 @@
 
     FileInfo info;
     int64_t revision;  // Ignored
-    if (!OrthancRestApi::GetIndex(call).LookupAttachment(info, revision, publicId, contentType) ||
+    if (!OrthancRestApi::GetIndex(call).LookupAttachment(info, revision, t, publicId, contentType) ||
         info.GetCompressedMD5() == "" ||
         info.GetUncompressedMD5() == "")
     {
@@ -2510,7 +2526,7 @@
     std::string data;
     std::string attachmentId;
 
-    context.ReadAttachment(data, revision, attachmentId, publicId, StringToContentType(name), false, true /* skipCache when you absolutely need the compressed data */);
+    context.ReadAttachment(data, revision, attachmentId, t, publicId, StringToContentType(name), false, true /* skipCache when you absolutely need the compressed data */);
 
     std::string actualMD5;
     Toolbox::ComputeMD5(actualMD5, data);
@@ -2525,7 +2541,7 @@
       }
       else
       {
-        context.ReadAttachment(data, revision, attachmentId, publicId, StringToContentType(name), true, true /* skipCache when you absolutely need the compressed data */);
+        context.ReadAttachment(data, revision, attachmentId, t, publicId, StringToContentType(name), true, true /* skipCache when you absolutely need the compressed data */);
         Toolbox::ComputeMD5(actualMD5, data);
         ok = (actualMD5 == info.GetUncompressedMD5());
       }
@@ -2695,9 +2711,10 @@
   template <enum CompressionType compression>
   static void ChangeAttachmentCompression(RestApiPostCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       call.GetDocumentation()
         .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
@@ -2714,16 +2731,17 @@
     std::string name = call.GetUriComponent("name", "");
     FileContentType contentType = StringToContentType(name);
 
-    OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression);
+    OrthancRestApi::GetContext(call).ChangeAttachmentCompression(t, publicId, contentType, compression);
     call.GetOutput().AnswerBuffer("{}", MimeType_Json);
   }
 
 
   static void IsAttachmentCompressed(RestApiGetCall& call)
   {
+    const ResourceType t = GetResourceTypeFromUri(call);
+
     if (call.IsDocumentation())
     {
-      ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
       std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
       AddAttachmentDocumentation(call, r);
       call.GetDocumentation()
@@ -2735,7 +2753,7 @@
     }
 
     FileInfo info;
-    if (GetAttachmentInfo(info, call))
+    if (GetAttachmentInfo(info, t, call))
     {
       std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1";
       call.GetOutput().AnswerBuffer(answer, MimeType_PlainText);
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -452,7 +452,7 @@
 
           FileInfo info;
           int64_t revision;  // Ignored
-          if (context_.GetIndex().LookupAttachment(info, revision, *it, FileContentType_Dicom))
+          if (context_.GetIndex().LookupAttachment(info, revision, ResourceType_Instance, *it, FileContentType_Dicom))
           {
             std::unique_ptr<File> resource(new File(*it + ".dcm"));
             resource->SetMimeType(MimeType_Dicom);
--- a/OrthancServer/Sources/ServerContext.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/ServerContext.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -962,12 +962,13 @@
 
   
   void ServerContext::AnswerAttachment(RestApiOutput& output,
+                                       ResourceType level,
                                        const std::string& resourceId,
                                        FileContentType content)
   {
     FileInfo attachment;
     int64_t revision;
-    if (!index_.LookupAttachment(attachment, revision, resourceId, content))
+    if (!index_.LookupAttachment(attachment, revision, level, resourceId, content))
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
@@ -979,7 +980,8 @@
   }
 
 
-  void ServerContext::ChangeAttachmentCompression(const std::string& resourceId,
+  void ServerContext::ChangeAttachmentCompression(ResourceType level,
+                                                  const std::string& resourceId,
                                                   FileContentType attachmentType,
                                                   CompressionType compression)
   {
@@ -990,7 +992,7 @@
 
     FileInfo attachment;
     int64_t revision;
-    if (!index_.LookupAttachment(attachment, revision, resourceId, attachmentType))
+    if (!index_.LookupAttachment(attachment, revision, level, resourceId, attachmentType))
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
@@ -1092,17 +1094,17 @@
     FileInfo attachment;
     int64_t revision;  // Ignored
 
-    if (index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_Dicom))
+    if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom))
     {
       attachments[FileContentType_Dicom] = attachment;
     }
 
-    if (index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_DicomUntilPixelData))
+    if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_DicomUntilPixelData))
     {
       attachments[FileContentType_DicomUntilPixelData] = attachment;
     }
 
-    if (index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_DicomAsJson))
+    if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_DicomAsJson))
     {
       attachments[FileContentType_DicomAsJson] = attachment;
     }
@@ -1295,7 +1297,7 @@
                                 const std::string& instancePublicId)
   {
     int64_t revision;
-    ReadAttachment(dicom, revision, attachmentId, instancePublicId, FileContentType_Dicom, true /* uncompress */);
+    ReadAttachment(dicom, revision, attachmentId, ResourceType_Instance, instancePublicId, FileContentType_Dicom, true /* uncompress */);
   }
 
 
@@ -1320,7 +1322,7 @@
   {
     FileInfo attachment;
     int64_t revision;  // Ignored
-    if (index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_DicomUntilPixelData))
+    if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_DicomUntilPixelData))
     {
       StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry());
 
@@ -1335,7 +1337,7 @@
       return false;
     }
     
-    if (!index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_Dicom))
+    if (!index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom))
     {
       throw OrthancException(ErrorCode_InternalError,
                              "Unable to read the DICOM file of instance " + instancePublicId);
@@ -1372,13 +1374,14 @@
   void ServerContext::ReadAttachment(std::string& result,
                                      int64_t& revision,
                                      std::string& attachmentId,
+                                     ResourceType level,
                                      const std::string& instancePublicId,
                                      FileContentType content,
                                      bool uncompressIfNeeded,
                                      bool skipCache)
   {
     FileInfo attachment;
-    if (!index_.LookupAttachment(attachment, revision, instancePublicId, content))
+    if (!index_.LookupAttachment(attachment, revision, level, instancePublicId, content))
     {
       throw OrthancException(ErrorCode_InternalError,
                              "Unable to read attachment " + EnumerationToString(content) +
--- a/OrthancServer/Sources/ServerContext.h	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/ServerContext.h	Tue Oct 29 15:03:32 2024 +0000
@@ -356,10 +356,12 @@
                                   bool isReconstruct = false);
 
     void AnswerAttachment(RestApiOutput& output,
+                          ResourceType level,
                           const std::string& resourceId,
                           FileContentType content);
 
-    void ChangeAttachmentCompression(const std::string& resourceId,
+    void ChangeAttachmentCompression(ResourceType level,
+                                     const std::string& resourceId,
                                      FileContentType attachmentType,
                                      CompressionType compression);
 
@@ -393,6 +395,7 @@
     void ReadAttachment(std::string& result,
                         int64_t& revision,
                         std::string& attachmentId,
+                        ResourceType level,
                         const std::string& instancePublicId,
                         FileContentType content,
                         bool uncompressIfNeeded,
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -541,7 +541,7 @@
       {
         FileInfo tmp;
         int64_t revision;  // ignored
-        if (index.LookupAttachment(tmp, revision, id, FileContentType_Dicom))
+        if (index.LookupAttachment(tmp, revision, ResourceType_Instance, id, FileContentType_Dicom))
         {
           instances_.push_back(Instance(id, tmp.GetUncompressedSize()));
         }
--- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Tue Oct 29 14:05:59 2024 +0000
+++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Tue Oct 29 15:03:32 2024 +0000
@@ -865,15 +865,15 @@
     {
       FileInfo nope;
       int64_t revision;
-      ASSERT_FALSE(context.GetIndex().LookupAttachment(nope, revision, id, FileContentType_DicomAsJson));
+      ASSERT_FALSE(context.GetIndex().LookupAttachment(nope, revision, ResourceType_Instance, id, FileContentType_DicomAsJson));
     }
 
     FileInfo dicom1, pixelData1;
     int64_t revision;
-    ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom1, revision, id, FileContentType_Dicom));
+    ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom1, revision, ResourceType_Instance, id, FileContentType_Dicom));
     ASSERT_EQ(0, revision);
     revision = -1;
-    ASSERT_TRUE(context.GetIndex().LookupAttachment(pixelData1, revision, id, FileContentType_DicomUntilPixelData));
+    ASSERT_TRUE(context.GetIndex().LookupAttachment(pixelData1, revision, ResourceType_Instance, id, FileContentType_DicomUntilPixelData));
     ASSERT_EQ(0, revision);
 
     context.GetIndex().GetGlobalStatistics(diskSize, uncompressedSize, countPatients, 
@@ -915,14 +915,14 @@
     {
       FileInfo nope;
       int64_t revision;
-      ASSERT_FALSE(context.GetIndex().LookupAttachment(nope, revision, id, FileContentType_DicomAsJson));
+      ASSERT_FALSE(context.GetIndex().LookupAttachment(nope, revision, ResourceType_Instance, id, FileContentType_DicomAsJson));
     }
 
     FileInfo dicom2, pixelData2;
-    ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom2, revision, id, FileContentType_Dicom));
+    ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom2, revision, ResourceType_Instance, id, FileContentType_Dicom));
     ASSERT_EQ(0, revision);
     revision = -1;
-    ASSERT_TRUE(context.GetIndex().LookupAttachment(pixelData2, revision, id, FileContentType_DicomUntilPixelData));
+    ASSERT_TRUE(context.GetIndex().LookupAttachment(pixelData2, revision, ResourceType_Instance, id, FileContentType_DicomUntilPixelData));
     ASSERT_EQ(0, revision);
 
     context.GetIndex().GetGlobalStatistics(diskSize, uncompressedSize, countPatients,