2018-12-28 17:26:10 +00:00
|
|
|
#include "CompressedReadBuffer.h"
|
2018-12-21 13:25:39 +00:00
|
|
|
#include <Compression/CompressionInfo.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/LZ4_decompress_faster.h>
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
bool CompressedReadBuffer::nextImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size_decompressed;
|
|
|
|
size_t size_compressed_without_checksum;
|
|
|
|
size_compressed = readCompressedData(size_decompressed, size_compressed_without_checksum);
|
|
|
|
if (!size_compressed)
|
|
|
|
return false;
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2020-03-19 23:48:53 +00:00
|
|
|
auto additional_size_at_the_end_of_buffer = codec->getAdditionalSizeAtTheEndOfBuffer();
|
|
|
|
|
|
|
|
/// This is for clang static analyzer.
|
|
|
|
assert(size_decompressed + additional_size_at_the_end_of_buffer > 0);
|
|
|
|
|
|
|
|
memory.resize(size_decompressed + additional_size_at_the_end_of_buffer);
|
2018-09-02 03:00:04 +00:00
|
|
|
working_buffer = Buffer(memory.data(), &memory[size_decompressed]);
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
decompress(working_buffer.begin(), size_decompressed, size_compressed_without_checksum);
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CompressedReadBuffer::readBig(char * to, size_t n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t bytes_read = 0;
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If there are unread bytes in the buffer, then we copy necessary to `to`.
|
|
|
|
if (pos < working_buffer.end())
|
|
|
|
bytes_read += read(to, std::min(static_cast<size_t>(working_buffer.end() - pos), n));
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If you need to read more - we will, if possible, uncompress at once to `to`.
|
|
|
|
while (bytes_read < n)
|
|
|
|
{
|
|
|
|
size_t size_decompressed;
|
|
|
|
size_t size_compressed_without_checksum;
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!readCompressedData(size_decompressed, size_compressed_without_checksum))
|
|
|
|
return bytes_read;
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2020-03-19 23:48:53 +00:00
|
|
|
auto additional_size_at_the_end_of_buffer = codec->getAdditionalSizeAtTheEndOfBuffer();
|
|
|
|
|
|
|
|
/// If the decompressed block fits entirely where it needs to be copied.
|
|
|
|
if (size_decompressed + additional_size_at_the_end_of_buffer <= n - bytes_read)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
decompress(to + bytes_read, size_decompressed, size_compressed_without_checksum);
|
|
|
|
bytes_read += size_decompressed;
|
|
|
|
bytes += size_decompressed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes += offset();
|
2020-03-19 23:48:53 +00:00
|
|
|
|
|
|
|
/// This is for clang static analyzer.
|
|
|
|
assert(size_decompressed + additional_size_at_the_end_of_buffer > 0);
|
|
|
|
|
|
|
|
memory.resize(size_decompressed + additional_size_at_the_end_of_buffer);
|
2018-09-02 03:00:04 +00:00
|
|
|
working_buffer = Buffer(memory.data(), &memory[size_decompressed]);
|
2017-04-01 07:20:54 +00:00
|
|
|
pos = working_buffer.begin();
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
decompress(working_buffer.begin(), size_decompressed, size_compressed_without_checksum);
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bytes_read += read(to + bytes_read, n - bytes_read);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return bytes_read;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|