changeset 6857:58740529b511 streaming

created DicomSequentialReader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 23 May 2026 22:07:39 +0200
parents f71f56755608
children a4f90fb6b3e2
files .hgignore OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/DataSource/DataSourceAnswer.cpp OrthancFramework/Sources/DataSource/DataSourceAnswer.h OrthancFramework/Sources/DataSource/DataSourceSequentialReader.cpp OrthancFramework/Sources/DataSource/DataSourceSequentialReader.h OrthancFramework/Sources/DataSource/DicomDataSource.cpp OrthancFramework/Sources/DataSource/DicomDataSource.h OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp OrthancFramework/Sources/DataSource/DicomSequentialReader.h OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp OrthancFramework/Sources/DataSource/TranscoderDataSource.h OrthancServer/Sources/OrthancGetRequestHandler.cpp OrthancServer/Sources/OrthancGetRequestHandler.h OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h
diffstat 16 files changed, 801 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sat May 23 11:56:03 2026 +0200
+++ b/.hgignore	Sat May 23 22:07:39 2026 +0200
@@ -2,8 +2,11 @@
 ThirdPartyDownloads/
 CMakeLists.txt.user
 *.cpp.orig
+*.cpp.rej
 *.h.orig
+*.h.rej
 *.patch.orig
+*.patch.rej
 .vs/
 .vscode/
 *~
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Sat May 23 22:07:39 2026 +0200
@@ -662,6 +662,7 @@
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceMemoryBudget.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceReader.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceRequest.cpp
+    ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceSequentialReader.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/StorageAreaDataSource.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileBuffer.cpp
     ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/FilesystemStorage.cpp
@@ -682,6 +683,7 @@
   if (ENABLE_DCMTK)
     list(APPEND ORTHANC_CORE_SOURCES_INTERNAL
       ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DicomDataSource.cpp
+      ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DicomSequentialReader.cpp
       ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/MultipleDicomReader.cpp
       ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/TranscoderDataSource.cpp
       ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomParsing/TranscodingCallable.cpp
--- a/OrthancFramework/Sources/DataSource/DataSourceAnswer.cpp	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceAnswer.cpp	Sat May 23 22:07:39 2026 +0200
@@ -75,10 +75,17 @@
 
   const IDataIdentifier& DataSourceAnswer::Item::GetId() const
   {
-    assert(id_.get() != NULL);
-    return *id_;
+    if (id_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      return *id_;
+    }
   }
 
+
   const boost::shared_ptr<IDynamicObject>& DataSourceAnswer::Item::GetValue() const
   {
     if (error_)
@@ -96,6 +103,19 @@
   }
 
 
+  IDataIdentifier* DataSourceAnswer::Item::ReleaseIdentifier()
+  {
+    if (id_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      return id_.release();
+    }
+  }
+
+
   void DataSourceAnswer::EnqueueInternal(Item* item)
   {
     std::unique_ptr<Item> protection(item);
--- a/OrthancFramework/Sources/DataSource/DataSourceAnswer.h	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceAnswer.h	Sat May 23 22:07:39 2026 +0200
@@ -75,6 +75,8 @@
       const IDataIdentifier& GetId() const;
 
       const boost::shared_ptr<IDynamicObject>& GetValue() const;
+
+      IDataIdentifier* ReleaseIdentifier();
     };
 
   private:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/DataSource/DataSourceSequentialReader.cpp	Sat May 23 22:07:39 2026 +0200
@@ -0,0 +1,291 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../PrecompiledHeaders.h"
+#include "DataSourceSequentialReader.h"
+
+#include "DataSourceReader.h"
+#include "../OrthancException.h"
+
+namespace Orthanc
+{
+  DataSourceSequentialReader::Item::Item(IDataIdentifier* id,
+                                         IDynamicObject* value,
+                                         size_t estimatedSize) :
+    id_(id),
+    value_(value),
+    estimatedSize_(estimatedSize)
+  {
+    if (id == NULL ||
+        value == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  DataSourceSequentialReader::Item::Item(IDataIdentifier* id,
+                                         const OrthancException& error) :
+    id_(id),
+    error_(new OrthancException(error)),
+    estimatedSize_(0)
+  {
+    if (id == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  const IDataIdentifier& DataSourceSequentialReader::Item::GetIdentifier() const
+  {
+    assert(id_.get() != NULL);
+    return *id_;
+  }
+
+
+  const IDynamicObject& DataSourceSequentialReader::Item::GetValue() const
+  {
+    if (value_.get() != NULL)
+    {
+      assert(error_.get() == NULL);
+      return *value_;
+    }
+    else if (error_.get() != NULL)
+    {
+      throw *error_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+  }
+
+
+  IDynamicObject* DataSourceSequentialReader::Item::ReleaseValue()
+  {
+    if (value_.get() != NULL)
+    {
+      assert(error_.get() == NULL);
+      return value_.release();
+    }
+    else if (error_.get() != NULL)
+    {
+      throw *error_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+  }
+
+
+  class DataSourceSequentialReader::Callable : public ICallable
+  {
+  private:
+    boost::shared_ptr<DataSourceReader>    reader_;
+    boost::shared_ptr<IValueDisconnector>  disconnector_;
+    std::unique_ptr<IDataIdentifier>       request_;
+    size_t                                 estimatedSize_;
+
+  public:
+    Callable(const boost::shared_ptr<DataSourceReader>& reader,
+             const boost::shared_ptr<IValueDisconnector>& disconnector,
+             IDataIdentifier* request,
+             size_t estimatedSize) :
+      reader_(reader),
+      disconnector_(disconnector),
+      request_(request),
+      estimatedSize_(estimatedSize)
+    {
+      if (reader.get() == NULL ||
+          disconnector.get() == NULL ||
+          request_ == NULL)
+      {
+        throw OrthancException(ErrorCode_NullPointer);
+      }
+    }
+
+    virtual IDynamicObject* Call() ORTHANC_OVERRIDE
+    {
+      std::unique_ptr<DataSourceAnswer::Item> answer(reader_->ReadSingle(request_.release()));
+
+      try
+      {
+        return new Item(answer->ReleaseIdentifier(), disconnector_->Apply(answer.release()), estimatedSize_);
+      }
+      catch (OrthancException& e)
+      {
+        return new Item(answer->ReleaseIdentifier(), e);
+      }
+      catch (...)
+      {
+        return new Item(answer->ReleaseIdentifier(), OrthancException(ErrorCode_InternalError));
+      }
+    }
+  };
+
+
+  void DataSourceSequentialReader::FillWindow()
+  {
+    while (!pendingRequests_.empty() &&
+           runningRequests_.size() < windowSize_)
+    {
+      size_t estimatedSize;
+      if (pendingRequests_.front()->EstimateValueSize(estimatedSize))
+      {
+        // Ensure that at least 1 callable is running even if too large for capacity
+        if (!runningRequests_.empty() &&
+            windowCapacity_ != 0 &&
+            runningSize_ + estimatedSize > windowCapacity_)
+        {
+          return;
+        }
+      }
+      else
+      {
+        estimatedSize = 0;
+      }
+
+      std::unique_ptr<IDataIdentifier> front(pendingRequests_.front());
+      pendingRequests_.pop_front();
+
+      runningSize_ += estimatedSize;
+
+      std::unique_ptr<ICallable> callable(new Callable(reader_, disconnector_, front.release(), estimatedSize));
+      runningRequests_.push_back(executor_->Submit(callable.release()));
+    }
+  }
+
+
+  DataSourceSequentialReader::DataSourceSequentialReader(const boost::shared_ptr<IExecutorService>& executor,
+                                                         const boost::shared_ptr<DataSourceReader>& reader,
+                                                         IValueDisconnector* disconnector /* takes ownership */,
+                                                         unsigned int windowSize,
+                                                         uint64_t windowCapacity) :
+    executor_(executor),
+    reader_(reader),
+    disconnector_(disconnector),
+    windowSize_(windowSize),
+    windowCapacity_(windowCapacity),
+    started_(false),
+    runningSize_(0)
+  {
+    if (executor.get() == NULL ||
+        reader.get() == NULL ||
+        disconnector == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    if (windowSize == 0)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }
+
+
+  DataSourceSequentialReader::~DataSourceSequentialReader()
+  {
+    for (PendingRequests::iterator it = pendingRequests_.begin(); it != pendingRequests_.end(); ++it)
+    {
+      assert(*it != NULL);
+      delete *it;
+    }
+
+    for (RunningRequests::iterator it = runningRequests_.begin(); it != runningRequests_.end(); ++it)
+    {
+      assert(*it != NULL);
+      delete *it;
+    }
+  }
+
+
+  void DataSourceSequentialReader::Submit(IDataIdentifier* request /* takes ownership */)
+  {
+    std::unique_ptr<IDataIdentifier> protection(request);
+
+    if (request == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+    else if (started_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      pendingRequests_.push_back(protection.release());
+    }
+  }
+
+
+  void DataSourceSequentialReader::Start()
+  {
+    if (started_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      started_ = true;
+      FillWindow();
+    }
+  }
+
+
+  bool DataSourceSequentialReader::HasNext() const
+  {
+    if (!started_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      return !runningRequests_.empty();
+    }
+  }
+
+
+  DataSourceSequentialReader::Item* DataSourceSequentialReader::Next()
+  {
+    if (!started_ ||
+        !HasNext())
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      std::unique_ptr<Future> future(runningRequests_.front());
+      runningRequests_.pop_front();
+
+      std::unique_ptr<Item> item(dynamic_cast<Item*>(future->ReleaseResult()));
+      runningSize_ -= item->GetEstimatedSize();
+
+      FillWindow();
+
+      return item.release();
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/DataSource/DataSourceSequentialReader.h	Sat May 23 22:07:39 2026 +0200
@@ -0,0 +1,135 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../MultiThreading/IExecutorService.h"
+#include "DataSourceAnswer.h"
+
+
+namespace Orthanc
+{
+  class DataSourceReader;
+  class OrthancException;
+
+  /**
+   * This class retrieves items from a data source in a deterministic
+   * order while still using parallel execution through a thread pool.
+   *
+   * Internally, it uses a sliding-window approach. Since items may
+   * complete out of order, the class buffers them until earlier items
+   * become available. This additional buffering is necessary because
+   * applying backpressure (as done in "DataSourceReader::Submit()")
+   * could otherwise lead to deadlock. If item ordering is not
+   * required, prefer "DataSourceReader::Submit()", which supports
+   * backpressure and avoids the extra memory overhead.
+   *
+   * Note that this class is not thread safe, it should be invoked
+   * from a single thread.
+   **/
+  class DataSourceSequentialReader : public boost::noncopyable
+  {
+  public:
+    class Item : public IDynamicObject
+    {
+    private:
+      std::unique_ptr<IDataIdentifier>   id_;
+      std::unique_ptr<IDynamicObject>    value_;
+      std::unique_ptr<OrthancException>  error_;
+      size_t                             estimatedSize_;
+
+    public:
+      Item(IDataIdentifier* id,
+           IDynamicObject* value,
+           size_t estimatedSize);
+
+      Item(IDataIdentifier* id,
+           const OrthancException& error);
+
+      const IDataIdentifier& GetIdentifier() const;
+
+      const IDynamicObject& GetValue() const;
+
+      size_t GetEstimatedSize() const
+      {
+        return estimatedSize_;
+      }
+
+      IDynamicObject* ReleaseValue();
+    };
+
+
+    /**
+     * Extracts the value from an answer item so that it can be
+     * buffered independently and is no longer subject to
+     * backpressure.
+     **/
+    class IValueDisconnector : public boost::noncopyable
+    {
+    public:
+      virtual ~IValueDisconnector()
+      {
+      }
+
+      virtual IDynamicObject* Apply(DataSourceAnswer::Item* source) = 0;
+    };
+
+
+  private:
+    class Callable;
+
+    typedef std::list<IDataIdentifier*>  PendingRequests;
+    typedef std::list<Future*>           RunningRequests;
+
+    boost::shared_ptr<IExecutorService>    executor_;
+    boost::shared_ptr<DataSourceReader>    reader_;
+    boost::shared_ptr<IValueDisconnector>  disconnector_;
+    unsigned int                           windowSize_;
+    uint64_t                               windowCapacity_;
+
+    bool             started_;
+    PendingRequests  pendingRequests_;
+    RunningRequests  runningRequests_;
+    uint64_t         runningSize_;
+
+    void FillWindow();
+
+  public:
+    DataSourceSequentialReader(const boost::shared_ptr<IExecutorService>& executor,
+                               const boost::shared_ptr<DataSourceReader>& reader,
+                               IValueDisconnector* disconnector /* takes ownership */,
+                               unsigned int windowSize /* number of elements in the sliding window */,
+                               uint64_t windowCapacity /* if 0, memory usage is only controlled by the size of the sliding window */);
+
+    ~DataSourceSequentialReader();
+
+    void Submit(IDataIdentifier* request /* takes ownership */);
+
+    void Start();
+
+    bool HasNext() const;
+
+    Item* Next();
+  };
+}
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Sat May 23 22:07:39 2026 +0200
@@ -115,8 +115,9 @@
   class DicomDataSource::Value : public IDynamicObject
   {
   private:
-    std::unique_ptr<ParsedDicomFile>   dicom_;
-    size_t                             size_;
+    Mutex                             mutex_;
+    std::unique_ptr<ParsedDicomFile>  dicom_;
+    size_t                            size_;
 
   public:
     Value(ParsedDicomFile* dicom,
@@ -130,15 +131,20 @@
       }
     }
 
+    size_t GetSize() const
+    {
+      return size_;
+    }
+
+    Mutex::ScopedLock* Lock()
+    {
+      return new Mutex::ScopedLock(mutex_);
+    }
+
     ParsedDicomFile& GetContent() const
     {
       return *dicom_;
     }
-
-    size_t GetSize() const
-    {
-      return size_;
-    }
   };
 
 
@@ -201,9 +207,18 @@
   }
 
 
+  DicomDataSource::Dicom::Lock::Lock(Dicom& that):
+    that_(that)
+  {
+    Value& value = dynamic_cast<Value&>(*that_.item_->GetValue());
+    lock_.reset(value.Lock());
+  }
+
+
   ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const
   {
-    return dynamic_cast<const Value&>(*that_.item_->GetValue()).GetContent();
+    const Value& value = dynamic_cast<const Value&>(*that_.item_->GetValue());
+    return value.GetContent();
   }
 
 
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.h	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h	Sat May 23 22:07:39 2026 +0200
@@ -67,7 +67,6 @@
     class Dicom : public boost::noncopyable
     {
     private:
-      Mutex                                    mutex_;
       std::unique_ptr<DataSourceAnswer::Item>  item_;   // Holding item puts backpressure on the data source
 
     public:
@@ -83,15 +82,11 @@
       class Lock : public boost::noncopyable
       {
       private:
-        Dicom&             that_;
-        Mutex::ScopedLock  lock_;
+        Dicom&                              that_;
+        std::unique_ptr<Mutex::ScopedLock>  lock_;
 
       public:
-        explicit Lock(Dicom& that) :
-          that_(that),
-          lock_(that.mutex_)
-        {
-        }
+        explicit Lock(Dicom& that);
 
         ParsedDicomFile& GetContent() const;
       };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Sat May 23 22:07:39 2026 +0200
@@ -0,0 +1,168 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../PrecompiledHeaders.h"
+#include "DicomSequentialReader.h"
+
+#include "../DicomParsing/ParsedDicomFile.h"
+#include "../OrthancException.h"
+#include "BaseDataIdentifier.h"
+#include "DicomDataSource.h"
+#include "TranscoderDataSource.h"
+
+
+namespace Orthanc
+{
+  class DicomSequentialReader::Disconnector : public DataSourceSequentialReader::IValueDisconnector
+  {
+  private:
+    Type  type_;
+
+  public:
+    Disconnector(Type type) :
+      type_(type)
+    {
+    }
+
+    virtual IDynamicObject* Apply(DataSourceAnswer::Item* source)
+    {
+      switch (type_)
+      {
+        case Type_Original:
+        {
+          std::unique_ptr<DicomDataSource::Dicom> dicom(new DicomDataSource::Dicom(source));
+          return dicom->Clone();
+        }
+
+        case Type_Transcoded:
+        {
+          std::unique_ptr<TranscoderDataSource::Transcoded> dicom(new TranscoderDataSource::Transcoded(source));
+          TranscoderDataSource::Transcoded::LockAsParsed lock(*dicom);
+          return lock.GetContent().Clone(true);
+        }
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+  };
+
+
+  DicomSequentialReader::DicomSequentialReader(Type type,
+                                               const boost::shared_ptr<IExecutorService>& executor,
+                                               DicomTransferSyntax transferSyntax,
+                                               const boost::shared_ptr<DataSourceReader>& reader,
+                                               unsigned int windowSize,
+                                               uint64_t windowCapacity) :
+    type_(type),
+    transferSyntax_(transferSyntax),
+    reader_(executor, reader, new Disconnector(type), windowSize, windowCapacity)
+  {
+  }
+
+
+  void DicomSequentialReader::SubmitInternal(const FileInfo& attachment,
+                                             IDynamicObject* userData)
+  {
+    std::unique_ptr<IDynamicObject> protection(userData);
+
+    std::unique_ptr<IDataIdentifier> id;
+
+    switch (type_)
+    {
+      case Type_Original:
+        id.reset(DicomDataSource::CreateWholeRequest(attachment));
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+
+    if (protection.get() != NULL)
+    {
+      dynamic_cast<BaseDataIdentifier&>(*id).SetUserData(protection.release());
+    }
+
+    reader_.Submit(id.release());
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor,
+                                                                  const boost::shared_ptr<DataSourceReader>& dicomReader,
+                                                                  unsigned int windowSize,
+                                                                  uint64_t windowCapacity)
+  {
+    return new DicomSequentialReader(
+      Type_Original, executor, DicomTransferSyntax_LittleEndianImplicit /* dummy value */,
+      dicomReader, windowSize, windowCapacity);
+  }
+
+
+  DicomSequentialReader* DicomSequentialReader::CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor,
+                                                                    DicomTransferSyntax transferSyntax,
+                                                                    const boost::shared_ptr<DataSourceReader>& transcoderReader,
+                                                                    unsigned int windowSize,
+                                                                    uint64_t windowCapacity)
+  {
+    return new DicomSequentialReader(
+      Type_Transcoded, executor, transferSyntax, transcoderReader, windowSize, windowCapacity);
+  }
+
+
+  void DicomSequentialReader::Submit(const FileInfo& attachment)
+  {
+    SubmitInternal(attachment, NULL);
+  }
+
+
+  void DicomSequentialReader::Submit(const FileInfo& attachment,
+                                     IDynamicObject* userData)
+  {
+    if (userData == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    SubmitInternal(attachment, userData);
+  }
+
+
+  void DicomSequentialReader::Start()
+  {
+    reader_.Start();
+  }
+
+
+  bool DicomSequentialReader::HasNext()
+  {
+    return reader_.HasNext();
+  }
+
+
+  ParsedDicomFile* DicomSequentialReader::Next()
+  {
+    std::unique_ptr<DataSourceSequentialReader::Item> item(reader_.Next());
+    return dynamic_cast<ParsedDicomFile*>(item->ReleaseValue());
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Sat May 23 22:07:39 2026 +0200
@@ -0,0 +1,85 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../Enumerations.h"
+#include "../FileStorage/FileInfo.h"
+#include "../MultiThreading/IExecutorService.h"
+#include "DataSourceSequentialReader.h"
+
+
+namespace Orthanc
+{
+  class ParsedDicomFile;
+
+  class ORTHANC_PUBLIC DicomSequentialReader : public boost::noncopyable
+  {
+  private:
+    class Disconnector;
+
+    enum Type
+    {
+      Type_Original,
+      Type_Transcoded
+    };
+
+    Type                         type_;
+    DicomTransferSyntax          transferSyntax_;
+    DataSourceSequentialReader   reader_;
+
+    DicomSequentialReader(Type type,
+                          const boost::shared_ptr<IExecutorService>& executor,
+                          DicomTransferSyntax transferSyntax,
+                          const boost::shared_ptr<DataSourceReader>& reader,
+                          unsigned int windowSize,
+                          uint64_t windowCapacity);
+
+    void SubmitInternal(const FileInfo& attachment,
+                        IDynamicObject* userData);
+
+  public:
+    static DicomSequentialReader* CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor,
+                                                    const boost::shared_ptr<DataSourceReader>& dicomReader,
+                                                    unsigned int windowSize,
+                                                    uint64_t windowCapacity);
+
+    static DicomSequentialReader* CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor,
+                                                      DicomTransferSyntax transferSyntax,
+                                                      const boost::shared_ptr<DataSourceReader>& transcoderReader,
+                                                      unsigned int windowSize,
+                                                      uint64_t windowCapacity);
+
+    void Submit(const FileInfo& attachment);
+
+    void Submit(const FileInfo& attachment,
+                IDynamicObject* userData /* takes ownership */);
+
+    void Start();
+
+    bool HasNext();
+
+    ParsedDicomFile* Next();
+  };
+}
--- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Sat May 23 22:07:39 2026 +0200
@@ -54,6 +54,7 @@
   class TranscoderDataSource::Value : public IDynamicObject
   {
   private:
+    Mutex                             parsedMutex_;
     std::unique_ptr<ParsedDicomFile>  parsed_;
     std::unique_ptr<std::string>      raw_;
     size_t                            size_;
@@ -103,6 +104,11 @@
       return raw_.get() != NULL;
     }
 
+    Mutex::ScopedLock* LockParsed()
+    {
+      return new Mutex::ScopedLock(parsedMutex_);
+    }
+
     ParsedDicomFile& GetParsed() const
     {
       if (parsed_.get() == NULL)
@@ -253,21 +259,19 @@
   }
 
 
-  TranscoderDataSource::Transcoded::LockAsParsed::LockAsParsed(Transcoded& that) :
-    lock_(that.mutex_)
+  TranscoderDataSource::Transcoded::LockAsParsed::LockAsParsed(Transcoded& that)
   {
-    const Value& value = dynamic_cast<const Value&>(*that.item_->GetValue());
+    Value& value = dynamic_cast<Value&>(*that.item_->GetValue());
 
     if (value.HasParsed())
     {
-      // Don't release the lock here
+      lock_.reset(value.LockParsed());
       content_ = &value.GetParsed();
     }
     else
     {
       assert(value.HasRawBuffer());
       parsed_.reset(new ParsedDicomFile(value.GetRawBuffer()));
-      lock_.Unlock();
 
       content_ = parsed_.get();
     }
@@ -278,12 +282,11 @@
 
   TranscoderDataSource::Transcoded::LockAsBuffer::LockAsBuffer(Transcoded& that)
   {
-    Mutex::ScopedLock lock(that.mutex_);
-
-    const Value& value = dynamic_cast<const Value&>(*that.item_->GetValue());
+    Value& value = dynamic_cast<Value&>(*that.item_->GetValue());
 
     if (value.HasParsed())
     {
+      std::unique_ptr<Mutex::ScopedLock> lock(value.LockParsed());
       serialized_.reset(new std::string);
       value.GetParsed().SaveToMemoryBuffer(*serialized_);
       content_ = serialized_.get();
--- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.h	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.h	Sat May 23 22:07:39 2026 +0200
@@ -56,7 +56,6 @@
     class Transcoded : public boost::noncopyable
     {
     private:
-      Mutex                                    mutex_;
       std::unique_ptr<DataSourceAnswer::Item>  item_;   // Holding item puts backpressure on the data source
 
     public:
@@ -71,9 +70,9 @@
          * is enabled in the DataSourceReader.
          **/
 
-        Mutex::ScopedLock                 lock_;
-        std::unique_ptr<ParsedDicomFile>  parsed_;
-        const ParsedDicomFile*            content_;
+        std::unique_ptr<Mutex::ScopedLock>  lock_;
+        std::unique_ptr<ParsedDicomFile>    parsed_;
+        const ParsedDicomFile*              content_;
 
       public:
         explicit LockAsParsed(Transcoded& that);
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Sat May 23 22:07:39 2026 +0200
@@ -65,29 +65,28 @@
       throw OrthancException(ErrorCode_ParameterOutOfRange);
     }
     
-    if (instancesLoader_.get() == NULL)
+    if (instancesLoader_.get() == NULL ||
+        !instancesLoader_->HasNext())
     {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      throw OrthancException(ErrorCode_InternalError);
     }
 
     {
-      std::unique_ptr<DicomDataSource::Dicom> item(instancesLoader_->Dequeue());
+      std::unique_ptr<ParsedDicomFile> dicom(instancesLoader_->Next());
 
-      if (item == NULL)
+      if (dicom.get() == NULL)
       {
         throw OrthancException(ErrorCode_InternalError);
       }
     
-      DicomDataSource::Dicom::Lock lock(*item);
-
-      if (lock.GetContent().GetDcmtkObject().getDataset() == NULL)
+      if (dicom->GetDcmtkObject().getDataset() == NULL)
       {
         throw OrthancException(ErrorCode_InternalError);
       }
     
       OFString a, b;
-      if (!lock.GetContent().GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPClassUID, a).good() ||
-          !lock.GetContent().GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPInstanceUID, b).good())
+      if (!dicom->GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPClassUID, a).good() ||
+          !dicom->GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPInstanceUID, b).good())
       {
         throw OrthancException(ErrorCode_NoSopClassOrInstance,
                                "Unable to determine the SOP class/instance for C-STORE with AET " +
@@ -97,7 +96,7 @@
       std::string sopClassUid(a.c_str());
       std::string sopInstanceUid(b.c_str());
 
-      return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, lock.GetContent().GetDcmtkObject());
+      return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, dicom->GetDcmtkObject());
     }
   }
 
@@ -567,8 +566,6 @@
       }
     }
 
-    instancesLoader_.reset(context_.CreateMultipleDicomReader());
-
     std::vector<FileInfo> filesInfo;
 
     for (std::list<std::string>::const_iterator
@@ -580,13 +577,16 @@
       context_.GetOrderedChildInstances(instancesIds_, filesInfo, *resourceId, level);
     }
 
-    // TODO-Streaming : Should instances be ordered?
+
+    instancesLoader_.reset(context_.CreateDicomSequentialReader());
 
     for (size_t i = 0; i < instancesIds_.size(); ++i)
     {
-      instancesLoader_->Enqueue(filesInfo[i]);
+      instancesLoader_->Submit(filesInfo[i]);
     }
 
+    instancesLoader_->Start();
+
     failedUIDs_.clear();
 
     completedCount_ = 0;
--- a/OrthancServer/Sources/OrthancGetRequestHandler.h	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancServer/Sources/OrthancGetRequestHandler.h	Sat May 23 22:07:39 2026 +0200
@@ -23,7 +23,7 @@
 #pragma once
 
 #include "../../OrthancFramework/Sources/Compatibility.h"  // For ORTHANC_OVERRIDE
-#include "../../OrthancFramework/Sources/DataSource/MultipleDicomReader.h"
+#include "../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h"
 #include "../../OrthancFramework/Sources/DicomNetworking/IGetRequestHandler.h"
 #include "../../OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h"
 
@@ -45,7 +45,7 @@
     std::vector<std::string> instancesIds_;
     size_t position_;
     std::string originatorAet_;
-    std::unique_ptr<MultipleDicomReader> instancesLoader_;
+    std::unique_ptr<DicomSequentialReader> instancesLoader_;
 
     unsigned int completedCount_;
     unsigned int warningCount_;
--- a/OrthancServer/Sources/ServerContext.cpp	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Sat May 23 22:07:39 2026 +0200
@@ -27,6 +27,7 @@
 #include "../../OrthancFramework/Sources/Cache/SharedArchive.h"
 #include "../../OrthancFramework/Sources/Constants.h"
 #include "../../OrthancFramework/Sources/DataSource/DataSourceReader.h"
+#include "../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h"
 #include "../../OrthancFramework/Sources/DataSource/TranscoderDataSource.h"
 #include "../../OrthancFramework/Sources/DicomFormat/DicomElement.h"
 #include "../../OrthancFramework/Sources/DicomFormat/DicomStreamReader.h"
@@ -612,6 +613,16 @@
         //transcoderReader_->SetCapacity(1 * GIGABYTE);  // 1 GB - TODO-Streaming - Parameter
         transcoderReader_->CreateCache(256 * MEGABYTE); // 256 MB - TODO-Streaming - Parameter
       }
+
+      {
+        std::unique_ptr<ThreadPool> pool(new ThreadPool);
+        pool->SetCountThreads(16);  // TODO-Streaming - Parameter
+        pool->SetLoggingThreadName("READER");
+        pool->SetDequeueTimeout(100);
+        pool->Start();
+
+        sequentialReaderThreadPool_.reset(pool.release());
+      }
     }
     catch (OrthancException&)
     {
@@ -2146,4 +2157,22 @@
   {
     return new MultipleDicomReader(dicomReader_);
   }
+
+
+  DicomSequentialReader* ServerContext::CreateDicomSequentialReader()
+  {
+    return DicomSequentialReader::CreateForOriginal(
+      sequentialReaderThreadPool_, dicomReader_,
+      4 /* TODO-Streaming: Parameter */,
+      0  /* TODO-Streaming: Parameter */);
+  }
+
+
+  DicomSequentialReader* ServerContext::CreateDicomSequentialReader(DicomTransferSyntax transcodingSyntax)
+  {
+    return DicomSequentialReader::CreateForTranscoded(
+      sequentialReaderThreadPool_, transcodingSyntax, transcoderReader_,
+      4 /* TODO-Streaming: Parameter */,
+      0  /* TODO-Streaming: Parameter */);
+  }
 }
--- a/OrthancServer/Sources/ServerContext.h	Sat May 23 11:56:03 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.h	Sat May 23 22:07:39 2026 +0200
@@ -42,7 +42,9 @@
 namespace Orthanc
 {
   class DicomElement;
+  class DicomSequentialReader;
   class DicomStoreUserConnection;
+  class IExecutorService;
   class OrthancPlugins;
   class ServerTranscoder;
   class SharedArchive;
@@ -50,7 +52,7 @@
   class ThreadPool;
   class TranscodingCallable;
   
-  
+
   /**
    * This class is responsible for maintaining the storage area on the
    * filesystem (including compression), as well as the index of the
@@ -292,6 +294,7 @@
     boost::shared_ptr<DataSourceReader>  dicomReader_;
     boost::shared_ptr<ThreadPool>        transcoderThreadPool_;
     boost::shared_ptr<DataSourceReader>  transcoderReader_;
+    boost::shared_ptr<IExecutorService>  sequentialReaderThreadPool_;
 
   public:
     ServerContext(IDatabaseWrapper& database,
@@ -592,5 +595,9 @@
     Future* SubmitTranscodingRequest(TranscodingCallable* callable);
 
     MultipleDicomReader* CreateMultipleDicomReader() const;
+
+    DicomSequentialReader* CreateDicomSequentialReader();
+
+    DicomSequentialReader* CreateDicomSequentialReader(DicomTransferSyntax transcodingSyntax);
   };
 }