changeset 1124:790ff7a5b3bf

IStorageArea abstraction
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Sep 2014 16:28:34 +0200
parents 6c5a77637b23
children 20ddab7e9ae8
files Core/Enumerations.h Core/FileStorage/CompressedFileStorageAccessor.cpp Core/FileStorage/CompressedFileStorageAccessor.h Core/FileStorage/FileStorageAccessor.cpp Core/FileStorage/FileStorageAccessor.h Core/FileStorage/FilesystemStorage.cpp Core/FileStorage/FilesystemStorage.h OrthancServer/ServerContext.cpp OrthancServer/ServerContext.h OrthancServer/main.cpp UnitTestsSources/FileStorageTests.cpp UnitTestsSources/ServerIndexTests.cpp
diffstat 12 files changed, 65 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.h	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/Enumerations.h	Fri Sep 05 16:28:34 2014 +0200
@@ -267,6 +267,7 @@
 
   enum FileContentType
   {
+    FileContentType_Unknown = 0,
     FileContentType_Dicom = 1,
     FileContentType_DicomAsJson = 2,
 
--- a/Core/FileStorage/CompressedFileStorageAccessor.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -37,6 +37,8 @@
 #include "FileStorageAccessor.h"
 #include "../HttpServer/BufferHttpSender.h"
 
+#include <memory>
+
 namespace Orthanc
 {
   FileInfo CompressedFileStorageAccessor::WriteInternal(const void* data,
@@ -70,7 +72,16 @@
         Toolbox::ComputeMD5(compressedMD5, compressed);
       }
 
-      std::string uuid = storage_.Create(compressed);
+      std::string uuid;
+      if (compressed.size() > 0)
+      {
+        uuid = storage_.Create(&compressed[0], compressed.size());
+      }
+      else
+      {
+        uuid = storage_.Create(NULL, 0);
+      }
+
       return FileInfo(uuid, type, size, md5,
                       CompressionType_Zlib, compressed.size(), compressedMD5);
     }
@@ -80,7 +91,7 @@
     }
   }
 
-  CompressedFileStorageAccessor::CompressedFileStorageAccessor(FilesystemStorage& storage) : 
+  CompressedFileStorageAccessor::CompressedFileStorageAccessor(IStorageArea& storage) : 
     storage_(storage)
   {
     compressionType_ = CompressionType_None;
--- a/Core/FileStorage/CompressedFileStorageAccessor.h	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.h	Fri Sep 05 16:28:34 2014 +0200
@@ -32,8 +32,8 @@
 
 #pragma once
 
+#include "IStorageArea.h"
 #include "StorageAccessor.h"
-#include "FilesystemStorage.h"
 #include "../Compression/ZlibCompressor.h"
 
 namespace Orthanc
@@ -41,7 +41,7 @@
   class CompressedFileStorageAccessor : public StorageAccessor
   {
   private:
-    FilesystemStorage& storage_;
+    IStorageArea& storage_;
     ZlibCompressor zlib_;
     CompressionType compressionType_;
 
@@ -51,7 +51,7 @@
                                    FileContentType type);
 
   public: 
-    CompressedFileStorageAccessor(FilesystemStorage& storage);
+    CompressedFileStorageAccessor(IStorageArea& storage);
 
     void SetCompressionForNextOperations(CompressionType compression)
     {
--- a/Core/FileStorage/FileStorageAccessor.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/FileStorageAccessor.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -33,6 +33,12 @@
 #include "../PrecompiledHeaders.h"
 #include "FileStorageAccessor.h"
 
+#include "../HttpServer/BufferHttpSender.h"
+
+#include <memory>
+
+#include <stdio.h>
+
 namespace Orthanc
 {
   FileInfo FileStorageAccessor::WriteInternal(const void* data,
@@ -48,4 +54,15 @@
 
     return FileInfo(storage_.Create(data, size), type, size, md5);
   }
+
+
+  HttpFileSender* FileStorageAccessor::ConstructHttpFileSender(const std::string& uuid)
+  {
+    std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender);
+
+    storage_.Read(sender->GetBuffer(), uuid);
+      
+    return sender.release();
+  }
+
 }
--- a/Core/FileStorage/FileStorageAccessor.h	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/FileStorageAccessor.h	Fri Sep 05 16:28:34 2014 +0200
@@ -33,15 +33,14 @@
 #pragma once
 
 #include "StorageAccessor.h"
-#include "FilesystemStorage.h"
-#include "../HttpServer/FilesystemHttpSender.h"
+#include "IStorageArea.h"
 
 namespace Orthanc
 {
   class FileStorageAccessor : public StorageAccessor
   {
   private:
-    FilesystemStorage& storage_;
+    IStorageArea& storage_;
     
   protected:
     virtual FileInfo WriteInternal(const void* data,
@@ -49,7 +48,7 @@
                                    FileContentType type);
 
   public:
-    FileStorageAccessor(FilesystemStorage& storage) : storage_(storage)
+    FileStorageAccessor(IStorageArea& storage) : storage_(storage)
     {
     }
 
@@ -59,9 +58,6 @@
       storage_.Read(content, uuid);
     }
 
-    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid)
-    {
-      return new FilesystemHttpSender(storage_.GetPath(uuid));
-    }
+    virtual HttpFileSender* ConstructHttpFileSender(const std::string& uuid);
   };
 }
--- a/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/FilesystemStorage.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -143,24 +143,8 @@
   }
 
 
-  std::string FilesystemStorage::Create(const std::vector<uint8_t>& content)
-  {
-    if (content.size() == 0)
-      return Create(NULL, 0);
-    else
-      return Create(&content[0], content.size());
-  }
-
-  std::string FilesystemStorage::Create(const std::string& content)
-  {
-    if (content.size() == 0)
-      return Create(NULL, 0);
-    else
-      return Create(&content[0], content.size());
-  }
-
   void FilesystemStorage::Read(std::string& content,
-                         const std::string& uuid) const
+                               const std::string& uuid) const
   {
     content.clear();
     Toolbox::ReadFile(content, GetPath(uuid).string());
--- a/Core/FileStorage/FilesystemStorage.h	Fri Sep 05 15:59:04 2014 +0200
+++ b/Core/FileStorage/FilesystemStorage.h	Fri Sep 05 16:28:34 2014 +0200
@@ -32,12 +32,14 @@
 
 #pragma once
 
+#include "IStorageArea.h"
+
 #include <boost/filesystem.hpp>
 #include <set>
 
 namespace Orthanc
 {
-  class FilesystemStorage : public boost::noncopyable
+  class FilesystemStorage : public IStorageArea
   {
     // TODO REMOVE THIS
     friend class FilesystemHttpSender;
@@ -51,14 +53,12 @@
   public:
     FilesystemStorage(std::string root);
 
-    std::string Create(const void* content, size_t size);
-
-    std::string Create(const std::vector<uint8_t>& content);
+    virtual std::string Create(const void* content, size_t size);
 
-    std::string Create(const std::string& content);
+    virtual void Read(std::string& content,
+                      const std::string& uuid) const;
 
-    void Read(std::string& content,
-              const std::string& uuid) const;
+    virtual void Remove(const std::string& uuid);
 
     void ListAllFiles(std::set<std::string>& result) const;
 
@@ -66,8 +66,6 @@
 
     void Clear();
 
-    void Remove(const std::string& uuid);
-
     uintmax_t GetCapacity() const;
 
     uintmax_t GetAvailableSpace() const;
--- a/OrthancServer/ServerContext.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -71,9 +71,9 @@
 
 namespace Orthanc
 {
-  ServerContext::ServerContext(const boost::filesystem::path& storagePath,
+  ServerContext::ServerContext(IStorageArea& storage,
                                const boost::filesystem::path& indexPath) :
-    storage_(storagePath.string()),
+    storage_(storage),
     index_(*this, indexPath.string()),
     accessor_(storage_),
     compressionEnabled_(false),
--- a/OrthancServer/ServerContext.h	Fri Sep 05 15:59:04 2014 +0200
+++ b/OrthancServer/ServerContext.h	Fri Sep 05 16:28:34 2014 +0200
@@ -34,7 +34,7 @@
 
 #include "../Core/Cache/MemoryCache.h"
 #include "../Core/FileStorage/CompressedFileStorageAccessor.h"
-#include "../Core/FileStorage/FilesystemStorage.h"
+#include "../Core/FileStorage/IStorageArea.h"
 #include "../Core/RestApi/RestApiOutput.h"
 #include "../Core/Lua/LuaContext.h"
 #include "ServerIndex.h"
@@ -43,6 +43,8 @@
 #include "Scheduler/ServerScheduler.h"
 #include "DicomInstanceToStore.h"
 
+#include <boost/filesystem.hpp>
+
 namespace Orthanc
 {
   class PluginsHttpHandler;
@@ -75,7 +77,7 @@
                                   const Json::Value& simplifiedDicom,
                                   const Json::Value& metadata);
 
-    FilesystemStorage storage_;
+    IStorageArea& storage_;
     ServerIndex index_;
     CompressedFileStorageAccessor accessor_;
     bool compressionEnabled_;
@@ -132,7 +134,7 @@
     };
 
 
-    ServerContext(const boost::filesystem::path& storagePath,
+    ServerContext(IStorageArea& storage,
                   const boost::filesystem::path& indexPath);
 
     ServerIndex& GetIndex()
--- a/OrthancServer/main.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/OrthancServer/main.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -37,6 +37,7 @@
 #include <glog/logging.h>
 #include <boost/algorithm/string/predicate.hpp>
 
+#include "../Core/FileStorage/FilesystemStorage.h"
 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h"
 #include "../Core/HttpServer/FilesystemHttpHandler.h"
 #include "../Core/Lua/LuaFunctionCall.h"
@@ -320,7 +321,9 @@
   boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr);
   boost::filesystem::path indexDirectory = Configuration::InterpretStringParameterAsPath(
     Configuration::GetGlobalStringParameter("IndexDirectory", storageDirectoryStr));
-  ServerContext context(storageDirectory, indexDirectory);
+
+  FilesystemStorage storage(storageDirectory.string());
+  ServerContext context(storage, indexDirectory);
 
   LOG(WARNING) << "Storage directory: " << storageDirectory;
   LOG(WARNING) << "Index directory: " << indexDirectory;
--- a/UnitTestsSources/FileStorageTests.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/UnitTestsSources/FileStorageTests.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -63,7 +63,7 @@
   FilesystemStorage s("UnitTestsStorage");
 
   std::string data = Toolbox::GenerateUuid();
-  std::string uid = s.Create(data);
+  std::string uid = s.Create(&data[0], data.size());
   std::string d;
   s.Read(d, uid);
   ASSERT_EQ(d.size(), data.size());
@@ -77,7 +77,7 @@
 
   std::vector<uint8_t> data;
   StringToVector(data, Toolbox::GenerateUuid());
-  std::string uid = s.Create(data);
+  std::string uid = s.Create(&data[0], data.size());
   std::string d;
   s.Read(d, uid);
   ASSERT_EQ(d.size(), data.size());
@@ -93,7 +93,8 @@
   std::list<std::string> u;
   for (unsigned int i = 0; i < 10; i++)
   {
-    u.push_back(s.Create(Toolbox::GenerateUuid()));
+    std::string t = Toolbox::GenerateUuid();
+    u.push_back(s.Create(&t[0], t.size()));
   }
 
   std::set<std::string> ss;
--- a/UnitTestsSources/ServerIndexTests.cpp	Fri Sep 05 15:59:04 2014 +0200
+++ b/UnitTestsSources/ServerIndexTests.cpp	Fri Sep 05 16:28:34 2014 +0200
@@ -38,6 +38,7 @@
 #include "../OrthancServer/ServerIndex.h"
 #include "../Core/Uuid.h"
 #include "../Core/DicomFormat/DicomNullValue.h"
+#include "../Core/FileStorage/FilesystemStorage.h"
 
 #include <ctype.h>
 #include <glog/logging.h>
@@ -569,8 +570,10 @@
 TEST(ServerIndex, AttachmentRecycling)
 {
   const std::string path = "UnitTestsStorage";
+
   Toolbox::RemoveFile(path + "/index");
-  ServerContext context(path, ":memory:");   // The SQLite DB is in memory
+  FilesystemStorage storage(path);
+  ServerContext context(storage, ":memory:");   // The SQLite DB is in memory
   ServerIndex& index = context.GetIndex();
 
   index.SetMaximumStorageSize(10);