changeset 6791:ebd42c3fb06a

added IDataIdentifier::EstimateValueSize()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 17 May 2026 18:10:27 +0200
parents f45d29cd02df
children 323f93009cc8
files OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h OrthancFramework/Sources/DataSource/DataSourceReader.cpp OrthancFramework/Sources/DataSource/IDataIdentifier.h OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/UnitTestsSources/JobsTests.cpp
diffstat 6 files changed, 57 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp	Sun May 17 18:10:27 2026 +0200
@@ -32,7 +32,8 @@
   {
     DataSourceMemoryBudget::DataSourceMemoryBudget(uint64_t maximumMemory) :
       maximumMemory_(maximumMemory),
-      currentMemory_(0)
+      currentMemory_(0),
+      reservations_(0)
     {
     }
 
@@ -41,13 +42,14 @@
     {
       boost::mutex::scoped_lock lock(mutex_);
 
-      while (currentMemory_ != 0 &&  // Make sure that 1 thread can always proceed
+      while (reservations_ != 0 &&  // Make sure that 1 thread can always proceed
              currentMemory_ + memory > maximumMemory_)
       {
         cond_.wait(lock);
       }
 
       currentMemory_ += memory;
+      reservations_++;
     }
 
 
@@ -58,6 +60,9 @@
       assert(currentMemory_ >= memory);
       currentMemory_ -= memory;
 
+      assert(reservations_ > 0);
+      reservations_--;
+
       cond_.notify_all();
     }
 
--- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h	Sun May 17 18:10:27 2026 +0200
@@ -44,6 +44,7 @@
       boost::condition_variable  cond_;
       const uint64_t             maximumMemory_;
       uint64_t                   currentMemory_;
+      unsigned int               reservations_;
 
     public:
       explicit DataSourceMemoryBudget(uint64_t maximumMemory);
@@ -53,6 +54,27 @@
       void Release(size_t memory) ORTHANC_NOEXCEPT;
 
       uint64_t GetCurrentMemory() ORTHANC_NOEXCEPT;
+
+      class Lock : public boost::noncopyable
+      {
+      private:
+        DataSourceMemoryBudget&  that_;
+        size_t                   memory_;
+
+      public:
+        explicit Lock(DataSourceMemoryBudget& that,
+                      size_t memory) :
+          that_(that),
+          memory_(memory)
+        {
+          that_.Acquire(memory_);
+        }
+
+        ~Lock()
+        {
+          that_.Release(memory_);
+        }
+      };
     };
   }
 }
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Sun May 17 18:10:27 2026 +0200
@@ -84,6 +84,14 @@
 
         if (!value)
         {
+          std::unique_ptr<Internals::DataSourceMemoryBudget::Lock> preReservation;
+
+          size_t estimatedSize = 0;
+          if (id_->EstimateValueSize(estimatedSize))
+          {
+            preReservation.reset(new Internals::DataSourceMemoryBudget::Lock(*budget_, estimatedSize));
+          }
+
           value.reset(source_.Load(*id_));
 
           if (!error && !value)
--- a/OrthancFramework/Sources/DataSource/IDataIdentifier.h	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/IDataIdentifier.h	Sun May 17 18:10:27 2026 +0200
@@ -40,5 +40,7 @@
 
     // If this method return "false", the loaded value bypasses the reader cache (if present)
     virtual bool GetCacheKey(std::string& key) const = 0;
+
+    virtual bool EstimateValueSize(size_t& target) const = 0;
   };
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Sun May 17 18:10:27 2026 +0200
@@ -84,6 +84,10 @@
       end_(end),
       customData_(customData)
     {
+      if (start > end)
+      {
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
     }
 
     Value* Read(IPluginStorageArea& area) const
@@ -91,7 +95,7 @@
       return new Value(area.ReadRange(uuid_, type_, start_, end_, customData_));
     }
 
-    bool GetCacheKey(std::string& key) const
+    virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE
     {
       // custom data is not part of the cache key, as it is for internal use by the plugin
       // (it is opaque to the Orthanc core)
@@ -101,6 +105,12 @@
              boost::lexical_cast<std::string>(end_));
       return true;
     }
+
+    virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE
+    {
+      assert(start_ <= end_);
+      return end_ - start_;
+    }
   };
 
 
--- a/OrthancFramework/UnitTestsSources/JobsTests.cpp	Sun May 17 17:51:50 2026 +0200
+++ b/OrthancFramework/UnitTestsSources/JobsTests.cpp	Sun May 17 18:10:27 2026 +0200
@@ -1944,6 +1944,13 @@
              boost::lexical_cast<std::string>(attachment_.GetContentType()));
       return true;
     }
+
+    virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE
+    {
+      // TODO - Support "read until pixel data"
+      target = attachment_.GetUncompressedSize();
+      return true;
+    }
   };
 
   class Value : public IDynamicObject