ClickHouse/dbms/Compression/CompressedWriteBuffer.cpp

64 lines
1.5 KiB
C++
Raw Normal View History

2016-10-25 06:49:24 +00:00
#include <memory>
#include <city.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 "CompressedWriteBuffer.h"
2018-12-20 17:37:02 +00:00
#include <Compression/CompressionFactory.h>
2016-10-25 06:49:24 +00:00
2020-03-26 12:35:31 +00:00
#include <Common/MemorySanitizer.h>
2016-10-25 06:49:24 +00:00
namespace DB
{
namespace ErrorCodes
{
}
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());
2020-03-26 12:35:31 +00:00
// FIXME remove this after fixing msan report in lz4.
// Almost always reproduces on stateless tests, the exact test unknown.
__msan_unpoison(compressed_buffer.data(), compressed_size);
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)
2019-01-21 18:04:08 +00:00
: BufferWithOwnMemory<WriteBuffer>(buf_size), out(out_), codec(std::move(codec_))
2018-12-20 17:37:02 +00:00
{
2016-10-25 06:49:24 +00:00
}
CompressedWriteBuffer::~CompressedWriteBuffer()
{
try
{
next();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
2016-10-25 06:49:24 +00:00
}
}