# HG changeset patch # User Sebastien Jodogne # Date 1445974294 -3600 # Node ID faf2ecab34726bb2879c20494190cee96ba8c359 # Parent c3d8ec63a179e8294c2e941cac4bf31856df1660 fix diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/OrthancFindRequestHandler.cpp --- 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 tmp; +#endif #if USE_LOOKUP_RESOURCE == 1 - bool finished = context_.Apply(tmp, finder); + std::vector resources, instances; + context_.GetIndex().FindCandidates(resources, instances, finder); - for (std::list::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 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; } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/Search/LookupResource.cpp --- 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& result, - const std::list& 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::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& result, - boost::mutex& databaseMutex, - IDatabaseWrapper& database, - IStorageArea& storageArea) const + void LookupResource::FindCandidates(std::list& result, + IDatabaseWrapper& database) const { - std::list 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& result, - boost::mutex& databaseMutex, - IDatabaseWrapper& database, - IStorageArea& storageArea) const - { - - std::list tmp; - bool finished = Apply(tmp, databaseMutex, database, storageArea); - - result.clear(); - - { - boost::mutex::scoped_lock lock(databaseMutex); - - for (std::list::const_iterator - it = tmp.begin(); it != tmp.end(); ++it) - { - result.push_back(database.GetPublicId(*it)); - } - } - - return finished; + candidates.Flatten(result); } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/Search/LookupResource.h --- 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 -#include namespace Orthanc { @@ -70,7 +69,6 @@ ResourceType level_; Levels levels_; Constraints unoptimizedConstraints_; - size_t maxResults_; bool AddInternal(ResourceType level, std::auto_ptr& constraint); @@ -79,41 +77,25 @@ ResourceType level, IDatabaseWrapper& database) const; - bool ApplyUnoptimizedConstraints(std::list& result, - const std::list& 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& result, + IDatabaseWrapper& database) const; - bool Apply(std::list& result, - boost::mutex& databaseMutex, - IDatabaseWrapper& database, - IStorageArea& storageArea) const; - - bool Apply(std::list& result, - boost::mutex& databaseMutex, - IDatabaseWrapper& database, - IStorageArea& storageArea) const; + bool IsMatch(const Json::Value& dicomAsJson) const; }; } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/Search/SetOfResources.cpp --- 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 children(new Resources); + if (resources_.get() != NULL) + { + std::auto_ptr children(new Resources); - for (Resources::const_iterator it = resources_->begin(); - it != resources_->end(); ++it) - { - std::list tmp; - database_.GetChildrenInternalId(tmp, *it); + for (Resources::const_iterator it = resources_->begin(); + it != resources_->end(); ++it) + { + std::list tmp; + database_.GetChildrenInternalId(tmp, *it); - for (std::list::const_iterator - child = tmp.begin(); child != tmp.end(); ++child) - { - children->insert(*child); + for (std::list::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& result, - size_t maxResults) - { - Flatten(result); - - if (maxResults != 0 && - result.size() > maxResults) - { - std::list::iterator cut = result.begin(); - std::advance(cut, maxResults); - result.erase(cut, result.end()); - return false; - } - else - { - return true; - } - } } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/Search/SetOfResources.h --- 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& result); - bool Flatten(std::list& result, - size_t maxResults); - void Clear() { resources_.reset(NULL); diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/ServerContext.h --- 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& result, - ::Orthanc::LookupResource& lookup) - { - return index_.Apply(result, lookup, area_); - } }; } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/ServerIndex.cpp --- 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& result, - ::Orthanc::LookupResource& lookup, - IStorageArea& area) + void ServerIndex::FindCandidates(std::vector& resources, + std::vector& instances, + ::Orthanc::LookupResource& lookup) { - return lookup.Apply(result, mutex_, db_, area); + boost::mutex::scoped_lock lock(mutex_); + + std::list tmp; + lookup.FindCandidates(tmp, db_); + + resources.resize(tmp.size()); + instances.resize(tmp.size()); + + size_t pos = 0; + for (std::list::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); + } } } diff -r c3d8ec63a179 -r faf2ecab3472 OrthancServer/ServerIndex.h --- 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& result, - ::Orthanc::LookupResource& lookup, - IStorageArea& area); + void FindCandidates(std::vector& resources, + std::vector& instances, + ::Orthanc::LookupResource& lookup); }; }