# HG changeset patch # User Sebastien Jodogne # Date 1779566859 -7200 # Node ID 58740529b5110b2570714e59e596a74e6db5436e # Parent f71f567556086bfcdad1a9ec8f6838c63a9fee9b created DicomSequentialReader diff -r f71f56755608 -r 58740529b511 .hgignore --- 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/ *~ diff -r f71f56755608 -r 58740529b511 OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake --- 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 diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DataSourceAnswer.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& 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 protection(item); diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DataSourceAnswer.h --- 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& GetValue() const; + + IDataIdentifier* ReleaseIdentifier(); }; private: diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DataSourceSequentialReader.cpp --- /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 + * . + **/ + + +#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 reader_; + boost::shared_ptr disconnector_; + std::unique_ptr request_; + size_t estimatedSize_; + + public: + Callable(const boost::shared_ptr& reader, + const boost::shared_ptr& 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 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 front(pendingRequests_.front()); + pendingRequests_.pop_front(); + + runningSize_ += estimatedSize; + + std::unique_ptr callable(new Callable(reader_, disconnector_, front.release(), estimatedSize)); + runningRequests_.push_back(executor_->Submit(callable.release())); + } + } + + + DataSourceSequentialReader::DataSourceSequentialReader(const boost::shared_ptr& executor, + const boost::shared_ptr& 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 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(runningRequests_.front()); + runningRequests_.pop_front(); + + std::unique_ptr item(dynamic_cast(future->ReleaseResult())); + runningSize_ -= item->GetEstimatedSize(); + + FillWindow(); + + return item.release(); + } + } +} diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DataSourceSequentialReader.h --- /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 + * . + **/ + + +#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 id_; + std::unique_ptr value_; + std::unique_ptr 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 PendingRequests; + typedef std::list RunningRequests; + + boost::shared_ptr executor_; + boost::shared_ptr reader_; + boost::shared_ptr disconnector_; + unsigned int windowSize_; + uint64_t windowCapacity_; + + bool started_; + PendingRequests pendingRequests_; + RunningRequests runningRequests_; + uint64_t runningSize_; + + void FillWindow(); + + public: + DataSourceSequentialReader(const boost::shared_ptr& executor, + const boost::shared_ptr& 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(); + }; +} diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DicomDataSource.cpp --- 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 dicom_; - size_t size_; + Mutex mutex_; + std::unique_ptr 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(*that_.item_->GetValue()); + lock_.reset(value.Lock()); + } + + ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const { - return dynamic_cast(*that_.item_->GetValue()).GetContent(); + const Value& value = dynamic_cast(*that_.item_->GetValue()); + return value.GetContent(); } diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DicomDataSource.h --- 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 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 lock_; public: - explicit Lock(Dicom& that) : - that_(that), - lock_(that.mutex_) - { - } + explicit Lock(Dicom& that); ParsedDicomFile& GetContent() const; }; diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp --- /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 + * . + **/ + + +#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 dicom(new DicomDataSource::Dicom(source)); + return dicom->Clone(); + } + + case Type_Transcoded: + { + std::unique_ptr 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& executor, + DicomTransferSyntax transferSyntax, + const boost::shared_ptr& 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 protection(userData); + + std::unique_ptr id; + + switch (type_) + { + case Type_Original: + id.reset(DicomDataSource::CreateWholeRequest(attachment)); + break; + + default: + throw OrthancException(ErrorCode_InternalError); + } + + if (protection.get() != NULL) + { + dynamic_cast(*id).SetUserData(protection.release()); + } + + reader_.Submit(id.release()); + } + + + DicomSequentialReader* DicomSequentialReader::CreateForOriginal(const boost::shared_ptr& executor, + const boost::shared_ptr& 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& executor, + DicomTransferSyntax transferSyntax, + const boost::shared_ptr& 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 item(reader_.Next()); + return dynamic_cast(item->ReleaseValue()); + } +} diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/DicomSequentialReader.h --- /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 + * . + **/ + + +#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& executor, + DicomTransferSyntax transferSyntax, + const boost::shared_ptr& reader, + unsigned int windowSize, + uint64_t windowCapacity); + + void SubmitInternal(const FileInfo& attachment, + IDynamicObject* userData); + + public: + static DicomSequentialReader* CreateForOriginal(const boost::shared_ptr& executor, + const boost::shared_ptr& dicomReader, + unsigned int windowSize, + uint64_t windowCapacity); + + static DicomSequentialReader* CreateForTranscoded(const boost::shared_ptr& executor, + DicomTransferSyntax transferSyntax, + const boost::shared_ptr& 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(); + }; +} diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp --- 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 parsed_; std::unique_ptr 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(*that.item_->GetValue()); + Value& value = dynamic_cast(*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(*that.item_->GetValue()); + Value& value = dynamic_cast(*that.item_->GetValue()); if (value.HasParsed()) { + std::unique_ptr lock(value.LockParsed()); serialized_.reset(new std::string); value.GetParsed().SaveToMemoryBuffer(*serialized_); content_ = serialized_.get(); diff -r f71f56755608 -r 58740529b511 OrthancFramework/Sources/DataSource/TranscoderDataSource.h --- 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 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 parsed_; - const ParsedDicomFile* content_; + std::unique_ptr lock_; + std::unique_ptr parsed_; + const ParsedDicomFile* content_; public: explicit LockAsParsed(Transcoded& that); diff -r f71f56755608 -r 58740529b511 OrthancServer/Sources/OrthancGetRequestHandler.cpp --- 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 item(instancesLoader_->Dequeue()); + std::unique_ptr 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 filesInfo; for (std::list::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; diff -r f71f56755608 -r 58740529b511 OrthancServer/Sources/OrthancGetRequestHandler.h --- 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 instancesIds_; size_t position_; std::string originatorAet_; - std::unique_ptr instancesLoader_; + std::unique_ptr instancesLoader_; unsigned int completedCount_; unsigned int warningCount_; diff -r f71f56755608 -r 58740529b511 OrthancServer/Sources/ServerContext.cpp --- 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 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 */); + } } diff -r f71f56755608 -r 58740529b511 OrthancServer/Sources/ServerContext.h --- 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 dicomReader_; boost::shared_ptr transcoderThreadPool_; boost::shared_ptr transcoderReader_; + boost::shared_ptr sequentialReaderThreadPool_; public: ServerContext(IDatabaseWrapper& database, @@ -592,5 +595,9 @@ Future* SubmitTranscodingRequest(TranscodingCallable* callable); MultipleDicomReader* CreateMultipleDicomReader() const; + + DicomSequentialReader* CreateDicomSequentialReader(); + + DicomSequentialReader* CreateDicomSequentialReader(DicomTransferSyntax transcodingSyntax); }; }