changeset 5677:dc96401dbe88 find-refactoring

starting the refactoring of /tools/find
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Jul 2024 19:03:23 +0200
parents b744a2cf408a
children 527918e9c5d9
files OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp OrthancServer/Sources/Database/BaseDatabaseWrapper.cpp OrthancServer/Sources/Database/BaseDatabaseWrapper.h OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/Database/Compatibility/GenericFind.h OrthancServer/Sources/Database/FindRequest.cpp OrthancServer/Sources/Database/FindRequest.h OrthancServer/Sources/Database/FindResponse.h OrthancServer/Sources/Database/IDatabaseWrapper.h OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.h OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/ResourceFinder.h
diffstat 13 files changed, 170 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -1280,7 +1280,8 @@
 
 
     virtual void ExecuteFind(FindResponse& response,
-                             const FindRequest& request) ORTHANC_OVERRIDE
+                             const FindRequest& request,
+                             const Capabilities& capabilities) ORTHANC_OVERRIDE
     {
       // TODO-FIND
       throw OrthancException(ErrorCode_NotImplemented);
@@ -1288,11 +1289,12 @@
 
 
     virtual void ExecuteFind(std::list<std::string>& identifiers,
-                             const FindRequest& request) ORTHANC_OVERRIDE
+                             const FindRequest& request,
+                             const Capabilities& capabilities) ORTHANC_OVERRIDE
     {
       // TODO-FIND
       Compatibility::GenericFind find(*this);
-      find.ExecuteFind(identifiers, request);
+      find.ExecuteFind(identifiers, request, capabilities);
     }
 
 
--- a/OrthancServer/Sources/Database/BaseDatabaseWrapper.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/BaseDatabaseWrapper.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -48,17 +48,19 @@
 
 
   void BaseDatabaseWrapper::BaseTransaction::ExecuteFind(FindResponse& response,
-                                                         const FindRequest& request)
+                                                         const FindRequest& request,
+                                                         const Capabilities& capabilities)
   {
     throw OrthancException(ErrorCode_NotImplemented);  // Not supported
   }
 
 
   void BaseDatabaseWrapper::BaseTransaction::ExecuteFind(std::list<std::string>& identifiers,
-                                                         const FindRequest& request)
+                                                         const FindRequest& request,
+                                                         const Capabilities& capabilities)
   {
     Compatibility::GenericFind find(*this);
-    find.ExecuteFind(identifiers, request);
+    find.ExecuteFind(identifiers, request, capabilities);
   }
 
 
--- a/OrthancServer/Sources/Database/BaseDatabaseWrapper.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/BaseDatabaseWrapper.h	Mon Jul 08 19:03:23 2024 +0200
@@ -49,10 +49,12 @@
                                           int64_t& uncompressedSize) ORTHANC_OVERRIDE;
 
       virtual void ExecuteFind(FindResponse& response,
-                               const FindRequest& request) ORTHANC_OVERRIDE;
+                               const FindRequest& request,
+                               const Capabilities& capabilities) ORTHANC_OVERRIDE;
 
       virtual void ExecuteFind(std::list<std::string>& identifiers,
-                               const FindRequest& request) ORTHANC_OVERRIDE;
+                               const FindRequest& request,
+                               const Capabilities& capabilities) ORTHANC_OVERRIDE;
 
       virtual void ExecuteExpand(FindResponse& response,
                                  const FindRequest& request,
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -114,8 +114,15 @@
     }
 
     void GenericFind::ExecuteFind(std::list<std::string>& identifiers,
-                                  const FindRequest& request)
+                                  const FindRequest& request,
+                                  const IDatabaseWrapper::Capabilities& capabilities)
     {
+      if (!request.GetLabels().empty() &&
+          !capabilities.HasLabelsSupport())
+      {
+        throw OrthancException(ErrorCode_NotImplemented, "The database backend doesn't support labels");
+      }
+
       if (IsRequestWithoutContraint(request) &&
           !request.GetOrthancIdentifiers().HasPatientId() &&
           !request.GetOrthancIdentifiers().HasStudyId() &&
@@ -203,7 +210,8 @@
       }
       else
       {
-        throw OrthancException(ErrorCode_NotImplemented);  // Not supported
+        printf("?????????????????????????\n");
+        throw OrthancException(ErrorCode_NotImplemented);
       }
     }
 
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.h	Mon Jul 08 19:03:23 2024 +0200
@@ -39,13 +39,14 @@
                                  int64_t internalId);
 
     public:
-      GenericFind(IDatabaseWrapper::ITransaction& transaction) :
+      explicit GenericFind(IDatabaseWrapper::ITransaction& transaction) :
         transaction_(transaction)
       {
       }
 
       void ExecuteFind(std::list<std::string>& identifiers,
-                       const FindRequest& request);
+                       const FindRequest& request,
+                       const IDatabaseWrapper::Capabilities& capabilities);
 
       void ExecuteExpand(FindResponse& response,
                          const FindRequest& request,
--- a/OrthancServer/Sources/Database/FindRequest.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/FindRequest.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -85,6 +85,7 @@
     hasLimits_(false),
     limitsSince_(0),
     limitsCount_(0),
+    labelsConstraint_(LabelsConstraint_All),
     retrieveMainDicomTags_(false),
     retrieveMetadata_(false),
     retrieveLabels_(false),
--- a/OrthancServer/Sources/Database/FindRequest.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/FindRequest.h	Mon Jul 08 19:03:23 2024 +0200
@@ -255,7 +255,7 @@
     uint64_t                             limitsSince_;
     uint64_t                             limitsCount_;
     std::set<std::string>                labels_;
-    LabelsConstraint                     labelsContraint_;
+    LabelsConstraint                     labelsConstraint_;
     std::deque<Ordering*>                ordering_;             // The ordering criteria (note: the order is important !)
 
     bool                                 retrieveMainDicomTags_;
@@ -346,7 +346,12 @@
 
     LabelsConstraint GetLabelsConstraint() const
     {
-      return labelsContraint_;
+      return labelsConstraint_;
+    }
+
+    void SetLabelsConstraint(LabelsConstraint constraint)
+    {
+      labelsConstraint_ = constraint;
     }
 
     void SetRetrieveMainDicomTags(bool retrieve)
--- a/OrthancServer/Sources/Database/FindResponse.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.h	Mon Jul 08 19:03:23 2024 +0200
@@ -208,7 +208,7 @@
       void AddChildIdentifier(ResourceType level,
                               const std::string& childId)
       {
-        GetChildrenInformation(level).AddIdentifier(childId);;
+        GetChildrenInformation(level).AddIdentifier(childId);
       }
 
       const std::set<std::string>& GetChildrenIdentifiers(ResourceType level) const
--- a/OrthancServer/Sources/Database/IDatabaseWrapper.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/IDatabaseWrapper.h	Mon Jul 08 19:03:23 2024 +0200
@@ -360,11 +360,13 @@
 
       // This is only implemented if "HasIntegratedFind()" is "true"
       virtual void ExecuteFind(FindResponse& response,
-                               const FindRequest& request) = 0;
+                               const FindRequest& request,
+                               const Capabilities& capabilities) = 0;
 
       // This is only implemented if "HasIntegratedFind()" is "false"
       virtual void ExecuteFind(std::list<std::string>& identifiers,
-                               const FindRequest& request) = 0;
+                               const FindRequest& request,
+                               const Capabilities& capabilities) = 0;
 
       /**
        * This is only implemented if "HasIntegratedFind()" is
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -3706,23 +3706,25 @@
   void StatelessDatabaseOperations::ExecuteFind(FindResponse& response,
                                                 const FindRequest& request)
   {
-    class IntegratedFind : public ReadOnlyOperationsT2<FindResponse&, const FindRequest&>
+    class IntegratedFind : public ReadOnlyOperationsT3<FindResponse&, const FindRequest&,
+                                                       const IDatabaseWrapper::Capabilities&>
     {
     public:
       virtual void ApplyTuple(ReadOnlyTransaction& transaction,
                               const Tuple& tuple) ORTHANC_OVERRIDE
       {
-        transaction.ExecuteFind(tuple.get<0>(), tuple.get<1>());
+        transaction.ExecuteFind(tuple.get<0>(), tuple.get<1>(), tuple.get<2>());
       }
     };
 
-    class FindStage : public ReadOnlyOperationsT2<std::list<std::string>&, const FindRequest&>
+    class FindStage : public ReadOnlyOperationsT3<std::list<std::string>&, const FindRequest&,
+                                                  const IDatabaseWrapper::Capabilities&>
     {
     public:
       virtual void ApplyTuple(ReadOnlyTransaction& transaction,
                               const Tuple& tuple) ORTHANC_OVERRIDE
       {
-        transaction.ExecuteFind(tuple.get<0>(), tuple.get<1>());
+        transaction.ExecuteFind(tuple.get<0>(), tuple.get<1>(), tuple.get<2>());
       }
     };
 
@@ -3736,6 +3738,8 @@
       }
     };
 
+    IDatabaseWrapper::Capabilities capabilities = db_.GetDatabaseCapabilities();
+
     if (db_.HasIntegratedFind())
     {
       /**
@@ -3743,7 +3747,7 @@
        * executed in one single transaction.
        **/
       IntegratedFind operations;
-      operations.Apply(*this, response, request);
+      operations.Apply(*this, response, request, capabilities);
     }
     else
     {
@@ -3755,7 +3759,7 @@
       std::list<std::string> identifiers;
 
       FindStage find;
-      find.Apply(*this, identifiers, request);
+      find.Apply(*this, identifiers, request, capabilities);
 
       ExpandStage expand;
 
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Mon Jul 08 19:03:23 2024 +0200
@@ -392,15 +392,17 @@
       }
 
       void ExecuteFind(FindResponse& response,
-                       const FindRequest& request)
+                       const FindRequest& request,
+                       const IDatabaseWrapper::Capabilities& capabilities)
       {
-        transaction_.ExecuteFind(response, request);
+        transaction_.ExecuteFind(response, request, capabilities);
       }
 
       void ExecuteFind(std::list<std::string>& identifiers,
-                       const FindRequest& request)
+                       const FindRequest& request,
+                       const IDatabaseWrapper::Capabilities& capabilities)
       {
-        transaction_.ExecuteFind(identifiers, request);
+        transaction_.ExecuteFind(identifiers, request, capabilities);
       }
 
       void ExecuteExpand(FindResponse& response,
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Mon Jul 08 19:03:23 2024 +0200
@@ -275,7 +275,7 @@
     else
     {
       /**
-       * VERSION IN ORTHANC <= 1.12.3
+       * VERSION IN ORTHANC <= 1.12.4
        **/
 
       std::list<std::string> result;
@@ -362,7 +362,7 @@
     else
     {
       /**
-       * VERSION IN ORTHANC <= 1.12.3
+       * VERSION IN ORTHANC <= 1.12.4
        **/
 
       Json::Value json;
@@ -3329,8 +3329,109 @@
       throw OrthancException(ErrorCode_BadRequest, 
                              "Field \"" + std::string(KEY_LABELS_CONSTRAINT) + "\" must be an array of strings");
     }
+    else if (false)
+    {
+      /**
+       * EXPERIMENTAL VERSION
+       **/
+
+      bool expand = false;
+      if (request.isMember(KEY_EXPAND))
+      {
+        expand = request[KEY_EXPAND].asBool();
+      }
+
+      const ResourceType level = StringToResourceType(request[KEY_LEVEL].asCString());
+
+      ResourceFinder finder(level, expand);
+      finder.SetFormat(OrthancRestApi::GetDicomFormat(request, DicomToJsonFormat_Human));
+
+      size_t limit = 0;
+      if (request.isMember(KEY_LIMIT))
+      {
+        int tmp = request[KEY_LIMIT].asInt();
+        if (tmp < 0)
+        {
+          throw OrthancException(ErrorCode_ParameterOutOfRange,
+                                 "Field \"" + std::string(KEY_LIMIT) + "\" must be a positive integer");
+        }
+
+        limit = static_cast<size_t>(tmp);
+      }
+
+      size_t since = 0;
+      if (request.isMember(KEY_SINCE))
+      {
+        int tmp = request[KEY_SINCE].asInt();
+        if (tmp < 0)
+        {
+          throw OrthancException(ErrorCode_ParameterOutOfRange,
+                                 "Field \"" + std::string(KEY_SINCE) + "\" must be a positive integer");
+        }
+
+        since = static_cast<size_t>(tmp);
+      }
+
+      if (request.isMember(KEY_LIMIT) ||
+          request.isMember(KEY_SINCE))
+      {
+        finder.SetLimits(since, limit);
+      }
+
+      if (request.isMember(KEY_REQUESTED_TAGS))
+      {
+        std::set<DicomTag> requestedTags;
+        FromDcmtkBridge::ParseListOfTags(requestedTags, request[KEY_REQUESTED_TAGS]);
+        finder.AddRequestedTags(requestedTags);
+      }
+
+      if (request.isMember(KEY_LABELS))  // New in Orthanc 1.12.0
+      {
+        for (Json::Value::ArrayIndex i = 0; i < request[KEY_LABELS].size(); i++)
+        {
+          if (request[KEY_LABELS][i].type() != Json::stringValue)
+          {
+            throw OrthancException(ErrorCode_BadRequest, "Field \"" + std::string(KEY_LABELS) + "\" must contain strings");
+          }
+          else
+          {
+            finder.AddLabel(request[KEY_LABELS][i].asString());
+          }
+        }
+      }
+
+      finder.SetLabelsConstraint(LabelsConstraint_All);
+
+      if (request.isMember(KEY_LABELS_CONSTRAINT))
+      {
+        const std::string& s = request[KEY_LABELS_CONSTRAINT].asString();
+        if (s == "All")
+        {
+          finder.SetLabelsConstraint(LabelsConstraint_All);
+        }
+        else if (s == "Any")
+        {
+          finder.SetLabelsConstraint(LabelsConstraint_Any);
+        }
+        else if (s == "None")
+        {
+          finder.SetLabelsConstraint(LabelsConstraint_None);
+        }
+        else
+        {
+          throw OrthancException(ErrorCode_BadRequest, "Field \"" + std::string(KEY_LABELS_CONSTRAINT) + "\" must be \"All\", \"Any\", or \"None\"");
+        }
+      }
+
+      Json::Value answer;
+      finder.Execute(answer, context);
+      call.GetOutput().AnswerJson(answer);
+    }
     else
     {
+      /**
+       * VERSION IN ORTHANC <= 1.12.4
+       **/
       bool expand = false;
       if (request.isMember(KEY_EXPAND))
       {
@@ -3499,7 +3600,7 @@
     else
     {
       /**
-       * VERSION IN ORTHANC <= 1.12.3
+       * VERSION IN ORTHANC <= 1.12.4
        **/
       std::list<std::string> a, b, c;
       a.push_back(call.GetUriComponent("id", ""));
--- a/OrthancServer/Sources/ResourceFinder.h	Sat Jul 06 15:04:28 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.h	Mon Jul 08 19:03:23 2024 +0200
@@ -111,6 +111,16 @@
 
     void AddRequestedTags(const std::set<DicomTag>& tags);
 
+    void AddLabel(const std::string& label)
+    {
+      request_.AddLabel(label);
+    }
+
+    void SetLabelsConstraint(LabelsConstraint constraint)
+    {
+      request_.SetLabelsConstraint(constraint);
+    }
+
     void Execute(Json::Value& target,
                  ServerContext& context) const;