changeset 4585:f0bdd99f3d81 db-changes

created a ITransactionContextFactory around ServerIndex::Listener
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 09 Mar 2021 16:40:38 +0100
parents b25941dcdbbe
children 1d96fe7e054e
files OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h
diffstat 2 files changed, 108 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerIndex.cpp	Tue Mar 09 16:18:24 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.cpp	Tue Mar 09 16:40:38 2021 +0100
@@ -76,7 +76,7 @@
     }      
   }
 
-  
+
   class ServerIndex::Listener : public IDatabaseListener,
                                 public ServerIndex::ITransactionContext
   {
@@ -286,6 +286,64 @@
   };
 
 
+  class ServerIndex::TransactionContextFactory : public ITransactionContextFactory
+  {
+  private:
+    class Context : public ITransactionContext
+    {
+    private:
+      Listener&  listener_;
+
+    public:
+      Context(ServerIndex& index) :
+        listener_(*index.listener_)
+      {
+      }
+
+      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);
+      }
+    };
+
+    ServerIndex& index_;
+      
+  public:
+    TransactionContextFactory(ServerIndex& index) :
+      index_(index)
+    {
+    }
+
+    virtual ITransactionContext* Create()
+    {
+      return new Context(index_);
+    }
+  };    
+  
+  
   class ServerIndex::Transaction
   {
   private:
@@ -562,6 +620,8 @@
     listener_.reset(new Listener(context));
     db_.SetListener(*listener_);
 
+    SetTransactionContextFactory(new TransactionContextFactory(*this));
+
     // Initial recycling if the parameters have changed since the last
     // execution of Orthanc
     StandaloneRecycling(maximumStorageSize_, maximumPatients_);
@@ -1089,6 +1149,11 @@
     {
       throw OrthancException(ErrorCode_InternalError);
     }
+
+    if (factory_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls, "No transaction context was provided");     
+    }
     
     unsigned int count = 0;
 
@@ -1096,6 +1161,13 @@
     {
       try
       {
+        std::unique_ptr<ITransactionContext>  context(factory_->Create());
+
+        if (context.get() == NULL)
+        {
+          throw OrthancException(ErrorCode_InternalError);
+        }
+        
         boost::mutex::scoped_lock lock(databaseMutex_);  // TODO - REMOVE
 
         if (readOperations != NULL)
@@ -1108,7 +1180,7 @@
           
           Transaction transaction(*this, TransactionType_ReadOnly);  // TODO - Only if not "TransactionType_Implicit"
           {
-            ReadOnlyTransaction t(db_, *listener_);
+            ReadOnlyTransaction t(db_, *context);
             readOperations->Apply(t);
           }
           transaction.Commit();
@@ -1119,8 +1191,7 @@
           
           Transaction transaction(*this, TransactionType_ReadWrite);
           {
-            assert(listener_.get() != NULL);
-            ReadWriteTransaction t(db_, *listener_);
+            ReadWriteTransaction t(db_, *context);
             writeOperations->Apply(t);
           }
           transaction.Commit();
@@ -1163,6 +1234,23 @@
   }
 
   
+  void ServerIndex::SetTransactionContextFactory(ITransactionContextFactory* factory)
+  {
+    if (factory == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+    else if (factory_.get() != NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      factory_.reset(factory);
+    }
+  }
+    
+
   void ServerIndex::Apply(IReadOnlyOperations& operations)
   {
     ApplyInternal(&operations, NULL);
--- a/OrthancServer/Sources/ServerIndex.h	Tue Mar 09 16:18:24 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.h	Tue Mar 09 16:40:38 2021 +0100
@@ -55,6 +55,7 @@
     typedef std::map<std::pair<ResourceType, MetadataType>, std::string>  MetadataMap;
 
   private:
+    class TransactionContextFactory;
     class Listener;
     class Transaction;
     class UnstableResourcePayload;
@@ -144,6 +145,17 @@
     };
 
     
+    class ITransactionContextFactory : public boost::noncopyable
+    {
+    public:
+      virtual ~ITransactionContextFactory()
+      {
+      }
+
+      virtual ITransactionContext* Create() = 0;
+    };
+
+
     class ReadOnlyTransaction : public boost::noncopyable
     {
     private:
@@ -459,10 +471,13 @@
   private:
     void ApplyInternal(IReadOnlyOperations* readOperations,
                        IReadWriteOperations* writeOperations);
-    
+
+    std::unique_ptr<ITransactionContextFactory>  factory_;
     unsigned int maxRetries_;
 
   public:
+    void SetTransactionContextFactory(ITransactionContextFactory* factory /* takes ownership */);
+    
     void Apply(IReadOnlyOperations& operations);
   
     void Apply(IReadWriteOperations& operations);