changeset 5955:34ba0344f240

/archive: fix file ordering
author Alain Mazy <am@orthanc.team>
date Thu, 16 Jan 2025 12:16:35 +0100
parents 4f726a09798e
children db4bae374968
files NEWS OrthancServer/CMakeLists.txt OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/SimpleInstanceOrdering.cpp OrthancServer/Sources/SimpleInstanceOrdering.h
diffstat 5 files changed, 276 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Jan 15 09:28:27 2025 +0100
+++ b/NEWS	Thu Jan 16 12:16:35 2025 +0100
@@ -22,6 +22,9 @@
 * Housekeeper plugin: fixed the "Force" configuration that was ineffective.
 * Housekeeper plugin: allow transcoding to lossy transfer syntax.  Orthanc will keep
   the SOPInstanceUID untouched in this case.
+* /archive: The numbers in the filenames now match the InstanceNumber when possible.
+  When not, the files are now, at least, ordered in the same order as the instances 
+  in the series.
 
 
 Plugins
--- a/OrthancServer/CMakeLists.txt	Wed Jan 15 09:28:27 2025 +0100
+++ b/OrthancServer/CMakeLists.txt	Thu Jan 16 12:16:35 2025 +0100
@@ -154,6 +154,7 @@
   ${CMAKE_SOURCE_DIR}/Sources/ServerJobs/StorageCommitmentScpJob.cpp
   ${CMAKE_SOURCE_DIR}/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp  
   ${CMAKE_SOURCE_DIR}/Sources/ServerToolbox.cpp
+  ${CMAKE_SOURCE_DIR}/Sources/SimpleInstanceOrdering.cpp
   ${CMAKE_SOURCE_DIR}/Sources/SliceOrdering.cpp
   ${CMAKE_SOURCE_DIR}/Sources/StorageCommitmentReports.cpp
   )
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Wed Jan 15 09:28:27 2025 +0100
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Thu Jan 16 12:16:35 2025 +0100
@@ -33,6 +33,7 @@
 #include "../../../OrthancFramework/Sources/MultiThreading/Semaphore.h"
 #include "../OrthancConfiguration.h"
 #include "../ServerContext.h"
+#include "../SimpleInstanceOrdering.h"
 
 #include <stdio.h>
 #include <boost/range/algorithm/count.hpp>
@@ -508,6 +509,7 @@
     virtual void Close() = 0;
 
     virtual void AddInstance(const std::string& instanceId,
+                             uint32_t index,
                              uint64_t uncompressedSize) = 0;
   };
 
@@ -518,12 +520,21 @@
     struct Instance
     {
       std::string  id_;
-      uint64_t     uncompressedSize_;
+      uint32_t     index_;
+      FileInfo     fileInfo_;
 
       Instance(const std::string& id,
-               uint64_t uncompressedSize) : 
+               uint32_t index,
+               FileInfo fileInfo) : 
         id_(id),
-        uncompressedSize_(uncompressedSize)
+        index_(index),
+        fileInfo_(fileInfo.GetUuid(),
+                  fileInfo.GetContentType(),
+                  fileInfo.GetUncompressedSize(),
+                  fileInfo.GetUncompressedMD5(),
+                  fileInfo.GetCompressionType(),
+                  fileInfo.GetCompressedSize(),
+                  fileInfo.GetCompressedMD5())
       {
       }
     };
@@ -536,22 +547,18 @@
     std::list<Instance>  instances_;   // Only at instance level
 
 
-    void AddResourceToExpand(ServerIndex& index,
-                             const std::string& id)
+    void AddResourceToExpand(const std::string& id)
     {
-      if (level_ == ArchiveResourceType_Instance)
-      {
-        FileInfo tmp;
-        int64_t revision;  // ignored
-        if (index.LookupAttachment(tmp, revision, ResourceType_Instance, id, FileContentType_Dicom))
-        {
-          instances_.push_back(Instance(id, tmp.GetUncompressedSize()));
-        }
-      }
-      else
-      {
-        resources_[id] = NULL;
-      }
+      assert(level_ != ArchiveResourceType_Instance);
+      resources_[id] = NULL;
+    }
+
+    void AddInstance(const std::string& id,
+                     uint32_t indexInSeries,
+                     const FileInfo& fileInfo)
+    {
+      assert(level_ == ArchiveResourceType_Instance);
+      instances_.push_back(Instance(id, indexInSeries, fileInfo));
     }
 
 
@@ -579,7 +586,7 @@
 
       if (level_ == ArchiveResourceType_Instance)
       {
-        AddResourceToExpand(index, id);
+        AddResourceToExpand(id);
       }
       else if (resource.GetLevel() == GetResourceLevel(level_))
       {
@@ -622,16 +629,31 @@
       {
         if (it->second == NULL)
         {
-          // This is resource is marked for expansion
-          std::list<std::string> children;
-          index.GetChildren(children, GetResourceLevel(level_), it->first);
-
+          // This resource is marked for expansion
           std::unique_ptr<ArchiveIndex> child(new ArchiveIndex(GetChildResourceType(level_)));
 
-          for (std::list<std::string>::const_iterator 
-                 it2 = children.begin(); it2 != children.end(); ++it2)
+          if (level_ == ArchiveResourceType_Series)
           {
-            child->AddResourceToExpand(index, *it2);
+            // Instances ordering is important !  
+            // From 1.12.6, when possible, the id in the filename will match the index in series.
+            // Only if there are duplicate index in series, we'll use a simple counter
+            SimpleInstanceOrdering orderedInstances(index, it->first);
+
+            for (size_t i = 0; i < orderedInstances.GetInstancesCount(); ++i)
+            {
+              child->AddInstance(orderedInstances.GetInstanceId(i), orderedInstances.GetInstanceIndexInSeries(i), orderedInstances.GetInstanceFileInfo(i));
+            }
+          }
+          else
+          {
+            std::list<std::string> children;
+            index.GetChildren(children, GetResourceLevel(level_), it->first);
+
+            for (std::list<std::string>::const_iterator 
+                  it2 = children.begin(); it2 != children.end(); ++it2)
+            {
+              child->AddResourceToExpand(*it2);
+            }
           }
 
           it->second = child.release();
@@ -650,7 +672,7 @@
         for (std::list<Instance>::const_iterator 
                it = instances_.begin(); it != instances_.end(); ++it)
         {
-          visitor.AddInstance(it->id_, it->uncompressedSize_);
+          visitor.AddInstance(it->id_,  it->index_, it->fileInfo_.GetUncompressedSize());
         }          
       }
       else
@@ -984,11 +1006,11 @@
     }
 
     virtual void AddInstance(const std::string& instanceId,
+                             uint32_t index,
                              uint64_t uncompressedSize) ORTHANC_OVERRIDE
     {
       char filename[24];
-      snprintf(filename, sizeof(filename) - 1, instanceFormat_, counter_);
-      counter_ ++;
+      snprintf(filename, sizeof(filename) - 1, instanceFormat_, index);
 
       commands_.AddWriteInstance(filename, instanceId, uncompressedSize);
     }
@@ -1018,6 +1040,7 @@
     }
 
     virtual void AddInstance(const std::string& instanceId,
+                             uint32_t indexNotUsed,
                              uint64_t uncompressedSize) ORTHANC_OVERRIDE
     {
       // "DICOM restricts the filenames on DICOM media to 8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Sources/SimpleInstanceOrdering.cpp	Thu Jan 16 12:16:35 2025 +0100
@@ -0,0 +1,159 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "PrecompiledHeadersServer.h"
+#include "SimpleInstanceOrdering.h"
+
+#include "../../OrthancFramework/Sources/Logging.h"
+#include "../../OrthancFramework/Sources/Toolbox.h"
+#include "../../OrthancFramework/Sources/SerializationToolbox.h"
+#include "ServerEnumerations.h"
+#include "ServerIndex.h"
+#include "ResourceFinder.h"
+
+#include <algorithm>
+#include <boost/lexical_cast.hpp>
+#include <boost/noncopyable.hpp>
+
+
+namespace Orthanc
+{
+  struct SimpleInstanceOrdering::Instance : public boost::noncopyable
+  {
+  private:
+    std::string   instanceId_;
+    uint32_t      indexInSeries_;
+		FileInfo      fileInfo_;
+
+
+  public:
+    Instance(const std::string& instanceId, uint32_t indexInSeries, FileInfo fileInfo) :
+      instanceId_(instanceId),
+      indexInSeries_(indexInSeries),
+			fileInfo_(fileInfo)
+    {
+    }
+
+    const std::string& GetIdentifier() const
+    {
+      return instanceId_;
+    }
+
+    uint32_t GetIndexInSeries() const
+    {
+      return indexInSeries_;
+    }
+ 
+    const FileInfo& GetFileInfo() const
+    {
+      return fileInfo_;
+    }
+  };
+
+  bool SimpleInstanceOrdering::IndexInSeriesComparator(const SimpleInstanceOrdering::Instance* a,
+                                                       const SimpleInstanceOrdering::Instance* b)
+  {
+    return a->GetIndexInSeries() < b->GetIndexInSeries();
+  }  
+
+
+  SimpleInstanceOrdering::SimpleInstanceOrdering(ServerIndex& index,
+                                                 const std::string& seriesId) :
+		hasDuplicateIndexInSeries_(false)
+  {
+		FindRequest request(ResourceType_Instance);
+		request.SetOrthancSeriesId(seriesId);
+		request.SetRetrieveMetadata(true);
+		request.SetRetrieveAttachments(true);
+
+		FindResponse response;
+		index.ExecuteFind(response, request);
+
+    std::set<uint32_t> allIndexInSeries;
+
+		for (size_t i = 0; i < response.GetSize(); ++i)
+		{
+			const FindResponse::Resource& resource = response.GetResourceByIndex(i);
+			std::string instanceId = resource.GetIdentifier();
+
+			std::string strIndexInSeries;
+			uint32_t indexInSeries = 0;
+			FileInfo fileInfo;
+			int64_t revisionNotUsed;
+			
+			if (resource.LookupMetadata(strIndexInSeries, ResourceType_Instance, MetadataType_Instance_IndexInSeries))
+			{
+				SerializationToolbox::ParseUnsignedInteger32(indexInSeries, strIndexInSeries);
+			}
+
+			if (resource.LookupAttachment(fileInfo, revisionNotUsed, FileContentType_Dicom))
+			{
+				allIndexInSeries.insert(indexInSeries);
+				instances_.push_back(new SimpleInstanceOrdering::Instance(instanceId, indexInSeries, fileInfo));
+			}
+		}
+
+		// if there are duplicates, the set will be smaller than the vector
+		hasDuplicateIndexInSeries_ = allIndexInSeries.size() != instances_.size(); 
+
+		std::sort(instances_.begin(), instances_.end(), IndexInSeriesComparator);
+  }
+
+  SimpleInstanceOrdering::~SimpleInstanceOrdering()
+  {
+    for (std::vector<Instance*>::iterator
+           it = instances_.begin(); it != instances_.end(); ++it)
+    {
+      if (*it != NULL)
+      {
+        delete *it;
+      }
+    }
+  }
+
+  const std::string& SimpleInstanceOrdering::GetInstanceId(size_t index) const
+	{
+		return instances_[index]->GetIdentifier();
+	}
+
+  uint32_t SimpleInstanceOrdering::GetInstanceIndexInSeries(size_t index) const
+	{
+		if (hasDuplicateIndexInSeries_)
+		{
+			// if there are duplicates, we count the instances from 0
+			return index; 
+		}
+		else
+		{
+			// if there are no duplicates, we return the real index in series (that may not start at 0 but that is more human friendly)
+			return instances_[index]->GetIndexInSeries();
+		}
+	}
+
+  const FileInfo& SimpleInstanceOrdering::GetInstanceFileInfo(size_t index) const
+	{
+		return instances_[index]->GetFileInfo();
+	}
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/Sources/SimpleInstanceOrdering.h	Thu Jan 16 12:16:35 2025 +0100
@@ -0,0 +1,61 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2023 Osimis S.A., Belgium
+ * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
+ * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../../OrthancFramework/Sources/DicomFormat/DicomMap.h"
+
+namespace Orthanc
+{
+  class ServerIndex;
+  class FileInfo;
+
+  class SimpleInstanceOrdering
+  {
+  private:
+    struct Instance;
+
+    std::vector<Instance*>   instances_;
+		bool                     hasDuplicateIndexInSeries_;
+
+  public:
+    SimpleInstanceOrdering(ServerIndex& index,
+                           const std::string& seriesId);
+    ~SimpleInstanceOrdering();
+
+    size_t GetInstancesCount() const
+    {
+      return instances_.size();
+    }
+
+    const std::string& GetInstanceId(size_t index) const;
+
+    uint32_t GetInstanceIndexInSeries(size_t index) const;
+
+    const FileInfo& GetInstanceFileInfo(size_t index) const;
+
+  private:
+    static bool IndexInSeriesComparator(const SimpleInstanceOrdering::Instance* a,
+                                        const SimpleInstanceOrdering::Instance* b);
+  };
+}