changeset 3033:5da6d1063d8f db-changes

effectively replacing LookupResource by DatabaseLookup in searches
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Dec 2018 13:58:28 +0100
parents 19ebb606910d
children 54e422fe31ce
files OrthancServer/OrthancFindRequestHandler.cpp OrthancServer/OrthancRestApi/OrthancRestResources.cpp OrthancServer/SQLiteDatabaseWrapper.cpp OrthancServer/Search/DatabaseLookup.cpp OrthancServer/Search/DatabaseLookup.h OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/ServerIndex.cpp OrthancServer/ServerToolbox.cpp
diffstat 9 files changed, 47 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancFindRequestHandler.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/OrthancFindRequestHandler.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -614,8 +614,7 @@
      * Build up the query object.
      **/
 
-    LookupResource lookup(level);
-    DatabaseLookup lookup2;
+    DatabaseLookup lookup;
 
     bool caseSensitivePN;
 
@@ -655,8 +654,7 @@
           sensitive = caseSensitivePN;
         }
 
-        lookup.AddDicomConstraint(tag, value, sensitive);
-        lookup2.AddDicomConstraint(tag, value, sensitive, true /* mandatory */);
+        lookup.AddDicomConstraint(tag, value, sensitive, true /* mandatory */);
       }
       else
       {
@@ -674,7 +672,7 @@
 
 
     LookupVisitor visitor(answers, context_, level, *filteredInput, sequencesToReturn);
-    context_.Apply(visitor, lookup, lookup2, 0 /* "since" is not relevant to C-FIND */, limit);
+    context_.Apply(visitor, lookup, level, 0 /* "since" is not relevant to C-FIND */, limit);
   }
 
 
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -1401,10 +1401,9 @@
         since = static_cast<size_t>(tmp);
       }
 
-      std::string level = request[KEY_LEVEL].asString();
+      ResourceType level = StringToResourceType(request[KEY_LEVEL].asCString());
 
-      LookupResource query(StringToResourceType(level.c_str()));
-      DatabaseLookup query2;
+      DatabaseLookup query;
 
       Json::Value::Members members = request[KEY_QUERY].getMemberNames();
       for (size_t i = 0; i < members.size(); i++)
@@ -1415,17 +1414,14 @@
                                  "Tag \"" + members[i] + "\" should be associated with a string");
         }
 
-        query.AddDicomConstraint(FromDcmtkBridge::ParseTag(members[i]), 
-                                 request[KEY_QUERY][members[i]].asString(),
-                                 caseSensitive);
-        query2.AddRestConstraint(FromDcmtkBridge::ParseTag(members[i]), 
-                                 request[KEY_QUERY][members[i]].asString(),
-                                 caseSensitive, true);
+        query.AddRestConstraint(FromDcmtkBridge::ParseTag(members[i]), 
+                                request[KEY_QUERY][members[i]].asString(),
+                                caseSensitive, true);
       }
 
       FindVisitor visitor;
-      context.Apply(visitor, query, query2, since, limit);
-      visitor.Answer(call.GetOutput(), context.GetIndex(), query.GetLevel(), expand);
+      context.Apply(visitor, query, level, since, limit);
+      visitor.Answer(call.GetOutput(), context.GetIndex(), level, expand);
     }
   }
 
--- a/OrthancServer/SQLiteDatabaseWrapper.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/SQLiteDatabaseWrapper.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -1503,12 +1503,6 @@
                                                    ResourceType queryLevel,
                                                    size_t limit)
   {
-    for (size_t i = 0; i < lookup.size(); i++)
-    {
-      std::cout << i << ": " << lookup[i].GetTag() << " - " << EnumerationToString(lookup[i].GetLevel());
-      std::cout << std::endl;
-    }
-    
     assert(ResourceType_Patient < ResourceType_Study &&
            ResourceType_Study < ResourceType_Series &&
            ResourceType_Series < ResourceType_Instance);
@@ -1531,8 +1525,6 @@
       }
     }
     
-    printf("ICI 2: [%s] -> [%s]\n", EnumerationToString(upperLevel), EnumerationToString(lowerLevel));
-    
     {
       SQLite::Statement s(db_, SQLITE_FROM_HERE, "DROP TABLE IF EXISTS Lookup");
       s.Run();
@@ -1592,13 +1584,10 @@
         sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
       }
 
-      printf("[%s]\n", sql.c_str());
-
       SQLite::Statement s(db_, sql);
 
       for (size_t i = 0; i < parameters.size(); i++)
       {
-        printf("   %lu = '%s'\n", i, parameters[i].c_str());
         s.BindString(i, parameters[i]);
       }
 
--- a/OrthancServer/Search/DatabaseLookup.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/Search/DatabaseLookup.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -76,7 +76,7 @@
   }
 
 
-  bool DatabaseLookup::IsMatch(const DicomMap& value)
+  bool DatabaseLookup::IsMatch(const DicomMap& value) const
   {
     for (size_t i = 0; i < constraints_.size(); i++)
     {
@@ -227,4 +227,24 @@
     AddDicomConstraintInternal(tag, FromDcmtkBridge::LookupValueRepresentation(tag),
                                dicomQuery, caseSensitive, mandatoryTag);
   }
+
+
+  bool DatabaseLookup::HasOnlyMainDicomTags() const
+  {
+    std::set<DicomTag> mainTags;
+    DicomMap::GetMainDicomTags(mainTags);
+
+    for (size_t i = 0; i < constraints_.size(); i++)
+    {
+      assert(constraints_[i] != NULL);
+      
+      if (mainTags.find(constraints_[i]->GetTag()) == mainTags.end())
+      {
+        // This is not a main DICOM tag
+        return false;
+      }
+    }
+
+    return true;
+  }
 }
--- a/OrthancServer/Search/DatabaseLookup.h	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/Search/DatabaseLookup.h	Wed Dec 19 13:58:28 2018 +0100
@@ -68,7 +68,7 @@
 
     void AddConstraint(DicomTagConstraint* constraint);  // Takes ownership
 
-    bool IsMatch(const DicomMap& value);
+    bool IsMatch(const DicomMap& value) const;
 
     void AddDicomConstraint(const DicomTag& tag,
                             const std::string& dicomQuery,
@@ -79,5 +79,7 @@
                            const std::string& dicomQuery,
                            bool caseSensitive,
                            bool mandatoryTag);
+
+    bool HasOnlyMainDicomTags() const;
   };
 }
--- a/OrthancServer/ServerContext.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/ServerContext.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -774,8 +774,8 @@
 
 
   void ServerContext::Apply(ILookupVisitor& visitor,
-                            const ::Orthanc::LookupResource& lookup,
-                            const DatabaseLookup& lookup2,
+                            const DatabaseLookup& lookup,
+                            ResourceType queryLevel,
                             size_t since,
                             size_t limit)
   {
@@ -807,7 +807,7 @@
                                "should be \"Always\", \"Never\" or \"Answers\": " + value);
       }
 
-      if (lookup.GetLevel() == ResourceType_Instance)
+      if (queryLevel == ResourceType_Instance)
       {
         databaseLimit = lock.GetConfiguration().GetUnsignedIntegerParameter("LimitFindInstances", 0);
       }
@@ -817,42 +817,14 @@
       }
     }      
 
-
     std::vector<std::string> resources, instances;
-    GetIndex().FindCandidates(resources, instances, lookup);
-
-    bool complete = true;
-
-#if 1
-    {
-      std::vector<std::string> resources2, instances2;
-
-      size_t lookupLimit = (databaseLimit == 0 ? 0 : databaseLimit + 1);      
-      GetIndex().ApplyLookupResources(resources2, instances2, lookup2, lookup.GetLevel(), lookupLimit);
 
-      if (databaseLimit != 0 &&
-          resources2.size() > databaseLimit)
-      {
-        complete = false;
-      }
-      
-      // Sanity checks
-      std::set<std::string> r;
-      for (size_t i = 0; i < resources2.size(); i++)
-      {
-        r.insert(resources2[i]);
-      }
+    const size_t lookupLimit = (databaseLimit == 0 ? 0 : databaseLimit + 1);      
+    GetIndex().ApplyLookupResources(resources, instances, lookup, queryLevel, lookupLimit);
 
-      printf("%d %d\n", resources2.size(), resources.size());
-      assert(resources2.size() >= resources.size());
-      
-      for (size_t i = 0; i < resources.size(); i++)
-      {
-        assert(r.find(resources[i]) != r.end());
-      }
-    }
-#endif
-    
+    bool complete = (databaseLimit == 0 ||
+                     resources.size() > databaseLimit);
+
     LOG(INFO) << "Number of candidate resources after fast DB filtering on main DICOM tags: " << resources.size();
 
     assert(resources.size() == instances.size());
--- a/OrthancServer/ServerContext.h	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/ServerContext.h	Wed Dec 19 13:58:28 2018 +0100
@@ -363,8 +363,8 @@
     void Stop();
 
     void Apply(ILookupVisitor& visitor,
-               const ::Orthanc::LookupResource& lookup,
-               const DatabaseLookup& lookup2,
+               const DatabaseLookup& lookup,
+               ResourceType queryLevel,
                size_t since,
                size_t limit);
 
--- a/OrthancServer/ServerIndex.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/ServerIndex.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -2556,7 +2556,6 @@
   }
 
 
-
   void ServerIndex::NormalizeLookup(std::vector<DatabaseConstraint>& target,
                                     const DatabaseLookup& source,
                                     ResourceType queryLevel) const
--- a/OrthancServer/ServerToolbox.cpp	Wed Dec 19 13:10:48 2018 +0100
+++ b/OrthancServer/ServerToolbox.cpp	Wed Dec 19 13:58:28 2018 +0100
@@ -170,6 +170,9 @@
 
       for (size_t i = 0; i < size; i++)
       {
+        // The identifiers tags are a subset of the main DICOM tags
+        assert(DicomMap::IsMainDicomTag(tags[i]));
+        
         const DicomValue* value = map.TestAndGetValue(tags[i]);
         if (value != NULL &&
             !value->IsNull() &&