2016-10-25 06:49:24 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <city.h>
|
2017-02-28 23:49:04 +00:00
|
|
|
#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
|
|
|
|
2017-06-23 20:22:35 +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
|
|
|
|
2017-04-01 09:19:00 +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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|