ClickHouse/src/Compression/CompressedWriteBuffer.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
2.3 KiB
C++
Raw Normal View History

2016-10-25 06:49:24 +00:00
#include <city.h>
#include <cstring>
2016-10-25 06:49:24 +00:00
2021-10-02 07:13:14 +00:00
#include <base/types.h>
2022-05-16 21:21:20 +00:00
#include <base/unaligned.h>
#include <base/defines.h>
2016-10-25 06:49:24 +00:00
2018-12-20 17:37:02 +00:00
#include <Compression/CompressionFactory.h>
2022-05-16 21:21:20 +00:00
#include "CompressedWriteBuffer.h"
2020-03-26 12:35:31 +00:00
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;
chassert(offset() <= INT_MAX);
UInt32 decompressed_size = static_cast<UInt32>(offset());
2018-12-20 17:37:02 +00:00
UInt32 compressed_reserve_size = codec->getCompressedReserveSize(decompressed_size);
2022-05-23 10:09:44 +00:00
/** During compression we need buffer with capacity >= compressed_reserve_size + CHECKSUM_SIZE.
*
* If output buffer has necessary capacity, we can compress data directly in output buffer.
* Then we can write checksum at the output buffer begin.
*
* If output buffer does not have necessary capacity. Compress data in temporary buffer.
* Then we can write checksum and temporary buffer in output buffer.
*/
if (out.available() >= compressed_reserve_size + CHECKSUM_SIZE)
{
2022-05-16 21:21:20 +00:00
char * out_checksum_ptr = out.position();
char * out_compressed_ptr = out.position() + CHECKSUM_SIZE;
2022-05-23 10:09:44 +00:00
UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, out_compressed_ptr);
CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(out_compressed_ptr, compressed_size);
2022-05-16 18:44:22 +00:00
memcpy(out_checksum_ptr, reinterpret_cast<const char *>(&checksum), CHECKSUM_SIZE);
out.position() += CHECKSUM_SIZE + compressed_size;
}
else
{
compressed_buffer.resize(compressed_reserve_size);
2022-05-23 10:09:44 +00:00
UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data());
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);
}
2018-12-20 17:37:02 +00:00
}
2016-10-25 06:49:24 +00:00
2021-11-10 22:58:56 +00:00
CompressedWriteBuffer::~CompressedWriteBuffer()
{
2021-11-11 17:27:23 +00:00
finalize();
}
2022-05-16 21:21:20 +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
}
}