Mercurial > hg > orthanc
comparison OrthancServer/Search/Compatibility/DatabaseLookup.cpp @ 3053:3f986ce336c8 db-changes
working on Compatibility::DatabaseLookup
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 20 Dec 2018 17:59:16 +0100 |
parents | c7db469bbe8e |
children | 3638de45a08c |
comparison
equal
deleted
inserted
replaced
3052:c7db469bbe8e | 3053:3f986ce336c8 |
---|---|
38 #include "../../../Core/OrthancException.h" | 38 #include "../../../Core/OrthancException.h" |
39 | 39 |
40 namespace Orthanc | 40 namespace Orthanc |
41 { | 41 { |
42 namespace Compatibility | 42 namespace Compatibility |
43 { | 43 { |
44 void DatabaseLookup::ApplyLookupResources(std::vector<std::string>& patientsId, | 44 static void ApplyLevel(SetOfResources& candidates, |
45 const std::vector<DatabaseConstraint>& lookup, | |
46 ResourceType level) | |
47 { | |
48 std::vector<DatabaseConstraint> identifiers; | |
49 | |
50 for (size_t i = 0; i < lookup.size(); i++) | |
51 { | |
52 if (lookup[i].GetLevel() == level && | |
53 lookup[i].IsIdentifier()) | |
54 { | |
55 identifiers.push_back(lookup[i]); | |
56 } | |
57 } | |
58 } | |
59 | |
60 | |
61 static std::string GetOneInstance(IDatabaseWrapper& database, | |
62 int64_t resource, | |
63 ResourceType level) | |
64 { | |
65 for (int i = level; i < ResourceType_Instance; i++) | |
66 { | |
67 assert(database.GetResourceType(resource) == static_cast<ResourceType>(i)); | |
68 | |
69 std::list<int64_t> children; | |
70 database.GetChildrenInternalId(children, resource); | |
71 | |
72 if (children.size() == 0) | |
73 { | |
74 throw OrthancException(ErrorCode_Database); | |
75 } | |
76 | |
77 resource = children.front(); | |
78 } | |
79 | |
80 return database.GetPublicId(resource); | |
81 } | |
82 | |
83 | |
84 void DatabaseLookup::ApplyLookupResources(std::vector<std::string>& resourcesId, | |
45 std::vector<std::string>* instancesId, | 85 std::vector<std::string>* instancesId, |
46 const std::vector<DatabaseConstraint>& lookup, | 86 const std::vector<DatabaseConstraint>& lookup, |
47 ResourceType queryLevel, | 87 ResourceType queryLevel, |
48 size_t limit) | 88 size_t limit) |
49 { | 89 { |
50 throw OrthancException(ErrorCode_NotImplemented); | 90 // This is a re-implementation of |
91 // "../../../Resources/Graveyard/DatabaseOptimizations/LookupResource.cpp" | |
92 | |
93 assert(ResourceType_Patient < ResourceType_Study && | |
94 ResourceType_Study < ResourceType_Series && | |
95 ResourceType_Series < ResourceType_Instance); | |
96 | |
97 ResourceType upperLevel = queryLevel; | |
98 ResourceType lowerLevel = queryLevel; | |
99 | |
100 for (size_t i = 0; i < lookup.size(); i++) | |
101 { | |
102 ResourceType level = lookup[i].GetLevel(); | |
103 | |
104 if (level < upperLevel) | |
105 { | |
106 upperLevel = level; | |
107 } | |
108 | |
109 if (level > lowerLevel) | |
110 { | |
111 lowerLevel = level; | |
112 } | |
113 } | |
114 | |
115 assert(upperLevel <= queryLevel && | |
116 queryLevel <= lowerLevel); | |
117 | |
118 SetOfResources candidates(database_, upperLevel); | |
119 | |
120 for (int level = upperLevel; level <= lowerLevel; level++) | |
121 { | |
122 ApplyLevel(candidates, lookup, static_cast<ResourceType>(level)); | |
123 | |
124 if (level != lowerLevel) | |
125 { | |
126 candidates.GoDown(); | |
127 } | |
128 } | |
129 | |
130 std::list<int64_t> resources; | |
131 candidates.Flatten(resources); | |
132 | |
133 // Climb up, up to queryLevel | |
134 | |
135 for (int level = lowerLevel; level > queryLevel; level--) | |
136 { | |
137 std::list<int64_t> parents; | |
138 for (std::list<int64_t>::const_iterator | |
139 it = resources.begin(); it != resources.end(); ++it) | |
140 { | |
141 int64_t parent; | |
142 if (database_.LookupParent(parent, *it)) | |
143 { | |
144 parents.push_back(parent); | |
145 } | |
146 } | |
147 | |
148 resources.swap(parents); | |
149 } | |
150 | |
151 // Apply the limit, if given | |
152 | |
153 if (limit != 0 && | |
154 resources.size() > limit) | |
155 { | |
156 resources.resize(limit); | |
157 } | |
158 | |
159 // Get the public ID of all the selected resources | |
160 | |
161 resourcesId.resize(resources.size()); | |
162 | |
163 if (instancesId != NULL) | |
164 { | |
165 instancesId->resize(resources.size()); | |
166 } | |
167 | |
168 size_t pos = 0; | |
169 | |
170 for (std::list<int64_t>::const_iterator | |
171 it = resources.begin(); it != resources.end(); ++it, pos++) | |
172 { | |
173 assert(database_.GetResourceType(*it) == queryLevel); | |
174 | |
175 resourcesId[pos] = database_.GetPublicId(*it); | |
176 | |
177 if (instancesId != NULL) | |
178 { | |
179 // Collect one child instance for each of the selected resources | |
180 if (queryLevel == ResourceType_Instance) | |
181 { | |
182 (*instancesId) [pos] = resourcesId[pos]; | |
183 } | |
184 else | |
185 { | |
186 (*instancesId) [pos] = GetOneInstance(database_, *it, queryLevel); | |
187 } | |
188 } | |
189 } | |
51 } | 190 } |
52 } | 191 } |
53 } | 192 } |