ClickHouse/dbms/src/IO/CompressedWriteBuffer.cpp

63 lines
1.4 KiB
C++
Raw Normal View History

2016-10-25 06:49:24 +00:00
#include <memory>
#include <city.h>
#include <lz4.h>
#include <lz4hc.h>
#include <zstd.h>
2017-07-28 14:14:07 +00:00
#include <string.h>
2016-10-25 06:49:24 +00:00
#include <common/unaligned.h>
2017-04-08 01:32:05 +00:00
#include <Core/Types.h>
2016-10-25 06:49:24 +00:00
#include <IO/CompressedWriteBuffer.h>
2018-12-20 17:37:02 +00:00
#include <Compression/CompressionFactory.h>
2016-10-25 06:49:24 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_COMPRESS;
extern const int UNKNOWN_COMPRESSION_METHOD;
2016-10-25 06:49:24 +00:00
}
2018-12-20 17:37:02 +00:00
static constexpr auto CHECKSUM_SIZE{sizeof(CityHash_v1_0_2::uint128)};
2016-10-25 06:49:24 +00:00
void CompressedWriteBuffer::nextImpl()
{
if (!offset())
return;
2018-12-20 17:37:02 +00:00
size_t decompressed_size = offset();
UInt32 compressed_reserve_size = codec->getCompressedReserveSize(decompressed_size);
compressed_buffer.resize(compressed_reserve_size);
UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data());
2018-12-20 17:37:02 +00:00
CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(compressed_buffer.data(), compressed_size);
out.write(reinterpret_cast<const char *>(&checksum), CHECKSUM_SIZE);
out.write(compressed_buffer.data(), compressed_size);
}
2016-10-25 06:49:24 +00:00
2018-12-20 17:37:02 +00:00
CompressedWriteBuffer::CompressedWriteBuffer(
WriteBuffer & out_,
CompressionCodecPtr codec_,
size_t buf_size)
: BufferWithOwnMemory<WriteBuffer>(buf_size), out(out_), codec(codec_)
{
2016-10-25 06:49:24 +00:00
}
CompressedWriteBuffer::~CompressedWriteBuffer()
{
try
{
next();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2016-10-25 06:49:24 +00:00
}
}