2011-11-18 20:17:27 +00:00
|
|
|
|
#pragma once
|
2010-06-04 18:25:25 +00:00
|
|
|
|
|
2014-01-15 14:53:20 +00:00
|
|
|
|
#include <DB/IO/CompressedReadBufferBase.h>
|
2010-06-04 18:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-15 14:53:20 +00:00
|
|
|
|
class CompressedReadBuffer : public CompressedReadBufferBase, public BufferWithOwnMemory<ReadBuffer>
|
2010-06-04 18:25:25 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
2014-07-22 10:35:24 +00:00
|
|
|
|
size_t size_compressed = 0;
|
|
|
|
|
|
2013-09-08 00:00:25 +00:00
|
|
|
|
bool nextImpl()
|
|
|
|
|
{
|
2014-01-15 14:53:20 +00:00
|
|
|
|
size_t size_decompressed;
|
2014-07-22 10:35:24 +00:00
|
|
|
|
size_compressed = readCompressedData(size_decompressed);
|
|
|
|
|
if (!size_compressed)
|
2013-09-08 00:00:25 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
2014-01-15 14:53:20 +00:00
|
|
|
|
memory.resize(size_decompressed);
|
|
|
|
|
working_buffer = Buffer(&memory[0], &memory[size_decompressed]);
|
2013-09-08 00:00:25 +00:00
|
|
|
|
|
|
|
|
|
decompress(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_)
|
2014-01-15 14:53:20 +00:00
|
|
|
|
: CompressedReadBufferBase(&in_), BufferWithOwnMemory<ReadBuffer>(0)
|
2012-02-23 23:34:05 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2013-09-08 00:00:25 +00:00
|
|
|
|
|
|
|
|
|
size_t readBig(char * to, size_t n)
|
|
|
|
|
{
|
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
|
|
|
|
|
|
/// Если в буфере есть непрочитанные байты, то скопируем сколько надо в to.
|
|
|
|
|
if (pos < working_buffer.end())
|
|
|
|
|
bytes_read += read(to, std::min(static_cast<size_t>(working_buffer.end() - pos), n));
|
|
|
|
|
|
|
|
|
|
/// Если надо ещё прочитать - будем, по возможности, разжимать сразу в to.
|
|
|
|
|
while (bytes_read < n)
|
|
|
|
|
{
|
2014-01-15 14:53:20 +00:00
|
|
|
|
size_t size_decompressed;
|
2013-09-08 00:00:25 +00:00
|
|
|
|
|
|
|
|
|
if (!readCompressedData(size_decompressed))
|
|
|
|
|
return bytes_read;
|
|
|
|
|
|
|
|
|
|
/// Если разжатый блок помещается целиком туда, куда его надо скопировать.
|
|
|
|
|
if (size_decompressed <= n - bytes_read)
|
|
|
|
|
{
|
|
|
|
|
decompress(to + bytes_read, size_decompressed);
|
|
|
|
|
bytes_read += size_decompressed;
|
|
|
|
|
bytes += size_decompressed;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-15 14:53:20 +00:00
|
|
|
|
bytes += offset();
|
|
|
|
|
memory.resize(size_decompressed);
|
|
|
|
|
working_buffer = Buffer(&memory[0], &memory[size_decompressed]);
|
2013-09-08 00:00:25 +00:00
|
|
|
|
pos = working_buffer.begin();
|
|
|
|
|
|
|
|
|
|
decompress(working_buffer.begin(), size_decompressed);
|
|
|
|
|
|
|
|
|
|
bytes_read += read(to + bytes_read, n - bytes_read);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
|
}
|
2014-07-22 10:35:24 +00:00
|
|
|
|
|
|
|
|
|
/// Сжатый размер текущего блока.
|
|
|
|
|
size_t getSizeCompressed() const
|
|
|
|
|
{
|
|
|
|
|
return size_compressed;
|
|
|
|
|
}
|
2010-06-04 18:25:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|