#pragma once #include #include #include #include #include #include namespace DB { class CompressedWriteBuffer final : public BufferWithOwnMemory { public: explicit CompressedWriteBuffer( WriteBuffer & out_, CompressionCodecPtr codec_ = CompressionCodecFactory::instance().getDefaultCodec(), size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, bool use_adaptive_buffer_size_ = false, size_t adaptive_buffer_initial_size = DBMS_DEFAULT_INITIAL_ADAPTIVE_BUFFER_SIZE); ~CompressedWriteBuffer() override; /// The amount of compressed data size_t getCompressedBytes() { nextIfAtEnd(); return out.count(); } /// How many uncompressed bytes were written to the buffer size_t getUncompressedBytes() { return count(); } /// How many bytes are in the buffer (not yet compressed) size_t getRemainingBytes() { nextIfAtEnd(); return offset(); } private: void nextImpl() override; void finalizeImpl() override; WriteBuffer & out; CompressionCodecPtr codec; /// If true, the size of internal buffer will be exponentially increased up to /// adaptive_buffer_max_size after each nextImpl call. It can be used to avoid /// large buffer allocation when actual size of written data is small. bool use_adaptive_buffer_size; size_t adaptive_buffer_max_size; PODArray compressed_buffer; }; }