changeset 4506:ac69c9f76c71

refactoring ServerIndex::Store()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Feb 2021 17:01:44 +0100
parents 97d103b57cd1
children b4c58795f3a8
files OrthancServer/Sources/DicomInstanceToStore.cpp OrthancServer/Sources/DicomInstanceToStore.h OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h OrthancServer/UnitTestsSources/ServerIndexTests.cpp
diffstat 6 files changed, 46 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/DicomInstanceToStore.cpp	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/Sources/DicomInstanceToStore.cpp	Wed Feb 10 17:01:44 2021 +0100
@@ -351,9 +351,9 @@
   }
 
 
-  DicomInstanceToStore::MetadataMap& DicomInstanceToStore::GetMetadata()
+  void DicomInstanceToStore::ClearMetadata()
   {
-    return pimpl_->metadata_;
+    pimpl_->metadata_.clear();
   }
 
 
--- a/OrthancServer/Sources/DicomInstanceToStore.h	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/Sources/DicomInstanceToStore.h	Wed Feb 10 17:01:44 2021 +0100
@@ -64,12 +64,14 @@
     void SetBuffer(const void* dicom,
                    size_t size);
 
+    // WARNING: The "ParsedDicomFile" is not copied
     void SetParsedDicomFile(ParsedDicomFile& parsed);
 
     const MetadataMap& GetMetadata() const;
 
-    MetadataMap& GetMetadata();
+    void ClearMetadata();
 
+    // This function is notably used by modify/anonymize operations
     void AddMetadata(ResourceType level,
                      MetadataType metadata,
                      const std::string& value);
--- a/OrthancServer/Sources/ServerContext.cpp	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/Sources/ServerContext.cpp	Wed Feb 10 17:01:44 2021 +0100
@@ -516,6 +516,9 @@
     uint64_t pixelDataOffset;
     hasPixelDataOffset = DicomStreamReader::LookupPixelDataOffset(
       pixelDataOffset, dicom.GetBufferData(), dicom.GetBufferSize());
+
+    std::string transferSyntax;
+    bool hasTransferSyntax = dicom.LookupTransferSyntax(transferSyntax);
         
     DicomMap summary;
     OrthancConfiguration::DefaultExtractDicomSummary(summary, dicom.GetParsedDicomFile());
@@ -525,8 +528,7 @@
       MetricsRegistry::Timer timer(GetMetricsRegistry(), "orthanc_store_dicom_duration_ms");
       StorageAccessor accessor(area_, GetMetricsRegistry());
 
-      DicomInstanceHasher hasher(summary);
-      resultPublicId = hasher.HashInstance();
+      resultPublicId = dicom.GetParsedDicomFile().GetHasher().HashInstance();
 
       Json::Value dicomAsJson;
       OrthancConfiguration::DefaultDicomDatasetToJson(dicomAsJson, dicom.GetParsedDicomFile());
@@ -590,17 +592,17 @@
 
       typedef std::map<MetadataType, std::string>  InstanceMetadata;
       InstanceMetadata  instanceMetadata;
-      StoreStatus status = index_.Store(instanceMetadata, dicom, summary, hasher, attachments,
-                                        overwrite, hasPixelDataOffset, pixelDataOffset);
+      StoreStatus status = index_.Store(
+        instanceMetadata, summary, attachments, dicom.GetMetadata(), dicom.GetOrigin(), overwrite,
+        hasTransferSyntax, transferSyntax, hasPixelDataOffset, pixelDataOffset);
 
       // Only keep the metadata for the "instance" level
-      dicom.GetMetadata().clear();
+      dicom.ClearMetadata();
 
       for (InstanceMetadata::const_iterator it = instanceMetadata.begin();
            it != instanceMetadata.end(); ++it)
       {
-        dicom.GetMetadata().insert(std::make_pair(std::make_pair(ResourceType_Instance, it->first),
-                                                  it->second));
+        dicom.AddMetadata(ResourceType_Instance, it->first, it->second);
       }
             
       if (status != StoreStatus_Success)
--- a/OrthancServer/Sources/ServerIndex.cpp	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.cpp	Wed Feb 10 17:01:44 2021 +0100
@@ -45,7 +45,6 @@
 #include "../../OrthancFramework/Sources/Toolbox.h"
 
 #include "Database/ResourcesContent.h"
-#include "DicomInstanceToStore.h"
 #include "OrthancConfiguration.h"
 #include "Search/DatabaseLookup.h"
 #include "Search/DicomTagConstraint.h"
@@ -756,24 +755,25 @@
 
   
   StoreStatus ServerIndex::Store(std::map<MetadataType, std::string>& instanceMetadata,
-                                 DicomInstanceToStore& instanceToStore,
                                  const DicomMap& dicomSummary,
-                                 DicomInstanceHasher& hasher,
                                  const Attachments& attachments,
+                                 const MetadataMap& metadata,
+                                 const DicomInstanceOrigin& origin,
                                  bool overwrite,
+                                 bool hasTransferSyntax,
+                                 const std::string& transferSyntax,
                                  bool hasPixelDataOffset,
                                  uint64_t pixelDataOffset)
   {
     boost::mutex::scoped_lock lock(mutex_);
 
-    const ServerIndex::MetadataMap& metadata = instanceToStore.GetMetadata();
-
     int64_t expectedInstances;
     const bool hasExpectedInstances =
       ComputeExpectedNumberOfInstances(expectedInstances, dicomSummary);
     
     instanceMetadata.clear();
 
+    DicomInstanceHasher hasher(dicomSummary);
     const std::string hashPatient = hasher.HashPatient();
     const std::string hashStudy = hasher.HashStudy();
     const std::string hashSeries = hasher.HashSeries();
@@ -928,7 +928,7 @@
 
           // New in Orthanc 1.9.0
           content.AddMetadata(status.seriesId_, MetadataType_RemoteAet,
-                              instanceToStore.GetOrigin().GetRemoteAetC());
+                              origin.GetRemoteAetC());
         }
 
         
@@ -937,36 +937,36 @@
         SetInstanceMetadata(content, instanceMetadata, instanceId,
                             MetadataType_Instance_ReceptionDate, now);
         SetInstanceMetadata(content, instanceMetadata, instanceId, MetadataType_RemoteAet,
-                            instanceToStore.GetOrigin().GetRemoteAetC());
+                            origin.GetRemoteAetC());
         SetInstanceMetadata(content, instanceMetadata, instanceId, MetadataType_Instance_Origin, 
-                            EnumerationToString(instanceToStore.GetOrigin().GetRequestOrigin()));
-
+                            EnumerationToString(origin.GetRequestOrigin()));
+
+
+        if (hasTransferSyntax)
+        {
+          // New in Orthanc 1.2.0
+          SetInstanceMetadata(content, instanceMetadata, instanceId,
+                              MetadataType_Instance_TransferSyntax, transferSyntax);
+        }
 
         {
           std::string s;
 
-          if (instanceToStore.LookupTransferSyntax(s))
-          {
-            // New in Orthanc 1.2.0
-            SetInstanceMetadata(content, instanceMetadata, instanceId,
-                                MetadataType_Instance_TransferSyntax, s);
-          }
-
-          if (instanceToStore.GetOrigin().LookupRemoteIp(s))
+          if (origin.LookupRemoteIp(s))
           {
             // New in Orthanc 1.4.0
             SetInstanceMetadata(content, instanceMetadata, instanceId,
                                 MetadataType_Instance_RemoteIp, s);
           }
 
-          if (instanceToStore.GetOrigin().LookupCalledAet(s))
+          if (origin.LookupCalledAet(s))
           {
             // New in Orthanc 1.4.0
             SetInstanceMetadata(content, instanceMetadata, instanceId,
                                 MetadataType_Instance_CalledAet, s);
           }
 
-          if (instanceToStore.GetOrigin().LookupHttpUsername(s))
+          if (origin.LookupHttpUsername(s))
           {
             // New in Orthanc 1.4.0
             SetInstanceMetadata(content, instanceMetadata, instanceId,
--- a/OrthancServer/Sources/ServerIndex.h	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.h	Wed Feb 10 17:01:44 2021 +0100
@@ -37,6 +37,7 @@
 #include "../../OrthancFramework/Sources/DicomFormat/DicomMap.h"
 
 #include "Database/IDatabaseWrapper.h"
+#include "DicomInstanceOrigin.h"
 
 #include <boost/thread.hpp>
 #include <boost/noncopyable.hpp>
@@ -44,8 +45,6 @@
 namespace Orthanc
 {
   class DatabaseLookup;
-  class DicomInstanceHasher;
-  class DicomInstanceToStore;
   class ParsedDicomFile;
   class ServerContext;
 
@@ -140,11 +139,13 @@
     void SetMaximumPatientCount(unsigned int count);
 
     StoreStatus Store(std::map<MetadataType, std::string>& instanceMetadata,
-                      DicomInstanceToStore& instance,
                       const DicomMap& dicomSummary,
-                      DicomInstanceHasher& hasher,
                       const Attachments& attachments,
+                      const MetadataMap& metadata,
+                      const DicomInstanceOrigin& origin,
                       bool overwrite,
+                      bool hasTransferSyntax,
+                      const std::string& transferSyntax,
                       bool hasPixelDataOffset,
                       uint64_t pixelDataOffset);
 
--- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Wed Feb 10 12:07:03 2021 +0100
+++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Wed Feb 10 17:01:44 2021 +0100
@@ -727,17 +727,21 @@
 
     ParsedDicomFile dicom(instance, GetDefaultDicomEncoding(), false /* be strict */);
 
-    std::map<MetadataType, std::string> instanceMetadata;
     DicomInstanceToStore toStore;
     toStore.SetParsedDicomFile(dicom);
 
+    std::map<MetadataType, std::string> instanceMetadata;
+
     {
       DicomMap summary;
       OrthancConfiguration::DefaultExtractDicomSummary(summary, toStore.GetParsedDicomFile());
 
-      DicomInstanceHasher hasher(summary);      
-      ASSERT_EQ(StoreStatus_Success, index.Store(instanceMetadata, toStore, summary, hasher, attachments,
-                                                 false /* don't overwrite */, true /* pixel data offset */, 42));
+      std::string transferSyntax;
+      bool hasTransferSyntax = dicom.LookupTransferSyntax(transferSyntax);
+      ASSERT_EQ(StoreStatus_Success, index.Store(
+                  instanceMetadata, summary, attachments, toStore.GetMetadata(),
+                  toStore.GetOrigin(), false /* don't overwrite */,
+                  hasTransferSyntax, transferSyntax, true /* pixel data offset */, 42));
     }
     
     ASSERT_EQ(6u, instanceMetadata.size());