changeset 1512:52dc56bcec7d

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Aug 2015 14:52:10 +0200
parents 7962563129c9
children fe07f82d83d3
files CMakeLists.txt Core/Compression/BufferCompressor.cpp Core/Compression/BufferCompressor.h Core/Compression/DeflateBaseCompressor.cpp Core/Compression/DeflateBaseCompressor.h Core/Compression/IBufferCompressor.h Core/Compression/ZlibCompressor.cpp Core/Compression/ZlibCompressor.h Core/FileStorage/CompressedFileStorageAccessor.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 10 files changed, 213 insertions(+), 222 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Aug 10 14:18:24 2015 +0200
+++ b/CMakeLists.txt	Mon Aug 10 14:52:10 2015 +0200
@@ -82,10 +82,10 @@
   Core/Cache/MemoryCache.cpp
   Core/Cache/SharedArchive.cpp
   Core/ChunkedBuffer.cpp
-  Core/Compression/BufferCompressor.cpp
+  Core/Compression/DeflateBaseCompressor.cpp
+  Core/Compression/HierarchicalZipWriter.cpp
+  Core/Compression/ZipWriter.cpp
   Core/Compression/ZlibCompressor.cpp
-  Core/Compression/ZipWriter.cpp
-  Core/Compression/HierarchicalZipWriter.cpp
   Core/OrthancException.cpp
   Core/DicomFormat/DicomArray.cpp
   Core/DicomFormat/DicomMap.cpp
--- a/Core/Compression/BufferCompressor.cpp	Mon Aug 10 14:18:24 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-/**
- * Orthanc - A Lightweight, RESTful DICOM Store
- * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
- * Department, University Hospital of Liege, 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.
- *
- * In addition, as a special exception, the copyright holders of this
- * program give permission to link the code of its release with the
- * OpenSSL project's "OpenSSL" library (or with modified versions of it
- * that use the same license as the "OpenSSL" library), and distribute
- * the linked executables. You must obey the GNU General Public License
- * in all respects for all of the code used other than "OpenSSL". If you
- * modify file(s) with this exception, you may extend this exception to
- * your version of the file(s), but you are not obligated to do so. If
- * you do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source files
- * in the program, then also delete it here.
- * 
- * 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 "../PrecompiledHeaders.h"
-#include "BufferCompressor.h"
-
-namespace Orthanc
-{
-  void BufferCompressor::Compress(std::string& output,
-                                  const std::vector<uint8_t>& input)
-  {
-    if (input.size() > 0)
-      Compress(output, &input[0], input.size());
-    else
-      Compress(output, NULL, 0);
-  }
-
-  void BufferCompressor::Uncompress(std::string& output,
-                                    const std::vector<uint8_t>& input)
-  {
-    if (input.size() > 0)
-      Uncompress(output, &input[0], input.size());
-    else
-      Uncompress(output, NULL, 0);
-  }
-
-  void BufferCompressor::Compress(std::string& output,
-                                  const std::string& input)
-  {
-    if (input.size() > 0)
-      Compress(output, &input[0], input.size());
-    else
-      Compress(output, NULL, 0);
-  }
-
-  void BufferCompressor::Uncompress(std::string& output,
-                                    const std::string& input)
-  {
-    if (input.size() > 0)
-      Uncompress(output, &input[0], input.size());
-    else
-      Uncompress(output, NULL, 0);
-  }
-}
--- a/Core/Compression/BufferCompressor.h	Mon Aug 10 14:18:24 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-/**
- * Orthanc - A Lightweight, RESTful DICOM Store
- * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
- * Department, University Hospital of Liege, 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.
- *
- * In addition, as a special exception, the copyright holders of this
- * program give permission to link the code of its release with the
- * OpenSSL project's "OpenSSL" library (or with modified versions of it
- * that use the same license as the "OpenSSL" library), and distribute
- * the linked executables. You must obey the GNU General Public License
- * in all respects for all of the code used other than "OpenSSL". If you
- * modify file(s) with this exception, you may extend this exception to
- * your version of the file(s), but you are not obligated to do so. If
- * you do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source files
- * in the program, then also delete it here.
- * 
- * 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 <string>
-#include <cstddef>
-#include <stdint.h>
-#include <vector>
-
-namespace Orthanc
-{
-  class BufferCompressor
-  {
-  public:
-    virtual ~BufferCompressor()
-    {
-    }
-
-    virtual void Compress(std::string& compressed,
-                          const void* uncompressed,
-                          size_t uncompressedSize) = 0;
-
-    virtual void Uncompress(std::string& uncompressed,
-                            const void* compressed,
-                            size_t compressedSize) = 0;
-
-    virtual void Compress(std::string& compressed,
-                          const std::vector<uint8_t>& uncompressed);
-
-    virtual void Uncompress(std::string& uncompressed,
-                            const std::vector<uint8_t>& compressed);
-
-    virtual void Compress(std::string& compressed,
-                          const std::string& uncompressed);
-
-    virtual void Uncompress(std::string& uncompressed,
-                            const std::string& compressed);
-  };
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Compression/DeflateBaseCompressor.cpp	Mon Aug 10 14:52:10 2015 +0200
@@ -0,0 +1,49 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, 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.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * 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 "../PrecompiledHeaders.h"
+#include "DeflateBaseCompressor.h"
+
+#include "../OrthancException.h"
+
+namespace Orthanc
+{
+  void DeflateBaseCompressor::SetCompressionLevel(uint8_t level)
+  {
+    if (level >= 10)
+    {
+      throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression");
+    }
+
+    compressionLevel_ = level;
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Compression/DeflateBaseCompressor.h	Mon Aug 10 14:52:10 2015 +0200
@@ -0,0 +1,71 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, 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.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * 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 "IBufferCompressor.h"
+
+#include <stdint.h>
+
+namespace Orthanc
+{
+  class DeflateBaseCompressor : public IBufferCompressor
+  {
+  private:
+    uint8_t compressionLevel_;
+    bool    prefixWithUncompressedSize_;
+
+  public:
+    DeflateBaseCompressor() : 
+      compressionLevel_(6),
+      prefixWithUncompressedSize_(false)
+    {
+    }
+
+    void SetCompressionLevel(uint8_t level);
+    
+    void SetPrefixWithUncompressedSize(bool prefix)
+    {
+      prefixWithUncompressedSize_ = prefix;
+    }
+
+    bool HasPrefixWithUncompressedSize() const
+    {
+      return prefixWithUncompressedSize_;
+    }
+
+    uint8_t GetCompressionLevel() const
+    {
+      return compressionLevel_;
+    }
+  };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/Compression/IBufferCompressor.h	Mon Aug 10 14:52:10 2015 +0200
@@ -0,0 +1,73 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, 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.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * 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 <string>
+#include <boost/noncopyable.hpp>
+
+namespace Orthanc
+{
+  class IBufferCompressor : public boost::noncopyable
+  {
+  public:
+    virtual ~IBufferCompressor()
+    {
+    }
+
+    virtual void Compress(std::string& compressed,
+                          const void* uncompressed,
+                          size_t uncompressedSize) = 0;
+
+    virtual void Uncompress(std::string& uncompressed,
+                            const void* compressed,
+                            size_t compressedSize) = 0;
+
+    static void Compress(std::string& compressed,
+                         IBufferCompressor& compressor,
+                         const std::string& uncompressed)
+    {
+      compressor.Compress(compressed, 
+                          uncompressed.size() == 0 ? NULL : uncompressed.c_str(), 
+                          uncompressed.size());
+    }
+
+    static void Uncompress(std::string& uncompressed,
+                           IBufferCompressor& compressor,
+                           const std::string& compressed)
+    {
+      compressor.Uncompress(uncompressed, 
+                            compressed.size() == 0 ? NULL : compressed.c_str(), 
+                            compressed.size());
+    }
+  };
+}
--- a/Core/Compression/ZlibCompressor.cpp	Mon Aug 10 14:18:24 2015 +0200
+++ b/Core/Compression/ZlibCompressor.cpp	Mon Aug 10 14:52:10 2015 +0200
@@ -40,17 +40,6 @@
 
 namespace Orthanc
 {
-  void ZlibCompressor::SetCompressionLevel(uint8_t level)
-  {
-    if (level >= 10)
-    {
-      throw OrthancException("Zlib compression level must be between 0 (no compression) and 9 (highest compression");
-    }
-
-    compressionLevel_ = level;
-  }
-
-
   void ZlibCompressor::Compress(std::string& compressed,
                                 const void* uncompressed,
                                 size_t uncompressedSize)
@@ -68,7 +57,7 @@
     }
 
     uint8_t* target;
-    if (prefixWithUncompressedSize_)
+    if (HasPrefixWithUncompressedSize())
     {
       compressed.resize(compressedSize + sizeof(uint64_t));
       target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
@@ -83,7 +72,7 @@
                           &compressedSize,
                           const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), 
                           uncompressedSize,
-                          compressionLevel_);
+                          GetCompressionLevel());
 
     if (error != Z_OK)
     {
@@ -100,7 +89,7 @@
     }
 
     // The compression was successful
-    if (prefixWithUncompressedSize_)
+    if (HasPrefixWithUncompressedSize())
     {
       uint64_t s = static_cast<uint64_t>(uncompressedSize);
       memcpy(&compressed[0], &s, sizeof(uint64_t));
--- a/Core/Compression/ZlibCompressor.h	Mon Aug 10 14:18:24 2015 +0200
+++ b/Core/Compression/ZlibCompressor.h	Mon Aug 10 14:52:10 2015 +0200
@@ -32,41 +32,16 @@
 
 #pragma once
 
-#include "BufferCompressor.h"
+#include "DeflateBaseCompressor.h"
 
 namespace Orthanc
 {
-  class ZlibCompressor : public BufferCompressor
+  class ZlibCompressor : public DeflateBaseCompressor
   {
-  private:
-    uint8_t compressionLevel_;
-    bool    prefixWithUncompressedSize_;
-
   public:
-    using BufferCompressor::Compress;
-    using BufferCompressor::Uncompress;
-
-    ZlibCompressor() : 
-      compressionLevel_(6),
-      prefixWithUncompressedSize_(true)
+    ZlibCompressor()
     {
-    }
-
-    void SetCompressionLevel(uint8_t level);
-    
-    void SetPrefixWithUncompressedSize(bool prefix)
-    {
-      prefixWithUncompressedSize_ = prefix;
-    }
-
-    bool HasPrefixWithUncompressedSize() const
-    {
-      return prefixWithUncompressedSize_;
-    }
-
-    uint8_t GetCompressionLevel() const
-    {
-      return compressionLevel_;
+      SetPrefixWithUncompressedSize(true);
     }
 
     virtual void Compress(std::string& compressed,
--- a/Core/FileStorage/CompressedFileStorageAccessor.cpp	Mon Aug 10 14:18:24 2015 +0200
+++ b/Core/FileStorage/CompressedFileStorageAccessor.cpp	Mon Aug 10 14:52:10 2015 +0200
@@ -135,7 +135,7 @@
     {
       std::string compressed;
       GetStorageArea().Read(compressed, uuid, type);
-      zlib_.Uncompress(content, compressed);
+      IBufferCompressor::Uncompress(content, zlib_, compressed);
       break;
     }
 
@@ -161,7 +161,7 @@
       GetStorageArea().Read(compressed, uuid, type);
 
       std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender);
-      zlib_.Uncompress(sender->GetBuffer(), compressed);
+      IBufferCompressor::Uncompress(sender->GetBuffer(), zlib_, compressed);
 
       return sender.release();
     }        
--- a/UnitTestsSources/UnitTestsMain.cpp	Mon Aug 10 14:18:24 2015 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Mon Aug 10 14:52:10 2015 +0200
@@ -98,14 +98,6 @@
   ASSERT_FALSE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b_"));
 }
 
-static void StringToVector(std::vector<uint8_t>& v,
-                           const std::string& s)
-{
-  v.resize(s.size());
-  for (size_t i = 0; i < s.size(); i++)
-    v[i] = s[i];
-}
-
 
 TEST(Zlib, Basic)
 {
@@ -114,20 +106,10 @@
  
   std::string compressed, compressed2;
   ZlibCompressor c;
-  c.Compress(compressed, s);
-
-  std::vector<uint8_t> v, vv;
-  StringToVector(v, s);
-  c.Compress(compressed2, v);
-  ASSERT_EQ(compressed, compressed2);
+  IBufferCompressor::Compress(compressed, c, s);
 
   std::string uncompressed;
-  c.Uncompress(uncompressed, compressed);
-  ASSERT_EQ(s.size(), uncompressed.size());
-  ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size()));
-
-  StringToVector(vv, compressed);
-  c.Uncompress(uncompressed, vv);
+  IBufferCompressor::Uncompress(uncompressed, c, compressed);
   ASSERT_EQ(s.size(), uncompressed.size());
   ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size()));
 }
@@ -141,10 +123,10 @@
   std::string compressed, compressed2;
   ZlibCompressor c;
   c.SetCompressionLevel(9);
-  c.Compress(compressed, s);
+  IBufferCompressor::Compress(compressed, c, s);
 
   c.SetCompressionLevel(0);
-  c.Compress(compressed2, s);
+  IBufferCompressor::Compress(compressed2, c, s);
 
   ASSERT_TRUE(compressed.size() < compressed2.size());
 }
@@ -157,32 +139,26 @@
  
   std::string compressed;
   ZlibCompressor c;
-  c.Compress(compressed, s);
+  IBufferCompressor::Compress(compressed, c, s);
 
   compressed[compressed.size() - 1] = 'a';
   std::string u;
 
-  ASSERT_THROW(c.Uncompress(u, compressed), OrthancException);
+  ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException);
 }
 
 
 TEST(Zlib, Empty)
 {
   std::string s = "";
-  std::vector<uint8_t> v, vv;
  
   std::string compressed, compressed2;
   ZlibCompressor c;
-  c.Compress(compressed, s);
-  c.Compress(compressed2, v);
+  IBufferCompressor::Compress(compressed, c, s);
   ASSERT_EQ(compressed, compressed2);
 
   std::string uncompressed;
-  c.Uncompress(uncompressed, compressed);
-  ASSERT_EQ(0u, uncompressed.size());
-
-  StringToVector(vv, compressed);
-  c.Uncompress(uncompressed, vv);
+  IBufferCompressor::Uncompress(uncompressed, c, compressed);
   ASSERT_EQ(0u, uncompressed.size());
 }