changeset 6868:276cc0b60611 streaming

introducing DicomSequentialReader::Item
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 26 May 2026 18:32:55 +0200
parents ff73909042f2
children 874a093dd254
files OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp OrthancFramework/Sources/DataSource/DicomSequentialReader.h OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/Sources/DataSource/StorageAreaDataSource.h OrthancFramework/Sources/StringMemoryBuffer.cpp OrthancFramework/Sources/StringMemoryBuffer.h OrthancServer/Sources/OrthancGetRequestHandler.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.cpp
diffstat 8 files changed, 189 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp	Tue May 26 18:32:55 2026 +0200
@@ -27,39 +27,121 @@
 
 #include "../DicomParsing/ParsedDicomFile.h"
 #include "../OrthancException.h"
+#include "../StringMemoryBuffer.h"
 #include "BaseDataIdentifier.h"
 #include "DicomDataSource.h"
+#include "StorageAreaDataSource.h"
 #include "TranscoderDataSource.h"
 
 
 namespace Orthanc
 {
+  DicomSequentialReader::Item::Item(ParsedDicomFile* parsed) :
+    parsed_(parsed)
+  {
+    if (parsed == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  DicomSequentialReader::Item::Item(const boost::shared_ptr<IMemoryBuffer>& raw) :
+    raw_(raw)
+  {
+    if (raw.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  DicomSequentialReader::Item::Item(ParsedDicomFile* parsed /* takes ownership */,
+                                    IMemoryBuffer* raw /* takes ownership */) :
+    parsed_(parsed),
+    raw_(raw)
+  {
+    if (parsed == NULL ||
+        raw == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  ParsedDicomFile& DicomSequentialReader::Item::GetParsedDicomFile()
+  {
+    if (parsed_.get() != NULL)
+    {
+      return *parsed_;
+    }
+    else if (raw_.get() != NULL)
+    {
+      parsed_.reset(new ParsedDicomFile(raw_->GetData(), raw_->GetSize()));
+      return *parsed_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
+  const IMemoryBuffer& DicomSequentialReader::Item::GetRawMemoryBuffer()
+  {
+    if (raw_.get() != NULL)
+    {
+      return *raw_;
+    }
+    else if (parsed_.get() != NULL)
+    {
+      std::string s;
+      parsed_->SaveToMemoryBuffer(s);
+
+      raw_.reset(StringMemoryBuffer::CreateFromSwap(s));
+      return *raw_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
   class DicomSequentialReader::Disconnector : public DataSourceSequentialReader::IValueDisconnector
   {
   private:
-    Type  type_;
+    SourceType  sourceType_;
 
   public:
-    Disconnector(Type type) :
-      type_(type)
+    Disconnector(SourceType sourceType) :
+      sourceType_(sourceType)
     {
     }
 
     virtual IDynamicObject* Apply(DataSourceAnswer::Item* source)
     {
-      switch (type_)
+      std::unique_ptr<DataSourceAnswer::Item> protection(source);
+
+      switch (sourceType_)
       {
-        case Type_Original:
+        case SourceType_StorageArea:
         {
-          std::unique_ptr<DicomDataSource::Dicom> dicom(new DicomDataSource::Dicom(source));
-          return dicom->Clone();
+          std::unique_ptr<StorageAreaDataSource::Range> range(new StorageAreaDataSource::Range(protection.release()));
+          return new Item(range->GetSharedBuffer());
         }
 
-        case Type_Transcoded:
+        case SourceType_Dicom:
         {
-          std::unique_ptr<TranscoderDataSource::Transcoded> dicom(new TranscoderDataSource::Transcoded(source));
+          std::unique_ptr<DicomDataSource::Dicom> dicom(new DicomDataSource::Dicom(protection.release()));
+          return new Item(dicom->Clone());
+        }
+
+        case SourceType_Transcoder:
+        {
+          std::unique_ptr<TranscoderDataSource::Transcoded> dicom(new TranscoderDataSource::Transcoded(protection.release()));
           TranscoderDataSource::Transcoded::LockAsParsed lock(*dicom);
-          return lock.GetContent().Clone(true);
+          return new Item(lock.GetContent().Clone(true));
         }
 
         default:
@@ -69,7 +151,7 @@
   };
 
 
-  DicomSequentialReader::DicomSequentialReader(Type type,
+  DicomSequentialReader::DicomSequentialReader(SourceType sourceType,
                                                const boost::shared_ptr<IExecutorService>& executor,
                                                DicomTransferSyntax targetSyntax,
                                                TranscodingSopInstanceUidMode mode,
@@ -78,12 +160,12 @@
                                                const boost::shared_ptr<DataSourceReader>& reader,
                                                unsigned int windowSize,
                                                uint64_t windowCapacity) :
-    type_(type),
+    sourceType_(sourceType),
     targetSyntax_(targetSyntax),
     mode_(mode),
     hasLossyQuality_(hasLossyQuality),
     lossyQuality_(lossyQuality),
-    reader_(executor, reader, new Disconnector(type), windowSize, windowCapacity)
+    reader_(executor, reader, new Disconnector(sourceType), windowSize, windowCapacity)
   {
   }
 
@@ -95,13 +177,17 @@
 
     std::unique_ptr<IDataIdentifier> id;
 
-    switch (type_)
+    switch (sourceType_)
     {
-      case Type_Original:
+      case SourceType_StorageArea:
+        id.reset(StorageAreaDataSource::CreateAttachmentRequest(attachment, true /* uncompress */));
+        break;
+
+      case SourceType_Dicom:
         id.reset(DicomDataSource::CreateWholeRequest(attachment));
         break;
 
-      case Type_Transcoded:
+      case SourceType_Transcoder:
         id.reset(TranscoderDataSource::CreateRequest(attachment, targetSyntax_, mode_, hasLossyQuality_, lossyQuality_));
         break;
 
@@ -124,7 +210,7 @@
                                                                   uint64_t windowCapacity)
   {
     return new DicomSequentialReader(
-      Type_Original, executor,
+      SourceType_Dicom, executor,
       DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew,
       false /* no lossy */, 0 /* unused lossy quality */,
       dicomReader, windowSize, windowCapacity);
@@ -141,7 +227,7 @@
                                                                     uint64_t windowCapacity)
   {
     return new DicomSequentialReader(
-      Type_Transcoded, executor,
+      SourceType_Transcoder, executor,
       targetSyntax, mode, hasLossyQuality, lossyQuality,
       transcoderReader, windowSize, windowCapacity);
   }
@@ -177,9 +263,9 @@
   }
 
 
-  ParsedDicomFile* DicomSequentialReader::Next()
+  DicomSequentialReader::Item* DicomSequentialReader::Next()
   {
     std::unique_ptr<DataSourceSequentialReader::Item> item(reader_.Next());
-    return dynamic_cast<ParsedDicomFile*>(item->ReleaseValue());
+    return dynamic_cast<Item*>(item->ReleaseValue());
   }
 }
--- a/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.h	Tue May 26 18:32:55 2026 +0200
@@ -24,8 +24,8 @@
 
 #pragma once
 
-#include "../Enumerations.h"
 #include "../FileStorage/FileInfo.h"
+#include "../IMemoryBuffer.h"
 #include "../MultiThreading/IExecutorService.h"
 #include "DataSourceSequentialReader.h"
 
@@ -36,23 +36,44 @@
 
   class ORTHANC_PUBLIC DicomSequentialReader : public boost::noncopyable
   {
+  public:
+    class ORTHANC_PUBLIC Item : public IDynamicObject
+    {
+    private:
+      std::unique_ptr<ParsedDicomFile>  parsed_;
+      boost::shared_ptr<IMemoryBuffer>  raw_;
+
+    public:
+      explicit Item(ParsedDicomFile* parsed /* takes ownership */);
+
+      explicit Item(const boost::shared_ptr<IMemoryBuffer>& raw);
+
+      Item(ParsedDicomFile* parsed /* takes ownership */,
+           IMemoryBuffer* raw /* takes ownership */);
+
+      ParsedDicomFile& GetParsedDicomFile();
+
+      const IMemoryBuffer& GetRawMemoryBuffer();
+    };
+
   private:
     class Disconnector;
 
-    enum Type
+    enum SourceType
     {
-      Type_Original,
-      Type_Transcoded
+      SourceType_StorageArea,
+      SourceType_Dicom,
+      SourceType_Transcoder
     };
 
-    Type                           type_;
+    SourceType                     sourceType_;
     DicomTransferSyntax            targetSyntax_;
     TranscodingSopInstanceUidMode  mode_;
     bool                           hasLossyQuality_;
     unsigned int                   lossyQuality_;
     DataSourceSequentialReader     reader_;
 
-    DicomSequentialReader(Type type,
+    DicomSequentialReader(SourceType sourceType,
                           const boost::shared_ptr<IExecutorService>& executor,
                           DicomTransferSyntax targetSyntax,
                           TranscodingSopInstanceUidMode mode,
@@ -89,6 +110,6 @@
 
     bool HasNext();
 
-    ParsedDicomFile* Next();
+    Item* Next();
   };
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Tue May 26 18:32:55 2026 +0200
@@ -340,6 +340,24 @@
   }
 
 
+  boost::shared_ptr<IMemoryBuffer> StorageAreaDataSource::Range::GetSharedBuffer() const
+  {
+    if (buffer_.get() != NULL)
+    {
+      return buffer_;
+    }
+    else if (item_.get() != NULL)
+    {
+      // TODO-Streaming : Any way to shared the value with the "Item"?
+      return boost::shared_ptr<IMemoryBuffer>(StringMemoryBuffer::CreateFromBuffer(GetData(), GetSize()));
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
   IDataIdentifier* StorageAreaDataSource::CreateRangeRequest(const std::string& uuid,
                                                              FileContentType type,
                                                              uint64_t start,
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Tue May 26 18:32:55 2026 +0200
@@ -71,7 +71,7 @@
     {
     private:
       std::unique_ptr<DataSourceAnswer::Item>  item_;   // Holding item puts backpressure on the data source (can be NULL)
-      std::unique_ptr<IMemoryBuffer>           buffer_;  // To be used if "item_ == NULL"
+      boost::shared_ptr<IMemoryBuffer>         buffer_;  // To be used if "item_ == NULL"
 
       const IMemoryBuffer& GetBuffer() const;
 
@@ -94,6 +94,8 @@
       {
         GetBuffer().CopyToString(target);
       }
+
+      boost::shared_ptr<IMemoryBuffer> GetSharedBuffer() const;
     };
 
     static IDataIdentifier* CreateRangeRequest(const std::string& uuid,
--- a/OrthancFramework/Sources/StringMemoryBuffer.cpp	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/StringMemoryBuffer.cpp	Tue May 26 18:32:55 2026 +0200
@@ -25,6 +25,8 @@
 #include "PrecompiledHeaders.h"
 #include "StringMemoryBuffer.h"
 
+#include "OrthancException.h"
+
 
 namespace Orthanc
 {
@@ -65,4 +67,21 @@
     result->Copy(buffer, start, end);
     return result.release();
   }
+
+
+  IMemoryBuffer* StringMemoryBuffer::CreateFromBuffer(const void* data,
+                                                      size_t size)
+  {
+    if (size != 0 &&
+        data == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+    else
+    {
+      std::string s;
+      s.assign(reinterpret_cast<const char*>(data), size);
+      return CreateFromSwap(s);
+    }
+  }
 }
--- a/OrthancFramework/Sources/StringMemoryBuffer.h	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancFramework/Sources/StringMemoryBuffer.h	Tue May 26 18:32:55 2026 +0200
@@ -69,5 +69,8 @@
     static IMemoryBuffer* CreateFromCopy(const std::string& buffer);
 
     static IMemoryBuffer* CreateFromCopy(const std::string& buffer, size_t start /* inclusive */, size_t end /* exclusive */);
+
+    static IMemoryBuffer* CreateFromBuffer(const void* data,
+                                           size_t size);
   };
 }
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Tue May 26 18:32:55 2026 +0200
@@ -77,17 +77,19 @@
        * "DicomSequentialReader" returns copies of the cached objects
        * (cf. "DataSourceSequentialReader::IValueDisconnector"). (*)
        **/
-      std::unique_ptr<ParsedDicomFile> dicom(instancesLoader_->Next());
+      std::unique_ptr<DicomSequentialReader::Item> next(instancesLoader_->Next());
+      assert(next.get() != NULL);
 
-      if (dicom.get() == NULL ||
-          dicom->GetDcmtkObject().getDataset() == NULL)
+      ParsedDicomFile& dicom = next->GetParsedDicomFile();
+
+      if (dicom.GetDcmtkObject().getDataset() == NULL)
       {
         throw OrthancException(ErrorCode_InternalError);
       }
     
       OFString a, b;
-      if (!dicom->GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPClassUID, a).good() ||
-          !dicom->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 +99,7 @@
       std::string sopClassUid(a.c_str());
       std::string sopInstanceUid(b.c_str());
 
-      return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, *dicom);
+      return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, dicom);
     }
   }
 
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 17:54:14 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Tue May 26 18:32:55 2026 +0200
@@ -566,9 +566,7 @@
         }
         else
         {
-          std::unique_ptr<ParsedDicomFile> next;
-
-          std::string content;
+          std::unique_ptr<DicomSequentialReader::Item> next;
 
           try
           {
@@ -581,14 +579,14 @@
             return;
           }
 
-          next->SaveToMemoryBuffer(content);
+          const IMemoryBuffer& dicom = next->GetRawMemoryBuffer();
 
           writer.OpenFile(filename_);
-          writer.Write(content);
+          writer.Write(dicom.GetData(), dicom.GetSize());
 
           if (dicomDir != NULL)
           {
-            dicomDir->Add(dicomDirFolder_, filename_, *next);
+            dicomDir->Add(dicomDirFolder_, filename_, next->GetParsedDicomFile());
           }
         }
       }