changeset 1753:faf2ecab3472 db-changes

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Oct 2015 20:31:34 +0100
parents c3d8ec63a179
children 3a4f7dc00f49
files OrthancServer/OrthancFindRequestHandler.cpp OrthancServer/Search/LookupResource.cpp OrthancServer/Search/LookupResource.h OrthancServer/Search/SetOfResources.cpp OrthancServer/Search/SetOfResources.h OrthancServer/ServerContext.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 8 files changed, 114 insertions(+), 205 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancFindRequestHandler.cpp	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/OrthancFindRequestHandler.cpp	Tue Oct 27 20:31:34 2015 +0100
@@ -338,7 +338,6 @@
 
 #if USE_LOOKUP_RESOURCE != 1
     ResourceFinder finder(context_);
-#endif
 
     switch (level)
     {
@@ -355,26 +354,44 @@
       default:
         throw OrthancException(ErrorCode_InternalError);
     }
+#else
+    size_t maxResults = (level == ResourceType_Instance) ? maxInstances_ : maxResults_;
 
-    std::list<std::string> tmp;
+#endif
 
 #if USE_LOOKUP_RESOURCE == 1
-    bool finished = context_.Apply(tmp, finder);
+    std::vector<std::string> resources, instances;
+    context_.GetIndex().FindCandidates(resources, instances, finder);
 
-    for (std::list<std::string>::const_iterator
-        it = tmp.begin(); it != tmp.end(); ++it)
+    assert(resources.size() == instances.size());
+    bool finished = true;
+
+    for (size_t i = 0; i < instances.size(); i++)
     {
-      // TODO
-      Json::Value resource;
-      context_.ReadJson(resource, *it);
-      AddAnswer(answers, resource, query);
+      Json::Value dicom;
+      context_.ReadJson(dicom, instances[i]);
+      
+      if (finder.IsMatch(dicom))
+      {
+        if (maxResults != 0 &&
+            answers.GetSize() >= maxResults)
+        {
+          finished = false;
+          break;
+        }
+        else
+        {
+          AddAnswer(answers, dicom, query);
+        }
+      }
     }
 
 #else
+    std::list<std::string> tmp;
     bool finished = finder.Apply(tmp, findQuery);
 #endif
 
-    LOG(INFO) << "Number of matching resources: " << tmp.size();
+    LOG(INFO) << "Number of matching resources: " << answers.GetSize();
 
     return finished;
   }
--- a/OrthancServer/Search/LookupResource.cpp	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/Search/LookupResource.cpp	Tue Oct 27 20:31:34 2015 +0100
@@ -275,70 +275,28 @@
 
 
 
-  bool LookupResource::ApplyUnoptimizedConstraints(std::list<int64_t>& result,
-                                                   const std::list<int64_t>& candidates,
-                                                   boost::mutex& databaseMutex,
-                                                   IDatabaseWrapper& database,
-                                                   IStorageArea& storageArea) const
+  bool LookupResource::IsMatch(const Json::Value& dicomAsJson) const
   {
-    assert(!unoptimizedConstraints_.empty());
-
-    StorageAccessor accessor(storageArea);
-
-    for (std::list<int64_t>::const_iterator candidate = candidates.begin(); 
-         candidate != candidates.end(); ++candidate)
+    for (Constraints::const_iterator it = unoptimizedConstraints_.begin(); 
+         it != unoptimizedConstraints_.end(); ++it)
     {
-      if (maxResults_ != 0 &&
-          result.size() >= maxResults_)
+      std::string tag = (*it)->GetTag().Format();
+      if (dicomAsJson.isMember(tag) &&
+          dicomAsJson[tag]["Type"] == "String")
       {
-        // We have enough results, not finished
-        return false;
-      }
-
-      int64_t instance;
-      FileInfo attachment;
-
-      {
-        boost::mutex::scoped_lock lock(databaseMutex);
-
-        if (!Toolbox::FindOneChildInstance(instance, database, *candidate, level_) ||
-            !database.LookupAttachment(attachment, instance, FileContentType_DicomAsJson))
+        std::string value = dicomAsJson[tag]["Value"].asString();
+        if (!(*it)->Match(value))
         {
-          continue;
+          return false;
         }
       }
-
-      Json::Value content;
-      accessor.Read(content, attachment);
-
-      bool match = true;
-
-      for (Constraints::const_iterator it = unoptimizedConstraints_.begin(); 
-           match && it != unoptimizedConstraints_.end(); ++it)
+      else
       {
-        std::string tag = (*it)->GetTag().Format();
-        if (content.isMember(tag) &&
-            content[tag]["Type"] == "String")
-        {
-          std::string value = content[tag]["Value"].asString();
-          if (!(*it)->Match(value))
-          {
-            match = false;
-          }
-        }
-        else
-        {
-          match = false;
-        }
-      }
-
-      if (match)
-      {
-        result.push_back(*candidate);
+        return false;
       }
     }
 
-    return true;  // Finished
+    return true;
   }
 
 
@@ -354,81 +312,40 @@
   }
 
 
-  bool LookupResource::Apply(std::list<int64_t>& result,
-                             boost::mutex& databaseMutex,
-                             IDatabaseWrapper& database,
-                             IStorageArea& storageArea) const
+  void LookupResource::FindCandidates(std::list<int64_t>& result,
+                                      IDatabaseWrapper& database) const
   {
-    std::list<int64_t> tmp;
+    SetOfResources candidates(database, level_);
 
+    switch (level_)
     {
-      boost::mutex::scoped_lock lock(databaseMutex);
-      SetOfResources candidates(database, level_);
+      case ResourceType_Patient:
+        ApplyLevel(candidates, ResourceType_Patient, database);
+        break;
 
-      switch (level_)
-      {
-        case ResourceType_Patient:
-          ApplyLevel(candidates, ResourceType_Patient, database);
-          break;
-
-        case ResourceType_Study:
-          ApplyLevel(candidates, ResourceType_Study, database);
-          break;
+      case ResourceType_Study:
+        ApplyLevel(candidates, ResourceType_Study, database);
+        break;
 
-        case ResourceType_Series:
-          ApplyLevel(candidates, ResourceType_Study, database);
-          candidates.GoDown();
-          ApplyLevel(candidates, ResourceType_Series, database);
-          break;
+      case ResourceType_Series:
+        ApplyLevel(candidates, ResourceType_Study, database);
+        candidates.GoDown();
+        ApplyLevel(candidates, ResourceType_Series, database);
+        break;
 
-        case ResourceType_Instance:
-          ApplyLevel(candidates, ResourceType_Study, database);
-          candidates.GoDown();
-          ApplyLevel(candidates, ResourceType_Series, database);
-          candidates.GoDown();
-          ApplyLevel(candidates, ResourceType_Instance, database);
-          break;
+      case ResourceType_Instance:
+        ApplyLevel(candidates, ResourceType_Study, database);
+        candidates.GoDown();
+        ApplyLevel(candidates, ResourceType_Series, database);
+        candidates.GoDown();
+        ApplyLevel(candidates, ResourceType_Instance, database);
+        break;
 
-        default:
-          throw OrthancException(ErrorCode_InternalError);
-      }
-
-      if (unoptimizedConstraints_.empty())
-      {
-        return candidates.Flatten(result, maxResults_);
-      }
-      else
-      {
-        candidates.Flatten(tmp);
-      }
+      default:
+        throw OrthancException(ErrorCode_InternalError);
     }
 
-    return ApplyUnoptimizedConstraints(result, tmp, databaseMutex, database, storageArea);
-  }
-
-
-  bool LookupResource::Apply(std::list<std::string>& result,
-                             boost::mutex& databaseMutex,
-                             IDatabaseWrapper& database,
-                             IStorageArea& storageArea) const
-  {
-
-    std::list<int64_t> tmp;
-    bool finished = Apply(tmp, databaseMutex, database, storageArea);
-
-    result.clear();
-
-    {
-      boost::mutex::scoped_lock lock(databaseMutex);
-
-      for (std::list<int64_t>::const_iterator
-             it = tmp.begin(); it != tmp.end(); ++it)
-      {
-        result.push_back(database.GetPublicId(*it));
-      }
-    }
-
-    return finished;
+    candidates.Flatten(result);
   }
 
 
--- a/OrthancServer/Search/LookupResource.h	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/Search/LookupResource.h	Tue Oct 27 20:31:34 2015 +0100
@@ -36,7 +36,6 @@
 #include "SetOfResources.h"
 
 #include <memory>
-#include <boost/thread/mutex.hpp>
 
 namespace Orthanc
 {
@@ -70,7 +69,6 @@
     ResourceType level_;
     Levels       levels_;
     Constraints  unoptimizedConstraints_;
-    size_t       maxResults_;
 
     bool AddInternal(ResourceType level,
                      std::auto_ptr<IFindConstraint>& constraint);
@@ -79,41 +77,25 @@
                     ResourceType level,
                     IDatabaseWrapper& database) const;
 
-    bool ApplyUnoptimizedConstraints(std::list<int64_t>& result,
-                                     const std::list<int64_t>& candidates,
-                                     boost::mutex& databaseMutex,
-                                     IDatabaseWrapper& database,
-                                     IStorageArea& storageArea) const;
-
   public:
     LookupResource(ResourceType level);
 
     ~LookupResource();
 
+    ResourceType GetLevel() const
+    {
+      return level_;
+    }
+
     void Add(IFindConstraint* constraint);   // Takes ownership
 
     void Add(const DicomTag& tag,
              const std::string& dicomQuery,
              bool caseSensitivePN);
 
-    void SetMaxResults(size_t maxResults)
-    {
-      maxResults_ = maxResults;
-    }
-
-    size_t GetMaxResults() const
-    {
-      return maxResults_;
-    }
+    void FindCandidates(std::list<int64_t>& result,
+                        IDatabaseWrapper& database) const;
 
-    bool Apply(std::list<int64_t>& result,
-               boost::mutex& databaseMutex,
-               IDatabaseWrapper& database,
-               IStorageArea& storageArea) const;
-
-    bool Apply(std::list<std::string>& result,
-               boost::mutex& databaseMutex,
-               IDatabaseWrapper& database,
-               IStorageArea& storageArea) const;
+    bool IsMatch(const Json::Value& dicomAsJson) const;
   };
 }
--- a/OrthancServer/Search/SetOfResources.cpp	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/Search/SetOfResources.cpp	Tue Oct 27 20:31:34 2015 +0100
@@ -75,23 +75,26 @@
       throw OrthancException(ErrorCode_BadSequenceOfCalls);
     }
 
-    std::auto_ptr<Resources> children(new Resources);
+    if (resources_.get() != NULL)
+    {
+      std::auto_ptr<Resources> children(new Resources);
 
-    for (Resources::const_iterator it = resources_->begin(); 
-         it != resources_->end(); ++it)
-    {
-      std::list<int64_t> tmp;
-      database_.GetChildrenInternalId(tmp, *it);
+      for (Resources::const_iterator it = resources_->begin(); 
+           it != resources_->end(); ++it)
+      {
+        std::list<int64_t> tmp;
+        database_.GetChildrenInternalId(tmp, *it);
 
-      for (std::list<int64_t>::const_iterator
-             child = tmp.begin(); child != tmp.end(); ++child)
-      {
-        children->insert(*child);
+        for (std::list<int64_t>::const_iterator
+               child = tmp.begin(); child != tmp.end(); ++child)
+        {
+          children->insert(*child);
+        }
       }
+
+      resources_ = children;
     }
 
-    resources_ = children;
-
     switch (level_)
     {
       case ResourceType_Patient:
@@ -150,24 +153,4 @@
       }
     }
   }
-
-
-  bool SetOfResources::Flatten(std::list<int64_t>& result,
-                               size_t maxResults)
-  {
-    Flatten(result);
-
-    if (maxResults != 0 &&
-        result.size() > maxResults)
-    {
-      std::list<int64_t>::iterator cut = result.begin();
-      std::advance(cut, maxResults);
-      result.erase(cut, result.end());
-      return false;
-    }
-    else
-    {
-      return true;
-    }
-  }
 }
--- a/OrthancServer/Search/SetOfResources.h	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/Search/SetOfResources.h	Tue Oct 27 20:31:34 2015 +0100
@@ -70,9 +70,6 @@
 
     void Flatten(std::list<std::string>& result);
 
-    bool Flatten(std::list<int64_t>& result,
-                 size_t maxResults);
-
     void Clear()
     {
       resources_.reset(NULL);
--- a/OrthancServer/ServerContext.h	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/ServerContext.h	Tue Oct 27 20:31:34 2015 +0100
@@ -261,12 +261,5 @@
 #endif
 
     bool HasPlugins() const;
-
-    
-    bool Apply(std::list<std::string>& result,
-               ::Orthanc::LookupResource& lookup)
-    {
-      return index_.Apply(result, lookup, area_);
-    }
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/ServerIndex.cpp	Tue Oct 27 20:31:34 2015 +0100
@@ -2115,10 +2115,30 @@
   }
 
 
-  bool ServerIndex::Apply(std::list<std::string>& result,
-                          ::Orthanc::LookupResource& lookup,
-                          IStorageArea& area)
+  void ServerIndex::FindCandidates(std::vector<std::string>& resources,
+                                   std::vector<std::string>& instances,
+                                   ::Orthanc::LookupResource& lookup)
   {
-    return lookup.Apply(result, mutex_, db_, area);
+    boost::mutex::scoped_lock lock(mutex_);
+   
+    std::list<int64_t> tmp;
+    lookup.FindCandidates(tmp, db_);
+
+    resources.resize(tmp.size());
+    instances.resize(tmp.size());
+
+    size_t pos = 0;
+    for (std::list<int64_t>::const_iterator
+           it = tmp.begin(); it != tmp.end(); ++it, pos++)
+    {
+      int64_t instance;
+      if (!Toolbox::FindOneChildInstance(instance, db_, *it, lookup.GetLevel()))
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      resources[pos] = db_.GetPublicId(*it);
+      instances[pos] = db_.GetPublicId(instance);
+    }
   }
 }
--- a/OrthancServer/ServerIndex.h	Tue Oct 27 17:45:05 2015 +0100
+++ b/OrthancServer/ServerIndex.h	Tue Oct 27 20:31:34 2015 +0100
@@ -263,8 +263,8 @@
 
     unsigned int GetDatabaseVersion();
 
-    bool Apply(std::list<std::string>& result,
-               ::Orthanc::LookupResource& lookup,
-               IStorageArea& area);
+    void FindCandidates(std::vector<std::string>& resources,
+                        std::vector<std::string>& instances,
+                        ::Orthanc::LookupResource& lookup);
   };
 }