changeset 4555:456ed3fcff81 db-changes

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 03 Mar 2021 17:13:41 +0100
parents efd90f778cd2
children 2a0f8031fb93
files OrthancServer/Sources/LuaScripting.cpp OrthancServer/Sources/ServerIndex.cpp OrthancServer/Sources/ServerIndex.h
diffstat 3 files changed, 164 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/LuaScripting.cpp	Wed Mar 03 16:31:57 2021 +0100
+++ b/OrthancServer/Sources/LuaScripting.cpp	Wed Mar 03 17:13:41 2021 +0100
@@ -136,7 +136,7 @@
       std::map<MetadataType, std::string> metadata_;      
 
     public:
-      GetInfoOperations(const ServerIndexChange& change) :
+      explicit GetInfoOperations(const ServerIndexChange& change) :
         change_(change),
         ok_(false)
       {
--- a/OrthancServer/Sources/ServerIndex.cpp	Wed Mar 03 16:31:57 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.cpp	Wed Mar 03 17:13:41 2021 +0100
@@ -1139,56 +1139,6 @@
   }
 
   
-  bool ServerIndex::LookupAttachment(FileInfo& attachment,
-                                     const std::string& instanceUuid,
-                                     FileContentType contentType)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-
-    int64_t id;
-    ResourceType type;
-    if (!db_.LookupResource(id, type, instanceUuid))
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-
-    if (db_.LookupAttachment(attachment, id, contentType))
-    {
-      assert(attachment.GetContentType() == contentType);
-      return true;
-    }
-    else
-    {
-      return false;
-    }
-  }
-
-
-
-  void ServerIndex::GetAllUuids(std::list<std::string>& target,
-                                ResourceType resourceType)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-    db_.GetAllPublicIds(target, resourceType);
-  }
-
-
-  void ServerIndex::GetAllUuids(std::list<std::string>& target,
-                                ResourceType resourceType,
-                                size_t since,
-                                size_t limit)
-  {
-    if (limit == 0)
-    {
-      target.clear();
-      return;
-    }
-
-    boost::mutex::scoped_lock lock(mutex_);
-    db_.GetAllPublicIds(target, resourceType, since, limit);
-  }
-
-
   template <typename T>
   static void FormatLog(Json::Value& target,
                         const std::list<T>& log,
@@ -2446,13 +2396,13 @@
     ReadOnlyFunction  func_;
 
   public:
-    ReadOnlyWrapper(ReadOnlyFunction  func) :
+    explicit ReadOnlyWrapper(ReadOnlyFunction  func) :
       func_(func)
     {
       assert(func_ != NULL);
     }
 
-    virtual void Apply(ReadOnlyTransaction& transaction)
+    virtual void Apply(ReadOnlyTransaction& transaction) ORTHANC_OVERRIDE
     {
       func_(transaction);
     }
@@ -2465,13 +2415,13 @@
     ReadWriteFunction  func_;
 
   public:
-    ReadWriteWrapper(ReadWriteFunction  func) :
+    explicit ReadWriteWrapper(ReadWriteFunction  func) :
       func_(func)
     {
       assert(func_ != NULL);
     }
 
-    virtual void Apply(ReadWriteTransaction& transaction)
+    virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
     {
       func_(transaction);
     }
@@ -2822,4 +2772,130 @@
     Operations operations(target, publicId, level);
     Apply(operations);
   }
+
+
+  bool ServerIndex::LookupAttachment(FileInfo& attachment,
+                                     const std::string& instancePublicId,
+                                     FileContentType contentType)
+  {
+    class Operations : public ServerIndex::IReadOnlyOperations
+    {
+    private:
+      FileInfo&        attachment_;
+      bool             found_;
+      std::string      instancePublicId_;
+      FileContentType  contentType_;
+
+    public:
+      Operations(FileInfo& attachment,
+                 const std::string& instancePublicId,
+                 FileContentType contentType) :
+        attachment_(attachment),
+        found_(false),
+        instancePublicId_(instancePublicId),
+        contentType_(contentType)
+      {
+      }
+      
+      virtual void Apply(ServerIndex::ReadOnlyTransaction& transaction) ORTHANC_OVERRIDE
+      {
+        int64_t internalId;
+        ResourceType type;
+        if (!transaction.LookupResource(internalId, type, instancePublicId_))
+        {
+          throw OrthancException(ErrorCode_UnknownResource);
+        }
+        else if (transaction.LookupAttachment(attachment_, internalId, contentType_))
+        {
+          assert(attachment_.GetContentType() == contentType_);
+          found_ = true;
+        }
+        else
+        {
+          found_ = false;
+        }
+      }
+
+      bool HasFound() const
+      {
+        return found_;
+      }
+    };
+
+    Operations operations(attachment, instancePublicId, contentType);
+    Apply(operations);
+    return operations.HasFound();
+  }
+
+
+
+  void ServerIndex::GetAllUuids(std::list<std::string>& target,
+                                ResourceType resourceType)
+  {
+    class Operations : public ServerIndex::IReadOnlyOperations
+    {
+    private:
+      std::list<std::string>& target_;
+      ResourceType            resourceType_;
+
+    public:
+      Operations(std::list<std::string>& target,
+                 ResourceType resourceType) :
+        target_(target),
+        resourceType_(resourceType)
+      {
+      }
+      
+      virtual void Apply(ServerIndex::ReadOnlyTransaction& transaction) ORTHANC_OVERRIDE
+      {
+        transaction.GetAllPublicIds(target_, resourceType_);
+      }
+    };
+
+    Operations operations(target, resourceType);
+    Apply(operations);
+  }
+
+
+  void ServerIndex::GetAllUuids(std::list<std::string>& target,
+                                ResourceType resourceType,
+                                size_t since,
+                                size_t limit)
+  {
+    if (limit == 0)
+    {
+      target.clear();
+    }
+    else
+    {
+      class Operations : public ServerIndex::IReadOnlyOperations
+      {
+      private:
+        std::list<std::string>& target_;
+        ResourceType            resourceType_;
+        size_t                  since_;
+        size_t                  limit_;
+
+      public:
+        Operations(std::list<std::string>& target,
+                   ResourceType resourceType,
+                   size_t since,
+                   size_t limit) :
+          target_(target),
+          resourceType_(resourceType),
+          since_(since),
+          limit_(limit)
+        {
+        }
+      
+        virtual void Apply(ServerIndex::ReadOnlyTransaction& transaction) ORTHANC_OVERRIDE
+        {
+          transaction.GetAllPublicIds(target_, resourceType_, since_, limit_);
+        }
+      };
+
+      Operations operations(target, resourceType, since, limit);
+      Apply(operations);
+    }
+  }
 }
--- a/OrthancServer/Sources/ServerIndex.h	Wed Mar 03 16:31:57 2021 +0100
+++ b/OrthancServer/Sources/ServerIndex.h	Wed Mar 03 17:13:41 2021 +0100
@@ -162,18 +162,6 @@
                              /* out */ uint64_t& countSeries, 
                              /* out */ uint64_t& countInstances);
 
-    bool LookupAttachment(FileInfo& attachment,
-                          const std::string& instanceUuid,
-                          FileContentType contentType);
-
-    void GetAllUuids(std::list<std::string>& target,
-                     ResourceType resourceType);
-
-    void GetAllUuids(std::list<std::string>& target,
-                     ResourceType resourceType,
-                     size_t since,
-                     size_t limit);
-
     bool DeleteResource(Json::Value& target /* out */,
                         const std::string& uuid,
                         ResourceType expectedType);
@@ -301,7 +289,7 @@
       IDatabaseWrapper&  db_;
       
     public:
-      ReadOnlyTransaction(IDatabaseWrapper& db) :
+      explicit ReadOnlyTransaction(IDatabaseWrapper& db) :
         db_(db)
       {
       }
@@ -331,7 +319,21 @@
                           int64_t id)
       {
         db_.GetAllMetadata(target, id);
-      }        
+      }
+
+      void GetAllPublicIds(std::list<std::string>& target,
+                           ResourceType resourceType)
+      {
+        return db_.GetAllPublicIds(target, resourceType);
+      }
+
+      void GetAllPublicIds(std::list<std::string>& target,
+                           ResourceType resourceType,
+                           size_t since,
+                           size_t limit)
+      {
+        return db_.GetAllPublicIds(target, resourceType, since, limit);
+      }  
 
       void GetChildrenPublicId(std::list<std::string>& target,
                                int64_t id)
@@ -372,7 +374,7 @@
     class ReadWriteTransaction : public ReadOnlyTransaction
     {
     public:
-      ReadWriteTransaction(IDatabaseWrapper& db) :
+      explicit ReadWriteTransaction(IDatabaseWrapper& db) :
         ReadOnlyTransaction(db)
       {
       }
@@ -402,13 +404,13 @@
     };
 
 
-    typedef void (*ReadOnlyFunction) (ReadOnlyTransaction& transaction);
-    typedef void (*ReadWriteFunction) (ReadWriteTransaction& transaction);
+    typedef void (*ReadOnlyFunction) (ReadOnlyTransaction& transaction);  // TODO - Is this useful?
+    typedef void (*ReadWriteFunction) (ReadWriteTransaction& transaction);  // TODO - Is this useful?
 
     
   private:
-    class ReadOnlyWrapper;
-    class ReadWriteWrapper;
+    class ReadOnlyWrapper;  // TODO - Is this useful?
+    class ReadWriteWrapper;  // TODO - Is this useful?
 
     void ApplyInternal(IReadOnlyOperations* readOperations,
                        IReadWriteOperations* writeOperations);
@@ -431,5 +433,17 @@
     void GetAllMetadata(std::map<MetadataType, std::string>& target,
                         const std::string& publicId,
                         ResourceType level);
+
+    void GetAllUuids(std::list<std::string>& target,
+                     ResourceType resourceType);
+
+    void GetAllUuids(std::list<std::string>& target,
+                     ResourceType resourceType,
+                     size_t since,
+                     size_t limit);
+
+    bool LookupAttachment(FileInfo& attachment,
+                          const std::string& instancePublicId,
+                          FileContentType contentType);
   };
 }