ClickHouse/dbms/Compression/ICompressionCodec.h

75 lines
2.7 KiB
C++
Raw Normal View History

2018-10-11 02:57:48 +00:00
#pragma once
#include <memory>
#include <IO/ReadBuffer.h>
#include <IO/WriteBuffer.h>
#include <IO/BufferWithOwnMemory.h>
#include <DataTypes/IDataType.h>
#include <boost/noncopyable.hpp>
#include <IO/UncompressedCache.h>
2018-12-28 18:15:26 +00:00
#include <Compression/LZ4_decompress_faster.h>
#include <Compression/CompressionInfo.h>
2018-10-11 02:57:48 +00:00
namespace DB
{
class ICompressionCodec;
using CompressionCodecPtr = std::shared_ptr<ICompressionCodec>;
using Codecs = std::vector<CompressionCodecPtr>;
/**
* Represents interface for compression codecs like LZ4, ZSTD, etc.
2018-10-11 02:57:48 +00:00
*/
class ICompressionCodec : private boost::noncopyable
{
public:
virtual ~ICompressionCodec() = default;
/// Byte which indicates codec in compressed file
2020-01-03 14:39:24 +00:00
virtual uint8_t getMethodByte() const = 0;
2018-12-19 17:20:18 +00:00
/// Codec description, for example "ZSTD(2)" or "LZ4,LZ4HC(5)"
2018-12-19 17:20:18 +00:00
virtual String getCodecDesc() const = 0;
/// Compressed bytes from uncompressed source to dest. Dest should preallocate memory
2019-12-26 03:07:38 +00:00
UInt32 compress(const char * source, UInt32 source_size, char * dest) const;
2018-12-19 17:20:18 +00:00
/// Decompress bytes from compressed source to dest. Dest should preallocate memory
2019-12-26 03:07:38 +00:00
UInt32 decompress(const char * source, UInt32 source_size, char * dest) const;
2018-12-19 17:20:18 +00:00
/// Number of bytes, that will be used to compress uncompressed_size bytes with current codec
virtual UInt32 getCompressedReserveSize(UInt32 uncompressed_size) const { return getHeaderSize() + getMaxCompressedDataSize(uncompressed_size); }
2018-12-19 17:20:18 +00:00
/// Some codecs (LZ4, for example) require additional bytes at end of buffer
2018-12-20 10:27:38 +00:00
virtual UInt32 getAdditionalSizeAtTheEndOfBuffer() const { return 0; }
/// Size of header in compressed data on disk
2019-12-19 19:23:49 +00:00
static constexpr UInt8 getHeaderSize() { return COMPRESSED_BLOCK_HEADER_SIZE; }
2018-12-19 17:20:18 +00:00
/// Read size of compressed block from compressed source
2018-12-19 17:20:18 +00:00
static UInt32 readCompressedBlockSize(const char * source);
2018-10-11 02:57:48 +00:00
/// Read size of decompressed block from compressed source
2018-12-19 17:20:18 +00:00
static UInt32 readDecompressedBlockSize(const char * source);
2018-10-11 02:57:48 +00:00
/// Read method byte from compressed source
2020-01-03 14:39:24 +00:00
static uint8_t readMethod(const char * source);
2018-10-11 02:57:48 +00:00
/// Some codecs may use information about column type which appears after codec creation
virtual void useInfoAboutType(DataTypePtr /* data_type */) { }
2018-12-19 17:20:18 +00:00
protected:
2018-10-11 02:57:48 +00:00
/// Return size of compressed data without header
virtual UInt32 getMaxCompressedDataSize(UInt32 uncompressed_size) const { return uncompressed_size; }
2018-10-11 02:57:48 +00:00
/// Actually compress data, without header
2018-12-19 17:20:18 +00:00
virtual UInt32 doCompressData(const char * source, UInt32 source_size, char * dest) const = 0;
2018-10-11 02:57:48 +00:00
/// Actually decompress data without header
2018-12-19 17:20:18 +00:00
virtual void doDecompressData(const char * source, UInt32 source_size, char * dest, UInt32 uncompressed_size) const = 0;
2018-10-11 02:57:48 +00:00
};
}