comparison Framework/Plugins/StorageBackend.cpp @ 219:dd6cfc250747

removed useless class StorageAreaBuffer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Mar 2021 14:52:55 +0200
parents 696bc0c9fddb
children a4918d57435c
comparison
equal deleted inserted replaced
218:90eb271f85b2 219:dd6cfc250747
81 81
82 statement.Execute(args); 82 statement.Execute(args);
83 } 83 }
84 84
85 85
86 void StorageBackend::Read(StorageAreaBuffer& target, 86 void StorageBackend::Read(StorageBackend::IFileContentVisitor& target,
87 DatabaseManager::Transaction& transaction, 87 DatabaseManager::Transaction& transaction,
88 const std::string& uuid, 88 const std::string& uuid,
89 OrthancPluginContentType type) 89 OrthancPluginContentType type)
90 { 90 {
91 DatabaseManager::CachedStatement statement( 91 DatabaseManager::CachedStatement statement(
159 int64_t size, 159 int64_t size,
160 OrthancPluginContentType type) 160 OrthancPluginContentType type)
161 { 161 {
162 try 162 try
163 { 163 {
164 if (backend_.get() == NULL)
165 {
166 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
167 }
168
164 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadWrite); 169 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadWrite);
165 backend_->Create(transaction, uuid, content, static_cast<size_t>(size), type); 170 backend_->Create(transaction, uuid, content, static_cast<size_t>(size), type);
166 transaction.Commit(); 171 transaction.Commit();
167 return OrthancPluginErrorCode_Success; 172 return OrthancPluginErrorCode_Success;
168 } 173 }
169 ORTHANC_PLUGINS_DATABASE_CATCH; 174 ORTHANC_PLUGINS_DATABASE_CATCH;
170 } 175 }
171 176
172 177
173 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) 178 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
174 static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64 *target, 179 static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64* target,
175 const char* uuid, 180 const char* uuid,
176 OrthancPluginContentType type) 181 OrthancPluginContentType type)
177 { 182 {
183 class Visitor : public StorageBackend::IFileContentVisitor
184 {
185 private:
186 OrthancPluginMemoryBuffer64* target_;
187 bool success_;
188
189 public:
190 Visitor(OrthancPluginMemoryBuffer64* target) :
191 target_(target),
192 success_(false)
193 {
194 }
195
196 bool IsSuccess() const
197 {
198 return success_;
199 }
200
201 virtual void Assign(const std::string& content) ORTHANC_OVERRIDE
202 {
203 if (success_)
204 {
205 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
206 }
207 else
208 {
209 assert(context_ != NULL);
210
211 if (OrthancPluginCreateMemoryBuffer64(context_, target_, static_cast<uint64_t>(content.size())) !=
212 OrthancPluginErrorCode_Success)
213 {
214 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
215 }
216
217 if (!content.empty())
218 {
219 memcpy(target_->data, content.c_str(), content.size());
220 }
221
222 success_ = true;
223 }
224 }
225 };
226
178 try 227 try
179 { 228 {
180 StorageAreaBuffer buffer(context_); 229 if (backend_.get() == NULL)
181 230 {
231 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
232 }
233
234 if (target == NULL)
235 {
236 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
237 }
238
239 Visitor visitor(target);
240
182 { 241 {
183 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly); 242 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly);
184 backend_->Read(buffer, transaction, uuid, type); 243 backend_->Read(visitor, transaction, uuid, type);
244
245 if (!visitor.IsSuccess())
246 {
247 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
248 }
249
185 transaction.Commit(); 250 transaction.Commit();
186 } 251 }
187
188 buffer.Move(target);
189 252
190 return OrthancPluginErrorCode_Success; 253 return OrthancPluginErrorCode_Success;
191 } 254 }
192 ORTHANC_PLUGINS_DATABASE_CATCH; 255 ORTHANC_PLUGINS_DATABASE_CATCH;
193 } 256 }
194 #else 257 #endif
195 static OrthancPluginErrorCode StorageRead(void** content, 258
259
260 static OrthancPluginErrorCode StorageRead(void** data,
196 int64_t* size, 261 int64_t* size,
197 const char* uuid, 262 const char* uuid,
198 OrthancPluginContentType type) 263 OrthancPluginContentType type)
199 { 264 {
265 class Visitor : public StorageBackend::IFileContentVisitor
266 {
267 private:
268 void** data_;
269 int64_t* size_;
270 bool success_;
271
272 public:
273 Visitor(void** data,
274 int64_t* size) :
275 data_(data),
276 size_(size),
277 success_(false)
278 {
279 }
280
281 bool IsSuccess() const
282 {
283 return success_;
284 }
285
286 virtual void Assign(const std::string& content) ORTHANC_OVERRIDE
287 {
288 if (success_)
289 {
290 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
291 }
292 else
293 {
294 if (content.empty())
295 {
296 *data_ = NULL;
297 *size_ = 0;
298 }
299 else
300 {
301 *size_ = static_cast<int64_t>(content.size());
302
303 if (static_cast<size_t>(*size_) != content.size())
304 {
305 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory,
306 "File cannot be stored in a 63bit buffer");
307 }
308
309 *data_ = malloc(*size_);
310 if (*data_ == NULL)
311 {
312 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
313 }
314
315 memcpy(*data_, content.c_str(), *size_);
316 }
317
318 success_ = true;
319 }
320 }
321 };
322
323
200 try 324 try
201 { 325 {
202 StorageAreaBuffer buffer(context_); 326 if (backend_.get() == NULL)
327 {
328 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
329 }
330
331 if (data == NULL ||
332 size == NULL)
333 {
334 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
335 }
336
337 Visitor visitor(data, size);
203 338
204 { 339 {
205 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly); 340 DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly);
206 backend_->Read(buffer, transaction, uuid, type); 341 backend_->Read(visitor, transaction, uuid, type);
342
343 if (!visitor.IsSuccess())
344 {
345 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
346 }
347
207 transaction.Commit(); 348 transaction.Commit();
208 } 349 }
209 350
210 *size = buffer.GetSize();
211 *content = buffer.ReleaseData();
212
213 return OrthancPluginErrorCode_Success; 351 return OrthancPluginErrorCode_Success;
214 } 352 }
215 ORTHANC_PLUGINS_DATABASE_CATCH; 353 ORTHANC_PLUGINS_DATABASE_CATCH;
216 } 354 }
217 #endif
218 355
219 356
220 static OrthancPluginErrorCode StorageRemove(const char* uuid, 357 static OrthancPluginErrorCode StorageRemove(const char* uuid,
221 OrthancPluginContentType type) 358 OrthancPluginContentType type)
222 { 359 {
250 { 387 {
251 context_ = context; 388 context_ = context;
252 backend_.reset(backend); 389 backend_.reset(backend);
253 backend_->GetManager().Open(); 390 backend_->GetManager().Open();
254 391
255 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) 392 bool hasLoadedV2 = false;
256 OrthancPluginRegisterStorageArea2(context_, StorageCreate, StorageReadWhole, 393
257 NULL /* TODO - StorageReadRange */, StorageRemove); 394 #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1
258 #else 395 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0)
259 OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove); 396 if (OrthancPluginCheckVersionAdvanced(context, 1, 9, 0) == 1)
397 {
398 OrthancPluginRegisterStorageArea2(context_, StorageCreate, StorageReadWhole,
399 NULL /* TODO - StorageReadRange */, StorageRemove);
400 hasLoadedV2 = true;
401 }
402 # endif
260 #endif 403 #endif
404
405 if (!hasLoadedV2)
406 {
407 OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove);
408 }
261 } 409 }
262 } 410 }
263 411
264 412
265 void StorageBackend::Finalize() 413 void StorageBackend::Finalize()
266 { 414 {
267 backend_.reset(NULL); 415 backend_.reset(NULL);
268 context_ = NULL; 416 context_ = NULL;
269 } 417 }
418
419
420 void StorageBackend::ReadToString(std::string& target,
421 DatabaseManager::Transaction& transaction,
422 const std::string& uuid,
423 OrthancPluginContentType type)
424 {
425 class Visitor : public StorageBackend::IFileContentVisitor
426 {
427 private:
428 std::string& target_;
429 bool success_;
430
431 public:
432 Visitor(std::string& target) :
433 target_(target),
434 success_(false)
435 {
436 }
437
438 bool IsSuccess() const
439 {
440 return success_;
441 }
442
443 virtual void Assign(const std::string& content) ORTHANC_OVERRIDE
444 {
445 if (success_)
446 {
447 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
448 }
449 else
450 {
451 target_.assign(content);
452 success_ = true;
453 }
454 }
455 };
456
457
458 Visitor visitor(target);
459
460 Read(visitor, transaction, uuid, type);
461
462 if (!visitor.IsSuccess())
463 {
464 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
465 }
466 }
270 } 467 }