comparison Framework/Plugins/IndexBackend.cpp @ 387:f35b17a38301

integration db-protobuf->mainline
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Apr 2023 17:12:08 +0200
parents 346fe629d638
children 3d6886f3e5b3
comparison
equal deleted inserted replaced
371:c1fe28de1bf6 387:f35b17a38301
26 #include "../Common/BinaryStringValue.h" 26 #include "../Common/BinaryStringValue.h"
27 #include "../Common/Integer64Value.h" 27 #include "../Common/Integer64Value.h"
28 #include "../Common/Utf8StringValue.h" 28 #include "../Common/Utf8StringValue.h"
29 #include "DatabaseBackendAdapterV2.h" 29 #include "DatabaseBackendAdapterV2.h"
30 #include "DatabaseBackendAdapterV3.h" 30 #include "DatabaseBackendAdapterV3.h"
31 #include "DatabaseBackendAdapterV4.h"
31 #include "GlobalProperties.h" 32 #include "GlobalProperties.h"
32 33
33 #include <Compatibility.h> // For std::unique_ptr<> 34 #include <Compatibility.h> // For std::unique_ptr<>
34 #include <Logging.h> 35 #include <Logging.h>
35 #include <OrthancException.h> 36 #include <OrthancException.h>
113 void IndexBackend::ReadChangesInternal(IDatabaseBackendOutput& output, 114 void IndexBackend::ReadChangesInternal(IDatabaseBackendOutput& output,
114 bool& done, 115 bool& done,
115 DatabaseManager& manager, 116 DatabaseManager& manager,
116 DatabaseManager::CachedStatement& statement, 117 DatabaseManager::CachedStatement& statement,
117 const Dictionary& args, 118 const Dictionary& args,
118 uint32_t maxResults) 119 uint32_t limit)
119 { 120 {
120 statement.Execute(args); 121 statement.Execute(args);
121 122
122 uint32_t count = 0; 123 uint32_t count = 0;
123 124
124 while (count < maxResults && 125 while (count < limit &&
125 !statement.IsDone()) 126 !statement.IsDone())
126 { 127 {
127 output.AnswerChange( 128 output.AnswerChange(
128 statement.ReadInteger64(0), 129 statement.ReadInteger64(0),
129 statement.ReadInteger32(1), 130 statement.ReadInteger32(1),
133 134
134 statement.Next(); 135 statement.Next();
135 count++; 136 count++;
136 } 137 }
137 138
138 done = (count < maxResults || 139 done = (count < limit ||
139 statement.IsDone()); 140 statement.IsDone());
140 } 141 }
141 142
142 143
143 void IndexBackend::ReadExportedResourcesInternal(IDatabaseBackendOutput& output, 144 void IndexBackend::ReadExportedResourcesInternal(IDatabaseBackendOutput& output,
144 bool& done, 145 bool& done,
145 DatabaseManager::CachedStatement& statement, 146 DatabaseManager::CachedStatement& statement,
146 const Dictionary& args, 147 const Dictionary& args,
147 uint32_t maxResults) 148 uint32_t limit)
148 { 149 {
149 statement.Execute(args); 150 statement.Execute(args);
150 151
151 uint32_t count = 0; 152 uint32_t count = 0;
152 153
153 while (count < maxResults && 154 while (count < limit &&
154 !statement.IsDone()) 155 !statement.IsDone())
155 { 156 {
156 int64_t seq = statement.ReadInteger64(0); 157 int64_t seq = statement.ReadInteger64(0);
157 OrthancPluginResourceType resourceType = 158 OrthancPluginResourceType resourceType =
158 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(1)); 159 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(1));
170 171
171 statement.Next(); 172 statement.Next();
172 count++; 173 count++;
173 } 174 }
174 175
175 done = (count < maxResults || 176 done = (count < limit ||
176 statement.IsDone()); 177 statement.IsDone());
177 } 178 }
178 179
179 180
180 void IndexBackend::ClearDeletedFiles(DatabaseManager& manager) 181 void IndexBackend::ClearDeletedFiles(DatabaseManager& manager)
516 517
517 518
518 void IndexBackend::GetAllPublicIds(std::list<std::string>& target, 519 void IndexBackend::GetAllPublicIds(std::list<std::string>& target,
519 DatabaseManager& manager, 520 DatabaseManager& manager,
520 OrthancPluginResourceType resourceType, 521 OrthancPluginResourceType resourceType,
521 uint64_t since, 522 int64_t since,
522 uint64_t limit) 523 uint32_t limit)
523 { 524 {
524 std::string suffix; 525 std::string suffix;
525 if (manager.GetDialect() == Dialect_MSSQL) 526 if (manager.GetDialect() == Dialect_MSSQL)
526 { 527 {
527 suffix = "OFFSET ${since} ROWS FETCH FIRST ${limit} ROWS ONLY"; 528 suffix = "OFFSET ${since} ROWS FETCH FIRST ${limit} ROWS ONLY";
553 /* Use GetOutput().AnswerChange() */ 554 /* Use GetOutput().AnswerChange() */
554 void IndexBackend::GetChanges(IDatabaseBackendOutput& output, 555 void IndexBackend::GetChanges(IDatabaseBackendOutput& output,
555 bool& done /*out*/, 556 bool& done /*out*/,
556 DatabaseManager& manager, 557 DatabaseManager& manager,
557 int64_t since, 558 int64_t since,
558 uint32_t maxResults) 559 uint32_t limit)
559 { 560 {
560 std::string suffix; 561 std::string suffix;
561 if (manager.GetDialect() == Dialect_MSSQL) 562 if (manager.GetDialect() == Dialect_MSSQL)
562 { 563 {
563 suffix = "OFFSET 0 ROWS FETCH FIRST ${limit} ROWS ONLY"; 564 suffix = "OFFSET 0 ROWS FETCH FIRST ${limit} ROWS ONLY";
576 statement.SetReadOnly(true); 577 statement.SetReadOnly(true);
577 statement.SetParameterType("limit", ValueType_Integer64); 578 statement.SetParameterType("limit", ValueType_Integer64);
578 statement.SetParameterType("since", ValueType_Integer64); 579 statement.SetParameterType("since", ValueType_Integer64);
579 580
580 Dictionary args; 581 Dictionary args;
581 args.SetIntegerValue("limit", maxResults + 1); 582 args.SetIntegerValue("limit", limit + 1);
582 args.SetIntegerValue("since", since); 583 args.SetIntegerValue("since", since);
583 584
584 ReadChangesInternal(output, done, manager, statement, args, maxResults); 585 ReadChangesInternal(output, done, manager, statement, args, limit);
585 } 586 }
586 587
587 588
588 void IndexBackend::GetChildrenInternalId(std::list<int64_t>& target /*out*/, 589 void IndexBackend::GetChildrenInternalId(std::list<int64_t>& target /*out*/,
589 DatabaseManager& manager, 590 DatabaseManager& manager,
626 /* Use GetOutput().AnswerExportedResource() */ 627 /* Use GetOutput().AnswerExportedResource() */
627 void IndexBackend::GetExportedResources(IDatabaseBackendOutput& output, 628 void IndexBackend::GetExportedResources(IDatabaseBackendOutput& output,
628 bool& done /*out*/, 629 bool& done /*out*/,
629 DatabaseManager& manager, 630 DatabaseManager& manager,
630 int64_t since, 631 int64_t since,
631 uint32_t maxResults) 632 uint32_t limit)
632 { 633 {
633 std::string suffix; 634 std::string suffix;
634 if (manager.GetDialect() == Dialect_MSSQL) 635 if (manager.GetDialect() == Dialect_MSSQL)
635 { 636 {
636 suffix = "OFFSET 0 ROWS FETCH FIRST ${limit} ROWS ONLY"; 637 suffix = "OFFSET 0 ROWS FETCH FIRST ${limit} ROWS ONLY";
647 statement.SetReadOnly(true); 648 statement.SetReadOnly(true);
648 statement.SetParameterType("limit", ValueType_Integer64); 649 statement.SetParameterType("limit", ValueType_Integer64);
649 statement.SetParameterType("since", ValueType_Integer64); 650 statement.SetParameterType("since", ValueType_Integer64);
650 651
651 Dictionary args; 652 Dictionary args;
652 args.SetIntegerValue("limit", maxResults + 1); 653 args.SetIntegerValue("limit", limit + 1);
653 args.SetIntegerValue("since", since); 654 args.SetIntegerValue("since", since);
654 655
655 ReadExportedResourcesInternal(output, done, statement, args, maxResults); 656 ReadExportedResourcesInternal(output, done, statement, args, limit);
656 } 657 }
657 658
658 659
659 /* Use GetOutput().AnswerChange() */ 660 /* Use GetOutput().AnswerChange() */
660 void IndexBackend::GetLastChange(IDatabaseBackendOutput& output, 661 void IndexBackend::GetLastChange(IDatabaseBackendOutput& output,
1007 statement.Execute(args); 1008 statement.Execute(args);
1008 } 1009 }
1009 1010
1010 1011
1011 void IndexBackend::LogExportedResource(DatabaseManager& manager, 1012 void IndexBackend::LogExportedResource(DatabaseManager& manager,
1012 const OrthancPluginExportedResource& resource) 1013 OrthancPluginResourceType resourceType,
1014 const char* publicId,
1015 const char* modality,
1016 const char* date,
1017 const char* patientId,
1018 const char* studyInstanceUid,
1019 const char* seriesInstanceUid,
1020 const char* sopInstanceUid)
1013 { 1021 {
1014 DatabaseManager::CachedStatement statement( 1022 DatabaseManager::CachedStatement statement(
1015 STATEMENT_FROM_HERE, manager, 1023 STATEMENT_FROM_HERE, manager,
1016 "INSERT INTO ExportedResources VALUES(${AUTOINCREMENT} ${type}, ${publicId}, " 1024 "INSERT INTO ExportedResources VALUES(${AUTOINCREMENT} ${type}, ${publicId}, "
1017 "${modality}, ${patient}, ${study}, ${series}, ${instance}, ${date})"); 1025 "${modality}, ${patient}, ${study}, ${series}, ${instance}, ${date})");
1024 statement.SetParameterType("series", ValueType_Utf8String); 1032 statement.SetParameterType("series", ValueType_Utf8String);
1025 statement.SetParameterType("instance", ValueType_Utf8String); 1033 statement.SetParameterType("instance", ValueType_Utf8String);
1026 statement.SetParameterType("date", ValueType_Utf8String); 1034 statement.SetParameterType("date", ValueType_Utf8String);
1027 1035
1028 Dictionary args; 1036 Dictionary args;
1029 args.SetIntegerValue("type", resource.resourceType); 1037 args.SetIntegerValue("type", resourceType);
1030 args.SetUtf8Value("publicId", resource.publicId); 1038 args.SetUtf8Value("publicId", publicId);
1031 args.SetUtf8Value("modality", resource.modality); 1039 args.SetUtf8Value("modality", modality);
1032 args.SetUtf8Value("patient", resource.patientId); 1040 args.SetUtf8Value("patient", patientId);
1033 args.SetUtf8Value("study", resource.studyInstanceUid); 1041 args.SetUtf8Value("study", studyInstanceUid);
1034 args.SetUtf8Value("series", resource.seriesInstanceUid); 1042 args.SetUtf8Value("series", seriesInstanceUid);
1035 args.SetUtf8Value("instance", resource.sopInstanceUid); 1043 args.SetUtf8Value("instance", sopInstanceUid);
1036 args.SetUtf8Value("date", resource.date); 1044 args.SetUtf8Value("date", date);
1037 1045
1038 statement.Execute(args); 1046 statement.Execute(args);
1039 } 1047 }
1040 1048
1041 1049
2608 if (backend == NULL) 2616 if (backend == NULL)
2609 { 2617 {
2610 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); 2618 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
2611 } 2619 }
2612 2620
2613 bool hasLoadedV3 = false; 2621 LOG(WARNING) << "The index plugin will use " << countConnections << " connection(s) to the database, "
2614 2622 << "and will retry up to " << maxDatabaseRetries << " time(s) in the case of a collision";
2623
2624 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
2625 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 0)
2626 if (OrthancPluginCheckVersionAdvanced(backend->GetContext(), 1, 12, 0) == 1)
2627 {
2628 OrthancDatabases::DatabaseBackendAdapterV4::Register(backend, countConnections, maxDatabaseRetries);
2629 return;
2630 }
2631 # endif
2632 #endif
2633
2615 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 2634 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
2616 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) 2635 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2)
2617 if (OrthancPluginCheckVersionAdvanced(backend->GetContext(), 1, 9, 2) == 1) 2636 if (OrthancPluginCheckVersionAdvanced(backend->GetContext(), 1, 9, 2) == 1)
2618 { 2637 {
2619 LOG(WARNING) << "The index plugin will use " << countConnections << " connection(s) to the database, "
2620 << "and will retry up to " << maxDatabaseRetries << " time(s) in the case of a collision";
2621
2622 OrthancDatabases::DatabaseBackendAdapterV3::Register(backend, countConnections, maxDatabaseRetries); 2638 OrthancDatabases::DatabaseBackendAdapterV3::Register(backend, countConnections, maxDatabaseRetries);
2623 hasLoadedV3 = true; 2639 return;
2624 } 2640 }
2625 # endif 2641 # endif
2626 #endif 2642 #endif
2627 2643
2628 if (!hasLoadedV3) 2644 LOG(WARNING) << "Performance warning: Your version of the Orthanc core or SDK doesn't support multiple readers/writers";
2629 { 2645 OrthancDatabases::DatabaseBackendAdapterV2::Register(backend);
2630 LOG(WARNING) << "Performance warning: Your version of the Orthanc core or SDK doesn't support multiple readers/writers";
2631 OrthancDatabases::DatabaseBackendAdapterV2::Register(backend);
2632 }
2633 } 2646 }
2634 2647
2635 2648
2636 bool IndexBackend::LookupGlobalIntegerProperty(int& target, 2649 bool IndexBackend::LookupGlobalIntegerProperty(int& target,
2637 DatabaseManager& manager, 2650 DatabaseManager& manager,
2677 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 2690 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
2678 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) 2691 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2)
2679 OrthancDatabases::DatabaseBackendAdapterV3::Finalize(); 2692 OrthancDatabases::DatabaseBackendAdapterV3::Finalize();
2680 # endif 2693 # endif
2681 #endif 2694 #endif
2695
2696 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
2697 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 0)
2698 OrthancDatabases::DatabaseBackendAdapterV4::Finalize();
2699 # endif
2700 #endif
2682 } 2701 }
2683 2702
2684 2703
2685 DatabaseManager* IndexBackend::CreateSingleDatabaseManager(IDatabaseBackend& backend) 2704 DatabaseManager* IndexBackend::CreateSingleDatabaseManager(IDatabaseBackend& backend)
2686 { 2705 {