changeset 1126:bf67431a7383

handling of file content type in IStorageArea
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Sep 2014 17:01:42 +0200
parents 20ddab7e9ae8
children f4e65808ea58
files Core/FileStorage/CompressedFileStorageAccessor.cpp Core/FileStorage/CompressedFileStorageAccessor.h Core/FileStorage/FileStorageAccessor.cpp Core/FileStorage/FileStorageAccessor.h Core/FileStorage/FilesystemStorage.cpp Core/FileStorage/FilesystemStorage.h Core/FileStorage/IStorageArea.h Core/FileStorage/StorageAccessor.h OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/ServerIndex.cpp UnitTestsSources/FileStorageTests.cpp
diffstat 12 files changed, 106 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/Core/FileStorage/CompressedFileStorageAccessor.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -56,7 +56,7 @@
     {
     case CompressionType_None:
     {
-      std::string uuid = storage_.Create(data, size);
+      std::string uuid = storage_.Create(data, size, type);
       return FileInfo(uuid, type, size, md5);
     }
 
@@ -75,11 +75,11 @@
       std::string uuid;
       if (compressed.size() > 0)
       {
-        uuid = storage_.Create(&compressed[0], compressed.size());
+        uuid = storage_.Create(&compressed[0], compressed.size(), type);
       }
       else
       {
-        uuid = storage_.Create(NULL, 0);
+        uuid = storage_.Create(NULL, 0, type);
       }
 
       return FileInfo(uuid, type, size, md5,
@@ -98,18 +98,19 @@
   }
 
   void CompressedFileStorageAccessor::Read(std::string& content,
-                                           const std::string& uuid)
+                                           const std::string& uuid,
+                                           FileContentType type)
   {
     switch (compressionType_)
     {
     case CompressionType_None:
-      storage_.Read(content, uuid);
+      storage_.Read(content, uuid, type);
       break;
 
     case CompressionType_Zlib:
     {
       std::string compressed;
-      storage_.Read(compressed, uuid);
+      storage_.Read(compressed, uuid, type);
       zlib_.Uncompress(content, compressed);
       break;
     }
@@ -119,20 +120,21 @@
     }
   }
 
-  HttpFileSender* CompressedFileStorageAccessor::ConstructHttpFileSender(const std::string& uuid)
+  HttpFileSender* CompressedFileStorageAccessor::ConstructHttpFileSender(const std::string& uuid,
+                                                                         FileContentType type)
   {
     switch (compressionType_)
     {
     case CompressionType_None:
     {
       FileStorageAccessor uncompressedAccessor(storage_);
-      return uncompressedAccessor.ConstructHttpFileSender(uuid);
+      return uncompressedAccessor.ConstructHttpFileSender(uuid, type);
     }
 
     case CompressionType_Zlib:
     {
       std::string compressed;
-      storage_.Read(compressed, uuid);
+      storage_.Read(compressed, uuid, type);
 
       std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender);
       zlib_.Uncompress(sender->GetBuffer(), compressed);
--- a/Core/FileStorage/CompressedFileStorageAccessor.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.h	Fri Sep 05 17:01:42 2014 +0200
@@ -64,8 +64,16 @@
     }
 
     virtual void Read(std::string& content,
-                      const std::string& uuid);
+                      const std::string& uuid,
+                      FileContentType type);
+
+    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid,
+                                                    FileContentType type);
 
-    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid);
+    virtual void Remove(const std::string& uuid,
+                        FileContentType type)
+    {
+      storage_.Remove(uuid, type);
+    }
   };
 }
--- a/Core/FileStorage/FileStorageAccessor.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/FileStorageAccessor.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -52,15 +52,16 @@
       Toolbox::ComputeMD5(md5, data, size);
     }
 
-    return FileInfo(storage_.Create(data, size), type, size, md5);
+    return FileInfo(storage_.Create(data, size, type), type, size, md5);
   }
 
 
-  HttpFileSender* FileStorageAccessor::ConstructHttpFileSender(const std::string& uuid)
+  HttpFileSender* FileStorageAccessor::ConstructHttpFileSender(const std::string& uuid,
+                                                               FileContentType type)
   {
     std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender);
 
-    storage_.Read(sender->GetBuffer(), uuid);
+    storage_.Read(sender->GetBuffer(), uuid, type);
       
     return sender.release();
   }
--- a/Core/FileStorage/FileStorageAccessor.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/FileStorageAccessor.h	Fri Sep 05 17:01:42 2014 +0200
@@ -53,11 +53,19 @@
     }
 
     virtual void Read(std::string& content,
-                      const std::string& uuid)
+                      const std::string& uuid,
+                      FileContentType type)
     {
-      storage_.Read(content, uuid);
+      storage_.Read(content, uuid, type);
     }
 
-    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid);
+    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid,
+                                                    FileContentType type);
+
+    virtual void Remove(const std::string& uuid,
+                        FileContentType type)
+    {
+      storage_.Remove(uuid, type);
+    }
   };
 }
--- a/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -85,7 +85,9 @@
     Toolbox::CreateDirectory(root);
   }
 
-  std::string FilesystemStorage::Create(const void* content, size_t size)
+  std::string FilesystemStorage::Create(const void* content, 
+                                        size_t size,
+                                        FileContentType /*type*/)
   {
     std::string uuid;
     boost::filesystem::path path;
@@ -144,7 +146,8 @@
 
 
   void FilesystemStorage::Read(std::string& content,
-                               const std::string& uuid) const
+                               const std::string& uuid,
+                               FileContentType /*type*/) const
   {
     content.clear();
     Toolbox::ReadFile(content, GetPath(uuid).string());
@@ -209,12 +212,13 @@
 
     for (List::const_iterator it = result.begin(); it != result.end(); ++it)
     {
-      Remove(*it);
+      Remove(*it, FileContentType_Unknown /*ignored in this class*/);
     }
   }
 
 
-  void FilesystemStorage::Remove(const std::string& uuid)
+  void FilesystemStorage::Remove(const std::string& uuid,
+                                 FileContentType /*type*/)
   {
     LOG(INFO) << "Deleting file " << uuid;
     namespace fs = boost::filesystem;
--- a/Core/FileStorage/FilesystemStorage.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/FilesystemStorage.h	Fri Sep 05 17:01:42 2014 +0200
@@ -53,12 +53,16 @@
   public:
     FilesystemStorage(std::string root);
 
-    virtual std::string Create(const void* content, size_t size);
+    virtual std::string Create(const void* content, 
+                               size_t size,
+                               FileContentType type);
 
     virtual void Read(std::string& content,
-                      const std::string& uuid) const;
+                      const std::string& uuid,
+                      FileContentType type) const;
 
-    virtual void Remove(const std::string& uuid);
+    virtual void Remove(const std::string& uuid,
+                        FileContentType type);
 
     void ListAllFiles(std::set<std::string>& result) const;
 
--- a/Core/FileStorage/IStorageArea.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/IStorageArea.h	Fri Sep 05 17:01:42 2014 +0200
@@ -32,7 +32,8 @@
 
 #pragma once
 
-#include <string>
+#include "../Enumerations.h"
+
 #include <boost/noncopyable.hpp>
 
 namespace Orthanc
@@ -44,11 +45,15 @@
     {
     }
 
-    virtual std::string Create(const void* content, size_t size) = 0;
+    virtual std::string Create(const void* content, 
+                               size_t size,
+                               FileContentType type) = 0;
 
     virtual void Read(std::string& content,
-                      const std::string& uuid) const = 0;
+                      const std::string& uuid,
+                      FileContentType type) const = 0;
 
-    virtual void Remove(const std::string& uuid) = 0;
+    virtual void Remove(const std::string& uuid,
+                        FileContentType type) = 0;
   };
 }
--- a/Core/FileStorage/StorageAccessor.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/Core/FileStorage/StorageAccessor.h	Fri Sep 05 17:01:42 2014 +0200
@@ -85,8 +85,13 @@
                    FileContentType type);
 
     virtual void Read(std::string& content,
-                      const std::string& uuid) = 0;
+                      const std::string& uuid,
+                      FileContentType type) = 0;
 
-    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid) = 0;
+    virtual void Remove(const std::string& uuid,
+                        FileContentType type) = 0;
+
+    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid,
+                                                    FileContentType type) = 0;
   };
 }
--- a/OrthancServer/ServerContext.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -73,9 +73,8 @@
 {
   ServerContext::ServerContext(IStorageArea& storage,
                                const boost::filesystem::path& indexPath) :
-    storage_(storage),
     index_(*this, indexPath.string()),
-    accessor_(storage_),
+    accessor_(storage),
     compressionEnabled_(false),
     provider_(*this),
     dicomCache_(provider_, DICOM_CACHE_SIZE),
@@ -98,9 +97,10 @@
     compressionEnabled_ = enabled;
   }
 
-  void ServerContext::RemoveFile(const std::string& fileUuid)
+  void ServerContext::RemoveFile(const std::string& fileUuid,
+                                 FileContentType type)
   {
-    storage_.Remove(fileUuid);
+    accessor_.Remove(fileUuid, type);
   }
 
 
@@ -322,8 +322,8 @@
             
       if (status != StoreStatus_Success)
       {
-        storage_.Remove(dicomInfo.GetUuid());
-        storage_.Remove(jsonInfo.GetUuid());
+        accessor_.Remove(dicomInfo.GetUuid(), FileContentType_Dicom);
+        accessor_.Remove(jsonInfo.GetUuid(), FileContentType_DicomAsJson);
       }
 
       switch (status)
@@ -405,7 +405,7 @@
 
     accessor_.SetCompressionForNextOperations(attachment.GetCompressionType());
 
-    std::auto_ptr<HttpFileSender> sender(accessor_.ConstructHttpFileSender(attachment.GetUuid()));
+    std::auto_ptr<HttpFileSender> sender(accessor_.ConstructHttpFileSender(attachment.GetUuid(), attachment.GetContentType()));
     sender->SetContentType("application/dicom");
     sender->SetDownloadFilename(instancePublicId + ".dcm");
     output.AnswerFile(*sender);
@@ -446,7 +446,7 @@
       accessor_.SetCompressionForNextOperations(CompressionType_None);
     }
 
-    accessor_.Read(result, attachment.GetUuid());
+    accessor_.Read(result, attachment.GetUuid(), attachment.GetContentType());
   }
 
 
@@ -510,7 +510,7 @@
 
     if (status != StoreStatus_Success)
     {
-      storage_.Remove(info.GetUuid());
+      accessor_.Remove(info.GetUuid(), info.GetContentType());
       return false;
     }
     else
--- a/OrthancServer/ServerContext.h	Fri Sep 05 16:28:47 2014 +0200
+++ b/OrthancServer/ServerContext.h	Fri Sep 05 17:01:42 2014 +0200
@@ -77,7 +77,6 @@
                                   const Json::Value& simplifiedDicom,
                                   const Json::Value& metadata);
 
-    IStorageArea& storage_;
     ServerIndex index_;
     CompressedFileStorageAccessor accessor_;
     bool compressionEnabled_;
@@ -149,7 +148,8 @@
       return compressionEnabled_;
     }
 
-    void RemoveFile(const std::string& fileUuid);
+    void RemoveFile(const std::string& fileUuid,
+                    FileContentType type);
 
     bool AddAttachment(const std::string& resourceId,
                        FileContentType attachmentType,
--- a/OrthancServer/ServerIndex.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/OrthancServer/ServerIndex.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -59,11 +59,22 @@
     class ServerIndexListener : public IServerIndexListener
     {
     private:
+      struct FileToRemove
+      {
+        std::string  uuid_;
+        FileContentType  type_;
+
+        FileToRemove(const FileInfo& info) : uuid_(info.GetUuid()), 
+                                             type_(info.GetContentType())
+        {
+        }
+      };
+
       ServerContext& context_;
       bool hasRemainingLevel_;
       ResourceType remainingType_;
       std::string remainingPublicId_;
-      std::list<std::string> pendingFilesToRemove_;
+      std::list<FileToRemove> pendingFilesToRemove_;
       uint64_t sizeOfFilesToRemove_;
 
     public:
@@ -90,11 +101,11 @@
 
       void CommitFilesToRemove()
       {
-        for (std::list<std::string>::iterator 
+        for (std::list<FileToRemove>::iterator 
                it = pendingFilesToRemove_.begin();
              it != pendingFilesToRemove_.end(); ++it)
         {
-          context_.RemoveFile(*it);
+          context_.RemoveFile(it->uuid_, it->type_);
         }
       }
 
@@ -122,7 +133,7 @@
       virtual void SignalFileDeleted(const FileInfo& info)
       {
         assert(Toolbox::IsUuid(info.GetUuid()));
-        pendingFilesToRemove_.push_back(info.GetUuid());
+        pendingFilesToRemove_.push_back(FileToRemove(info));
         sizeOfFilesToRemove_ += info.GetCompressedSize();
       }
 
--- a/UnitTestsSources/FileStorageTests.cpp	Fri Sep 05 16:28:47 2014 +0200
+++ b/UnitTestsSources/FileStorageTests.cpp	Fri Sep 05 17:01:42 2014 +0200
@@ -63,9 +63,9 @@
   FilesystemStorage s("UnitTestsStorage");
 
   std::string data = Toolbox::GenerateUuid();
-  std::string uid = s.Create(&data[0], data.size());
+  std::string uid = s.Create(&data[0], data.size(), FileContentType_Unknown);
   std::string d;
-  s.Read(d, uid);
+  s.Read(d, uid, FileContentType_Unknown);
   ASSERT_EQ(d.size(), data.size());
   ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
   ASSERT_EQ(s.GetSize(uid), data.size());
@@ -77,9 +77,9 @@
 
   std::vector<uint8_t> data;
   StringToVector(data, Toolbox::GenerateUuid());
-  std::string uid = s.Create(&data[0], data.size());
+  std::string uid = s.Create(&data[0], data.size(), FileContentType_Unknown);
   std::string d;
-  s.Read(d, uid);
+  s.Read(d, uid, FileContentType_Unknown);
   ASSERT_EQ(d.size(), data.size());
   ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
   ASSERT_EQ(s.GetSize(uid), data.size());
@@ -94,7 +94,7 @@
   for (unsigned int i = 0; i < 10; i++)
   {
     std::string t = Toolbox::GenerateUuid();
-    u.push_back(s.Create(&t[0], t.size()));
+    u.push_back(s.Create(&t[0], t.size(), FileContentType_Unknown));
   }
 
   std::set<std::string> ss;
@@ -107,7 +107,7 @@
   {
     ASSERT_TRUE(ss.find(*i) != ss.end());
     if (c < 5)
-      s.Remove(*i);
+      s.Remove(*i, FileContentType_Unknown);
   }
 
   s.ListAllFiles(ss);
@@ -128,7 +128,7 @@
   FileInfo info = accessor.Write(data, FileContentType_Dicom);
   
   std::string r;
-  accessor.Read(r, info.GetUuid());
+  accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
 
   ASSERT_EQ(data, r);
   ASSERT_EQ(CompressionType_None, info.GetCompressionType());
@@ -148,7 +148,7 @@
   FileInfo info = accessor.Write(data, FileContentType_Dicom);
   
   std::string r;
-  accessor.Read(r, info.GetUuid());
+  accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
 
   ASSERT_EQ(data, r);
   ASSERT_EQ(CompressionType_None, info.GetCompressionType());
@@ -169,7 +169,7 @@
   FileInfo info = accessor.Write(data, FileContentType_Dicom);
   
   std::string r;
-  accessor.Read(r, info.GetUuid());
+  accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
 
   ASSERT_EQ(0, memcmp(&r[0], &data[0], data.size()));
   ASSERT_EQ(CompressionType_None, info.GetCompressionType());
@@ -189,7 +189,7 @@
   FileInfo info = accessor.Write(data, FileContentType_Dicom);
   
   std::string r;
-  accessor.Read(r, info.GetUuid());
+  accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
 
   ASSERT_EQ(data, r);
   ASSERT_EQ(CompressionType_Zlib, info.GetCompressionType());
@@ -214,16 +214,16 @@
   FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom);
   
   accessor.SetCompressionForNextOperations(CompressionType_Zlib);
-  accessor.Read(r, compressedInfo.GetUuid());
+  accessor.Read(r, compressedInfo.GetUuid(), FileContentType_Unknown);
   ASSERT_EQ(compressedData, r);
 
   accessor.SetCompressionForNextOperations(CompressionType_None);
-  accessor.Read(r, compressedInfo.GetUuid());
+  accessor.Read(r, compressedInfo.GetUuid(), FileContentType_Unknown);
   ASSERT_NE(compressedData, r);
 
   /*
   // This test is too slow on Windows
   accessor.SetCompressionForNextOperations(CompressionType_Zlib);
-  ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid()), OrthancException);
+  ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid(), FileContentType_Unknown), OrthancException);
   */
 }