From 56a648d0ffc370123c4f1047b72d0d80080a1d9b Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Fri, 19 Jul 2013 23:28:22 +0200 Subject: [PATCH] THRIFT-2093 added the ability to set compression level in C++ zlib transport Patch: Randy Abernethy --- .../src/thrift/transport/TZlibTransport.cpp | 3 +- lib/cpp/src/thrift/transport/TZlibTransport.h | 47 ++++--------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/lib/cpp/src/thrift/transport/TZlibTransport.cpp b/lib/cpp/src/thrift/transport/TZlibTransport.cpp index 98b25b89..d77eedd0 100644 --- a/lib/cpp/src/thrift/transport/TZlibTransport.cpp +++ b/lib/cpp/src/thrift/transport/TZlibTransport.cpp @@ -21,7 +21,6 @@ #include #include #include -#include using std::string; @@ -57,7 +56,7 @@ void TZlibTransport::initZlib() { // Have to set this flag so we know whether to de-initialize. r_init = true; - rv = deflateInit(wstream_, Z_DEFAULT_COMPRESSION); + rv = deflateInit(wstream_, comp_level_); checkZlibRv(rv, wstream_->msg); } diff --git a/lib/cpp/src/thrift/transport/TZlibTransport.h b/lib/cpp/src/thrift/transport/TZlibTransport.h index 607969c8..dd9dd14a 100644 --- a/lib/cpp/src/thrift/transport/TZlibTransport.h +++ b/lib/cpp/src/thrift/transport/TZlibTransport.h @@ -23,6 +23,7 @@ #include #include #include +#include struct z_stream_s; @@ -59,12 +60,7 @@ class TZlibTransportException : public TTransportException { }; /** - * This transport uses zlib's compressed format on the "far" side. - * - * There are two kinds of TZlibTransport objects: - * - Standalone objects are used to encode self-contained chunks of data - * (like structures). They include checksums. - * - Non-standalone transports are used for RPC. They are not implemented yet. + * This transport uses zlib to compress on write and decompress on read * * TODO(dreiss): Don't do an extra copy of the compressed data if * the underlying transport is TBuffered or TMemory. @@ -72,7 +68,7 @@ class TZlibTransportException : public TTransportException { */ class TZlibTransport : public TVirtualTransport { public: - + /** * @param transport The transport to read compressed data from * and write compressed data to. @@ -80,14 +76,14 @@ class TZlibTransport : public TVirtualTransport { * @param crbuf_size Compressed buffer size for reading. * @param uwbuf_size Uncompressed buffer size for writing. * @param cwbuf_size Compressed buffer size for writing. - * - * TODO(dreiss): Write a constructor that isn't a pain. + * @param comp_level Compression level (0=none[fast], 6=default, 9=max[slow]). */ TZlibTransport(boost::shared_ptr transport, int urbuf_size = DEFAULT_URBUF_SIZE, int crbuf_size = DEFAULT_CRBUF_SIZE, int uwbuf_size = DEFAULT_UWBUF_SIZE, - int cwbuf_size = DEFAULT_CWBUF_SIZE) : + int cwbuf_size = DEFAULT_CWBUF_SIZE, + int16_t comp_level = Z_DEFAULT_COMPRESSION) : transport_(transport), urpos_(0), uwpos_(0), @@ -102,7 +98,8 @@ class TZlibTransport : public TVirtualTransport { uwbuf_(NULL), cwbuf_(NULL), rstream_(NULL), - wstream_(NULL) + wstream_(NULL), + comp_level_(comp_level) { if (uwbuf_size_ < MIN_DIRECT_DEFLATE_SIZE) { // Have to copy this into a local because of a linking issue. @@ -198,32 +195,6 @@ class TZlibTransport : public TVirtualTransport { void flushToZlib(const uint8_t* buf, int len, int flush); bool readFromZlib(); - private: - // Deprecated constructor signature. - // - // This used to be the constructor signature. If you are getting a compile - // error because you are trying to use this constructor, you need to update - // your code as follows: - // - Remove the use_for_rpc argument in the constructur. - // There is no longer any distinction between RPC and standalone zlib - // transports. (Previously, only standalone was allowed, anyway.) - // - Replace TZlibTransport::flush() calls with TZlibTransport::finish() - // in your code. Previously, flush() used to finish the zlib stream. - // Now flush() only flushes out pending data, so more writes can be - // performed after a flush(). The finish() method can be used to finalize - // the zlib stream. - // - // If we don't declare this constructor, old code written as - // TZlibTransport(trans, false) still compiles but behaves incorrectly. - // The second bool argument is converted to an integer and used as the - // urbuf_size. - TZlibTransport(boost::shared_ptr transport, - bool use_for_rpc, - int urbuf_size = DEFAULT_URBUF_SIZE, - int crbuf_size = DEFAULT_CRBUF_SIZE, - int uwbuf_size = DEFAULT_UWBUF_SIZE, - int cwbuf_size = DEFAULT_CWBUF_SIZE); - protected: // Writes smaller than this are buffered up. // Larger (or equal) writes are dumped straight to zlib. @@ -251,6 +222,8 @@ class TZlibTransport : public TVirtualTransport { struct z_stream_s* rstream_; struct z_stream_s* wstream_; + + const int comp_level_; }; -- 2.17.1