Mercurial > hg > orthanc
changeset 5210:6a7a244c777d db-protobuf
cont OrthancPluginDatabaseV4::Transaction
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sun, 02 Apr 2023 12:53:33 +0200 |
parents | 154d37a56500 |
children | 27e6ec2811e3 |
files | OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp |
diffstat | 1 files changed, 283 insertions(+), 110 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Fri Mar 31 17:33:33 2023 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Sun Apr 02 12:53:33 2023 +0200 @@ -49,18 +49,6 @@ } - static FileInfo Convert(const DatabasePluginMessages::FileInfo& source) - { - return FileInfo(source.uuid(), - static_cast<FileContentType>(source.content_type()), - source.uncompressed_size(), - source.uncompressed_hash(), - static_cast<CompressionType>(source.compression_type()), - source.compressed_size(), - source.compressed_hash()); - } - - static ResourceType Convert(DatabasePluginMessages::ResourceType type) { switch (type) @@ -105,6 +93,42 @@ } + static FileInfo Convert(const DatabasePluginMessages::FileInfo& source) + { + return FileInfo(source.uuid(), + static_cast<FileContentType>(source.content_type()), + source.uncompressed_size(), + source.uncompressed_hash(), + static_cast<CompressionType>(source.compression_type()), + source.compressed_size(), + source.compressed_hash()); + } + + + static ServerIndexChange Convert(const DatabasePluginMessages::ServerIndexChange& source) + { + return ServerIndexChange(source.seq(), + static_cast<ChangeType>(source.change_type()), + Convert(source.resource_type()), + source.public_id(), + source.date()); + } + + + static ExportedResource Convert(const DatabasePluginMessages::ExportedResource& source) + { + return ExportedResource(source.seq(), + Convert(source.resource_type()), + source.public_id(), + source.modality(), + source.date(), + source.patient_id(), + source.study_instance_uid(), + source.series_instance_uid(), + source.sop_instance_uid()); + } + + static void Execute(DatabasePluginMessages::Response& response, const OrthancPluginDatabaseV4& database, const DatabasePluginMessages::Request& request) @@ -173,6 +197,14 @@ } + void ExecuteTransaction(DatabasePluginMessages::TransactionResponse& response, + DatabasePluginMessages::TransactionOperation operation) + { + DatabasePluginMessages::TransactionRequest request; // Ignored + ExecuteTransaction(response, operation, request); + } + + void ExecuteTransaction(DatabasePluginMessages::TransactionOperation operation, const DatabasePluginMessages::TransactionRequest& request) { @@ -202,13 +234,11 @@ virtual ~Transaction() { - { - DatabasePluginMessages::DatabaseRequest request; - request.mutable_finalize_transaction()->set_transaction(reinterpret_cast<intptr_t>(transaction_)); + DatabasePluginMessages::DatabaseRequest request; + request.mutable_finalize_transaction()->set_transaction(reinterpret_cast<intptr_t>(transaction_)); - DatabasePluginMessages::DatabaseResponse response; - ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request); - } + DatabasePluginMessages::DatabaseResponse response; + ExecuteDatabase(response, database_, DatabasePluginMessages::OPERATION_FINALIZE_TRANSACTION, request); } @@ -220,12 +250,10 @@ virtual void Commit(int64_t fileSizeDelta) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_commit()->set_file_size_delta(fileSizeDelta); + DatabasePluginMessages::TransactionRequest request; + request.mutable_commit()->set_file_size_delta(fileSizeDelta); - ExecuteTransaction(DatabasePluginMessages::OPERATION_COMMIT, request); - } + ExecuteTransaction(DatabasePluginMessages::OPERATION_COMMIT, request); } @@ -233,20 +261,18 @@ const FileInfo& attachment, int64_t revision) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_add_attachment()->set_id(id); - request.mutable_add_attachment()->mutable_attachment()->set_uuid(attachment.GetUuid()); - request.mutable_add_attachment()->mutable_attachment()->set_content_type(attachment.GetContentType()); - request.mutable_add_attachment()->mutable_attachment()->set_uncompressed_size(attachment.GetUncompressedSize()); - request.mutable_add_attachment()->mutable_attachment()->set_uncompressed_hash(attachment.GetUncompressedMD5()); - request.mutable_add_attachment()->mutable_attachment()->set_compression_type(attachment.GetCompressionType()); - request.mutable_add_attachment()->mutable_attachment()->set_compressed_size(attachment.GetCompressedSize()); - request.mutable_add_attachment()->mutable_attachment()->set_compressed_hash(attachment.GetCompressedMD5()); - request.mutable_add_attachment()->set_revision(revision); + DatabasePluginMessages::TransactionRequest request; + request.mutable_add_attachment()->set_id(id); + request.mutable_add_attachment()->mutable_attachment()->set_uuid(attachment.GetUuid()); + request.mutable_add_attachment()->mutable_attachment()->set_content_type(attachment.GetContentType()); + request.mutable_add_attachment()->mutable_attachment()->set_uncompressed_size(attachment.GetUncompressedSize()); + request.mutable_add_attachment()->mutable_attachment()->set_uncompressed_hash(attachment.GetUncompressedMD5()); + request.mutable_add_attachment()->mutable_attachment()->set_compression_type(attachment.GetCompressionType()); + request.mutable_add_attachment()->mutable_attachment()->set_compressed_size(attachment.GetCompressedSize()); + request.mutable_add_attachment()->mutable_attachment()->set_compressed_hash(attachment.GetCompressedMD5()); + request.mutable_add_attachment()->set_revision(revision); - ExecuteTransaction(DatabasePluginMessages::OPERATION_ADD_ATTACHMENT, request); - } + ExecuteTransaction(DatabasePluginMessages::OPERATION_ADD_ATTACHMENT, request); } @@ -265,84 +291,76 @@ virtual void DeleteAttachment(int64_t id, FileContentType attachment) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_delete_attachment()->set_id(id); - request.mutable_delete_attachment()->set_type(attachment); + DatabasePluginMessages::TransactionRequest request; + request.mutable_delete_attachment()->set_id(id); + request.mutable_delete_attachment()->set_type(attachment); - DatabasePluginMessages::TransactionResponse response; - ExecuteTransaction(response, DatabasePluginMessages::OPERATION_DELETE_ATTACHMENT, request); + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_DELETE_ATTACHMENT, request); - listener_.SignalAttachmentDeleted(Convert(response.delete_attachment().deleted_attachment())); - } + listener_.SignalAttachmentDeleted(Convert(response.delete_attachment().deleted_attachment())); } virtual void DeleteMetadata(int64_t id, MetadataType type) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_delete_metadata()->set_id(id); - request.mutable_delete_metadata()->set_type(type); + DatabasePluginMessages::TransactionRequest request; + request.mutable_delete_metadata()->set_id(id); + request.mutable_delete_metadata()->set_type(type); - ExecuteTransaction(DatabasePluginMessages::OPERATION_DELETE_METADATA, request); - } + ExecuteTransaction(DatabasePluginMessages::OPERATION_DELETE_METADATA, request); } virtual void DeleteResource(int64_t id) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_delete_resource()->set_id(id); + DatabasePluginMessages::TransactionRequest request; + request.mutable_delete_resource()->set_id(id); - DatabasePluginMessages::TransactionResponse response; - ExecuteTransaction(response, DatabasePluginMessages::OPERATION_DELETE_RESOURCE, request); + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_DELETE_RESOURCE, request); - for (int i = 0; i < response.delete_resource().deleted_attachments().size(); i++) - { - listener_.SignalAttachmentDeleted(Convert(response.delete_resource().deleted_attachments(i))); - } + for (int i = 0; i < response.delete_resource().deleted_attachments().size(); i++) + { + listener_.SignalAttachmentDeleted(Convert(response.delete_resource().deleted_attachments(i))); + } - for (int i = 0; i < response.delete_resource().deleted_resources().size(); i++) - { - listener_.SignalResourceDeleted(Convert(response.delete_resource().deleted_resources(i).level()), - response.delete_resource().deleted_resources(i).public_id()); - } + for (int i = 0; i < response.delete_resource().deleted_resources().size(); i++) + { + listener_.SignalResourceDeleted(Convert(response.delete_resource().deleted_resources(i).level()), + response.delete_resource().deleted_resources(i).public_id()); + } - if (response.delete_resource().is_remaining_ancestor()) - { - listener_.SignalRemainingAncestor(Convert(response.delete_resource().remaining_ancestor().level()), - response.delete_resource().remaining_ancestor().public_id()); - } - } + if (response.delete_resource().is_remaining_ancestor()) + { + listener_.SignalRemainingAncestor(Convert(response.delete_resource().remaining_ancestor().level()), + response.delete_resource().remaining_ancestor().public_id()); + } } virtual void GetAllMetadata(std::map<MetadataType, std::string>& target, int64_t id) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_get_all_metadata()->set_id(id); + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_all_metadata()->set_id(id); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_METADATA, request); - DatabasePluginMessages::TransactionResponse response; - ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_METADATA, request); - - target.clear(); - for (int i = 0; i < response.get_all_metadata().metadata().size(); i++) + target.clear(); + for (int i = 0; i < response.get_all_metadata().metadata().size(); i++) + { + MetadataType key = static_cast<MetadataType>(response.get_all_metadata().metadata(i).type()); + + if (target.find(key) == target.end()) { - MetadataType key = static_cast<MetadataType>(response.get_all_metadata().metadata(i).type()); - - if (target.find(key) == target.end()) - { - target[key] = response.get_all_metadata().metadata(i).value(); - } - else - { - throw OrthancException(ErrorCode_DatabasePlugin); - } + target[key] = response.get_all_metadata().metadata(i).value(); + } + else + { + throw OrthancException(ErrorCode_DatabasePlugin); } } } @@ -351,18 +369,16 @@ virtual void GetAllPublicIds(std::list<std::string>& target, ResourceType resourceType) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_get_all_public_ids()->set_resource_type(Convert(resourceType)); + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_all_public_ids()->set_resource_type(Convert(resourceType)); - DatabasePluginMessages::TransactionResponse response; - ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS, request); + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS, request); - target.clear(); - for (int i = 0; i < response.get_all_public_ids().ids().size(); i++) - { - target.push_back(response.get_all_public_ids().ids(i)); - } + target.clear(); + for (int i = 0; i < response.get_all_public_ids().ids().size(); i++) + { + target.push_back(response.get_all_public_ids().ids(i)); } } @@ -372,20 +388,18 @@ size_t since, size_t limit) ORTHANC_OVERRIDE { - { - DatabasePluginMessages::TransactionRequest request; - request.mutable_get_all_public_ids_with_limits()->set_resource_type(Convert(resourceType)); - request.mutable_get_all_public_ids_with_limits()->set_since(since); - request.mutable_get_all_public_ids_with_limits()->set_limit(limit); + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_all_public_ids_with_limits()->set_resource_type(Convert(resourceType)); + request.mutable_get_all_public_ids_with_limits()->set_since(since); + request.mutable_get_all_public_ids_with_limits()->set_limit(limit); - DatabasePluginMessages::TransactionResponse response; - ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS_WITH_LIMITS, request); + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS_WITH_LIMITS, request); - target.clear(); - for (int i = 0; i < response.get_all_public_ids_with_limits().ids().size(); i++) - { - target.push_back(response.get_all_public_ids_with_limits().ids(i)); - } + target.clear(); + for (int i = 0; i < response.get_all_public_ids_with_limits().ids().size(); i++) + { + target.push_back(response.get_all_public_ids_with_limits().ids(i)); } } @@ -395,18 +409,54 @@ int64_t since, uint32_t maxResults) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_changes()->set_since(since); + request.mutable_get_changes()->set_limit(maxResults); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_CHANGES, request); + + done = response.get_changes().done(); + + target.clear(); + for (int i = 0; i < response.get_changes().changes().size(); i++) + { + target.push_back(Convert(response.get_changes().changes(i))); + } } virtual void GetChildrenInternalId(std::list<int64_t>& target, int64_t id) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_children_internal_id()->set_id(id); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_CHILDREN_INTERNAL_ID, request); + + target.clear(); + for (int i = 0; i < response.get_children_internal_id().ids().size(); i++) + { + target.push_back(response.get_children_internal_id().ids(i)); + } } virtual void GetChildrenPublicId(std::list<std::string>& target, int64_t id) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_children_public_id()->set_id(id); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_CHILDREN_PUBLIC_ID, request); + + target.clear(); + for (int i = 0; i < response.get_children_public_id().ids().size(); i++) + { + target.push_back(response.get_children_public_id().ids(i)); + } } @@ -415,69 +465,192 @@ int64_t since, uint32_t maxResults) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_exported_resources()->set_since(since); + request.mutable_get_exported_resources()->set_limit(maxResults); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_EXPORTED_RESOURCES, request); + + done = response.get_exported_resources().done(); + + target.clear(); + for (int i = 0; i < response.get_exported_resources().resources().size(); i++) + { + target.push_back(Convert(response.get_exported_resources().resources(i))); + } } virtual void GetLastChange(std::list<ServerIndexChange>& target /*out*/) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_LAST_CHANGE); + + target.clear(); + if (response.get_last_change().found()) + { + target.push_back(Convert(response.get_last_change().change())); + } } virtual void GetLastExportedResource(std::list<ExportedResource>& target /*out*/) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_LAST_EXPORTED_RESOURCE); + + target.clear(); + if (response.get_last_exported_resource().found()) + { + target.push_back(Convert(response.get_last_exported_resource().resource())); + } } virtual void GetMainDicomTags(DicomMap& target, int64_t id) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_main_dicom_tags()->set_id(id); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_MAIN_DICOM_TAGS, request); + + target.Clear(); + + for (int i = 0; i < response.get_main_dicom_tags().tags().size(); i++) + { + const DatabasePluginMessages::GetMainDicomTags_Response_Tag& tag = response.get_main_dicom_tags().tags(i); + if (tag.group() > 0xffffu || + tag.element() > 0xffffu) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + target.SetValue(tag.group(), tag.element(), tag.value(), false); + } + } } virtual std::string GetPublicId(int64_t resourceId) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_public_id()->set_id(resourceId); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_PUBLIC_ID, request); + return response.get_public_id().id(); } virtual uint64_t GetResourcesCount(ResourceType resourceType) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_resources_count()->set_type(Convert(resourceType)); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_RESOURCES_COUNT, request); + return response.get_resources_count().count(); } virtual ResourceType GetResourceType(int64_t resourceId) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_get_resource_type()->set_id(resourceId); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_RESOURCE_TYPE, request); + return Convert(response.get_resource_type().type()); } virtual uint64_t GetTotalCompressedSize() ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_TOTAL_COMPRESSED_SIZE); + return response.get_total_compressed_size().size(); } virtual uint64_t GetTotalUncompressedSize() ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_GET_TOTAL_UNCOMPRESSED_SIZE); + return response.get_total_uncompressed_size().size(); } virtual bool IsProtectedPatient(int64_t internalId) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_is_protected_patient()->set_patient_id(internalId); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_IS_PROTECTED_PATIENT, request); + return response.is_protected_patient().protected_patient(); } virtual void ListAvailableAttachments(std::set<FileContentType>& target, int64_t id) ORTHANC_OVERRIDE { + DatabasePluginMessages::TransactionRequest request; + request.mutable_list_available_attachments()->set_id(id); + + DatabasePluginMessages::TransactionResponse response; + ExecuteTransaction(response, DatabasePluginMessages::OPERATION_LIST_AVAILABLE_ATTACHMENTS, request); + + target.clear(); + for (int i = 0; i < response.list_available_attachments().attachments().size(); i++) + { + FileContentType attachment = static_cast<FileContentType>(response.list_available_attachments().attachments(i)); + + if (target.find(attachment) == target.end()) + { + target.insert(attachment); + } + else + { + throw OrthancException(ErrorCode_DatabasePlugin); + } + } } virtual void LogChange(int64_t internalId, const ServerIndexChange& change) ORTHANC_OVERRIDE { + // TODO => Simplify "IDatabaseWrapper" + + DatabasePluginMessages::TransactionRequest request; + request.mutable_log_change()->set_change_type(change.GetChangeType()); + request.mutable_log_change()->set_resource_id(internalId); + request.mutable_log_change()->set_resource_type(Convert(change.GetResourceType())); + request.mutable_log_change()->set_date(change.GetDate()); + + ExecuteTransaction(DatabasePluginMessages::OPERATION_LOG_CHANGE, request); } virtual void LogExportedResource(const ExportedResource& resource) ORTHANC_OVERRIDE { + // TODO: "seq" is ignored, could be simplified in "ExportedResource" + + DatabasePluginMessages::TransactionRequest request; + request.mutable_log_exported_resource()->set_resource_type(Convert(resource.GetResourceType())); + request.mutable_log_exported_resource()->set_public_id(resource.GetPublicId()); + request.mutable_log_exported_resource()->set_modality(resource.GetModality()); + request.mutable_log_exported_resource()->set_date(resource.GetDate()); + request.mutable_log_exported_resource()->set_patient_id(resource.GetPatientId()); + request.mutable_log_exported_resource()->set_study_instance_uid(resource.GetStudyInstanceUid()); + request.mutable_log_exported_resource()->set_series_instance_uid(resource.GetSeriesInstanceUid()); + request.mutable_log_exported_resource()->set_sop_instance_uid(resource.GetSopInstanceUid()); + + ExecuteTransaction(DatabasePluginMessages::OPERATION_LOG_EXPORTED_RESOURCE, request); }