Mercurial > hg > orthanc-databases
comparison Framework/Plugins/IndexBackend.cpp @ 201:42990b2dd51b
create IDatabaseBackendOutput only if needed
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 18 Mar 2021 16:51:51 +0100 |
parents | 3236894320d6 |
children | 2089d4071408 |
comparison
equal
deleted
inserted
replaced
200:30b210616f4f | 201:42990b2dd51b |
---|---|
169 } | 169 } |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 | 173 |
174 void IndexBackend::ReadChangesInternal(bool& done, | 174 void IndexBackend::ReadChangesInternal(OrthancPlugins::IDatabaseBackendOutput& output, |
175 bool& done, | |
175 DatabaseManager::CachedStatement& statement, | 176 DatabaseManager::CachedStatement& statement, |
176 const Dictionary& args, | 177 const Dictionary& args, |
177 uint32_t maxResults) | 178 uint32_t maxResults) |
178 { | 179 { |
179 statement.Execute(args); | 180 statement.Execute(args); |
181 uint32_t count = 0; | 182 uint32_t count = 0; |
182 | 183 |
183 while (count < maxResults && | 184 while (count < maxResults && |
184 !statement.IsDone()) | 185 !statement.IsDone()) |
185 { | 186 { |
186 GetOutput().AnswerChange( | 187 output.AnswerChange( |
187 ReadInteger64(statement, 0), | 188 ReadInteger64(statement, 0), |
188 ReadInteger32(statement, 1), | 189 ReadInteger32(statement, 1), |
189 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 3)), | 190 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 3)), |
190 GetPublicId(ReadInteger64(statement, 2)), | 191 GetPublicId(ReadInteger64(statement, 2)), |
191 ReadString(statement, 4)); | 192 ReadString(statement, 4)); |
197 done = (count < maxResults || | 198 done = (count < maxResults || |
198 statement.IsDone()); | 199 statement.IsDone()); |
199 } | 200 } |
200 | 201 |
201 | 202 |
202 void IndexBackend::ReadExportedResourcesInternal(bool& done, | 203 void IndexBackend::ReadExportedResourcesInternal(OrthancPlugins::IDatabaseBackendOutput& output, |
204 bool& done, | |
203 DatabaseManager::CachedStatement& statement, | 205 DatabaseManager::CachedStatement& statement, |
204 const Dictionary& args, | 206 const Dictionary& args, |
205 uint32_t maxResults) | 207 uint32_t maxResults) |
206 { | 208 { |
207 statement.Execute(args); | 209 statement.Execute(args); |
214 int64_t seq = ReadInteger64(statement, 0); | 216 int64_t seq = ReadInteger64(statement, 0); |
215 OrthancPluginResourceType resourceType = | 217 OrthancPluginResourceType resourceType = |
216 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 1)); | 218 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 1)); |
217 std::string publicId = ReadString(statement, 2); | 219 std::string publicId = ReadString(statement, 2); |
218 | 220 |
219 GetOutput().AnswerExportedResource(seq, | 221 output.AnswerExportedResource(seq, |
220 resourceType, | 222 resourceType, |
221 publicId, | 223 publicId, |
222 ReadString(statement, 3), // modality | 224 ReadString(statement, 3), // modality |
223 ReadString(statement, 8), // date | 225 ReadString(statement, 8), // date |
224 ReadString(statement, 4), // patient ID | 226 ReadString(statement, 4), // patient ID |
225 ReadString(statement, 5), // study instance UID | 227 ReadString(statement, 5), // study instance UID |
226 ReadString(statement, 6), // series instance UID | 228 ReadString(statement, 6), // series instance UID |
227 ReadString(statement, 7)); // sop instance UID | 229 ReadString(statement, 7)); // sop instance UID |
228 | 230 |
229 statement.Next(); | 231 statement.Next(); |
230 count++; | 232 count++; |
231 } | 233 } |
232 | 234 |
233 done = (count < maxResults || | 235 done = (count < maxResults || |
253 | 255 |
254 statement.Execute(); | 256 statement.Execute(); |
255 } | 257 } |
256 | 258 |
257 | 259 |
258 void IndexBackend::SignalDeletedFiles() | 260 void IndexBackend::SignalDeletedFiles(OrthancPlugins::IDatabaseBackendOutput& output) |
259 { | 261 { |
260 DatabaseManager::CachedStatement statement( | 262 DatabaseManager::CachedStatement statement( |
261 STATEMENT_FROM_HERE, manager_, | 263 STATEMENT_FROM_HERE, manager_, |
262 "SELECT * FROM DeletedFiles"); | 264 "SELECT * FROM DeletedFiles"); |
263 | 265 |
268 { | 270 { |
269 std::string a = ReadString(statement, 0); | 271 std::string a = ReadString(statement, 0); |
270 std::string b = ReadString(statement, 5); | 272 std::string b = ReadString(statement, 5); |
271 std::string c = ReadString(statement, 6); | 273 std::string c = ReadString(statement, 6); |
272 | 274 |
273 GetOutput().SignalDeletedAttachment(a.c_str(), | 275 output.SignalDeletedAttachment(a.c_str(), |
274 ReadInteger32(statement, 1), | 276 ReadInteger32(statement, 1), |
275 ReadInteger64(statement, 3), | 277 ReadInteger64(statement, 3), |
276 b.c_str(), | 278 b.c_str(), |
277 ReadInteger32(statement, 4), | 279 ReadInteger32(statement, 4), |
278 ReadInteger64(statement, 2), | 280 ReadInteger64(statement, 2), |
279 c.c_str()); | 281 c.c_str()); |
280 | 282 |
281 statement.Next(); | 283 statement.Next(); |
282 } | 284 } |
283 } | 285 } |
284 | 286 |
285 | 287 |
286 void IndexBackend::SignalDeletedResources() | 288 void IndexBackend::SignalDeletedResources(OrthancPlugins::IDatabaseBackendOutput& output) |
287 { | 289 { |
288 DatabaseManager::CachedStatement statement( | 290 DatabaseManager::CachedStatement statement( |
289 STATEMENT_FROM_HERE, manager_, | 291 STATEMENT_FROM_HERE, manager_, |
290 "SELECT * FROM DeletedResources"); | 292 "SELECT * FROM DeletedResources"); |
291 | 293 |
292 statement.SetReadOnly(true); | 294 statement.SetReadOnly(true); |
293 statement.Execute(); | 295 statement.Execute(); |
294 | 296 |
295 while (!statement.IsDone()) | 297 while (!statement.IsDone()) |
296 { | 298 { |
297 GetOutput().SignalDeletedResource( | 299 output.SignalDeletedResource( |
298 ReadString(statement, 1), | 300 ReadString(statement, 1), |
299 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); | 301 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); |
300 | 302 |
301 statement.Next(); | 303 statement.Next(); |
302 } | 304 } |
303 } | 305 } |
304 | 306 |
305 | 307 |
306 IndexBackend::IndexBackend(IDatabaseFactory* factory) : | 308 IndexBackend::IndexBackend(OrthancPluginContext* context, |
309 IDatabaseFactory* factory) : | |
310 context_(context), | |
307 manager_(factory) | 311 manager_(factory) |
308 { | 312 { |
309 } | 313 } |
310 | 314 |
311 | 315 |
316 void IndexBackend::SetOutputFactory(OrthancPlugins::IDatabaseBackendOutput::IFactory* factory) | |
317 { | |
318 if (factory == NULL) | |
319 { | |
320 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
321 } | |
322 else if (outputFactory_.get() != NULL) | |
323 { | |
324 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
325 } | |
326 else | |
327 { | |
328 outputFactory_.reset(factory); | |
329 } | |
330 } | |
331 | |
332 | |
333 OrthancPlugins::IDatabaseBackendOutput* IndexBackend::CreateOutput() | |
334 { | |
335 if (outputFactory_.get() == NULL) | |
336 { | |
337 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
338 } | |
339 else | |
340 { | |
341 return outputFactory_->CreateOutput(); | |
342 } | |
343 } | |
344 | |
345 | |
312 void IndexBackend::AddAttachment(int64_t id, | 346 void IndexBackend::AddAttachment(int64_t id, |
313 const OrthancPluginAttachment& attachment) | 347 const OrthancPluginAttachment& attachment) |
314 { | 348 { |
315 DatabaseManager::CachedStatement statement( | 349 DatabaseManager::CachedStatement statement( |
316 STATEMENT_FROM_HERE, manager_, | 350 STATEMENT_FROM_HERE, manager_, |
376 | 410 |
377 statement.Execute(); | 411 statement.Execute(); |
378 } | 412 } |
379 | 413 |
380 | 414 |
381 void IndexBackend::DeleteAttachment(int64_t id, | 415 void IndexBackend::DeleteAttachment(OrthancPlugins::IDatabaseBackendOutput& output, |
416 int64_t id, | |
382 int32_t attachment) | 417 int32_t attachment) |
383 { | 418 { |
384 ClearDeletedFiles(); | 419 ClearDeletedFiles(); |
385 | 420 |
386 { | 421 { |
396 args.SetIntegerValue("type", static_cast<int>(attachment)); | 431 args.SetIntegerValue("type", static_cast<int>(attachment)); |
397 | 432 |
398 statement.Execute(args); | 433 statement.Execute(args); |
399 } | 434 } |
400 | 435 |
401 SignalDeletedFiles(); | 436 SignalDeletedFiles(output); |
402 } | 437 } |
403 | 438 |
404 | 439 |
405 void IndexBackend::DeleteMetadata(int64_t id, | 440 void IndexBackend::DeleteMetadata(int64_t id, |
406 int32_t metadataType) | 441 int32_t metadataType) |
418 | 453 |
419 statement.Execute(args); | 454 statement.Execute(args); |
420 } | 455 } |
421 | 456 |
422 | 457 |
423 void IndexBackend::DeleteResource(int64_t id) | 458 void IndexBackend::DeleteResource(OrthancPlugins::IDatabaseBackendOutput& output, |
459 int64_t id) | |
424 { | 460 { |
425 assert(manager_.GetDialect() != Dialect_MySQL); | 461 assert(manager_.GetDialect() != Dialect_MySQL); |
426 | 462 |
427 ClearDeletedFiles(); | 463 ClearDeletedFiles(); |
428 ClearDeletedResources(); | 464 ClearDeletedResources(); |
456 | 492 |
457 statement.Execute(); | 493 statement.Execute(); |
458 | 494 |
459 if (!statement.IsDone()) | 495 if (!statement.IsDone()) |
460 { | 496 { |
461 GetOutput().SignalRemainingAncestor( | 497 output.SignalRemainingAncestor( |
462 ReadString(statement, 1), | 498 ReadString(statement, 1), |
463 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); | 499 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); |
464 | 500 |
465 // There is at most 1 remaining ancestor | 501 // There is at most 1 remaining ancestor |
466 assert((statement.Next(), statement.IsDone())); | 502 assert((statement.Next(), statement.IsDone())); |
467 } | 503 } |
468 } | 504 } |
469 | 505 |
470 SignalDeletedFiles(); | 506 SignalDeletedFiles(output); |
471 SignalDeletedResources(); | 507 SignalDeletedResources(output); |
472 } | 508 } |
473 | 509 |
474 | 510 |
475 void IndexBackend::GetAllInternalIds(std::list<int64_t>& target, | 511 void IndexBackend::GetAllInternalIds(std::list<int64_t>& target, |
476 OrthancPluginResourceType resourceType) | 512 OrthancPluginResourceType resourceType) |
530 ReadListOfStrings(target, statement, args); | 566 ReadListOfStrings(target, statement, args); |
531 } | 567 } |
532 | 568 |
533 | 569 |
534 /* Use GetOutput().AnswerChange() */ | 570 /* Use GetOutput().AnswerChange() */ |
535 void IndexBackend::GetChanges(bool& done /*out*/, | 571 void IndexBackend::GetChanges(OrthancPlugins::IDatabaseBackendOutput& output, |
572 bool& done /*out*/, | |
536 int64_t since, | 573 int64_t since, |
537 uint32_t maxResults) | 574 uint32_t maxResults) |
538 { | 575 { |
539 DatabaseManager::CachedStatement statement( | 576 DatabaseManager::CachedStatement statement( |
540 STATEMENT_FROM_HERE, manager_, | 577 STATEMENT_FROM_HERE, manager_, |
546 | 583 |
547 Dictionary args; | 584 Dictionary args; |
548 args.SetIntegerValue("limit", maxResults + 1); | 585 args.SetIntegerValue("limit", maxResults + 1); |
549 args.SetIntegerValue("since", since); | 586 args.SetIntegerValue("since", since); |
550 | 587 |
551 ReadChangesInternal(done, statement, args, maxResults); | 588 ReadChangesInternal(output, done, statement, args, maxResults); |
552 } | 589 } |
553 | 590 |
554 | 591 |
555 void IndexBackend::GetChildrenInternalId(std::list<int64_t>& target /*out*/, | 592 void IndexBackend::GetChildrenInternalId(std::list<int64_t>& target /*out*/, |
556 int64_t id) | 593 int64_t id) |
587 ReadListOfStrings(target, statement, args); | 624 ReadListOfStrings(target, statement, args); |
588 } | 625 } |
589 | 626 |
590 | 627 |
591 /* Use GetOutput().AnswerExportedResource() */ | 628 /* Use GetOutput().AnswerExportedResource() */ |
592 void IndexBackend::GetExportedResources(bool& done /*out*/, | 629 void IndexBackend::GetExportedResources(OrthancPlugins::IDatabaseBackendOutput& output, |
630 bool& done /*out*/, | |
593 int64_t since, | 631 int64_t since, |
594 uint32_t maxResults) | 632 uint32_t maxResults) |
595 { | 633 { |
596 DatabaseManager::CachedStatement statement( | 634 DatabaseManager::CachedStatement statement( |
597 STATEMENT_FROM_HERE, manager_, | 635 STATEMENT_FROM_HERE, manager_, |
603 | 641 |
604 Dictionary args; | 642 Dictionary args; |
605 args.SetIntegerValue("limit", maxResults + 1); | 643 args.SetIntegerValue("limit", maxResults + 1); |
606 args.SetIntegerValue("since", since); | 644 args.SetIntegerValue("since", since); |
607 | 645 |
608 ReadExportedResourcesInternal(done, statement, args, maxResults); | 646 ReadExportedResourcesInternal(output, done, statement, args, maxResults); |
609 } | 647 } |
610 | 648 |
611 | 649 |
612 /* Use GetOutput().AnswerChange() */ | 650 /* Use GetOutput().AnswerChange() */ |
613 void IndexBackend::GetLastChange() | 651 void IndexBackend::GetLastChange(OrthancPlugins::IDatabaseBackendOutput& output) |
614 { | 652 { |
615 DatabaseManager::CachedStatement statement( | 653 DatabaseManager::CachedStatement statement( |
616 STATEMENT_FROM_HERE, manager_, | 654 STATEMENT_FROM_HERE, manager_, |
617 "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); | 655 "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); |
618 | 656 |
619 statement.SetReadOnly(true); | 657 statement.SetReadOnly(true); |
620 | 658 |
621 Dictionary args; | 659 Dictionary args; |
622 | 660 |
623 bool done; // Ignored | 661 bool done; // Ignored |
624 ReadChangesInternal(done, statement, args, 1); | 662 ReadChangesInternal(output, done, statement, args, 1); |
625 } | 663 } |
626 | 664 |
627 | 665 |
628 /* Use GetOutput().AnswerExportedResource() */ | 666 /* Use GetOutput().AnswerExportedResource() */ |
629 void IndexBackend::GetLastExportedResource() | 667 void IndexBackend::GetLastExportedResource(OrthancPlugins::IDatabaseBackendOutput& output) |
630 { | 668 { |
631 DatabaseManager::CachedStatement statement( | 669 DatabaseManager::CachedStatement statement( |
632 STATEMENT_FROM_HERE, manager_, | 670 STATEMENT_FROM_HERE, manager_, |
633 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); | 671 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); |
634 | 672 |
635 statement.SetReadOnly(true); | 673 statement.SetReadOnly(true); |
636 | 674 |
637 Dictionary args; | 675 Dictionary args; |
638 | 676 |
639 bool done; // Ignored | 677 bool done; // Ignored |
640 ReadExportedResourcesInternal(done, statement, args, 1); | 678 ReadExportedResourcesInternal(output, done, statement, args, 1); |
641 } | 679 } |
642 | 680 |
643 | 681 |
644 /* Use GetOutput().AnswerDicomTag() */ | 682 /* Use GetOutput().AnswerDicomTag() */ |
645 void IndexBackend::GetMainDicomTags(int64_t id) | 683 void IndexBackend::GetMainDicomTags(OrthancPlugins::IDatabaseBackendOutput& output, |
684 int64_t id) | |
646 { | 685 { |
647 DatabaseManager::CachedStatement statement( | 686 DatabaseManager::CachedStatement statement( |
648 STATEMENT_FROM_HERE, manager_, | 687 STATEMENT_FROM_HERE, manager_, |
649 "SELECT * FROM MainDicomTags WHERE id=${id}"); | 688 "SELECT * FROM MainDicomTags WHERE id=${id}"); |
650 | 689 |
656 | 695 |
657 statement.Execute(args); | 696 statement.Execute(args); |
658 | 697 |
659 while (!statement.IsDone()) | 698 while (!statement.IsDone()) |
660 { | 699 { |
661 GetOutput().AnswerDicomTag(static_cast<uint16_t>(ReadInteger64(statement, 1)), | 700 output.AnswerDicomTag(static_cast<uint16_t>(ReadInteger64(statement, 1)), |
662 static_cast<uint16_t>(ReadInteger64(statement, 2)), | 701 static_cast<uint16_t>(ReadInteger64(statement, 2)), |
663 ReadString(statement, 3)); | 702 ReadString(statement, 3)); |
664 statement.Next(); | 703 statement.Next(); |
665 } | 704 } |
666 } | 705 } |
667 | 706 |
668 | 707 |
958 statement.Execute(args); | 997 statement.Execute(args); |
959 } | 998 } |
960 | 999 |
961 | 1000 |
962 /* Use GetOutput().AnswerAttachment() */ | 1001 /* Use GetOutput().AnswerAttachment() */ |
963 bool IndexBackend::LookupAttachment(int64_t id, | 1002 bool IndexBackend::LookupAttachment(OrthancPlugins::IDatabaseBackendOutput& output, |
1003 int64_t id, | |
964 int32_t contentType) | 1004 int32_t contentType) |
965 { | 1005 { |
966 DatabaseManager::CachedStatement statement( | 1006 DatabaseManager::CachedStatement statement( |
967 STATEMENT_FROM_HERE, manager_, | 1007 STATEMENT_FROM_HERE, manager_, |
968 "SELECT uuid, uncompressedSize, compressionType, compressedSize, " | 1008 "SELECT uuid, uncompressedSize, compressionType, compressedSize, " |
982 { | 1022 { |
983 return false; | 1023 return false; |
984 } | 1024 } |
985 else | 1025 else |
986 { | 1026 { |
987 GetOutput().AnswerAttachment(ReadString(statement, 0), | 1027 output.AnswerAttachment(ReadString(statement, 0), |
988 contentType, | 1028 contentType, |
989 ReadInteger64(statement, 1), | 1029 ReadInteger64(statement, 1), |
990 ReadString(statement, 4), | 1030 ReadString(statement, 4), |
991 ReadInteger32(statement, 2), | 1031 ReadInteger32(statement, 2), |
992 ReadInteger64(statement, 3), | 1032 ReadInteger64(statement, 3), |
993 ReadString(statement, 5)); | 1033 ReadString(statement, 5)); |
994 return true; | 1034 return true; |
995 } | 1035 } |
996 } | 1036 } |
997 | 1037 |
998 | 1038 |
1651 #endif | 1691 #endif |
1652 | 1692 |
1653 | 1693 |
1654 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1 | 1694 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1 |
1655 // New primitive since Orthanc 1.5.2 | 1695 // New primitive since Orthanc 1.5.2 |
1656 void IndexBackend::LookupResources(const std::vector<Orthanc::DatabaseConstraint>& lookup, | 1696 void IndexBackend::LookupResources(OrthancPlugins::IDatabaseBackendOutput& output, |
1697 const std::vector<Orthanc::DatabaseConstraint>& lookup, | |
1657 OrthancPluginResourceType queryLevel, | 1698 OrthancPluginResourceType queryLevel, |
1658 uint32_t limit, | 1699 uint32_t limit, |
1659 bool requestSomeInstance) | 1700 bool requestSomeInstance) |
1660 { | 1701 { |
1661 LookupFormatter formatter(manager_.GetDialect()); | 1702 LookupFormatter formatter(manager_.GetDialect()); |
1706 | 1747 |
1707 while (!statement.IsDone()) | 1748 while (!statement.IsDone()) |
1708 { | 1749 { |
1709 if (requestSomeInstance) | 1750 if (requestSomeInstance) |
1710 { | 1751 { |
1711 GetOutput().AnswerMatchingResource(ReadString(statement, 0), ReadString(statement, 1)); | 1752 output.AnswerMatchingResource(ReadString(statement, 0), ReadString(statement, 1)); |
1712 } | 1753 } |
1713 else | 1754 else |
1714 { | 1755 { |
1715 GetOutput().AnswerMatchingResource(ReadString(statement, 0)); | 1756 output.AnswerMatchingResource(ReadString(statement, 0)); |
1716 } | 1757 } |
1717 | 1758 |
1718 statement.Next(); | 1759 statement.Next(); |
1719 } | 1760 } |
1720 } | 1761 } |