changeset 6785:967edd507e0e

getting rid of IObjectSizeProvider
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 13 May 2026 17:26:47 +0200
parents 40683448f9ae
children e10d890319c5
files OrthancFramework/Sources/Cache/IObjectSizeProvider.h OrthancFramework/Sources/Cache/SharedObjectCache.cpp OrthancFramework/Sources/Cache/SharedObjectCache.h OrthancFramework/Sources/DataSource/DataSourceReader.cpp OrthancFramework/Sources/DataSource/DataSourceReader.h OrthancFramework/Sources/DataSource/IDataSource.h OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/Sources/DataSource/StorageAreaDataSource.h
diffstat 8 files changed, 30 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Cache/IObjectSizeProvider.h	Wed May 13 16:45:00 2026 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/**
- * 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 "../IDynamicObject.h"
-
-
-namespace Orthanc
-{
-  class ORTHANC_PUBLIC IObjectSizeProvider : public boost::noncopyable
-  {
-  public:
-    virtual ~IObjectSizeProvider()
-    {
-    }
-
-    virtual size_t GetSize(const IDynamicObject& object) const = 0;
-  };
-}
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.cpp	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp	Wed May 13 17:26:47 2026 +0200
@@ -81,16 +81,10 @@
   }
 
 
-  SharedObjectCache::SharedObjectCache(IObjectSizeProvider* provider,
-                                       size_t capacity) :
-    provider_(provider),
+  SharedObjectCache::SharedObjectCache(size_t capacity) :
     capacity_(capacity),
     currentSize_(0)
   {
-    if (provider == NULL)
-    {
-      throw OrthancException(ErrorCode_NullPointer);
-    }
   }
 
 
@@ -127,17 +121,16 @@
 
 
   void SharedObjectCache::Store(const std::string& id,
-                                const boost::shared_ptr<IDynamicObject>& value)
+                                const boost::shared_ptr<IDynamicObject>& value,
+                                size_t size)
   {
     if (value.get() == NULL)
     {
       throw OrthancException(ErrorCode_NullPointer);
     }
 
-    assert(provider_.get() != NULL);
-    size_t size = provider_->GetSize(*value) + sizeof(Item);
-
     // Caching items with zero size would break the logic of "MakeRoom()", hence "sizeof(Item)"
+    size += sizeof(Item);
     assert(size != 0);
 
     if (size > capacity_)
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.h	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h	Wed May 13 17:26:47 2026 +0200
@@ -24,8 +24,8 @@
 
 #pragma once
 
+#include "../IDynamicObject.h"
 #include "../MultiThreading/Mutex.h"
-#include "IObjectSizeProvider.h"
 #include "LeastRecentlyUsedIndex.h"
 
 #include <boost/shared_ptr.hpp>
@@ -40,18 +40,16 @@
 
     typedef std::map<std::string, Item*>  Content;
 
-    Mutex                                 mutex_;
-    std::unique_ptr<IObjectSizeProvider>  provider_;
-    LeastRecentlyUsedIndex<std::string>   lru_;
-    Content                               content_;
-    size_t                                capacity_;
-    size_t                                currentSize_;
+    Mutex                                mutex_;
+    LeastRecentlyUsedIndex<std::string>  lru_;
+    Content                              content_;
+    size_t                               capacity_;
+    size_t                               currentSize_;
 
     void MakeRoom(size_t newObjectSize);
 
   public:
-    SharedObjectCache(IObjectSizeProvider* provider /* takes ownership */,
-                      size_t capacity);
+    SharedObjectCache(size_t capacity);
 
     ~SharedObjectCache();
 
@@ -67,7 +65,8 @@
      * be read by other threads.
      **/
     void Store(const std::string& id,
-               const boost::shared_ptr<IDynamicObject>& value);
+               const boost::shared_ptr<IDynamicObject>& value,
+               size_t size);
 
     void Invalidate(const std::string& id);
   };
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp	Wed May 13 17:26:47 2026 +0200
@@ -100,7 +100,7 @@
 
         if (!error && cache_ && hasCacheKey)
         {
-          cache_->Store(cacheKey, value);
+          cache_->Store(cacheKey, value, source_.GetValueSize(*value));
         }
       }
 
@@ -111,7 +111,7 @@
 
       if (!error)
       {
-        size = source_.GetSize(*value);
+        size = source_.GetValueSize(*value);
         budget_->Acquire(size);   // may block; "DataSourceAnswer" CAN be destroyed here
       }
 
@@ -167,10 +167,9 @@
   }
 
 
-  void DataSourceReader::CreateCache(IObjectSizeProvider* provider /* takes ownership */,
-                                     size_t capacity)
+  void DataSourceReader::CreateCache(size_t capacity)
   {
-    cache_ = boost::make_shared<SharedObjectCache>(provider, capacity);
+    cache_ = boost::make_shared<SharedObjectCache>(capacity);
   }
 
 
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.h	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DataSourceReader.h	Wed May 13 17:26:47 2026 +0200
@@ -60,8 +60,7 @@
       return *source_;
     }
 
-    void CreateCache(IObjectSizeProvider* provider /* takes ownership */,
-                     size_t capacity);
+    void CreateCache(size_t capacity);
 
     void SetMaximumMemory(uint64_t maximumMemory);
 
--- a/OrthancFramework/Sources/DataSource/IDataSource.h	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/IDataSource.h	Wed May 13 17:26:47 2026 +0200
@@ -24,14 +24,20 @@
 
 #pragma once
 
-#include "../Cache/IObjectSizeProvider.h"
+#include "../IDynamicObject.h"
 #include "IDataIdentifier.h"
 
 namespace Orthanc
 {
-  class ORTHANC_PUBLIC IDataSource : public IObjectSizeProvider
+  class ORTHANC_PUBLIC IDataSource : public boost::noncopyable
   {
   public:
+    virtual ~IDataSource()
+    {
+    }
+
     virtual IDynamicObject* Load(const IDataIdentifier& identifier) = 0;
+
+    virtual size_t GetValueSize(const IDynamicObject& value) const = 0;
   };
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Wed May 13 17:26:47 2026 +0200
@@ -114,10 +114,10 @@
     return *area_;
   }
 
-  size_t StorageAreaDataSource::GetSize(const IDynamicObject& object) const
+  size_t StorageAreaDataSource::GetValueSize(const IDynamicObject& value) const
   {
-    const Value& value = dynamic_cast<const Value&>(object);
-    return value.GetBuffer().GetSize();
+    const Value& tmp = dynamic_cast<const Value&>(value);
+    return tmp.GetBuffer().GetSize();
   }
 
   IDynamicObject* StorageAreaDataSource::Load(const IDataIdentifier& identifier)
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Wed May 13 16:45:00 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Wed May 13 17:26:47 2026 +0200
@@ -49,7 +49,7 @@
 
     IPluginStorageArea& GetArea() const;
 
-    virtual size_t GetSize(const IDynamicObject& object) const ORTHANC_OVERRIDE;
+    virtual size_t GetValueSize(const IDynamicObject& value) const ORTHANC_OVERRIDE;
 
     virtual IDynamicObject* Load(const IDataIdentifier& identifier) ORTHANC_OVERRIDE;