changeset 6866:4bd9dd9e3b9c streaming

replaced ThreadedInstancesLoader by DicomSequentialReader in ArchiveJob
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 26 May 2026 16:22:09 +0200
parents 634a8082ce04
children ff73909042f2
files OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.h
diffstat 3 files changed, 54 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Tue May 26 15:52:35 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Tue May 26 16:22:09 2026 +0200
@@ -192,7 +192,7 @@
       IDicomTranscoder::DicomImage source;
       source.SetExternalBuffer(range->GetData(), range->GetSize());
 
-      std::unique_ptr<IDicomTranscoder::DicomImage> target;
+      std::unique_ptr<IDicomTranscoder::DicomImage> target(new IDicomTranscoder::DicomImage);
 
       std::set<DicomTransferSyntax> allowedSyntaxes;
       allowedSyntaxes.insert(targetSyntax_);
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 15:52:35 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 16:22:09 2026 +0200
@@ -27,6 +27,7 @@
 #include "../../../OrthancFramework/Sources/Cache/SharedArchive.h"
 #include "../../../OrthancFramework/Sources/Compression/HierarchicalZipWriter.h"
 #include "../../../OrthancFramework/Sources/Constants.h"
+#include "../../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h"
 #include "../../../OrthancFramework/Sources/DicomParsing/DicomDirWriter.h"
 #include "../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h"
 #include "../../../OrthancFramework/Sources/Logging.h"
@@ -37,7 +38,6 @@
 #include "../OrthancConfiguration.h"
 #include "../ServerContext.h"
 #include "../SimpleInstanceOrdering.h"
-#include "ThreadedInstancesLoader.h"
 
 #include <cassert>
 #include <stdio.h>
@@ -476,12 +476,12 @@
     {
     }
 
-    virtual void Preload(ThreadedInstancesLoader& instancesLoader) const = 0;
+    virtual void Preload(DicomSequentialReader& instancesLoader) const = 0;
 
     // In the "archive" flavor (without DICOMDIR), dicomDir is NULL
     // In the "media" flavor (with DICOMDIR), dicomDir is not NULL
     virtual void Execute(HierarchicalZipWriter& writer,
-                         ThreadedInstancesLoader& instancesLoader,
+                         DicomSequentialReader& instancesLoader,
                          DicomDirWriter* dicomDir) const = 0;
   };
 
@@ -500,12 +500,12 @@
       {
       }
 
-      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      virtual void Preload(DicomSequentialReader& instancesLoader) const ORTHANC_OVERRIDE
       {
       }
 
       virtual void Execute(HierarchicalZipWriter& writer,
-                           ThreadedInstancesLoader& instancesLoader,
+                           DicomSequentialReader& instancesLoader,
                            DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
       {
         writer.OpenDirectory(filename_);
@@ -516,12 +516,12 @@
     class CloseDirectoryCommand : public IZipCommand
     {
     public:
-      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      virtual void Preload(DicomSequentialReader& instancesLoader) const ORTHANC_OVERRIDE
       {
       }
 
       virtual void Execute(HierarchicalZipWriter& writer,
-                           ThreadedInstancesLoader& instancesLoader,
+                           DicomSequentialReader& instancesLoader,
                            DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
       {
         writer.CloseDirectory();
@@ -551,36 +551,45 @@
                dicomDirFolder == MEDIA_IMAGES_FOLDER);
       }
 
-      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      virtual void Preload(DicomSequentialReader& instancesLoader) const ORTHANC_OVERRIDE
       {
-        instancesLoader.PreloadDicomInstance(instanceId_, fileInfo_);
+        instancesLoader.Submit(fileInfo_);
       }
 
       virtual void Execute(HierarchicalZipWriter& writer,
-                           ThreadedInstancesLoader& instancesLoader,
+                           DicomSequentialReader& instancesLoader,
                            DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
       {
-        std::string content;
-
-        try
+        if (!instancesLoader.HasNext())
         {
-          LOG(INFO) << "Adding instance " << instanceId_ << " in zip";
-          instancesLoader.WaitDicomInstance(content, instanceId_);
+          throw OrthancException(ErrorCode_InternalError);
         }
-        catch (OrthancException& e)
+        else
         {
-          LOG(WARNING) << "An instance was removed after the job was issued: " << instanceId_;
-          return;
-        }
+          std::unique_ptr<ParsedDicomFile> next;
 
-        writer.OpenFile(filename_);
+          std::string content;
 
-        writer.Write(content);
+          try
+          {
+            LOG(INFO) << "Adding instance " << instanceId_ << " in zip";
+            next.reset(instancesLoader.Next());
+          }
+          catch (OrthancException& e)
+          {
+            LOG(WARNING) << "An instance was removed after the job was issued: " << instanceId_;
+            return;
+          }
 
-        if (dicomDir != NULL)
-        {
-          std::unique_ptr<ParsedDicomFile> parsed(new ParsedDicomFile(content));
-          dicomDir->Add(dicomDirFolder_, filename_, *parsed);
+          next->SaveToMemoryBuffer(content);
+
+          writer.OpenFile(filename_);
+          writer.Write(content);
+
+          if (dicomDir != NULL)
+          {
+            dicomDir->Add(dicomDirFolder_, filename_, *next);
+          }
         }
       }
     };
@@ -932,7 +941,7 @@
       return commands_.GetSize() + 1;
     }
 
-    void PreloadAllCommands(ThreadedInstancesLoader& instancesLoader)
+    void PreloadAllCommands(DicomSequentialReader& instancesLoader)
     {
       for (size_t i = 0; i < commands_.GetSize(); i++)
       {
@@ -940,7 +949,7 @@
       }
     }
 
-    void RunStep(ThreadedInstancesLoader& instancesLoader,
+    void RunStep(DicomSequentialReader& instancesLoader,
                  size_t index)
     {
       if (index > commands_.GetSize())
@@ -1194,8 +1203,19 @@
       instancesCount_ = writer_->GetInstancesCount();
       uncompressedSize_ = writer_->GetUncompressedSize();
 
-      instancesLoader_.reset(new ThreadedInstancesLoader(context_, loaderThreads_, transcode_, transferSyntax_, lossyQuality_, "ARCH"));
+      if (transcode_)
+      {
+        instancesLoader_.reset(context_.CreateTranscodedSequentialReader(
+                                 transferSyntax_, TranscodingSopInstanceUidMode_AllowNew, hasLossyQuality_, lossyQuality_));
+      }
+      else
+      {
+        instancesLoader_.reset(context_.CreateDicomSequentialReader());
+      }
+
       writer_->PreloadAllCommands(*instancesLoader_);
+
+      instancesLoader_->Start();
     }
   }
 
@@ -1235,10 +1255,7 @@
       writer_.reset();
     }
 
-    if (instancesLoader_.get() != NULL)
-    {
-      instancesLoader_->Clear(false);
-    }
+    instancesLoader_.reset(NULL);
 
     if (asynchronousTarget_.get() != NULL)
     {
@@ -1307,10 +1324,7 @@
       asynchronousTarget_.reset();
 
       // clear the loader threads
-      if (instancesLoader_.get() != NULL)
-      {
-        instancesLoader_->Clear(true);
-      }
+      instancesLoader_.reset(NULL);
     }
   }
 
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Tue May 26 15:52:35 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Tue May 26 16:22:09 2026 +0200
@@ -33,9 +33,9 @@
 
 namespace Orthanc
 {
+  class DicomSequentialReader;
   class ServerContext;
-  class ThreadedInstancesLoader;
-  
+
   class ArchiveJob : public IJob
   {
   private:
@@ -51,7 +51,7 @@
     std::unique_ptr<ZipWriter::IOutputStream>  synchronousTarget_;  // Only valid before "Start()"
     std::unique_ptr<TemporaryFile>        asynchronousTarget_;
     ServerContext&                        context_;
-    std::unique_ptr<ThreadedInstancesLoader>  instancesLoader_;
+    std::unique_ptr<DicomSequentialReader>  instancesLoader_;
     boost::shared_ptr<ArchiveIndex>       archive_;
     bool                                  isMedia_;
     bool                                  enableExtendedSopClass_;