changeset 6874:aa4e5201fb9f streaming

using DicomSequentialReader in StoreJob
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 28 May 2026 19:42:16 +0200
parents 42ca9122d003
children cfe3cd4a4974
files OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp OrthancFramework/Sources/DataSource/DicomSequentialReader.h OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h OrthancServer/Sources/ServerJobs/StoreJob.cpp OrthancServer/Sources/ServerJobs/StoreJob.h
diffstat 8 files changed, 90 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Thu May 28 19:42:16 2026 +0200
@@ -156,7 +156,7 @@
         case SourceType_StorageArea:
         {
           std::unique_ptr<StorageAreaDataSource::Range> range(new StorageAreaDataSource::Range(protection.release()));
-          return new Item(range->GetBuffer());
+          return new Item(range->GetBuffer());  // The buffer is shared through the cache
         }
 
         case SourceType_Dicom:
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Thu May 28 19:42:16 2026 +0200
@@ -55,6 +55,7 @@
 
       ParsedDicomFile& GetParsedDicomFile();
 
+      // There is a "const" here, as the value could be shared through the cache, so it cannot be modified
       const IMemoryBuffer& GetRawMemoryBuffer();
 
       void SetUserData(IDynamicObject* userData /* takes ownership */);
--- a/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp	Thu May 28 19:42:16 2026 +0200
@@ -48,19 +48,14 @@
     assert(IsStarted());
     OpenConnection();
 
-    if (instancesLoader_.get() == NULL)
-    {
-      StartLoaderThreads();
-    }
-
     LOG(INFO) << "Sending instance " << instance << " to modality \"" 
               << parameters_.GetRemoteModality().GetApplicationEntityTitle() << "\"";
 
-    std::string dicom;
+    std::unique_ptr<DicomSequentialReader::Item> item;
 
     try
     {
-      instancesLoader_->WaitDicomInstance(dicom, instance);
+      item.reset(WaitDicomInstance(instance));
     }
     catch (OrthancException& e)
     {
@@ -68,9 +63,10 @@
       return false;
     }
 
+    const IMemoryBuffer& dicom = item->GetRawMemoryBuffer();
+
     std::string sopClassUid, sopInstanceUid;
-    context_.PerformCStoreWithTranscoding(sopClassUid, sopInstanceUid, *connection_,
-                                          dicom.empty() ? NULL : dicom.c_str(), dicom.size(),
+    context_.PerformCStoreWithTranscoding(sopClassUid, sopInstanceUid, *connection_, dicom.GetData(), dicom.GetSize(),
                                           HasMoveOriginator(), moveOriginatorAet_, moveOriginatorId_);
 
     if (storageCommitment_)
--- a/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h	Thu May 28 19:42:16 2026 +0200
@@ -56,11 +56,6 @@
     
     virtual bool HandleTrailingStep() ORTHANC_OVERRIDE;
 
-    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
-    {
-      return "CSTO";
-    }
-
   public:
     explicit DicomModalityStoreJob(ServerContext& context);
 
--- a/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp	Thu May 28 19:42:16 2026 +0200
@@ -38,10 +38,7 @@
 {
   bool OrthancPeerStoreJob::HandleInstance(const std::string& instance)
   {
-    if (instancesLoader_.get() == NULL)
-    {
-      StartLoaderThreads();
-    }
+    assert(IsStarted());
 
     if (client_.get() == NULL)
     {
@@ -63,8 +60,10 @@
 
     try
     {
-      std::string dicom;
-      instancesLoader_->WaitDicomInstance(dicom, instance);
+      std::unique_ptr<DicomSequentialReader::Item> item;
+      item.reset(WaitDicomInstance(instance));
+
+      const IMemoryBuffer& dicom = item->GetRawMemoryBuffer();
 
       if (transcode_)
       {
@@ -72,7 +71,7 @@
         syntaxes.insert(transferSyntax_);
         
         IDicomTranscoder::DicomImage source, transcoded;
-        source.SetExternalBuffer(dicom);
+        source.SetExternalBuffer(dicom.GetData(), dicom.GetSize());
 
         if (context_.GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew))
         {
@@ -81,12 +80,12 @@
         }
         else
         {
-          body.swap(dicom);
+          dicom.CopyToString(body);
         }
       }
       else
       {
-        body.swap(dicom);
+        dicom.CopyToString(body);
       }
     }
     catch (OrthancException& e)
--- a/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h	Thu May 28 19:42:16 2026 +0200
@@ -49,11 +49,6 @@
     
     virtual bool HandleTrailingStep() ORTHANC_OVERRIDE;
 
-    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
-    {
-      return "PSTO";
-    }
-
   public:
     explicit OrthancPeerStoreJob(ServerContext& context) :
       StoreJob(context),
--- a/OrthancServer/Sources/ServerJobs/StoreJob.cpp	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/StoreJob.cpp	Thu May 28 19:42:16 2026 +0200
@@ -35,6 +35,24 @@
 
 namespace Orthanc
 {
+  class StoreJob::ReaderUserData : public IDynamicObject
+  {
+  private:
+    std::string instanceId_;
+
+  public:
+    ReaderUserData(const std::string& instanceId) :
+      instanceId_(instanceId)
+    {
+    }
+
+    const std::string& GetInstanceId() const
+    {
+      return instanceId_;
+    }
+  };
+
+
   StoreJob::StoreJob(ServerContext& context) :
     context_(context)
   {
@@ -63,6 +81,51 @@
   }
 
 
+  DicomSequentialReader::Item* StoreJob::WaitDicomInstance(const std::string& instanceId)
+  {
+    assert(IsStarted());
+
+    if (instancesLoader_.get() == NULL)
+    {
+      instancesLoader_.reset(context_.GetDicomSequentialReaderFactory().CreateForOriginalRawMemoryBuffer());
+
+      const size_t position = GetPosition(); // in case we reload a job in progress, we shall not preload the first instances
+
+      if (instanceId != instancesIds_[position])
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      for (size_t i = position; i < instancesIds_.size(); ++i)
+      {
+        instancesLoader_->Submit(filesInfo_[i], new ReaderUserData(instancesIds_[i]));
+      }
+
+      instancesLoader_->Start();
+    }
+
+    if (!instancesLoader_->HasNext())
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
+    std::unique_ptr<DicomSequentialReader::Item> next(instancesLoader_->Next());
+    assert(next.get() != NULL &&
+           next->HasUserData());
+
+    {
+      // This is just a sanity check, the class "MoveReaderUserData" could be removed
+      const ReaderUserData& userData = dynamic_cast<const ReaderUserData&>(next->GetUserData());
+      if (userData.GetInstanceId() != instanceId)
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+
+    return next.release();
+  }
+
+
   void StoreJob::AddInstances(const std::vector<std::string>& instancesIds,
                               const std::vector<FileInfo>& filesInfo)
   {
@@ -81,24 +144,6 @@
     }
   }
 
-
-  void StoreJob::StartLoaderThreads()
-  {
-    size_t loaderThreads = 1;
-    {
-      OrthancConfiguration::ReaderLock lock;
-      loaderThreads = lock.GetConfiguration().GetLoaderThreads();
-    }
-
-    instancesLoader_.reset(new ThreadedInstancesLoader(context_, loaderThreads, false, DicomTransferSyntax_LittleEndianImplicit /* dummy value not used*/, 0, GetLoaderPrefix()));
-
-    size_t position = GetPosition(); // in case we reload a job in progress, we shall not preload the first instances
-    for (size_t i = position; i < instancesIds_.size(); ++i)
-    {
-      instancesLoader_->PreloadDicomInstance(instancesIds_[i], filesInfo_[i]);
-    }
-  }
-
   
   void StoreJob::Stop(JobStopReason reason)
   {
--- a/OrthancServer/Sources/ServerJobs/StoreJob.h	Thu May 28 18:24:26 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/StoreJob.h	Thu May 28 19:42:16 2026 +0200
@@ -24,27 +24,30 @@
 #pragma once
 
 #include "../../../OrthancFramework/Sources/Compatibility.h"
-#include "../../../OrthancFramework/Sources/JobsEngine/SetOfInstancesJob.h"
+#include "../../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h"
 #include "../../../OrthancFramework/Sources/FileStorage/FileInfo.h"
-#include "ThreadedInstancesLoader.h"
+#include "../../../OrthancFramework/Sources/JobsEngine/SetOfInstancesJob.h"
 
 #include <vector>
 
 namespace Orthanc
 {
+  class DicomSequentialReader;
   class ServerContext;
 
   class StoreJob : public SetOfInstancesJob
   {
+  private:
+    class ReaderUserData;
+
+    std::unique_ptr<DicomSequentialReader>  instancesLoader_;
+    std::vector<std::string>                instancesIds_;
+    std::vector<FileInfo>                   filesInfo_;
+
   protected:
-    ServerContext&                             context_;
-    std::unique_ptr<ThreadedInstancesLoader>   instancesLoader_;
-    std::vector<std::string>                   instancesIds_;
-    std::vector<FileInfo>                      filesInfo_;
+    ServerContext&  context_;
 
-    virtual const char* GetLoaderPrefix() const = 0;
-
-    void StartLoaderThreads();
+    DicomSequentialReader::Item* WaitDicomInstance(const std::string& instanceId);
 
   public:
     explicit StoreJob(ServerContext& context);