# HG changeset patch # User Sebastien Jodogne # Date 1730217473 0 # Node ID ea547160f27eb3149fd5e3b9389d6a7b3fbd47a9 # Parent 6a4e47163b3ce2a0fd712b8751a04d661a05e890 StatelessDatabaseOperations: reimplementing LookupAttachment() diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp --- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -1665,9 +1665,14 @@ target->AddLabel(source.labels(i)); } + if (source.attachments().size() != source.attachments_revisions().size()) + { + throw OrthancException(ErrorCode_DatabasePlugin); + } + for (int i = 0; i < source.attachments().size(); i++) { - target->AddAttachment(Convert(source.attachments(i))); + target->AddAttachment(Convert(source.attachments(i)), source.attachments_revisions(i)); } Convert(*target, ResourceType_Patient, source.patient_content()); diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto --- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto Tue Oct 29 15:57:53 2024 +0000 @@ -961,6 +961,7 @@ string one_instance_public_id = 13; repeated Metadata one_instance_metadata = 14; repeated FileInfo one_instance_attachments = 15; + repeated int64 attachments_revisions = 16; } } diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/Compatibility/GenericFind.cpp --- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -497,7 +497,7 @@ if (transaction_.LookupAttachment(info, revision, internalId, *it) && info.GetContentType() == *it) { - resource->AddAttachment(info); + resource->AddAttachment(info, revision); } else { diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/FindResponse.cpp --- a/OrthancServer/Sources/Database/FindResponse.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/FindResponse.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -455,11 +455,14 @@ } - void FindResponse::Resource::AddAttachment(const FileInfo& attachment) + void FindResponse::Resource::AddAttachment(const FileInfo& attachment, + int64_t revision) { - if (attachments_.find(attachment.GetContentType()) == attachments_.end()) + if (attachments_.find(attachment.GetContentType()) == attachments_.end() && + revisions_.find(attachment.GetContentType()) == revisions_.end()) { attachments_[attachment.GetContentType()] = attachment; + revisions_[attachment.GetContentType()] = revision; } else { @@ -468,17 +471,36 @@ } - bool FindResponse::Resource::LookupAttachment(FileInfo& target, FileContentType type) const + bool FindResponse::Resource::LookupAttachment(FileInfo& target, + int64_t& revision, + FileContentType type) const { std::map::const_iterator it = attachments_.find(type); + std::map::const_iterator it2 = revisions_.find(type); + if (it != attachments_.end()) { - target = it->second; - return true; + if (it2 != revisions_.end()) + { + target = it->second; + revision = it2->second; + return true; + } + else + { + throw OrthancException(ErrorCode_InternalError); + } } else { - return false; + if (it2 == revisions_.end()) + { + return false; + } + else + { + throw OrthancException(ErrorCode_InternalError); + } } } diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/FindResponse.h --- a/OrthancServer/Sources/Database/FindResponse.h Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/FindResponse.h Tue Oct 29 15:57:53 2024 +0000 @@ -123,6 +123,7 @@ ChildrenInformation childrenInstancesInformation_; std::set labels_; std::map attachments_; + std::map revisions_; bool hasOneInstanceMetadataAndAttachments_; std::string oneInstancePublicId_; std::map oneInstanceMetadata_; @@ -263,9 +264,11 @@ return labels_; } - void AddAttachment(const FileInfo& attachment); + void AddAttachment(const FileInfo& attachment, + int64_t revision); bool LookupAttachment(FileInfo& target, + int64_t& revision, FileContentType type) const; const std::map& GetAttachments() const diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp --- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -987,7 +987,7 @@ s.ColumnInt64(C8_BIG_INT_1), s.ColumnString(C4_STRING_2), static_cast(s.ColumnInt(C7_INT_2)), s.ColumnInt64(C9_BIG_INT_2), s.ColumnString(C5_STRING_3)); - res.AddAttachment(file); + res.AddAttachment(file, 0 /* TODO - REVISIONS */); }; break; case QUERY_MAIN_DICOM_TAGS: diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h --- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h Tue Oct 29 15:57:53 2024 +0000 @@ -102,8 +102,8 @@ virtual bool HasIntegratedFind() const ORTHANC_OVERRIDE { - return true; // => This uses specialized SQL commands - //return false; // => This uses Compatibility/GenericFind + //return true; // => This uses specialized SQL commands + return false; // => This uses Compatibility/GenericFind } /** diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp --- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -599,7 +599,14 @@ } else if (response.GetSize() == 1) { - target = response.GetResourceByIndex(0).GetMetadata(level); + if (response.GetResourceByIndex(0).GetLevel() != level) + { + throw OrthancException(ErrorCode_DatabasePlugin); + } + else + { + target = response.GetResourceByIndex(0).GetMetadata(level); + } } else { @@ -611,43 +618,35 @@ bool StatelessDatabaseOperations::LookupAttachment(FileInfo& attachment, int64_t& revision, ResourceType level, - const std::string& instancePublicId, + const std::string& publicId, FileContentType contentType) { - class Operations : public ReadOnlyOperationsT6 + FindRequest request(level); + request.SetOrthancId(level, publicId); + request.SetRetrieveAttachments(true); + + FindResponse response; + ExecuteFind(response, request); + + if (response.GetSize() == 0) { - public: - virtual void ApplyTuple(ReadOnlyTransaction& transaction, - const Tuple& tuple) ORTHANC_OVERRIDE + throw OrthancException(ErrorCode_UnknownResource); + } + else if (response.GetSize() == 1) + { + if (response.GetResourceByIndex(0).GetLevel() != level) { - int64_t internalId; - ResourceType type; - if (!transaction.LookupResource(internalId, type, tuple.get<3>())) - { - throw OrthancException(ErrorCode_UnknownResource); - } - - if (type != tuple.get<5>()) - { - throw OrthancException(ErrorCode_DatabasePlugin); - } - - if (transaction.LookupAttachment(tuple.get<1>(), tuple.get<2>(), internalId, tuple.get<4>())) - { - assert(tuple.get<1>().GetContentType() == tuple.get<4>()); - tuple.get<0>() = true; - } - else - { - tuple.get<0>() = false; - } + throw OrthancException(ErrorCode_DatabasePlugin); + } + else + { + return response.GetResourceByIndex(0).LookupAttachment(attachment, revision, contentType); } - }; - - bool found; - Operations operations; - operations.Apply(*this, found, attachment, revision, instancePublicId, contentType, level); - return found; + } + else + { + throw OrthancException(ErrorCode_DatabasePlugin); + } } diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/Database/StatelessDatabaseOperations.h --- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h Tue Oct 29 15:57:53 2024 +0000 @@ -541,7 +541,7 @@ bool LookupAttachment(FileInfo& attachment, int64_t& revision, ResourceType level, - const std::string& instancePublicId, + const std::string& publicId, FileContentType contentType); void GetChanges(Json::Value& target, diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/OrthancWebDav.cpp --- a/OrthancServer/Sources/OrthancWebDav.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/OrthancWebDav.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -145,7 +145,8 @@ if (resource.GetLevel() == ResourceType_Instance) { FileInfo info; - if (resource.LookupAttachment(info, FileContentType_Dicom)) + int64_t revision; + if (resource.LookupAttachment(info, revision, FileContentType_Dicom)) { std::unique_ptr f(new File(uid + ".dcm")); f->SetMimeType(MimeType_Dicom); diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/Sources/ResourceFinder.cpp --- a/OrthancServer/Sources/ResourceFinder.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/Sources/ResourceFinder.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -326,7 +326,8 @@ if (responseContent_ & ResponseContentFlags_AttachmentsLegacy) { FileInfo info; - if (resource.LookupAttachment(info, FileContentType_Dicom)) + int64_t revision; + if (resource.LookupAttachment(info, revision, FileContentType_Dicom)) { target["FileSize"] = static_cast(info.GetUncompressedSize()); target["FileUuid"] = info.GetUuid(); diff -r 6a4e47163b3c -r ea547160f27e OrthancServer/UnitTestsSources/ServerIndexTests.cpp --- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp Tue Oct 29 15:21:15 2024 +0000 +++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp Tue Oct 29 15:57:53 2024 +0000 @@ -869,7 +869,7 @@ } FileInfo dicom1, pixelData1; - int64_t revision; + int64_t revision = -1; ASSERT_TRUE(context.GetIndex().LookupAttachment(dicom1, revision, ResourceType_Instance, id, FileContentType_Dicom)); ASSERT_EQ(0, revision); revision = -1;