Mercurial > hg > orthanc
comparison UnitTests/ServerIndex.cpp @ 181:2dece1526c06
simplifying db schema
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Nov 2012 18:09:19 +0100 |
parents | |
children | 93ff5babcaf8 |
comparison
equal
deleted
inserted
replaced
180:626777d01dc4 | 181:2dece1526c06 |
---|---|
1 #include "gtest/gtest.h" | |
2 | |
3 #include <ctype.h> | |
4 | |
5 #include "../Core/SQLite/Connection.h" | |
6 #include "../Core/Compression/ZlibCompressor.h" | |
7 #include "../Core/DicomFormat/DicomTag.h" | |
8 #include "../Core/DicomFormat/DicomArray.h" | |
9 #include "../Core/FileStorage.h" | |
10 #include "../OrthancCppClient/HttpClient.h" | |
11 #include "../Core/HttpServer/HttpHandler.h" | |
12 #include "../Core/OrthancException.h" | |
13 #include "../Core/Toolbox.h" | |
14 #include "../Core/Uuid.h" | |
15 #include "../OrthancServer/FromDcmtkBridge.h" | |
16 #include "../OrthancServer/OrthancInitialization.h" | |
17 #include "../OrthancServer/ServerIndex.h" | |
18 #include "EmbeddedResources.h" | |
19 | |
20 #include <glog/logging.h> | |
21 #include <boost/thread.hpp> | |
22 | |
23 | |
24 namespace Orthanc | |
25 { | |
26 enum CompressionType | |
27 { | |
28 CompressionType_None = 1, | |
29 CompressionType_Zlib = 2 | |
30 }; | |
31 | |
32 enum MetadataType | |
33 { | |
34 MetadataType_Instance_RemoteAet = 1, | |
35 MetadataType_Instance_IndexInSeries = 2, | |
36 MetadataType_Series_ExpectedNumberOfInstances = 3 | |
37 }; | |
38 | |
39 class IServerIndexListener | |
40 { | |
41 public: | |
42 virtual ~IServerIndexListener() | |
43 { | |
44 } | |
45 | |
46 virtual void SignalResourceDeleted(ResourceType type, | |
47 const std::string& parentPublicId) = 0; | |
48 | |
49 virtual void SignalFileDeleted(const std::string& fileUuid) = 0; | |
50 | |
51 }; | |
52 | |
53 namespace Internals | |
54 { | |
55 class SignalFileDeleted : public SQLite::IScalarFunction | |
56 { | |
57 private: | |
58 IServerIndexListener& listener_; | |
59 | |
60 public: | |
61 SignalFileDeleted(IServerIndexListener& listener) : | |
62 listener_(listener) | |
63 { | |
64 } | |
65 | |
66 virtual const char* GetName() const | |
67 { | |
68 return "SignalFileDeleted"; | |
69 } | |
70 | |
71 virtual unsigned int GetCardinality() const | |
72 { | |
73 return 1; | |
74 } | |
75 | |
76 virtual void Compute(SQLite::FunctionContext& context) | |
77 { | |
78 listener_.SignalFileDeleted(context.GetStringValue(0)); | |
79 } | |
80 }; | |
81 | |
82 class SignalResourceDeleted : public SQLite::IScalarFunction | |
83 { | |
84 public: | |
85 virtual const char* GetName() const | |
86 { | |
87 return "SignalResourceDeleted"; | |
88 } | |
89 | |
90 virtual unsigned int GetCardinality() const | |
91 { | |
92 return 2; | |
93 } | |
94 | |
95 virtual void Compute(SQLite::FunctionContext& context) | |
96 { | |
97 LOG(INFO) << "A resource has been removed, of type " | |
98 << context.GetIntValue(0) | |
99 << ", with parent " | |
100 << context.GetIntValue(1); | |
101 } | |
102 }; | |
103 } | |
104 | |
105 | |
106 class ServerIndexHelper | |
107 { | |
108 private: | |
109 IServerIndexListener& listener_; | |
110 SQLite::Connection db_; | |
111 boost::mutex mutex_; | |
112 | |
113 void Open(const std::string& path); | |
114 | |
115 public: | |
116 void SetGlobalProperty(const std::string& name, | |
117 const std::string& value) | |
118 { | |
119 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO GlobalProperties VALUES(?, ?)"); | |
120 s.BindString(0, name); | |
121 s.BindString(1, value); | |
122 s.Run(); | |
123 } | |
124 | |
125 bool FindGlobalProperty(std::string& target, | |
126 const std::string& name) | |
127 { | |
128 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
129 "SELECT value FROM GlobalProperties WHERE name=?"); | |
130 s.BindString(0, name); | |
131 | |
132 if (!s.Step()) | |
133 { | |
134 return false; | |
135 } | |
136 else | |
137 { | |
138 target = s.ColumnString(0); | |
139 return true; | |
140 } | |
141 } | |
142 | |
143 std::string GetGlobalProperty(const std::string& name, | |
144 const std::string& defaultValue = "") | |
145 { | |
146 std::string s; | |
147 if (FindGlobalProperty(s, name)) | |
148 { | |
149 return s; | |
150 } | |
151 else | |
152 { | |
153 return defaultValue; | |
154 } | |
155 } | |
156 | |
157 int64_t CreateResource(const std::string& publicId, | |
158 ResourceType type) | |
159 { | |
160 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
161 s.BindInt(0, type); | |
162 s.BindString(1, publicId); | |
163 s.Run(); | |
164 return db_.GetLastInsertRowId(); | |
165 } | |
166 | |
167 bool FindResource(const std::string& publicId, | |
168 int64_t& id, | |
169 ResourceType& type) | |
170 { | |
171 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
172 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
173 s.BindString(0, publicId); | |
174 | |
175 if (!s.Step()) | |
176 { | |
177 return false; | |
178 } | |
179 else | |
180 { | |
181 id = s.ColumnInt(0); | |
182 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
183 | |
184 // Check whether there is a single resource with this public id | |
185 assert(!s.Step()); | |
186 | |
187 return true; | |
188 } | |
189 } | |
190 | |
191 void AttachChild(int64_t parent, | |
192 int64_t child) | |
193 { | |
194 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
195 s.BindInt(0, parent); | |
196 s.BindInt(1, child); | |
197 s.Run(); | |
198 } | |
199 | |
200 void DeleteResource(int64_t id) | |
201 { | |
202 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
203 s.BindInt(0, id); | |
204 s.Run(); | |
205 } | |
206 | |
207 void SetMetadata(int64_t id, | |
208 MetadataType type, | |
209 const std::string& value) | |
210 { | |
211 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
212 s.BindInt(0, id); | |
213 s.BindInt(1, type); | |
214 s.BindString(2, value); | |
215 s.Run(); | |
216 } | |
217 | |
218 bool FindMetadata(std::string& target, | |
219 int64_t id, | |
220 MetadataType type) | |
221 { | |
222 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
223 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
224 s.BindInt(0, id); | |
225 s.BindInt(1, type); | |
226 | |
227 if (!s.Step()) | |
228 { | |
229 return false; | |
230 } | |
231 else | |
232 { | |
233 target = s.ColumnString(0); | |
234 return true; | |
235 } | |
236 } | |
237 | |
238 std::string GetMetadata(int64_t id, | |
239 MetadataType type, | |
240 const std::string& defaultValue = "") | |
241 { | |
242 std::string s; | |
243 if (FindMetadata(s, id, type)) | |
244 { | |
245 return s; | |
246 } | |
247 else | |
248 { | |
249 return defaultValue; | |
250 } | |
251 } | |
252 | |
253 void AttachFile(int64_t id, | |
254 const std::string& name, | |
255 const std::string& fileUuid, | |
256 size_t uncompressedSize, | |
257 CompressionType compressionType) | |
258 { | |
259 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?)"); | |
260 s.BindInt(0, id); | |
261 s.BindString(1, name); | |
262 s.BindString(2, fileUuid); | |
263 s.BindInt(3, uncompressedSize); | |
264 s.BindInt(4, compressionType); | |
265 s.Run(); | |
266 } | |
267 | |
268 bool FindFile(int64_t id, | |
269 const std::string& name, | |
270 std::string& fileUuid, | |
271 size_t& uncompressedSize, | |
272 CompressionType& compressionType) | |
273 { | |
274 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
275 "SELECT uuid, uncompressedSize, compressionType FROM AttachedFiles WHERE id=? AND name=?"); | |
276 s.BindInt(0, id); | |
277 s.BindString(1, name); | |
278 | |
279 if (!s.Step()) | |
280 { | |
281 return false; | |
282 } | |
283 else | |
284 { | |
285 fileUuid = s.ColumnString(0); | |
286 uncompressedSize = s.ColumnInt(1); | |
287 compressionType = static_cast<CompressionType>(s.ColumnInt(2)); | |
288 return true; | |
289 } | |
290 } | |
291 | |
292 void SetMainDicomTags(int64_t id, | |
293 const DicomMap& tags) | |
294 { | |
295 DicomArray flattened(tags); | |
296 for (size_t i = 0; i < flattened.GetSize(); i++) | |
297 { | |
298 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
299 s.BindInt(0, id); | |
300 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); | |
301 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); | |
302 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); | |
303 s.Run(); | |
304 } | |
305 } | |
306 | |
307 void GetMainDicomTags(DicomMap& map, | |
308 int64_t id) | |
309 { | |
310 map.Clear(); | |
311 | |
312 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
313 s.BindInt(0, id); | |
314 while (s.Step()) | |
315 { | |
316 map.SetValue(s.ColumnInt(1), | |
317 s.ColumnInt(2), | |
318 s.ColumnString(3)); | |
319 } | |
320 } | |
321 | |
322 int64_t GetTableRecordCount(const std::string& table) | |
323 { | |
324 char buf[128]; | |
325 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
326 SQLite::Statement s(db_, buf); | |
327 | |
328 assert(s.Step()); | |
329 int64_t c = s.ColumnInt(0); | |
330 assert(!s.Step()); | |
331 | |
332 return c; | |
333 } | |
334 | |
335 ServerIndexHelper(const std::string& path, | |
336 IServerIndexListener& listener) : | |
337 listener_(listener) | |
338 { | |
339 Open(path); | |
340 } | |
341 | |
342 ServerIndexHelper(IServerIndexListener& listener) : | |
343 listener_(listener) | |
344 { | |
345 Open(""); | |
346 } | |
347 }; | |
348 | |
349 | |
350 | |
351 void ServerIndexHelper::Open(const std::string& path) | |
352 { | |
353 if (path == "") | |
354 { | |
355 db_.OpenInMemory(); | |
356 } | |
357 else | |
358 { | |
359 db_.Open(path); | |
360 } | |
361 | |
362 if (!db_.DoesTableExist("GlobalProperties")) | |
363 { | |
364 LOG(INFO) << "Creating the database"; | |
365 std::string query; | |
366 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE_2); | |
367 db_.Execute(query); | |
368 } | |
369 | |
370 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
371 db_.Register(new Internals::SignalResourceDeleted); | |
372 } | |
373 | |
374 | |
375 class ServerIndexListener : public IServerIndexListener | |
376 { | |
377 public: | |
378 virtual void SignalResourceDeleted(ResourceType type, | |
379 const std::string& parentPublicId) | |
380 { | |
381 } | |
382 | |
383 virtual void SignalFileDeleted(const std::string& fileUuid) | |
384 { | |
385 LOG(INFO) << "A file must be removed: " << fileUuid; | |
386 } | |
387 }; | |
388 | |
389 /* | |
390 class ServerIndex2 | |
391 { | |
392 private: | |
393 ServerIndexListener listener_; | |
394 ServerIndexHelper helper_; | |
395 | |
396 void Open(const std::string& storagePath) | |
397 { | |
398 boost::filesystem::path p = storagePath; | |
399 | |
400 try | |
401 { | |
402 boost::filesystem::create_directories(storagePath); | |
403 } | |
404 catch (boost::filesystem::filesystem_error) | |
405 { | |
406 } | |
407 | |
408 p /= "index"; | |
409 } | |
410 | |
411 public: | |
412 ServerIndexHelper(const std::string& storagePath) : | |
413 helper_(storagePath) | |
414 { | |
415 Open(storagePath); | |
416 } | |
417 }; | |
418 */ | |
419 } | |
420 | |
421 | |
422 | |
423 using namespace Orthanc; | |
424 | |
425 TEST(ServerIndexHelper, Simple) | |
426 { | |
427 ServerIndexListener listener; | |
428 /*Toolbox::RemoveFile("toto"); | |
429 ServerIndexHelper index("toto", listener);*/ | |
430 ServerIndexHelper index(listener); | |
431 | |
432 LOG(WARNING) << "ok"; | |
433 | |
434 int64_t a[] = { | |
435 index.CreateResource("a", ResourceType_Patient), | |
436 index.CreateResource("b", ResourceType_Study), | |
437 index.CreateResource("c", ResourceType_Series), | |
438 index.CreateResource("d", ResourceType_Instance), | |
439 index.CreateResource("e", ResourceType_Instance), | |
440 index.CreateResource("f", ResourceType_Instance), | |
441 index.CreateResource("g", ResourceType_Study) | |
442 }; | |
443 | |
444 index.SetGlobalProperty("Hello", "World"); | |
445 | |
446 index.AttachChild(a[0], a[1]); | |
447 index.AttachChild(a[1], a[2]); | |
448 index.AttachChild(a[2], a[3]); | |
449 index.AttachChild(a[2], a[4]); | |
450 index.AttachChild(a[6], a[5]); | |
451 index.AttachFile(a[4], "_json", "my json file", 42, CompressionType_Zlib); | |
452 index.AttachFile(a[4], "_dicom", "my dicom file", 42, CompressionType_None); | |
453 index.SetMetadata(a[4], MetadataType_Instance_RemoteAet, "PINNACLE"); | |
454 | |
455 DicomMap m; | |
456 m.SetValue(0x0010, 0x0010, "PatientName"); | |
457 index.SetMainDicomTags(a[3], m); | |
458 | |
459 int64_t b; | |
460 ResourceType t; | |
461 ASSERT_TRUE(index.FindResource("g", b, t)); | |
462 ASSERT_EQ(7, b); | |
463 ASSERT_EQ(ResourceType_Study, t); | |
464 | |
465 std::string s; | |
466 | |
467 ASSERT_TRUE(index.FindMetadata(s, a[4], MetadataType_Instance_RemoteAet)); | |
468 ASSERT_FALSE(index.FindMetadata(s, a[4], MetadataType_Instance_IndexInSeries)); | |
469 ASSERT_EQ("PINNACLE", s); | |
470 ASSERT_EQ("PINNACLE", index.GetMetadata(a[4], MetadataType_Instance_RemoteAet)); | |
471 ASSERT_EQ("None", index.GetMetadata(a[4], MetadataType_Instance_IndexInSeries, "None")); | |
472 | |
473 ASSERT_TRUE(index.FindGlobalProperty(s, "Hello")); | |
474 ASSERT_FALSE(index.FindGlobalProperty(s, "Hello2")); | |
475 ASSERT_EQ("World", s); | |
476 ASSERT_EQ("World", index.GetGlobalProperty("Hello")); | |
477 ASSERT_EQ("None", index.GetGlobalProperty("Hello2", "None")); | |
478 | |
479 size_t us; | |
480 CompressionType ct; | |
481 ASSERT_TRUE(index.FindFile(a[4], "_json", s, us, ct)); | |
482 ASSERT_EQ("my json file", s); | |
483 ASSERT_EQ(42, us); | |
484 ASSERT_EQ(CompressionType_Zlib, ct); | |
485 | |
486 ASSERT_EQ(7, index.GetTableRecordCount("Resources")); | |
487 ASSERT_EQ(2, index.GetTableRecordCount("AttachedFiles")); | |
488 ASSERT_EQ(1, index.GetTableRecordCount("Metadata")); | |
489 ASSERT_EQ(1, index.GetTableRecordCount("MainDicomTags")); | |
490 index.DeleteResource(a[0]); | |
491 ASSERT_EQ(2, index.GetTableRecordCount("Resources")); | |
492 ASSERT_EQ(0, index.GetTableRecordCount("Metadata")); | |
493 ASSERT_EQ(0, index.GetTableRecordCount("AttachedFiles")); | |
494 ASSERT_EQ(0, index.GetTableRecordCount("MainDicomTags")); | |
495 index.DeleteResource(a[6]); | |
496 ASSERT_EQ(0, index.GetTableRecordCount("Resources")); | |
497 ASSERT_EQ(1, index.GetTableRecordCount("GlobalProperties")); | |
498 } |