| 1 | #pragma once |
| 2 | |
| 3 | #include <IO/WriteBuffer.h> |
| 4 | #include <IO/BufferWithOwnMemory.h> |
| 5 | #include <IO/CompressionMethod.h> |
| 6 | |
| 7 | #include <zlib.h> |
| 8 | |
| 9 | |
| 10 | namespace DB |
| 11 | { |
| 12 | |
| 13 | namespace ErrorCodes |
| 14 | { |
| 15 | extern const int ZLIB_DEFLATE_FAILED; |
| 16 | } |
| 17 | |
| 18 | /// Performs compression using zlib library and writes compressed data to out_ WriteBuffer. |
| 19 | class ZlibDeflatingWriteBuffer : public BufferWithOwnMemory<WriteBuffer> |
| 20 | { |
| 21 | public: |
| 22 | ZlibDeflatingWriteBuffer( |
| 23 | std::unique_ptr<WriteBuffer> out_, |
| 24 | CompressionMethod compression_method, |
| 25 | int compression_level, |
| 26 | size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, |
| 27 | char * existing_memory = nullptr, |
| 28 | size_t alignment = 0); |
| 29 | |
| 30 | /// Flush all pending data and write zlib footer to the underlying buffer. |
| 31 | /// After the first call to this function, subsequent calls will have no effect and |
| 32 | /// an attempt to write to this buffer will result in exception. |
| 33 | void finish(); |
| 34 | |
| 35 | ~ZlibDeflatingWriteBuffer() override; |
| 36 | |
| 37 | private: |
| 38 | void nextImpl() override; |
| 39 | |
| 40 | std::unique_ptr<WriteBuffer> out; |
| 41 | z_stream zstr; |
| 42 | bool finished = false; |
| 43 | }; |
| 44 | |
| 45 | } |
| 46 | |