changeset 4567:b812a5f2cef3 db-changes

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Mar 2021 14:41:29 +0100
parents f52d0bc19e07
children a3e6aa2b07b0
files OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h
diffstat 2 files changed, 330 insertions(+), 175 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerIndex.cpp	Mon Mar 08 13:54:02 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.cpp	Mon Mar 08 14:41:29 2021 +0100
@@ -600,37 +600,6 @@
   }
 
 
-  uint64_t ServerIndex::IncrementGlobalSequenceInternal(GlobalProperty property)
-  {
-    std::string oldValue;
-
-    if (db_.LookupGlobalProperty(oldValue, property))
-    {
-      uint64_t oldNumber;
-      
-      try
-      {
-        oldNumber = boost::lexical_cast<uint64_t>(oldValue);
-      }
-      catch (boost::bad_lexical_cast&)
-      {
-        LOG(ERROR) << "Cannot read the global sequence "
-                   << boost::lexical_cast<std::string>(property) << ", resetting it";
-        oldNumber = 0;
-      }
-
-      db_.SetGlobalProperty(property, boost::lexical_cast<std::string>(oldNumber + 1));
-      return oldNumber + 1;
-    }
-    else
-    {
-      // Initialize the sequence at "1"
-      db_.SetGlobalProperty(property, "1");
-      return 1;
-    }
-  }
-
-
   bool ServerIndex::IsUnstableResource(int64_t id)
   {
     return unstableResources_.Contains(id);
@@ -1236,93 +1205,6 @@
   }
 
 
-  void ServerIndex::SetProtectedPatient(const std::string& publicId,
-                                        bool isProtected)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    Transaction transaction(*this);
-
-    // Lookup for the requested resource
-    int64_t id;
-    ResourceType type;
-    if (!db_.LookupResource(id, type, publicId) ||
-        type != ResourceType_Patient)
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-
-    db_.SetProtectedPatient(id, isProtected);
-    transaction.Commit(0);
-
-    if (isProtected)
-      LOG(INFO) << "Patient " << publicId << " has been protected";
-    else
-      LOG(INFO) << "Patient " << publicId << " has been unprotected";
-  }
-
-
-  void ServerIndex::SetMetadata(const std::string& publicId,
-                                MetadataType type,
-                                const std::string& value)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    Transaction t(*this);
-
-    ResourceType rtype;
-    int64_t id;
-    if (!db_.LookupResource(id, rtype, publicId))
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-
-    db_.SetMetadata(id, type, value);
-
-    if (IsUserMetadata(type))
-    {
-      LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId);
-    }
-
-    t.Commit(0);
-  }
-
-
-  void ServerIndex::DeleteMetadata(const std::string& publicId,
-                                   MetadataType type)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    Transaction t(*this);
-
-    ResourceType rtype;
-    int64_t id;
-    if (!db_.LookupResource(id, rtype, publicId))
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-
-    db_.DeleteMetadata(id, type);
-
-    if (IsUserMetadata(type))
-    {
-      LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId);
-    }
-
-    t.Commit(0);
-  }
-
-
-  uint64_t ServerIndex::IncrementGlobalSequence(GlobalProperty sequence)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    Transaction transaction(*this);
-
-    uint64_t seq = IncrementGlobalSequenceInternal(sequence);
-    transaction.Commit(0);
-
-    return seq;
-  }
-
-
-
   void ServerIndex::LogChange(ChangeType changeType,
                               const std::string& publicId)
   {
@@ -1341,25 +1223,6 @@
   }
 
 
-  void ServerIndex::DeleteChanges()
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-
-    Transaction transaction(*this);
-    db_.ClearChanges();
-    transaction.Commit(0);
-  }
-
-  void ServerIndex::DeleteExportedResources()
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-
-    Transaction transaction(*this);
-    db_.ClearExportedResources();
-    transaction.Commit(0);
-  }
-
-
   void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that,
                                                    unsigned int threadSleep)
   {
@@ -1517,19 +1380,6 @@
   }
 
 
-  void ServerIndex::SetGlobalProperty(GlobalProperty property,
-                                      const std::string& value)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-
-    Transaction transaction(*this);
-    db_.SetGlobalProperty(property, value);
-    transaction.Commit(0);
-  }
-
-
-
-
   void ServerIndex::ReconstructInstance(const ParsedDicomFile& dicom)
   {
     DicomMap summary;
@@ -1862,7 +1712,7 @@
         else
         {
           assert(writeOperations != NULL);
-          ReadWriteTransaction t(db_);
+          ReadWriteTransaction t(db_, *this);
           writeOperations->Apply(t, *listener_);          
         }
 
@@ -1924,7 +1774,7 @@
     class Operations : public ReadOnlyOperationsT4<bool&, Json::Value&, const std::string&, ResourceType>
     {
     private:
-      ServerIndex&  index_;
+      ServerIndex&  index_;     // TODO - REMOVE
 
     public:
       explicit Operations(ServerIndex& index) :
@@ -3294,4 +3144,263 @@
     Operations operations(publicId, remoteModality);
     Apply(operations);
   }
+
+
+  void ServerIndex::SetProtectedPatient(const std::string& publicId,
+                                        bool isProtected)
+  {
+    class Operations : public IReadWriteOperations
+    {
+    private:
+      const std::string&  publicId_;
+      bool                isProtected_;
+
+    public:
+      Operations(const std::string& publicId,
+                 bool isProtected) :
+        publicId_(publicId),
+        isProtected_(isProtected)
+      {
+      }
+
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        // Lookup for the requested resource
+        int64_t id;
+        ResourceType type;
+        if (!transaction.LookupResource(id, type, publicId_) ||
+            type != ResourceType_Patient)
+        {
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+        }
+        else
+        {
+          transaction.SetProtectedPatient(id, isProtected_);
+        }
+      }
+    };
+
+    Operations operations(publicId, isProtected);
+    Apply(operations);
+
+    if (isProtected)
+    {
+      LOG(INFO) << "Patient " << publicId << " has been protected";
+    }
+    else
+    {
+      LOG(INFO) << "Patient " << publicId << " has been unprotected";
+    }
+  }
+
+
+  void ServerIndex::SetMetadata(const std::string& publicId,
+                                MetadataType type,
+                                const std::string& value)
+  {
+    class Operations : public IReadWriteOperations
+    {
+    private:
+      const std::string&  publicId_;
+      MetadataType        type_;
+      const std::string&  value_;
+
+    public:
+      Operations(const std::string& publicId,
+                 MetadataType type,
+                 const std::string& value) :
+        publicId_(publicId),
+        type_(type),
+        value_(value)
+      {
+      }
+
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        ResourceType rtype;
+        int64_t id;
+        if (!transaction.LookupResource(id, rtype, publicId_))
+        {
+          throw OrthancException(ErrorCode_UnknownResource);
+        }
+        else
+        {
+          transaction.SetMetadata(id, type_, value_);
+
+          if (IsUserMetadata(type_))
+          {
+            transaction.LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId_);
+          }
+        }
+      }
+    };
+
+    Operations operations(publicId, type, value);
+    Apply(operations);
+  }
+
+
+  void ServerIndex::DeleteMetadata(const std::string& publicId,
+                                   MetadataType type)
+  {
+    class Operations : public IReadWriteOperations
+    {
+    private:
+      const std::string&  publicId_;
+      MetadataType        type_;
+
+    public:
+      Operations(const std::string& publicId,
+                 MetadataType type) :
+        publicId_(publicId),
+        type_(type)
+      {
+      }
+
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        ResourceType rtype;
+        int64_t id;
+        if (!transaction.LookupResource(id, rtype, publicId_))
+        {
+          throw OrthancException(ErrorCode_UnknownResource);
+        }
+        else
+        {
+          transaction.DeleteMetadata(id, type_);
+
+          if (IsUserMetadata(type_))
+          {
+            transaction.LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId_);
+          }
+        }
+      }
+    };
+
+    Operations operations(publicId, type);
+    Apply(operations);
+  }
+
+
+  uint64_t ServerIndex::IncrementGlobalSequence(GlobalProperty sequence)
+  {
+    class Operations : public IReadWriteOperations
+    {
+    private:
+      uint64_t       newValue_;
+      GlobalProperty sequence_;
+
+    public:
+      Operations(GlobalProperty sequence) :
+        sequence_(sequence)
+      {
+      }
+
+      uint64_t GetNewValue() const
+      {
+        return newValue_;
+      }
+
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        std::string oldString;
+
+        if (transaction.LookupGlobalProperty(oldString, sequence_))
+        {
+          uint64_t oldValue;
+      
+          try
+          {
+            oldValue = boost::lexical_cast<uint64_t>(oldString);
+          }
+          catch (boost::bad_lexical_cast&)
+          {
+            LOG(ERROR) << "Cannot read the global sequence "
+                       << boost::lexical_cast<std::string>(sequence_) << ", resetting it";
+            oldValue = 0;
+          }
+
+          newValue_ = oldValue + 1;
+        }
+        else
+        {
+          // Initialize the sequence at "1"
+          newValue_ = 1;
+        }
+
+        transaction.SetGlobalProperty(sequence_, boost::lexical_cast<std::string>(newValue_));
+      }
+    };
+
+    Operations operations(sequence);
+    Apply(operations);
+    return operations.GetNewValue();
+  }
+
+
+  void ServerIndex::DeleteChanges()
+  {
+    class Operations : public IReadWriteOperations
+    {
+    public:
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        transaction.ClearChanges();
+      }
+    };
+
+    Operations operations;
+    Apply(operations);
+  }
+
+  
+  void ServerIndex::DeleteExportedResources()
+  {
+    class Operations : public IReadWriteOperations
+    {
+    public:
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        transaction.ClearExportedResources();
+      }
+    };
+
+    Operations operations;
+    Apply(operations);
+  }
+
+
+  void ServerIndex::SetGlobalProperty(GlobalProperty property,
+                                      const std::string& value)
+  {
+    class Operations : public IReadWriteOperations
+    {
+    private:
+      GlobalProperty      property_;
+      const std::string&  value_;
+      
+    public:
+      Operations(GlobalProperty property,
+                 const std::string& value) :
+        property_(property),
+        value_(value)
+      {
+      }
+        
+      virtual void Apply(ReadWriteTransaction& transaction,
+                         Listener& listener) ORTHANC_OVERRIDE
+      {
+        transaction.SetGlobalProperty(property_, value_);
+      }
+    };
+
+    Operations operations(property, value);
+    Apply(operations);
+  }
 }
--- a/OrthancServer/Sources/ServerIndex.h	Mon Mar 08 13:54:02 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.h	Mon Mar 08 14:41:29 2021 +0100
@@ -106,8 +106,6 @@
                            const std::string& publicId,
                            int64_t internalId);
 
-    uint64_t IncrementGlobalSequenceInternal(GlobalProperty property);
-
     void NormalizeLookup(std::vector<DatabaseConstraint>& target,
                          const DatabaseLookup& source,
                          ResourceType level) const;
@@ -155,34 +153,15 @@
                       bool hasPixelDataOffset,
                       uint64_t pixelDataOffset);
 
-    void SetProtectedPatient(const std::string& publicId,
-                             bool isProtected);
-
-    void SetMetadata(const std::string& publicId,
-                     MetadataType type,
-                     const std::string& value);
-
-    void DeleteMetadata(const std::string& publicId,
-                        MetadataType type);
-
-    uint64_t IncrementGlobalSequence(GlobalProperty sequence);
-
     void LogChange(ChangeType changeType,
                    const std::string& publicId);
 
-    void DeleteChanges();
-
-    void DeleteExportedResources();
-
     StoreStatus AddAttachment(const FileInfo& attachment,
                               const std::string& publicId);
 
     void DeleteAttachment(const std::string& publicId,
                           FileContentType type);
 
-    void SetGlobalProperty(GlobalProperty property,
-                           const std::string& value);
-
     void ReconstructInstance(const ParsedDicomFile& dicom);
 
 
@@ -388,10 +367,31 @@
 
     class ReadWriteTransaction : public ReadOnlyTransaction
     {
+    private:
+      ServerIndex&  index_;
+      
     public:
-      explicit ReadWriteTransaction(IDatabaseWrapper& db) :
-        ReadOnlyTransaction(db)
+      ReadWriteTransaction(IDatabaseWrapper& db,
+                           ServerIndex& index) :
+        ReadOnlyTransaction(db),
+        index_(index)   // TODO - REMOVE
+      {
+      }
+
+      void ClearChanges()
       {
+        db_.ClearChanges();
+      }
+
+      void ClearExportedResources()
+      {
+        db_.ClearExportedResources();
+      }
+
+      void DeleteMetadata(int64_t id,
+                          MetadataType type)
+      {
+        db_.DeleteMetadata(id, type);
       }
 
       void DeleteResource(int64_t id)
@@ -399,10 +399,37 @@
         db_.DeleteResource(id);
       }
 
+      void LogChange(int64_t internalId,
+                     ChangeType changeType,
+                     ResourceType resourceType,
+                     const std::string& publicId)
+      {
+        index_.LogChange(internalId, changeType, resourceType, publicId);
+      }
+
       void LogExportedResource(const ExportedResource& resource)
       {
         db_.LogExportedResource(resource);
       }
+
+      void SetGlobalProperty(GlobalProperty property,
+                             const std::string& value)
+      {
+        db_.SetGlobalProperty(property, value);
+      }
+
+      void SetMetadata(int64_t id,
+                       MetadataType type,
+                       const std::string& value)
+      {
+        return db_.SetMetadata(id, type, value);
+      }
+
+      void SetProtectedPatient(int64_t internalId, 
+                               bool isProtected)
+      {
+        db_.SetProtectedPatient(internalId, isProtected);
+      }
     };
 
 
@@ -549,5 +576,24 @@
 
     void LogExportedResource(const std::string& publicId,
                              const std::string& remoteModality);
+
+    void SetProtectedPatient(const std::string& publicId,
+                             bool isProtected);
+
+    void SetMetadata(const std::string& publicId,
+                     MetadataType type,
+                     const std::string& value);
+
+    void DeleteMetadata(const std::string& publicId,
+                        MetadataType type);
+
+    uint64_t IncrementGlobalSequence(GlobalProperty sequence);
+
+    void DeleteChanges();
+
+    void DeleteExportedResources();
+
+    void SetGlobalProperty(GlobalProperty property,
+                           const std::string& value);
   };
 }