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

68 lines
1.3 KiB
C
Raw Normal View History

2010-06-04 18:25:25 +00:00
#ifndef DBMS_COMMON_COMPRESSED_WRITEBUFFER_H
#define DBMS_COMMON_COMPRESSED_WRITEBUFFER_H
#include <quicklz/quicklz_level1.h>
#include <DB/IO/WriteBuffer.h>
#include <DB/IO/CompressedStream.h>
namespace DB
{
class CompressedWriteBuffer : public WriteBuffer
{
private:
WriteBuffer & out;
char compressed_buffer[DBMS_COMPRESSING_STREAM_BUFFER_SIZE + QUICKLZ_ADDITIONAL_SPACE];
char scratch[QLZ_SCRATCH_COMPRESS];
2011-05-05 19:10:17 +00:00
size_t compressed_bytes;
2010-06-04 18:25:25 +00:00
public:
2011-05-05 19:10:17 +00:00
CompressedWriteBuffer(WriteBuffer & out_) : out(out_), compressed_bytes(0) {}
2010-06-04 18:25:25 +00:00
void next()
{
size_t compressed_size = qlz_compress(
2011-05-13 19:40:56 +00:00
working_buffer.begin(),
2010-06-04 18:25:25 +00:00
compressed_buffer,
2011-05-13 19:40:56 +00:00
pos - working_buffer.begin(),
2010-06-04 18:25:25 +00:00
scratch);
out.write(compressed_buffer, compressed_size);
2011-05-13 19:40:56 +00:00
pos = working_buffer.begin();
2011-05-05 19:10:17 +00:00
compressed_bytes += compressed_size;
}
/// Объём данных, которые были сжаты
2011-05-05 19:10:17 +00:00
size_t getCompressedBytes()
{
nextIfAtEnd();
return compressed_bytes;
}
/// Сколько несжатых байт было записано в буфер
2011-05-05 19:10:17 +00:00
size_t getUncompressedBytes()
{
return count();
}
/// Сколько байт находится в буфере (ещё не сжато)
size_t getRemainingBytes()
2011-05-05 19:10:17 +00:00
{
nextIfAtEnd();
2011-05-13 19:40:56 +00:00
return pos - working_buffer.begin();
2010-06-04 18:25:25 +00:00
}
~CompressedWriteBuffer()
{
next();
}
};
}
#endif