# HG changeset patch # User Sebastien Jodogne # Date 1617022375 -7200 # Node ID dd6cfc25074744f106de4c04efb35deff433a133 # Parent 90eb271f85b21d6b955da55a77e8119f5be6783f removed useless class StorageAreaBuffer diff -r 90eb271f85b2 -r dd6cfc250747 Framework/Plugins/IndexBackend.cpp --- a/Framework/Plugins/IndexBackend.cpp Fri Mar 26 18:06:46 2021 +0100 +++ b/Framework/Plugins/IndexBackend.cpp Mon Mar 29 14:52:55 2021 +0200 @@ -2214,13 +2214,11 @@ void IndexBackend::Register(IndexBackend& backend) { - OrthancPluginContext* context = backend.GetContext(); - bool hasLoadedV3 = false; #if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 # if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) - if (OrthancPluginCheckVersionAdvanced(context, 1, 9, 2) == 1) + if (OrthancPluginCheckVersionAdvanced(backend.GetContext(), 1, 9, 2) == 1) { OrthancDatabases::DatabaseBackendAdapterV3::Register(backend); hasLoadedV3 = true; diff -r 90eb271f85b2 -r dd6cfc250747 Framework/Plugins/StorageAreaBuffer.cpp --- a/Framework/Plugins/StorageAreaBuffer.cpp Fri Mar 26 18:06:46 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,212 +0,0 @@ -/** - * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics - * Department, University Hospital of Liege, Belgium - * Copyright (C) 2017-2021 Osimis S.A., Belgium - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - **/ - - -#include "StorageAreaBuffer.h" - -#include - -#include -#include - - -namespace OrthancDatabases -{ -#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) - /** - * If Orthanc SDK >= 1.9.0 - **/ - - StorageAreaBuffer::StorageAreaBuffer(OrthancPluginContext* context) : - context_(context) - { - buffer_.data = NULL; - buffer_.size = 0; - } - - - void StorageAreaBuffer::Clear() - { - if (buffer_.data != NULL) - { - if (context_ == NULL) // Are we running the unit tests? - { - free(buffer_.data); - } - else - { - OrthancPluginFreeMemoryBuffer64(context_, &buffer_); - } - - buffer_.data = NULL; - buffer_.size = 0; - } - } - - - int64_t StorageAreaBuffer::GetSize() const - { - return buffer_.size; - } - - - const void* StorageAreaBuffer::GetData() const - { - return buffer_.data; - } - - - void StorageAreaBuffer::Assign(const std::string& content) - { - Clear(); - - if (context_ == NULL) // Are we running the unit tests? - { - buffer_.size = static_cast(content.size()); - - if (content.empty()) - { - buffer_.data = NULL; - } - else - { - buffer_.data = malloc(content.size()); - } - } - else - { - if (OrthancPluginCreateMemoryBuffer64(context_, &buffer_, static_cast(content.size())) != - OrthancPluginErrorCode_Success) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory); - } - } - - if (!content.empty()) - { - memcpy(buffer_.data, content.c_str(), content.size()); - } - } - - - void StorageAreaBuffer::Move(OrthancPluginMemoryBuffer64* target) - { - *target = buffer_; - buffer_.data = NULL; - buffer_.size = 0; - } - - - void StorageAreaBuffer::ToString(std::string& target) - { - target.resize(buffer_.size); - - if (buffer_.size != 0) - { - memcpy(&target[0], buffer_.data, buffer_.size); - } - } - - -#else - /** - * If Orthanc SDK <= 1.8.2 - **/ - - StorageAreaBuffer::StorageAreaBuffer(OrthancPluginContext* /* not used in this flavor */) : - data_(NULL), - size_(0) - { - } - - - void StorageAreaBuffer::Clear() - { - if (data_ != NULL) - { - free(data_); - data_ = NULL; - size_ = 0; - } - } - - - int64_t StorageAreaBuffer::GetSize() const - { - return size_; - } - - - const void* StorageAreaBuffer::GetData() const - { - return data_; - } - - - void StorageAreaBuffer::Assign(const std::string& content) - { - Clear(); - - size_ = static_cast(content.size()); - - if (static_cast(size_) != content.size()) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory, - "File cannot be stored in a 63bit buffer"); - } - - if (content.empty()) - { - data_ = NULL; - } - else - { - data_ = malloc(size_); - - if (data_ == NULL) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory); - } - - memcpy(data_, content.c_str(), size_); - } - } - - - void* StorageAreaBuffer::ReleaseData() - { - void* result = data_; - data_ = NULL; - size_ = 0; - return result; - } - - - void StorageAreaBuffer::ToString(std::string& target) - { - target.resize(size_); - - if (size_ != 0) - { - memcpy(&target[0], data_, size_); - } - } -#endif -} diff -r 90eb271f85b2 -r dd6cfc250747 Framework/Plugins/StorageAreaBuffer.h --- a/Framework/Plugins/StorageAreaBuffer.h Fri Mar 26 18:06:46 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/** - * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics - * Department, University Hospital of Liege, Belgium - * Copyright (C) 2017-2021 Osimis S.A., Belgium - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU Affero General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - **/ - - -#pragma once - -#include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" - -#include -#include - - - -namespace OrthancDatabases -{ - class StorageAreaBuffer : public boost::noncopyable - { - private: -#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) - OrthancPluginContext* context_; - OrthancPluginMemoryBuffer64 buffer_; -#else - void* data_; - int64_t size_; -#endif - - public: - StorageAreaBuffer(OrthancPluginContext* context); - - ~StorageAreaBuffer() - { - Clear(); - } - - void Clear(); - - void Assign(const std::string& content); - - int64_t GetSize() const; - - const void* GetData() const; - -#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) - void Move(OrthancPluginMemoryBuffer64* target); -#else - void* ReleaseData(); -#endif - - void ToString(std::string& target); - }; -} diff -r 90eb271f85b2 -r dd6cfc250747 Framework/Plugins/StorageBackend.cpp --- a/Framework/Plugins/StorageBackend.cpp Fri Mar 26 18:06:46 2021 +0100 +++ b/Framework/Plugins/StorageBackend.cpp Mon Mar 29 14:52:55 2021 +0200 @@ -83,7 +83,7 @@ } - void StorageBackend::Read(StorageAreaBuffer& target, + void StorageBackend::Read(StorageBackend::IFileContentVisitor& target, DatabaseManager::Transaction& transaction, const std::string& uuid, OrthancPluginContentType type) @@ -161,6 +161,11 @@ { try { + if (backend_.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadWrite); backend_->Create(transaction, uuid, content, static_cast(size), type); transaction.Commit(); @@ -171,44 +176,79 @@ #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) - static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64 *target, + static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64* target, const char* uuid, OrthancPluginContentType type) { - try + class Visitor : public StorageBackend::IFileContentVisitor { - StorageAreaBuffer buffer(context_); - + private: + OrthancPluginMemoryBuffer64* target_; + bool success_; + + public: + Visitor(OrthancPluginMemoryBuffer64* target) : + target_(target), + success_(false) { - DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly); - backend_->Read(buffer, transaction, uuid, type); - transaction.Commit(); } - buffer.Move(target); + bool IsSuccess() const + { + return success_; + } - return OrthancPluginErrorCode_Success; - } - ORTHANC_PLUGINS_DATABASE_CATCH; - } -#else - static OrthancPluginErrorCode StorageRead(void** content, - int64_t* size, - const char* uuid, - OrthancPluginContentType type) - { + virtual void Assign(const std::string& content) ORTHANC_OVERRIDE + { + if (success_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + else + { + assert(context_ != NULL); + + if (OrthancPluginCreateMemoryBuffer64(context_, target_, static_cast(content.size())) != + OrthancPluginErrorCode_Success) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory); + } + + if (!content.empty()) + { + memcpy(target_->data, content.c_str(), content.size()); + } + + success_ = true; + } + } + }; + try { - StorageAreaBuffer buffer(context_); - + if (backend_.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + if (target == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + + Visitor visitor(target); + { DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly); - backend_->Read(buffer, transaction, uuid, type); + backend_->Read(visitor, transaction, uuid, type); + + if (!visitor.IsSuccess()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); + } + transaction.Commit(); } - - *size = buffer.GetSize(); - *content = buffer.ReleaseData(); return OrthancPluginErrorCode_Success; } @@ -216,6 +256,103 @@ } #endif + + static OrthancPluginErrorCode StorageRead(void** data, + int64_t* size, + const char* uuid, + OrthancPluginContentType type) + { + class Visitor : public StorageBackend::IFileContentVisitor + { + private: + void** data_; + int64_t* size_; + bool success_; + + public: + Visitor(void** data, + int64_t* size) : + data_(data), + size_(size), + success_(false) + { + } + + bool IsSuccess() const + { + return success_; + } + + virtual void Assign(const std::string& content) ORTHANC_OVERRIDE + { + if (success_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + else + { + if (content.empty()) + { + *data_ = NULL; + *size_ = 0; + } + else + { + *size_ = static_cast(content.size()); + + if (static_cast(*size_) != content.size()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory, + "File cannot be stored in a 63bit buffer"); + } + + *data_ = malloc(*size_); + if (*data_ == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory); + } + + memcpy(*data_, content.c_str(), *size_); + } + + success_ = true; + } + } + }; + + + try + { + if (backend_.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + if (data == NULL || + size == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + + Visitor visitor(data, size); + + { + DatabaseManager::Transaction transaction(backend_->GetManager(), TransactionType_ReadOnly); + backend_->Read(visitor, transaction, uuid, type); + + if (!visitor.IsSuccess()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); + } + + transaction.Commit(); + } + + return OrthancPluginErrorCode_Success; + } + ORTHANC_PLUGINS_DATABASE_CATCH; + } + static OrthancPluginErrorCode StorageRemove(const char* uuid, OrthancPluginContentType type) @@ -252,12 +389,23 @@ backend_.reset(backend); backend_->GetManager().Open(); -#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) - OrthancPluginRegisterStorageArea2(context_, StorageCreate, StorageReadWhole, - NULL /* TODO - StorageReadRange */, StorageRemove); -#else - OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove); + bool hasLoadedV2 = false; + +#if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 +# if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 0) + if (OrthancPluginCheckVersionAdvanced(context, 1, 9, 0) == 1) + { + OrthancPluginRegisterStorageArea2(context_, StorageCreate, StorageReadWhole, + NULL /* TODO - StorageReadRange */, StorageRemove); + hasLoadedV2 = true; + } +# endif #endif + + if (!hasLoadedV2) + { + OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove); + } } } @@ -267,4 +415,53 @@ backend_.reset(NULL); context_ = NULL; } + + + void StorageBackend::ReadToString(std::string& target, + DatabaseManager::Transaction& transaction, + const std::string& uuid, + OrthancPluginContentType type) + { + class Visitor : public StorageBackend::IFileContentVisitor + { + private: + std::string& target_; + bool success_; + + public: + Visitor(std::string& target) : + target_(target), + success_(false) + { + } + + bool IsSuccess() const + { + return success_; + } + + virtual void Assign(const std::string& content) ORTHANC_OVERRIDE + { + if (success_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + else + { + target_.assign(content); + success_ = true; + } + } + }; + + + Visitor visitor(target); + + Read(visitor, transaction, uuid, type); + + if (!visitor.IsSuccess()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); + } + } } diff -r 90eb271f85b2 -r dd6cfc250747 Framework/Plugins/StorageBackend.h --- a/Framework/Plugins/StorageBackend.h Fri Mar 26 18:06:46 2021 +0100 +++ b/Framework/Plugins/StorageBackend.h Mon Mar 29 14:52:55 2021 +0200 @@ -21,7 +21,6 @@ #pragma once -#include "StorageAreaBuffer.h" #include "../Common/DatabaseManager.h" #include @@ -35,6 +34,16 @@ DatabaseManager manager_; public: + class IFileContentVisitor : public boost::noncopyable + { + public: + virtual ~IFileContentVisitor() + { + } + + virtual void Assign(const std::string& content) = 0; + }; + explicit StorageBackend(IDatabaseFactory* factory); virtual ~StorageBackend() @@ -53,20 +62,26 @@ const std::string& uuid, const void* content, size_t size, - OrthancPluginContentType type); + OrthancPluginContentType type) ORTHANC_FINAL; - virtual void Read(StorageAreaBuffer& target, + virtual void Read(IFileContentVisitor& target, DatabaseManager::Transaction& transaction, const std::string& uuid, - OrthancPluginContentType type); + OrthancPluginContentType type) ORTHANC_FINAL; virtual void Remove(DatabaseManager::Transaction& transaction, const std::string& uuid, - OrthancPluginContentType type); + OrthancPluginContentType type) ORTHANC_FINAL; static void Register(OrthancPluginContext* context, StorageBackend* backend); // Takes ownership static void Finalize(); + + // For unit tests + void ReadToString(std::string& target, + DatabaseManager::Transaction& transaction, + const std::string& uuid, + OrthancPluginContentType type); }; } diff -r 90eb271f85b2 -r dd6cfc250747 MySQL/UnitTests/UnitTestsMain.cpp --- a/MySQL/UnitTests/UnitTestsMain.cpp Fri Mar 26 18:06:46 2021 +0100 +++ b/MySQL/UnitTests/UnitTestsMain.cpp Mon Mar 29 14:52:55 2021 +0200 @@ -163,8 +163,8 @@ storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown); } - OrthancDatabases::StorageAreaBuffer buffer(NULL /* we are running unit tests */); - ASSERT_THROW(storageArea.Read(buffer, transaction, "nope", OrthancPluginContentType_Unknown), + std::string buffer; + ASSERT_THROW(storageArea.ReadToString(buffer, transaction, "nope", OrthancPluginContentType_Unknown), Orthanc::OrthancException); ASSERT_EQ(10, CountFiles(db)); @@ -180,16 +180,13 @@ if (i == 5) { - ASSERT_THROW(storageArea.Read(buffer, transaction, uuid, OrthancPluginContentType_Unknown), + ASSERT_THROW(storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown), Orthanc::OrthancException); } else { - storageArea.Read(buffer, transaction, uuid, OrthancPluginContentType_Unknown); - - std::string s; - buffer.ToString(s); - ASSERT_EQ(expected, s); + storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown); + ASSERT_EQ(expected, buffer); } } diff -r 90eb271f85b2 -r dd6cfc250747 PostgreSQL/Plugins/PostgreSQLStorageArea.h --- a/PostgreSQL/Plugins/PostgreSQLStorageArea.h Fri Mar 26 18:06:46 2021 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLStorageArea.h Mon Mar 29 14:52:55 2021 +0200 @@ -40,17 +40,18 @@ { } - virtual Dialect GetDialect() const + virtual Dialect GetDialect() const ORTHANC_OVERRIDE { return Dialect_PostgreSQL; } - virtual IDatabase* Open() + virtual IDatabase* Open() ORTHANC_OVERRIDE { return that_.OpenInternal(); } - virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, unsigned int& connectionRetryInterval) + virtual void GetConnectionRetriesParameters(unsigned int& maxConnectionRetries, + unsigned int& connectionRetryInterval) ORTHANC_OVERRIDE { maxConnectionRetries = that_.parameters_.GetMaxConnectionRetries(); connectionRetryInterval = that_.parameters_.GetConnectionRetryInterval(); diff -r 90eb271f85b2 -r dd6cfc250747 PostgreSQL/UnitTests/PostgreSQLTests.cpp --- a/PostgreSQL/UnitTests/PostgreSQLTests.cpp Fri Mar 26 18:06:46 2021 +0100 +++ b/PostgreSQL/UnitTests/PostgreSQLTests.cpp Mon Mar 29 14:52:55 2021 +0200 @@ -366,8 +366,8 @@ storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown); } - StorageAreaBuffer buffer(NULL /* we are running unit tests */); - ASSERT_THROW(storageArea.Read(buffer, transaction, "nope", OrthancPluginContentType_Unknown), + std::string buffer; + ASSERT_THROW(storageArea.ReadToString(buffer, transaction, "nope", OrthancPluginContentType_Unknown), Orthanc::OrthancException); ASSERT_EQ(10, CountLargeObjects(db)); @@ -382,16 +382,13 @@ if (i == 5) { - ASSERT_THROW(storageArea.Read(buffer, transaction, uuid, OrthancPluginContentType_Unknown), + ASSERT_THROW(storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown), Orthanc::OrthancException); } else { - storageArea.Read(buffer, transaction, uuid, OrthancPluginContentType_Unknown); - - std::string s; - buffer.ToString(s); - ASSERT_EQ(expected, s); + storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown); + ASSERT_EQ(expected, buffer); } } diff -r 90eb271f85b2 -r dd6cfc250747 Resources/CMake/DatabasesPluginConfiguration.cmake --- a/Resources/CMake/DatabasesPluginConfiguration.cmake Fri Mar 26 18:06:46 2021 +0100 +++ b/Resources/CMake/DatabasesPluginConfiguration.cmake Mon Mar 29 14:52:55 2021 +0200 @@ -79,7 +79,6 @@ ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/DatabaseBackendAdapterV3.cpp ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/GlobalProperties.cpp ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/IndexBackend.cpp - ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/StorageAreaBuffer.cpp ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/StorageBackend.cpp ${ORTHANC_DATABASES_ROOT}/Resources/Orthanc/Databases/DatabaseConstraint.cpp ${ORTHANC_DATABASES_ROOT}/Resources/Orthanc/Databases/ISqlLookupFormatter.cpp