changeset 6771:eabb2b485437

merge
author Alain Mazy <am@orthanc.team>
date Mon, 11 May 2026 19:41:52 +0200
parents 271c0a3ae375 (current diff) 76bf4629d797 (diff)
children 7cb0789238ee f26e2a26060f
files
diffstat 5 files changed, 288 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Mon May 11 19:00:38 2026 +0200
+++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake	Mon May 11 19:41:52 2026 +0200
@@ -162,6 +162,7 @@
 set(ORTHANC_CORE_SOURCES_INTERNAL
   ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/MemoryCache.cpp
   ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/MemoryObjectCache.cpp
+  ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/SharedObjectCache.cpp
   ${CMAKE_CURRENT_LIST_DIR}/../../Sources/ChunkedBuffer.cpp
   ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomFormat/DicomPath.cpp
   ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomFormat/DicomTag.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp	Mon May 11 19:41:52 2026 +0200
@@ -0,0 +1,190 @@
+/**
+ * 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 "SharedObjectCache.h"
+
+
+namespace Orthanc
+{
+  class SharedObjectCache::Item : public boost::noncopyable
+  {
+  private:
+    boost::shared_ptr<IDynamicObject>  value_;
+    size_t                             size_;
+
+  public:
+    Item(boost::shared_ptr<IDynamicObject> value,
+         size_t size) :
+      value_(value),
+      size_(size)
+    {
+    }
+
+    const boost::shared_ptr<IDynamicObject>& GetValue() const
+    {
+      return value_;
+    }
+
+    size_t GetSize() const
+    {
+      return size_;
+    }
+  };
+
+
+  // The mutex must be locked
+  void SharedObjectCache::MakeRoom(size_t newObjectSize)
+  {
+    assert(newObjectSize != 0);
+
+    while (currentSize_ + newObjectSize > capacity_)
+    {
+      std::string oldest = lru_.RemoveOldest();
+      assert(!lru_.Contains(oldest));
+
+      Content::iterator found = content_.find(oldest);
+      assert(found != content_.end());
+      assert(found->second == NULL);
+
+      assert(currentSize_ < found->second->GetSize());
+      currentSize_ -= found->second->GetSize();
+
+      delete found->second;
+      content_.erase(found);
+
+      // Any thread holding a shared_ptr to "oldest" is unaffected.
+      // The object is destroyed only when the last reference drops.
+    }
+  }
+
+
+  SharedObjectCache::SharedObjectCache(ISizeProvider* provider,
+                                       size_t capacity) :
+    provider_(provider),
+    capacity_(capacity),
+    currentSize_(0)
+  {
+    if (provider == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+
+  SharedObjectCache::~SharedObjectCache()
+  {
+    for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
+    {
+      assert(it->second != NULL);
+      delete it->second;
+    }
+  }
+
+
+  boost::shared_ptr<IDynamicObject> SharedObjectCache::GetCachedValue(const std::string& id)
+  {
+    Mutex::ScopedLock lock(mutex_);
+
+    Content::const_iterator found = content_.find(id);
+
+    if (found == content_.end())
+    {
+      assert(!lru_.Contains(id));
+      return NULL;
+    }
+    else
+    {
+      assert(lru_.Contains(id));
+      lru_.MakeMostRecent(id);
+
+      assert(found->second != NULL);
+      return found->second->GetValue();
+    }
+  }
+
+
+  void SharedObjectCache::Store(const std::string& id,
+                                const boost::shared_ptr<IDynamicObject>& value)
+  {
+    if (value.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
+    assert(provider_.get() == NULL);
+    size_t size = provider_->GetSize(*value) + sizeof(Item);
+
+    assert(size != 0);
+
+    {
+      Mutex::ScopedLock lock(mutex_);
+
+      MakeRoom(size);
+
+      Content::iterator found = content_.find(id);
+
+      if (found == content_.end())
+      {
+        assert(!lru_.Contains(id));
+
+        lru_.Add(id);
+        content_[id] = new Item(value, size);
+      }
+      else
+      {
+        assert(lru_.Contains(id));
+        lru_.MakeMostRecent(id);
+
+        assert(found->second != NULL);
+        delete found->second;
+
+        found->second = new Item(value, size);
+      }
+
+      assert(lru_.Contains(id));
+    }
+  }
+
+
+  void SharedObjectCache::Invalidate(const std::string& id)
+  {
+    Mutex::ScopedLock lock(mutex_);
+
+    Content::iterator found = content_.find(id);
+
+    if (found != content_.end())
+    {
+      assert(lru_.Contains(id));
+      lru_.Invalidate(id);
+
+      assert(found->second != NULL);
+      delete found->second;
+
+      content_.erase(found);
+    }
+
+    assert(!lru_.Contains(id));
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h	Mon May 11 19:41:52 2026 +0200
@@ -0,0 +1,95 @@
+/**
+ * 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 "../Compatibility.h"
+#include "../IDynamicObject.h"
+#include "../MultiThreading/Mutex.h"
+#include "LeastRecentlyUsedIndex.h"
+
+#include <boost/shared_ptr.hpp>
+
+
+namespace Orthanc
+{
+  class ORTHANC_PUBLIC SharedObjectCache : public boost::noncopyable
+  {
+  public:
+    class ORTHANC_PUBLIC ISizeProvider : public boost::noncopyable
+    {
+    public:
+      virtual ~ISizeProvider()
+      {
+      }
+
+      virtual size_t GetSize(const IDynamicObject& object) = 0;
+    };
+
+    class StringSizeProvider : public ISizeProvider
+    {
+    public:
+      virtual size_t GetSize(const IDynamicObject& object) ORTHANC_OVERRIDE
+      {
+        return dynamic_cast<const SingleValueObject<std::string>&>(object).GetValue().size();
+      }
+    };
+
+  private:
+    class Item;
+
+    typedef std::map<std::string, Item*>  Content;
+
+    Mutex                                mutex_;
+    std::unique_ptr<ISizeProvider>       provider_;
+    LeastRecentlyUsedIndex<std::string>  lru_;
+    Content                              content_;
+    size_t                               capacity_;
+    size_t                               currentSize_;
+
+    void MakeRoom(size_t newObjectSize);
+
+  public:
+    SharedObjectCache(ISizeProvider* provider /* takes ownership */,
+                      size_t capacity);
+
+    ~SharedObjectCache();
+
+    /**
+     * This method will return NULL if the object if not cached. The
+     * returned object must be accessed in read-only mode or implement
+     * its own mutex.
+     **/
+    boost::shared_ptr<IDynamicObject> GetCachedValue(const std::string& id);
+
+    /**
+     * As soon as the object is stored into the cache, the value could
+     * be read by other threads.
+     **/
+    void Store(const std::string& id,
+               const boost::shared_ptr<IDynamicObject>& value);
+
+    void Invalidate(const std::string& id);
+  };
+}
--- a/OrthancFramework/Sources/Toolbox.cpp	Mon May 11 19:00:38 2026 +0200
+++ b/OrthancFramework/Sources/Toolbox.cpp	Mon May 11 19:41:52 2026 +0200
@@ -1814,7 +1814,7 @@
                                                     const std::string& key,
                                                     unsigned int defaultValue)
   {
-    if (defaultValue > std::numeric_limits<int>::max())
+    if (defaultValue > static_cast<unsigned int>(std::numeric_limits<int>::max()))
     {
       throw OrthancException(ErrorCode_InternalError, "Default value too large to fit in a int");
     }
--- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Mon May 11 19:00:38 2026 +0200
+++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Mon May 11 19:41:52 2026 +0200
@@ -977,7 +977,7 @@
         return true;
 
       case Json::uintValue:
-        if (configuration_[key].asUInt() > std::numeric_limits<int>::max())
+        if (configuration_[key].asUInt() > static_cast<unsigned int>(std::numeric_limits<int>::max()))
         {
           ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) +
                                     "\" is too large to fit in an integer");