ClickHouse/dbms/include/DB/IO/CompressedReadBuffer.h

71 lines
1.9 KiB
C
Raw Normal View History

2011-11-18 20:17:27 +00:00
#pragma once
2010-06-04 18:25:25 +00:00
2010-06-07 12:48:32 +00:00
#include <vector>
2010-06-04 18:25:25 +00:00
#include <city.h>
2011-06-17 21:19:39 +00:00
#include <quicklz/quicklz_level1.h>
2012-01-05 18:35:22 +00:00
#include <lz4/lz4.h>
2010-06-04 18:25:25 +00:00
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
#include <DB/IO/ReadBuffer.h>
#include <DB/IO/BufferWithOwnMemory.h>
2011-06-17 21:19:39 +00:00
#include <DB/IO/CompressedStream.h>
2010-06-04 18:25:25 +00:00
namespace DB
{
class CompressedReadBuffer : public BufferWithOwnMemory<ReadBuffer>
2010-06-04 18:25:25 +00:00
{
private:
ReadBuffer & in;
std::vector<char> compressed_buffer;
2011-06-17 21:19:39 +00:00
char scratch[QLZ_SCRATCH_DECOMPRESS];
bool nextImpl()
2010-06-04 18:25:25 +00:00
{
if (in.eof())
return false;
uint128 checksum;
in.readStrict(reinterpret_cast<char *>(&checksum), sizeof(checksum));
2011-06-26 21:30:59 +00:00
2011-06-17 21:19:39 +00:00
in.readStrict(&compressed_buffer[0], QUICKLZ_HEADER_SIZE);
2010-06-04 18:25:25 +00:00
2011-06-17 21:19:39 +00:00
size_t size_compressed = qlz_size_compressed(&compressed_buffer[0]);
if (size_compressed > DBMS_MAX_COMPRESSED_SIZE)
throw Exception("Too large size_compressed. Most likely corrupted data.", ErrorCodes::TOO_LARGE_SIZE_COMPRESSED);
2011-06-26 21:30:59 +00:00
2011-06-17 21:19:39 +00:00
size_t size_decompressed = qlz_size_decompressed(&compressed_buffer[0]);
2010-06-04 18:25:25 +00:00
compressed_buffer.resize(size_compressed);
memory.resize(size_decompressed);
internal_buffer.resize(size_decompressed);
working_buffer.resize(size_decompressed);
2010-06-07 12:48:32 +00:00
2011-06-17 21:19:39 +00:00
in.readStrict(&compressed_buffer[QUICKLZ_HEADER_SIZE], size_compressed - QUICKLZ_HEADER_SIZE);
2010-06-07 12:48:32 +00:00
if (checksum != CityHash128(&compressed_buffer[0], size_compressed))
throw Exception("Checksum doesnt match: corrupted data.", ErrorCodes::CHECKSUM_DOESNT_MATCH);
2011-06-26 21:30:59 +00:00
2012-01-05 18:35:22 +00:00
/// Старший бит первого байта определяет использованный метод сжатия.
if ((compressed_buffer[0] & 0x80) == 0)
qlz_decompress(&compressed_buffer[0], working_buffer.begin(), scratch);
else
LZ4_uncompress(&compressed_buffer[QUICKLZ_HEADER_SIZE], working_buffer.begin(), size_decompressed);
2010-06-04 18:25:25 +00:00
return true;
}
2011-06-26 21:30:59 +00:00
public:
CompressedReadBuffer(ReadBuffer & in_)
: in(in_),
compressed_buffer(QUICKLZ_HEADER_SIZE)
{
}
2010-06-04 18:25:25 +00:00
};
}