diff OrthancServer/Sources/ServerContext.cpp @ 4627:f7d5372b59b3 db-changes

handling revisions of attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Apr 2021 15:11:59 +0200
parents 95ffe3b6ef7c
children 88e892e25a51
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerContext.cpp	Mon Apr 19 10:28:43 2021 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Tue Apr 20 15:11:59 2021 +0200
@@ -753,13 +753,16 @@
                                        FileContentType content)
   {
     FileInfo attachment;
-    if (!index_.LookupAttachment(attachment, resourceId, content))
+    int64_t revision;
+    if (!index_.LookupAttachment(attachment, revision, resourceId, content))
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
-
-    StorageAccessor accessor(area_, GetMetricsRegistry());
-    accessor.AnswerFile(output, attachment, GetFileContentMime(content));
+    else
+    {
+      StorageAccessor accessor(area_, GetMetricsRegistry());
+      accessor.AnswerFile(output, attachment, GetFileContentMime(content));
+    }
   }
 
 
@@ -773,7 +776,8 @@
               << compression; 
 
     FileInfo attachment;
-    if (!index_.LookupAttachment(attachment, resourceId, attachmentType))
+    int64_t revision;
+    if (!index_.LookupAttachment(attachment, revision, resourceId, attachmentType))
     {
       throw OrthancException(ErrorCode_UnknownResource);
     }
@@ -794,7 +798,8 @@
 
     try
     {
-      StoreStatus status = index_.AddAttachment(modified, resourceId);
+      int64_t newRevision;  // ignored
+      StoreStatus status = index_.AddAttachment(newRevision, modified, resourceId, true, revision);
       if (status != StoreStatus_Success)
       {
         accessor.Remove(modified);
@@ -833,8 +838,9 @@
      **/
     
     FileInfo attachment;
+    int64_t revision;
 
-    if (index_.LookupAttachment(attachment, instancePublicId, FileContentType_DicomUntilPixelData))
+    if (index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_DicomUntilPixelData))
     {
       std::string dicom;
 
@@ -893,7 +899,7 @@
 
       if (hasPixelDataOffset &&
           area_.HasReadRange() &&
-          index_.LookupAttachment(attachment, instancePublicId, FileContentType_Dicom) &&
+          index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_Dicom) &&
           attachment.GetCompressionType() == CompressionType_None)
       {
         /**
@@ -922,7 +928,7 @@
         }
       }
       else if (ignoreTagLength.empty() &&
-               index_.LookupAttachment(attachment, instancePublicId, FileContentType_DicomAsJson))
+               index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_DicomAsJson))
       {
         /**
          * CASE 3: This instance was created using Orthanc <=
@@ -978,8 +984,10 @@
             if (!area_.HasReadRange() ||
                 compressionEnabled_)
             {
-              AddAttachment(instancePublicId, FileContentType_DicomUntilPixelData,
-                            dicom.empty() ? NULL: dicom.c_str(), pixelDataOffset);
+              int64_t newRevision;
+              AddAttachment(newRevision, instancePublicId, FileContentType_DicomUntilPixelData,
+                            dicom.empty() ? NULL: dicom.c_str(), pixelDataOffset,
+                            false /* no old revision */, -1 /* dummy revision */);
             }
           }
         }
@@ -999,7 +1007,8 @@
   void ServerContext::ReadDicom(std::string& dicom,
                                 const std::string& instancePublicId)
   {
-    ReadAttachment(dicom, instancePublicId, FileContentType_Dicom, true /* uncompress */);
+    int64_t revision;
+    ReadAttachment(dicom, revision, instancePublicId, FileContentType_Dicom, true /* uncompress */);
   }
     
 
@@ -1012,14 +1021,14 @@
     }
     
     FileInfo attachment;
-    if (!index_.LookupAttachment(attachment, instancePublicId, FileContentType_Dicom))
+    int64_t revision;  // Ignored
+    if (!index_.LookupAttachment(attachment, revision, instancePublicId, FileContentType_Dicom))
     {
       throw OrthancException(ErrorCode_InternalError,
                              "Unable to read the DICOM file of instance " + instancePublicId);
     }
 
     std::string s;
-    int64_t revision;  // Ignored
 
     if (attachment.GetCompressionType() == CompressionType_None &&
         index_.LookupMetadata(s, revision, instancePublicId, ResourceType_Instance,
@@ -1046,12 +1055,13 @@
   
 
   void ServerContext::ReadAttachment(std::string& result,
+                                     int64_t& revision,
                                      const std::string& instancePublicId,
                                      FileContentType content,
                                      bool uncompressIfNeeded)
   {
     FileInfo attachment;
-    if (!index_.LookupAttachment(attachment, instancePublicId, content))
+    if (!index_.LookupAttachment(attachment, revision, instancePublicId, content))
     {
       throw OrthancException(ErrorCode_InternalError,
                              "Unable to read attachment " + EnumerationToString(content) +
@@ -1147,10 +1157,13 @@
   }
 
 
-  bool ServerContext::AddAttachment(const std::string& resourceId,
+  bool ServerContext::AddAttachment(int64_t& newRevision,
+                                    const std::string& resourceId,
                                     FileContentType attachmentType,
                                     const void* data,
-                                    size_t size)
+                                    size_t size,
+                                    bool hasOldRevision,
+                                    int64_t oldRevision)
   {
     LOG(INFO) << "Adding attachment " << EnumerationToString(attachmentType) << " to resource " << resourceId;
     
@@ -1160,7 +1173,7 @@
     StorageAccessor accessor(area_, GetMetricsRegistry());
     FileInfo attachment = accessor.Write(data, size, attachmentType, compression, storeMD5_);
 
-    StoreStatus status = index_.AddAttachment(attachment, resourceId);
+    StoreStatus status = index_.AddAttachment(newRevision, attachment, resourceId, hasOldRevision, oldRevision);
     if (status != StoreStatus_Success)
     {
       accessor.Remove(attachment);