From: Ved-vampir Date: Fri, 10 Jun 2016 14:13:23 +0000 (+0300) Subject: compressor: zlib compressor plugin cleanup X-Git-Tag: ses5-milestone5~388^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=34c2ea9f096c2dd48536181d081096ccab913752;p=ceph.git compressor: zlib compressor plugin cleanup Signed-off-by: Alyona Kiseleva --- diff --git a/src/compressor/zlib/CMakeLists.txt b/src/compressor/zlib/CMakeLists.txt index 77766dc9fa0e..f80131e7b608 100644 --- a/src/compressor/zlib/CMakeLists.txt +++ b/src/compressor/zlib/CMakeLists.txt @@ -2,7 +2,7 @@ set(zlib_sources CompressionPluginZlib.cc - CompressionZlib.cc + ZlibCompressor.cc ) add_library(ceph_zlib SHARED ${zlib_sources}) diff --git a/src/compressor/zlib/CompressionPluginZlib.cc b/src/compressor/zlib/CompressionPluginZlib.cc index 90fd561d2d61..7140fccba2f7 100644 --- a/src/compressor/zlib/CompressionPluginZlib.cc +++ b/src/compressor/zlib/CompressionPluginZlib.cc @@ -16,7 +16,7 @@ // ----------------------------------------------------------------------------- #include "ceph_ver.h" #include "compressor/CompressionPlugin.h" -#include "CompressionZlib.h" +#include "ZlibCompressor.h" #include "common/debug.h" #define dout_subsys ceph_subsys_mon @@ -32,7 +32,7 @@ public: ostream *ss) { if (compressor == 0) { - CompressionZlib *interface = new CompressionZlib(); + ZlibCompressor *interface = new ZlibCompressor(); compressor = CompressorRef(interface); } *cs = compressor; diff --git a/src/compressor/zlib/CompressionZlib.cc b/src/compressor/zlib/CompressionZlib.cc deleted file mode 100644 index 27aba8cf464d..000000000000 --- a/src/compressor/zlib/CompressionZlib.cc +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Ceph - scalable distributed file system - * - * Copyright (C) 2015 Mirantis, Inc. - * - * Author: Alyona Kiseleva - * - * 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 "common/debug.h" -#include "CompressionZlib.h" -#include "osd/osd_types.h" -// ----------------------------------------------------------------------------- - -#include - -// ----------------------------------------------------------------------------- -#define dout_subsys ceph_subsys_compressor -#undef dout_prefix -#define dout_prefix _prefix(_dout) -// ----------------------------------------------------------------------------- - -// ----------------------------------------------------------------------------- - -static ostream& -_prefix(std::ostream* _dout) -{ - return *_dout << "CompressionZlib: "; -} -// ----------------------------------------------------------------------------- - -const long unsigned int max_len = 2048; - -int CompressionZlib::compress(const bufferlist &in, bufferlist &out) -{ - int ret; - unsigned have; - z_stream strm; - unsigned char* c_in; - int level = 5; - - /* allocate deflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - ret = deflateInit(&strm, level); - if (ret != Z_OK) { - dout(1) << "Compression init error: init return " - << ret << " instead of Z_OK" << dendl; - return -1; - } - - unsigned char c_out [max_len]; - - for (std::list::const_iterator i = in.buffers().begin(); - i != in.buffers().end();) { - - c_in = (unsigned char*) (*i).c_str(); - long unsigned int len = (*i).length(); - ++i; - - strm.avail_in = len; - int flush = i != in.buffers().end() ? Z_NO_FLUSH : Z_FINISH; - - strm.next_in = c_in; - - do { - strm.avail_out = max_len; - strm.next_out = c_out; - ret = deflate(&strm, flush); /* no bad return value */ - if (ret == Z_STREAM_ERROR) { - dout(1) << "Compression error: compress return Z_STREAM_ERROR(" - << ret << ")" << dendl; - deflateEnd(&strm); - return -1; - } - have = max_len - strm.avail_out; - out.append((char*)c_out, have); - } while (strm.avail_out == 0); - if (strm.avail_in != 0) { - dout(10) << "Compression error: unused input" << dendl; - deflateEnd(&strm); - return -1; - } - } - - deflateEnd(&strm); - return 0; -} - -int CompressionZlib::decompress(bufferlist::iterator &p, size_t compressed_size, bufferlist &out) -{ - int ret; - unsigned have; - z_stream strm; - const char* c_in; - - /* allocate inflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = 0; - strm.next_in = Z_NULL; - ret = inflateInit(&strm); - if (ret != Z_OK) { - dout(1) << "Decompression init error: init return " - << ret << " instead of Z_OK" << dendl; - return -1; - } - - unsigned char c_out[max_len]; - size_t remaining = MIN( p.get_remaining(), compressed_size); - while(remaining) { - - long unsigned int len = p.get_ptr_and_advance(remaining, &c_in); - remaining -= len; - strm.avail_in = len; - strm.next_in = (unsigned char*)c_in; - - do { - strm.avail_out = max_len; - strm.next_out = c_out; - ret = inflate(&strm, Z_NO_FLUSH); - if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) { - dout(1) << "Decompression error: decompress return " - << ret << dendl; - inflateEnd(&strm); - return -1; - } - have = max_len - strm.avail_out; - out.append((char*)c_out, have); - } while (strm.avail_out == 0); - - } - - /* clean up and return */ - (void)inflateEnd(&strm); - return 0; -} - -int CompressionZlib::decompress(const bufferlist &in, bufferlist &out) -{ - bufferlist::iterator i = const_cast(in).begin(); - return decompress(i, in.length(), out); -} diff --git a/src/compressor/zlib/CompressionZlib.h b/src/compressor/zlib/CompressionZlib.h deleted file mode 100644 index 133f91232fb1..000000000000 --- a/src/compressor/zlib/CompressionZlib.h +++ /dev/null @@ -1,33 +0,0 @@ -// -*- 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 Mirantis, Inc. - * - * Author: Alyona Kiseleva - * - * 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_ZLIB_H -#define CEPH_COMPRESSION_ZLIB_H - -#include "compressor/Compressor.h" - -class CompressionZlib : public Compressor { - const char version = '1'; - -public: - CompressionZlib() : Compressor("zlib") {} - int compress(const bufferlist &in, bufferlist &out) override; - int decompress(const bufferlist &in, bufferlist &out) override; - int decompress(bufferlist::iterator &p, size_t compressed_len, bufferlist &out) override; - }; - - -#endif diff --git a/src/compressor/zlib/Makefile.am b/src/compressor/zlib/Makefile.am index 7f97ea424a00..a0056123b89b 100644 --- a/src/compressor/zlib/Makefile.am +++ b/src/compressor/zlib/Makefile.am @@ -1,12 +1,12 @@ # zlib plugin noinst_HEADERS += \ - compressor/zlib/CompressionZlib.h + compressor/zlib/ZlibCompressor.h zlib_sources = \ common/buffer.cc \ compressor/Compressor.cc \ compressor/zlib/CompressionPluginZlib.cc \ - compressor/zlib/CompressionZlib.cc + compressor/zlib/ZlibCompressor.cc compressor/zlib/CompressionPluginZlib.cc: ./ceph_ver.h diff --git a/src/compressor/zlib/ZlibCompressor.cc b/src/compressor/zlib/ZlibCompressor.cc new file mode 100644 index 000000000000..747ffae132e5 --- /dev/null +++ b/src/compressor/zlib/ZlibCompressor.cc @@ -0,0 +1,150 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2015 Mirantis, Inc. + * + * Author: Alyona Kiseleva + * + * 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 "common/debug.h" +#include "ZlibCompressor.h" +#include "osd/osd_types.h" +// ----------------------------------------------------------------------------- + +#include + +// ----------------------------------------------------------------------------- +#define dout_subsys ceph_subsys_compressor +#undef dout_prefix +#define dout_prefix _prefix(_dout) +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- + +static ostream& +_prefix(std::ostream* _dout) +{ + return *_dout << "ZlibCompressor: "; +} +// ----------------------------------------------------------------------------- + +const long unsigned int max_len = 2048; + +int ZlibCompressor::compress(const bufferlist &in, bufferlist &out) +{ + int ret; + unsigned have; + z_stream strm; + unsigned char* c_in; + int level = 5; + + /* allocate deflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + ret = deflateInit(&strm, level); + if (ret != Z_OK) { + dout(1) << "Compression init error: init return " + << ret << " instead of Z_OK" << dendl; + return -1; + } + + for (std::list::const_iterator i = in.buffers().begin(); + i != in.buffers().end();) { + + c_in = (unsigned char*) (*i).c_str(); + long unsigned int len = (*i).length(); + ++i; + + strm.avail_in = len; + int flush = i != in.buffers().end() ? Z_NO_FLUSH : Z_FINISH; + + strm.next_in = c_in; + + do { + strm.avail_out = max_len; + bufferptr ptr = buffer::create_page_aligned(max_len); + strm.next_out = (unsigned char*)ptr.c_str(); + ret = deflate(&strm, flush); /* no bad return value */ + if (ret == Z_STREAM_ERROR) { + dout(1) << "Compression error: compress return Z_STREAM_ERROR(" + << ret << ")" << dendl; + deflateEnd(&strm); + return -1; + } + have = max_len - strm.avail_out; + out.append(ptr, 0, have); + } while (strm.avail_out == 0); + if (strm.avail_in != 0) { + dout(10) << "Compression error: unused input" << dendl; + deflateEnd(&strm); + return -1; + } + } + + deflateEnd(&strm); + return 0; +} + +int ZlibCompressor::decompress(bufferlist::iterator &p, size_t compressed_size, bufferlist &out) +{ + int ret; + unsigned have; + z_stream strm; + const char* c_in; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + ret = inflateInit(&strm); + if (ret != Z_OK) { + dout(1) << "Decompression init error: init return " + << ret << " instead of Z_OK" << dendl; + return -1; + } + + size_t remaining = MIN(p.get_remaining(), compressed_size); + while(remaining) { + + long unsigned int len = p.get_ptr_and_advance(remaining, &c_in); + remaining -= len; + strm.avail_in = len; + strm.next_in = (unsigned char*)c_in; + + do { + strm.avail_out = max_len; + bufferptr ptr = buffer::create_page_aligned(max_len); + strm.next_out = (unsigned char*)ptr.c_str(); + ret = inflate(&strm, Z_NO_FLUSH); + if (ret != Z_OK && ret != Z_STREAM_END && ret != Z_BUF_ERROR) { + dout(1) << "Decompression error: decompress return " + << ret << dendl; + inflateEnd(&strm); + return -1; + } + have = max_len - strm.avail_out; + out.append(ptr, 0, have); + } while (strm.avail_out == 0); + + } + + /* clean up and return */ + (void)inflateEnd(&strm); + return 0; +} + +int ZlibCompressor::decompress(const bufferlist &in, bufferlist &out) +{ + bufferlist::iterator i = const_cast(in).begin(); + return decompress(i, in.length(), out); +} diff --git a/src/compressor/zlib/ZlibCompressor.h b/src/compressor/zlib/ZlibCompressor.h new file mode 100644 index 000000000000..7cac1a68b1a5 --- /dev/null +++ b/src/compressor/zlib/ZlibCompressor.h @@ -0,0 +1,32 @@ +// -*- 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 Mirantis, Inc. + * + * Author: Alyona Kiseleva + * + * 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_ZLIB_H +#define CEPH_COMPRESSION_ZLIB_H + +#include "compressor/Compressor.h" + +class ZlibCompressor : public Compressor { +public: + ZlibCompressor() : Compressor("zlib") {} + + int compress(const bufferlist &in, bufferlist &out) override; + int decompress(const bufferlist &in, bufferlist &out) override; + int decompress(bufferlist::iterator &p, size_t compressed_len, bufferlist &out) override; + }; + + +#endif diff --git a/src/test/compressor/test_compression_zlib.cc b/src/test/compressor/test_compression_zlib.cc index 642264b3380e..4932fbd6ff85 100644 --- a/src/test/compressor/test_compression_zlib.cc +++ b/src/test/compressor/test_compression_zlib.cc @@ -19,14 +19,14 @@ #include #include #include "global/global_init.h" -#include "compressor/zlib/CompressionZlib.h" +#include "compressor/zlib/ZlibCompressor.h" #include "common/ceph_argparse.h" #include "global/global_context.h" #include "common/config.h" -TEST(CompressionZlib, compress_decompress) +TEST(ZlibCompressor, compress_decompress) { - CompressionZlib sp; + ZlibCompressor sp; EXPECT_STREQ(sp.get_type().c_str(), "zlib"); const char* test = "This is test text"; int len = strlen(test); @@ -49,9 +49,9 @@ TEST(CompressionZlib, compress_decompress) EXPECT_TRUE(exp.contents_equal(after)); } -TEST(CompressionZlib, compress_decompress_chunk) +TEST(ZlibCompressor, compress_decompress_chunk) { - CompressionZlib sp; + ZlibCompressor sp; EXPECT_STREQ(sp.get_type().c_str(), "zlib"); const char* test = "This is test text"; buffer::ptr test2 ("1234567890", 10);