changeset 4590:4a0bf1019335 db-changes

simplification of ServerIndex::Listener as ServerIndex::TransactionContext
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Mar 2021 16:04:09 +0100
parents bec74e29f86b
children ff8170d17d90
files OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h
diffstat 2 files changed, 23 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerIndex.cpp	Fri Mar 12 15:33:47 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.cpp	Fri Mar 12 16:04:09 2021 +0100
@@ -51,7 +51,7 @@
 
 namespace Orthanc
 {
-  class ServerIndex::Listener : public StatelessDatabaseOperations::ITransactionContext
+  class ServerIndex::TransactionContext : public StatelessDatabaseOperations::ITransactionContext
   {
   private:
     struct FileToRemove
@@ -85,7 +85,6 @@
     std::list<FileToRemove> pendingFilesToRemove_;
     std::list<ServerIndexChange> pendingChanges_;
     uint64_t sizeOfFilesToRemove_;
-    bool insideTransaction_;
     uint64_t sizeOfAddedAttachments_;
 
     void Reset()
@@ -97,33 +96,6 @@
       sizeOfAddedAttachments_ = 0;
     }
 
-  public:
-    explicit Listener(ServerContext& context) :
-      context_(context),
-      insideTransaction_(false)
-    {
-      Reset();
-      assert(ResourceType_Patient < ResourceType_Study &&
-             ResourceType_Study < ResourceType_Series &&
-             ResourceType_Series < ResourceType_Instance);
-    }
-
-    void StartTransaction()
-    {
-      Reset();
-      insideTransaction_ = true;
-    }
-
-    void EndTransaction()
-    {
-      insideTransaction_ = false;
-    }
-
-    uint64_t GetSizeOfFilesToRemove()
-    {
-      return sizeOfFilesToRemove_;
-    }
-
     void CommitFilesToRemove()
     {
       for (std::list<FileToRemove>::const_iterator 
@@ -152,6 +124,16 @@
       }
     }
 
+  public:
+    explicit TransactionContext(ServerContext& context) :
+      context_(context)
+    {
+      Reset();
+      assert(ResourceType_Patient < ResourceType_Study &&
+             ResourceType_Study < ResourceType_Series &&
+             ResourceType_Series < ResourceType_Instance);
+    }
+
     virtual void SignalRemainingAncestor(ResourceType parentType,
                                          const std::string& publicId) ORTHANC_OVERRIDE
     {
@@ -192,14 +174,7 @@
                  << EnumerationToString(change.GetResourceType()) << ": " 
                  << EnumerationToString(change.GetChangeType());
 
-      if (insideTransaction_)
-      {
-        pendingChanges_.push_back(change);
-      }
-      else
-      {
-        context_.SignalChange(change);
-      }
+      pendingChanges_.push_back(change);
     }
 
     virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE
@@ -207,35 +182,13 @@
       sizeOfAddedAttachments_ += compressedSize;
     }
 
-    bool HasRemainingLevel() const
-    {
-      return hasRemainingLevel_;
-    }
-
-    ResourceType GetRemainingType() const
-    {
-      assert(HasRemainingLevel());
-      return remainingType_;
-    }
-
-    const std::string& GetRemainingPublicId() const
-    {
-      assert(HasRemainingLevel());
-      return remainingPublicId_;
-    }
-
-    uint64_t GetSizeOfAddedAttachments() const
-    {
-      return sizeOfAddedAttachments_;
-    }
-
     virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */,
                                       ResourceType& remainingLevel   /* out */) ORTHANC_OVERRIDE
     {
-      if (HasRemainingLevel())
+      if (hasRemainingLevel_)
       {
-        remainingPublicId = GetRemainingPublicId();
-        remainingLevel = GetRemainingType();
+        remainingPublicId = remainingPublicId_;
+        remainingLevel = remainingType_;
         return true;
       }
       else
@@ -269,8 +222,8 @@
 
     virtual int64_t GetCompressedSizeDelta() ORTHANC_OVERRIDE
     {
-      return (static_cast<int64_t>(GetSizeOfAddedAttachments()) -
-              static_cast<int64_t>(GetSizeOfFilesToRemove()));
+      return (static_cast<int64_t>(sizeOfAddedAttachments_) -
+              static_cast<int64_t>(sizeOfFilesToRemove_));
     }
   };
 
@@ -278,90 +231,17 @@
   class ServerIndex::TransactionContextFactory : public ITransactionContextFactory
   {
   private:
-    class Context : public ITransactionContext
-    {
-    private:
-      Listener&  listener_;
-
-    public:
-      Context(ServerIndex& index) :
-        listener_(*index.listener_)
-      {
-        listener_.StartTransaction();
-      }
-
-      ~Context()
-      {
-        listener_.EndTransaction();
-      }
-
-      virtual bool IsUnstableResource(int64_t id) ORTHANC_OVERRIDE
-      {
-        return listener_.IsUnstableResource(id);
-      }
-
-      virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */,
-                                        ResourceType& remainingLevel   /* out */) ORTHANC_OVERRIDE
-      {
-        return listener_.LookupRemainingLevel(remainingPublicId, remainingLevel);
-      }
-
-      virtual void MarkAsUnstable(int64_t id,
-                                  Orthanc::ResourceType type,
-                                  const std::string& publicId) ORTHANC_OVERRIDE
-      {
-        listener_.MarkAsUnstable(id, type, publicId);
-      }
-
-      virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE
-      {
-        listener_.SignalAttachmentsAdded(compressedSize);
-      }
-
-      virtual void SignalChange(const ServerIndexChange& change) ORTHANC_OVERRIDE
-      {
-        listener_.SignalChange(change);
-      }
-
-      virtual void Commit() ORTHANC_OVERRIDE
-      {
-        listener_.Commit();
-      }
-
-      virtual int64_t GetCompressedSizeDelta() ORTHANC_OVERRIDE
-      {
-        return listener_.GetCompressedSizeDelta();
-      }
-
-      virtual void SignalRemainingAncestor(ResourceType parentType,
-                                           const std::string& publicId) ORTHANC_OVERRIDE
-      {
-        listener_.SignalRemainingAncestor(parentType, publicId);
-      }
-
-      virtual void SignalAttachmentDeleted(const FileInfo& info) ORTHANC_OVERRIDE
-      {
-        listener_.SignalAttachmentDeleted(info);
-      }
-
-      virtual void SignalResourceDeleted(ResourceType type,
-                                         const std::string& publicId) ORTHANC_OVERRIDE
-      {
-        listener_.SignalResourceDeleted(type, publicId);
-      }
-    };
-
-    ServerIndex& index_;
+    ServerContext& context_;
       
   public:
-    TransactionContextFactory(ServerIndex& index) :
-      index_(index)
+    TransactionContextFactory(ServerContext& context) :
+      context_(context)
     {
     }
 
     virtual ITransactionContext* Create()
     {
-      return new Context(index_);
+      return new TransactionContext(context_);
     }
   };    
   
@@ -452,9 +332,7 @@
     maximumStorageSize_(0),
     maximumPatients_(0)
   {
-    listener_.reset(new Listener(context));
-
-    SetTransactionContextFactory(new TransactionContextFactory(*this));
+    SetTransactionContextFactory(new TransactionContextFactory(context));
 
     // Initial recycling if the parameters have changed since the last
     // execution of Orthanc
--- a/OrthancServer/Sources/ServerIndex.h	Fri Mar 12 15:33:47 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.h	Fri Mar 12 16:04:09 2021 +0100
@@ -45,8 +45,8 @@
   class ServerIndex : public StatelessDatabaseOperations
   {
   private:
+    class TransactionContext;
     class TransactionContextFactory;
-    class Listener;
     class UnstableResourcePayload;
 
     bool done_;
@@ -54,7 +54,6 @@
     boost::thread flushThread_;
     boost::thread unstableResourcesMonitorThread_;
 
-    std::unique_ptr<Listener> listener_;
     LeastRecentlyUsedIndex<int64_t, UnstableResourcePayload>  unstableResources_;
 
     uint64_t     maximumStorageSize_;