comparison Framework/Plugins/DatabaseBackendAdapterV4.cpp @ 373:be7de633695c db-protobuf

started DatabaseBackendAdapterV4
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 Mar 2023 12:47:54 +0200
parents
children 824d70ce85ff
comparison
equal deleted inserted replaced
372:b5e2c1e48828 373:be7de633695c
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Affero General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "DatabaseBackendAdapterV4.h"
24
25 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
26 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 12, 0)
27
28 #include <OrthancDatabasePlugin.pb.h> // Include protobuf messages
29
30 #include <Logging.h>
31 #include <MultiThreading/SharedMessageQueue.h>
32 #include <OrthancException.h>
33
34 #include <stdexcept>
35 #include <list>
36 #include <string>
37 #include <cassert>
38
39
40 #define ORTHANC_PLUGINS_DATABASE_CATCH(context) \
41
42
43 namespace OrthancDatabases
44 {
45 static bool isBackendInUse_ = false; // Only for sanity checks
46
47
48 static void ProcessDatabaseOperation(Orthanc::DatabasePluginMessages::DatabaseResponse& response,
49 const Orthanc::DatabasePluginMessages::DatabaseRequest& request,
50 IndexBackend& backend)
51 {
52 switch (request.operation())
53 {
54 case Orthanc::DatabasePluginMessages::OPERATION_GET_SYSTEM_INFORMATION:
55 response.mutable_get_system_information()->set_supports_revisions(backend.HasRevisionsSupport());
56 break;
57
58 default:
59 LOG(ERROR) << "Not implemented database operation from protobuf: " << request.operation();
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
61 }
62 }
63
64
65 static void ProcessTransactionOperation(Orthanc::DatabasePluginMessages::TransactionResponse& response,
66 const Orthanc::DatabasePluginMessages::TransactionRequest& request,
67 IndexBackend& backend)
68 {
69 switch (request.operation())
70 {
71 default:
72 LOG(ERROR) << "Not implemented transaction operation from protobuf: " << request.operation();
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
74 }
75 }
76
77
78 static OrthancPluginErrorCode CallBackend(OrthancPluginMemoryBuffer64* serializedResponse,
79 void* rawBackend,
80 const void* requestData,
81 uint64_t requestSize)
82 {
83 Orthanc::DatabasePluginMessages::Request request;
84 if (!request.ParseFromArray(requestData, requestSize))
85 {
86 LOG(ERROR) << "Cannot parse message from the Orthanc core using protobuf";
87 return OrthancPluginErrorCode_InternalError;
88 }
89
90 if (rawBackend == NULL)
91 {
92 LOG(ERROR) << "Received a NULL pointer from the database";
93 return OrthancPluginErrorCode_InternalError;
94 }
95
96 IndexBackend& backend = *reinterpret_cast<IndexBackend*>(rawBackend);
97
98 try
99 {
100 Orthanc::DatabasePluginMessages::Response response;
101
102 switch (request.type())
103 {
104 case Orthanc::DatabasePluginMessages::REQUEST_DATABASE:
105 ProcessDatabaseOperation(*response.mutable_database_response(), request.database_request(), backend);
106 break;
107
108 case Orthanc::DatabasePluginMessages::REQUEST_TRANSACTION:
109 ProcessTransactionOperation(*response.mutable_transaction_response(), request.transaction_request(), backend);
110 break;
111
112 default:
113 LOG(ERROR) << "Not implemented request type from protobuf: " << request.type();
114 break;
115 }
116
117 std::string s;
118 if (!response.SerializeToString(&s))
119 {
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Cannot serialize to protobuf");
121 }
122
123 if (OrthancPluginCreateMemoryBuffer64(backend.GetContext(), serializedResponse, s.size()) != OrthancPluginErrorCode_Success)
124 {
125 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory, "Cannot allocate a memory buffer");
126 }
127
128 if (!s.empty())
129 {
130 assert(serializedResponse->size == s.size());
131 memcpy(serializedResponse->data, s.c_str(), s.size());
132 }
133
134 return OrthancPluginErrorCode_Success;
135 }
136 catch (::Orthanc::OrthancException& e)
137 {
138 LOG(ERROR) << "Exception in database back-end: " << e.What();
139 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
140 }
141 catch (::std::runtime_error& e)
142 {
143 LOG(ERROR) << "Exception in database back-end: " << e.what();
144 return OrthancPluginErrorCode_DatabasePlugin;
145 }
146 catch (...)
147 {
148 LOG(ERROR) << "Native exception";
149 return OrthancPluginErrorCode_DatabasePlugin;
150 }
151 }
152
153 static void FinalizeBackend(void* rawBackend)
154 {
155 if (rawBackend != NULL)
156 {
157 IndexBackend* backend = reinterpret_cast<IndexBackend*>(rawBackend);
158
159 if (isBackendInUse_)
160 {
161 isBackendInUse_ = false;
162 }
163 else
164 {
165 LOG(ERROR) << "More than one index backend was registered, internal error";
166 }
167
168 delete backend;
169 }
170 else
171 {
172 LOG(ERROR) << "Received a null pointer from the Orthanc core, internal error";
173 }
174 }
175
176
177 void DatabaseBackendAdapterV4::Register(IndexBackend* backend,
178 size_t countConnections,
179 unsigned int maxDatabaseRetries)
180 {
181 std::unique_ptr<IndexBackend> protection(backend);
182
183 if (isBackendInUse_)
184 {
185 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
186 }
187
188 if (backend == NULL)
189 {
190 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
191 }
192
193 OrthancPluginContext* context = backend->GetContext();
194
195 if (OrthancPluginRegisterDatabaseBackendV4(context, protection.release(), maxDatabaseRetries,
196 CallBackend, FinalizeBackend) != OrthancPluginErrorCode_Success)
197 {
198 delete backend;
199 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Unable to register the database backend");
200 }
201
202 isBackendInUse_ = true;
203 }
204
205
206 void DatabaseBackendAdapterV4::Finalize()
207 {
208 if (isBackendInUse_)
209 {
210 LOG(ERROR) << "The Orthanc core has not destructed the index backend, internal error";
211 }
212 }
213 }
214
215 # endif
216 #endif