changeset 6862:51fcff053afe

refactoring ArchiveJob
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 26 May 2026 15:28:25 +0200
parents 8d72870bbfa1
children a8418d969c84
files OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.h
diffstat 2 files changed, 110 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 11:25:25 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 15:28:25 2026 +0200
@@ -39,6 +39,7 @@
 #include "../SimpleInstanceOrdering.h"
 #include "ThreadedInstancesLoader.h"
 
+#include <cassert>
 #include <stdio.h>
 #include <boost/range/algorithm/count.hpp>
 
@@ -467,124 +468,137 @@
   };
 
 
+  class ArchiveJob::IZipCommand : public boost::noncopyable
+  {
+  public:
+    virtual ~IZipCommand()
+    {
+    }
+
+    virtual void Preload(ThreadedInstancesLoader& 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,
+                         DicomDirWriter* dicomDir) const = 0;
+  };
+
 
   class ArchiveJob::ZipCommands : public boost::noncopyable
   {
   private:
-    enum Type
+    class OpenDirectoryCommand : public IZipCommand
     {
-      Type_OpenDirectory,
-      Type_CloseDirectory,
-      Type_WriteInstance
+    private:
+      std::string   filename_;
+
+    public:
+      OpenDirectoryCommand(const std::string& filename) :
+        filename_(filename)
+      {
+      }
+
+      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      {
+      }
+
+      virtual void Execute(HierarchicalZipWriter& writer,
+                           ThreadedInstancesLoader& instancesLoader,
+                           DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
+      {
+        writer.OpenDirectory(filename_);
+      }
     };
 
-    class Command : public boost::noncopyable
+
+    class CloseDirectoryCommand : public IZipCommand
+    {
+    public:
+      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      {
+      }
+
+      virtual void Execute(HierarchicalZipWriter& writer,
+                           ThreadedInstancesLoader& instancesLoader,
+                           DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
+      {
+        writer.CloseDirectory();
+      }
+    };
+
+
+    class WriteInstanceCommand : public IZipCommand
     {
     private:
-      Type          type_;
       std::string   filename_;
       std::string   instanceId_;
       FileInfo      fileInfo_;
+      std::string   dicomDirFolder_;
 
     public:
-      explicit Command(Type type) :
-        type_(type)
-      {
-        assert(type_ == Type_CloseDirectory);
-      }
-        
-      Command(Type type,
-              const std::string& filename) :
-        type_(type),
-        filename_(filename)
-      {
-        assert(type_ == Type_OpenDirectory);
-      }
-        
-      Command(Type type,
-              const std::string& filename,
-              const std::string& instanceId,
-              const FileInfo& fileInfo) :
-        type_(type),
+      WriteInstanceCommand(const std::string& filename,
+                           const std::string& instanceId,
+                           const FileInfo& fileInfo,
+                           const std::string& dicomDirFolder /* in practice, this is the constant "IMAGES" */) :
         filename_(filename),
         instanceId_(instanceId),
-        fileInfo_(fileInfo)
-      {
-        assert(type_ == Type_WriteInstance);
-      }
-        
-      void Apply(HierarchicalZipWriter& writer,
-                 ThreadedInstancesLoader& instancesLoader,
-                 DicomDirWriter* dicomDir,
-                 const std::string& dicomDirFolder,
-                 bool transcode,
-                 DicomTransferSyntax transferSyntax) const
+        fileInfo_(fileInfo),
+        dicomDirFolder_(dicomDirFolder)
       {
-        switch (type_)
-        {
-          case Type_OpenDirectory:
-            writer.OpenDirectory(filename_);
-            break;
+        assert(dicomDirFolder.empty() ||
+               dicomDirFolder == MEDIA_IMAGES_FOLDER);
+      }
 
-          case Type_CloseDirectory:
-            writer.CloseDirectory();
-            break;
+      virtual void Preload(ThreadedInstancesLoader& instancesLoader) const ORTHANC_OVERRIDE
+      {
+      }
 
-          case Type_WriteInstance:
-          {
-            std::string content;
+      virtual void Execute(HierarchicalZipWriter& writer,
+                           ThreadedInstancesLoader& instancesLoader,
+                           DicomDirWriter* dicomDir) const ORTHANC_OVERRIDE
+      {
+        std::string content;
 
-            try
-            {
-              LOG(INFO) << "Adding instance " << instanceId_ << " in zip";
-              instancesLoader.WaitDicomInstance(content, instanceId_);
-            }
-            catch (OrthancException& e)
-            {
-              LOG(WARNING) << "An instance was removed after the job was issued: " << instanceId_;
-              return;
-            }
-
-            writer.OpenFile(filename_);
+        try
+        {
+          LOG(INFO) << "Adding instance " << instanceId_ << " in zip";
+          instancesLoader.WaitDicomInstance(content, instanceId_);
+        }
+        catch (OrthancException& e)
+        {
+          LOG(WARNING) << "An instance was removed after the job was issued: " << instanceId_;
+          return;
+        }
 
-            writer.Write(content);
+        writer.OpenFile(filename_);
 
-            if (dicomDir != NULL)
-            {
-              std::unique_ptr<ParsedDicomFile> parsed(new ParsedDicomFile(content));
+        writer.Write(content);
 
-              dicomDir->Add(dicomDirFolder, filename_, *parsed);
-            }
-              
-            break;
-          }
-
-          default:
-            throw OrthancException(ErrorCode_InternalError);
+        if (dicomDir != NULL)
+        {
+          std::unique_ptr<ParsedDicomFile> parsed(new ParsedDicomFile(content));
+          dicomDir->Add(dicomDirFolder_, filename_, *parsed);
         }
       }
     };
       
-    std::deque<Command*>  commands_;
-    uint64_t              uncompressedSize_;
-    unsigned int          instancesCount_;
-    ThreadedInstancesLoader& instancesLoader_;
+    std::deque<IZipCommand*>  commands_;
+    uint64_t                  uncompressedSize_;
+    unsigned int              instancesCount_;
+    ThreadedInstancesLoader&  instancesLoader_;
 
       
     void ApplyInternal(HierarchicalZipWriter& writer,
-                       ThreadedInstancesLoader& instancesLoader,
                        size_t index,
-                       DicomDirWriter* dicomDir,
-                       const std::string& dicomDirFolder,
-                       bool transcode,
-                       DicomTransferSyntax transferSyntax) const
+                       DicomDirWriter* dicomDir) const
     {
       if (index >= commands_.size())
       {
         throw OrthancException(ErrorCode_ParameterOutOfRange);
       }
 
-      commands_[index]->Apply(writer, instancesLoader, dicomDir, dicomDirFolder, transcode, transferSyntax);
+      commands_[index]->Execute(writer, instancesLoader_, dicomDir);
     }
       
   public:
@@ -597,7 +611,7 @@
       
     ~ZipCommands()
     {
-      for (std::deque<Command*>::iterator it = commands_.begin();
+      for (std::deque<IZipCommand*>::iterator it = commands_.begin();
            it != commands_.end(); ++it)
       {
         assert(*it != NULL);
@@ -622,42 +636,36 @@
 
     // "media" flavor (with DICOMDIR)
     void Apply(HierarchicalZipWriter& writer,
-               ThreadedInstancesLoader& instancesLoader,
                size_t index,
-               DicomDirWriter& dicomDir,
-               const std::string& dicomDirFolder,
-               bool transcode,
-               DicomTransferSyntax transferSyntax) const
+               DicomDirWriter& dicomDir) const
     {
-      ApplyInternal(writer, instancesLoader, index, &dicomDir, dicomDirFolder, transcode, transferSyntax);
+      ApplyInternal(writer, index, &dicomDir);
     }
 
     // "archive" flavor (without DICOMDIR)
     void Apply(HierarchicalZipWriter& writer,
-               ThreadedInstancesLoader& instancesLoader,
-               size_t index,
-               bool transcode,
-               DicomTransferSyntax transferSyntax) const
+               size_t index) const
     {
-      ApplyInternal(writer, instancesLoader, index, NULL, "", transcode, transferSyntax);
+      ApplyInternal(writer, index, NULL);
     }
       
     void AddOpenDirectory(const std::string& filename)
     {
-      commands_.push_back(new Command(Type_OpenDirectory, filename));
+      commands_.push_back(new OpenDirectoryCommand(filename));
     }
 
     void AddCloseDirectory()
     {
-      commands_.push_back(new Command(Type_CloseDirectory));
+      commands_.push_back(new CloseDirectoryCommand);
     }
 
     void AddWriteInstance(const std::string& filename,
                           const std::string& instanceId,
-                          const FileInfo& fileInfo)
+                          const FileInfo& fileInfo,
+                          const std::string& dicomDirFolder)
     {
       instancesLoader_.PreloadDicomInstance(instanceId, fileInfo);
-      commands_.push_back(new Command(Type_WriteInstance, filename, instanceId, fileInfo));
+      commands_.push_back(new WriteInstanceCommand(filename, instanceId, fileInfo, dicomDirFolder));
       instancesCount_ ++;
       uncompressedSize_ += fileInfo.GetUncompressedSize();
     }
@@ -784,7 +792,7 @@
       char filename[24];
       snprintf(filename, sizeof(filename) - 1, instanceFormat_, index);
 
-      commands_.AddWriteInstance(filename, instanceId, fileInfo);
+      commands_.AddWriteInstance(filename, instanceId, fileInfo, "" /* no DICOMDIR */);
     }
   };
 
@@ -819,7 +827,7 @@
       // characters (some systems wrongly use 8.3, but this does not
       // conform to the standard)."
       std::string filename = "IM" + boost::lexical_cast<std::string>(counter_);
-      commands_.AddWriteInstance(filename, instanceId, fileInfo);
+      commands_.AddWriteInstance(filename, instanceId, fileInfo, MEDIA_IMAGES_FOLDER);
 
       counter_ ++;
     }
@@ -974,13 +982,12 @@
         if (isMedia_)
         {
           assert(dicomDir_.get() != NULL);
-          commands_.Apply(*zip_, instancesLoader_, index, *dicomDir_,
-                          MEDIA_IMAGES_FOLDER, transcode, transferSyntax);
+          commands_.Apply(*zip_, index, *dicomDir_);
         }
         else
         {
           assert(dicomDir_.get() == NULL);
-          commands_.Apply(*zip_, instancesLoader_, index, transcode, transferSyntax);
+          commands_.Apply(*zip_, index);
         }
       }
     }
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Tue May 26 11:25:25 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Tue May 26 15:28:25 2026 +0200
@@ -44,6 +44,7 @@
     class IArchiveVisitor;
     class MediaIndexVisitor;
     class ResourceIdentifiers;
+    class IZipCommand;
     class ZipCommands;
     class ZipWriterIterator;