summaryrefslogtreecommitdiffstats
path: root/src/compressor/zstd
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/compressor/zstd
parentInitial commit. (diff)
downloadceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz
ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/compressor/zstd')
-rw-r--r--src/compressor/zstd/CMakeLists.txt36
-rw-r--r--src/compressor/zstd/CompressionPluginZstd.cc36
-rw-r--r--src/compressor/zstd/CompressionPluginZstd.h43
-rw-r--r--src/compressor/zstd/ZstdCompressor.h107
4 files changed, 222 insertions, 0 deletions
diff --git a/src/compressor/zstd/CMakeLists.txt b/src/compressor/zstd/CMakeLists.txt
new file mode 100644
index 000000000..b53b0d944
--- /dev/null
+++ b/src/compressor/zstd/CMakeLists.txt
@@ -0,0 +1,36 @@
+# zstd
+
+# libzstd - build it statically
+set(ZSTD_C_FLAGS "-fPIC -Wno-unused-variable -O3")
+
+include(ExternalProject)
+ExternalProject_Add(zstd_ext
+ SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/zstd/build/cmake
+ CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
+ -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+ -DCMAKE_C_FLAGS=${ZSTD_C_FLAGS}
+ -DCMAKE_AR=${CMAKE_AR}
+ -DCMAKE_POSITION_INDEPENDENT_CODE=${ENABLE_SHARED}
+ -G${CMAKE_GENERATOR}
+ BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/libzstd
+ BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target libzstd_static
+ BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a"
+ INSTALL_COMMAND "true")
+
+add_library(zstd STATIC IMPORTED)
+set_target_properties(zstd PROPERTIES
+ INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/src/zstd/lib"
+ IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libzstd/lib/libzstd.a")
+add_dependencies(zstd zstd_ext)
+
+set(zstd_sources
+ CompressionPluginZstd.cc
+)
+
+add_library(ceph_zstd SHARED ${zstd_sources})
+target_link_libraries(ceph_zstd PRIVATE zstd $<$<PLATFORM_ID:Windows>:ceph-common>)
+set_target_properties(ceph_zstd PROPERTIES
+ VERSION 2.0.0
+ SOVERSION 2
+ INSTALL_RPATH "")
+install(TARGETS ceph_zstd DESTINATION ${compressor_plugin_dir})
diff --git a/src/compressor/zstd/CompressionPluginZstd.cc b/src/compressor/zstd/CompressionPluginZstd.cc
new file mode 100644
index 000000000..92aee7343
--- /dev/null
+++ b/src/compressor/zstd/CompressionPluginZstd.cc
@@ -0,0 +1,36 @@
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2015 Mirantis, Inc.
+ *
+ * Author: Alyona Kiseleva <akiselyova@mirantis.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ */
+
+#include "acconfig.h"
+#include "ceph_ver.h"
+#include "common/ceph_context.h"
+#include "CompressionPluginZstd.h"
+
+// -----------------------------------------------------------------------------
+
+const char *__ceph_plugin_version()
+{
+ return CEPH_GIT_NICE_VER;
+}
+
+// -----------------------------------------------------------------------------
+
+int __ceph_plugin_init(CephContext *cct,
+ const std::string& type,
+ const std::string& name)
+{
+ auto instance = cct->get_plugin_registry();
+
+ return instance->add(type, name, new CompressionPluginZstd(cct));
+}
diff --git a/src/compressor/zstd/CompressionPluginZstd.h b/src/compressor/zstd/CompressionPluginZstd.h
new file mode 100644
index 000000000..632e7c6dc
--- /dev/null
+++ b/src/compressor/zstd/CompressionPluginZstd.h
@@ -0,0 +1,43 @@
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2015 Mirantis, Inc.
+ *
+ * Author: Alyona Kiseleva <akiselyova@mirantis.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ */
+
+#ifndef CEPH_COMPRESSION_PLUGIN_ZSTD_H
+#define CEPH_COMPRESSION_PLUGIN_ZSTD_H
+
+// -----------------------------------------------------------------------------
+#include "ceph_ver.h"
+#include "compressor/CompressionPlugin.h"
+#include "ZstdCompressor.h"
+// -----------------------------------------------------------------------------
+
+class CompressionPluginZstd : public ceph::CompressionPlugin {
+
+public:
+
+ explicit CompressionPluginZstd(CephContext* cct) : CompressionPlugin(cct)
+ {}
+
+ int factory(CompressorRef *cs,
+ std::ostream *ss) override
+ {
+ if (compressor == 0) {
+ ZstdCompressor *interface = new ZstdCompressor(cct);
+ compressor = CompressorRef(interface);
+ }
+ *cs = compressor;
+ return 0;
+ }
+};
+
+#endif
diff --git a/src/compressor/zstd/ZstdCompressor.h b/src/compressor/zstd/ZstdCompressor.h
new file mode 100644
index 000000000..95b492deb
--- /dev/null
+++ b/src/compressor/zstd/ZstdCompressor.h
@@ -0,0 +1,107 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2015 Haomai Wang <haomaiwang@gmail.com>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+
+#ifndef CEPH_ZSTDCOMPRESSOR_H
+#define CEPH_ZSTDCOMPRESSOR_H
+
+#define ZSTD_STATIC_LINKING_ONLY
+#include "zstd/lib/zstd.h"
+
+#include "include/buffer.h"
+#include "include/encoding.h"
+#include "compressor/Compressor.h"
+
+class ZstdCompressor : public Compressor {
+ public:
+ ZstdCompressor(CephContext *cct) : Compressor(COMP_ALG_ZSTD, "zstd"), cct(cct) {}
+
+ int compress(const ceph::buffer::list &src, ceph::buffer::list &dst, boost::optional<int32_t> &compressor_message) override {
+ ZSTD_CStream *s = ZSTD_createCStream();
+ ZSTD_initCStream_srcSize(s, cct->_conf->compressor_zstd_level, src.length());
+ auto p = src.begin();
+ size_t left = src.length();
+
+ size_t const out_max = ZSTD_compressBound(left);
+ ceph::buffer::ptr outptr = ceph::buffer::create_small_page_aligned(out_max);
+ ZSTD_outBuffer_s outbuf;
+ outbuf.dst = outptr.c_str();
+ outbuf.size = outptr.length();
+ outbuf.pos = 0;
+
+ while (left) {
+ ceph_assert(!p.end());
+ struct ZSTD_inBuffer_s inbuf;
+ inbuf.pos = 0;
+ inbuf.size = p.get_ptr_and_advance(left, (const char**)&inbuf.src);
+ left -= inbuf.size;
+ ZSTD_EndDirective const zed = (left==0) ? ZSTD_e_end : ZSTD_e_continue;
+ size_t r = ZSTD_compressStream2(s, &outbuf, &inbuf, zed);
+ if (ZSTD_isError(r)) {
+ return -EINVAL;
+ }
+ }
+ ceph_assert(p.end());
+
+ ZSTD_freeCStream(s);
+
+ // prefix with decompressed length
+ ceph::encode((uint32_t)src.length(), dst);
+ dst.append(outptr, 0, outbuf.pos);
+ return 0;
+ }
+
+ int decompress(const ceph::buffer::list &src, ceph::buffer::list &dst, boost::optional<int32_t> compressor_message) override {
+ auto i = std::cbegin(src);
+ return decompress(i, src.length(), dst, compressor_message);
+ }
+
+ int decompress(ceph::buffer::list::const_iterator &p,
+ size_t compressed_len,
+ ceph::buffer::list &dst,
+ boost::optional<int32_t> compressor_message) override {
+ if (compressed_len < 4) {
+ return -1;
+ }
+ compressed_len -= 4;
+ uint32_t dst_len;
+ ceph::decode(dst_len, p);
+
+ ceph::buffer::ptr dstptr(dst_len);
+ ZSTD_outBuffer_s outbuf;
+ outbuf.dst = dstptr.c_str();
+ outbuf.size = dstptr.length();
+ outbuf.pos = 0;
+ ZSTD_DStream *s = ZSTD_createDStream();
+ ZSTD_initDStream(s);
+ while (compressed_len > 0) {
+ if (p.end()) {
+ return -1;
+ }
+ ZSTD_inBuffer_s inbuf;
+ inbuf.pos = 0;
+ inbuf.size = p.get_ptr_and_advance(compressed_len,
+ (const char**)&inbuf.src);
+ ZSTD_decompressStream(s, &outbuf, &inbuf);
+ compressed_len -= inbuf.size;
+ }
+ ZSTD_freeDStream(s);
+
+ dst.append(dstptr, 0, outbuf.pos);
+ return 0;
+ }
+ private:
+ CephContext *const cct;
+};
+
+#endif