comparison OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp @ 5214:a9d00b17a48e db-protobuf

implemented OrthancPluginDatabaseV4::Upgrade()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Apr 2023 13:30:06 +0200
parents 055428d92772
children 8b6da4fdf9fe
comparison
equal deleted inserted replaced
5213:055428d92772 5214:a9d00b17a48e
28 #endif 28 #endif
29 29
30 #include "../../../OrthancFramework/Sources/Logging.h" 30 #include "../../../OrthancFramework/Sources/Logging.h"
31 #include "../../../OrthancFramework/Sources/OrthancException.h" 31 #include "../../../OrthancFramework/Sources/OrthancException.h"
32 #include "../../Sources/Database/ResourcesContent.h" 32 #include "../../Sources/Database/ResourcesContent.h"
33 #include "../../Sources/Database/VoidDatabaseListener.h"
33 #include "PluginsEnumerations.h" 34 #include "PluginsEnumerations.h"
34 35
35 #include "OrthancDatabasePlugin.pb.h" // Auto-generated file 36 #include "OrthancDatabasePlugin.pb.h" // Auto-generated file
36 37
37 #include <cassert> 38 #include <cassert>
223 224
224 225
225 public: 226 public:
226 Transaction(OrthancPluginDatabaseV4& database, 227 Transaction(OrthancPluginDatabaseV4& database,
227 IDatabaseListener& listener, 228 IDatabaseListener& listener,
228 void* transaction) : 229 TransactionType type) :
229 database_(database), 230 database_(database),
230 listener_(listener), 231 listener_(listener),
231 transaction_(transaction) 232 transaction_(NULL)
232 { 233 {
234 DatabasePluginMessages::DatabaseRequest request;
235
236 switch (type)
237 {
238 case TransactionType_ReadOnly:
239 request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_ONLY);
240 break;
241
242 case TransactionType_ReadWrite:
243 request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_WRITE);
244 break;
245
246 default:
247 throw OrthancException(ErrorCode_ParameterOutOfRange);
248 }
249
250 DatabasePluginMessages::DatabaseResponse response;
251 ExecuteDatabase(response, database, DatabasePluginMessages::OPERATION_START_TRANSACTION, request);
252
253 transaction_ = reinterpret_cast<void*>(response.start_transaction().transaction());
254
255 if (transaction_ == NULL)
256 {
257 throw OrthancException(ErrorCode_NullPointer);
258 }
233 } 259 }
234 260
235 261
236 virtual ~Transaction() 262 virtual ~Transaction()
237 { 263 {
238 DatabasePluginMessages::DatabaseRequest request; 264 try
239 request.mutable_finalize_transaction()->set_transaction(reinterpret_cast<intptr_t>(transaction_)); 265 {
240 266 DatabasePluginMessages::DatabaseRequest request;
241 DatabasePluginMessages::DatabaseResponse response; 267 request.mutable_finalize_transaction()->set_transaction(reinterpret_cast<intptr_t>(transaction_));
242 ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request); 268
269 DatabasePluginMessages::DatabaseResponse response;
270 ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request);
271 }
272 catch (OrthancException& e)
273 {
274 // Destructors must not throw exceptions
275 LOG(ERROR) << "Cannot finalize the database engine: " << e.What();
276 }
243 } 277 }
244 278
245 279
246 virtual void Rollback() ORTHANC_OVERRIDE 280 virtual void Rollback() ORTHANC_OVERRIDE
247 { 281 {
1202 { 1236 {
1203 if (!open_) 1237 if (!open_)
1204 { 1238 {
1205 throw OrthancException(ErrorCode_BadSequenceOfCalls); 1239 throw OrthancException(ErrorCode_BadSequenceOfCalls);
1206 } 1240 }
1207 1241 else
1208 DatabasePluginMessages::DatabaseRequest request; 1242 {
1209 1243 return new Transaction(*this, listener, type);
1210 switch (type) 1244 }
1211 {
1212 case TransactionType_ReadOnly:
1213 request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_ONLY);
1214 break;
1215
1216 case TransactionType_ReadWrite:
1217 request.mutable_start_transaction()->set_type(DatabasePluginMessages::TRANSACTION_READ_WRITE);
1218 break;
1219
1220 default:
1221 throw OrthancException(ErrorCode_InternalError);
1222 }
1223
1224 DatabasePluginMessages::DatabaseResponse response;
1225 ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_START_TRANSACTION, request);
1226
1227 return new Transaction(*this, listener, reinterpret_cast<void*>(response.start_transaction().transaction()));
1228 } 1245 }
1229 1246
1230 1247
1231 unsigned int OrthancPluginDatabaseV4::GetDatabaseVersion() 1248 unsigned int OrthancPluginDatabaseV4::GetDatabaseVersion()
1232 { 1249 {
1248 { 1265 {
1249 throw OrthancException(ErrorCode_BadSequenceOfCalls); 1266 throw OrthancException(ErrorCode_BadSequenceOfCalls);
1250 } 1267 }
1251 else 1268 else
1252 { 1269 {
1253 // TODO 1270 VoidDatabaseListener listener;
1254 throw OrthancException(ErrorCode_NotImplemented); 1271 Transaction transaction(*this, listener, TransactionType_ReadWrite);
1272
1273 try
1274 {
1275 DatabasePluginMessages::DatabaseRequest request;
1276 request.mutable_upgrade()->set_target_version(targetVersion);
1277 request.mutable_upgrade()->set_storage_area(reinterpret_cast<intptr_t>(&storageArea));
1278
1279 DatabasePluginMessages::DatabaseResponse response;
1280
1281 ExecuteDatabase(response, *this, DatabasePluginMessages::OPERATION_UPGRADE, request);
1282 transaction.Commit(0);
1283 }
1284 catch (OrthancException& e)
1285 {
1286 transaction.Rollback();
1287 throw;
1288 }
1255 } 1289 }
1256 } 1290 }
1257 1291
1258 1292
1259 bool OrthancPluginDatabaseV4::HasRevisionsSupport() const 1293 bool OrthancPluginDatabaseV4::HasRevisionsSupport() const