2019-02-13 22:42:38 +00:00
|
|
|
#include <Common/config.h>
|
|
|
|
#if USE_BROTLI
|
|
|
|
|
2019-02-13 20:31:54 +00:00
|
|
|
#include <IO/BrotliWriteBuffer.h>
|
2019-02-13 22:42:38 +00:00
|
|
|
#include <brotli/encode.h>
|
2019-02-13 20:31:54 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-02-13 22:42:38 +00:00
|
|
|
class BrotliWriteBuffer::BrotliStateWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BrotliStateWrapper()
|
|
|
|
: state(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BrotliStateWrapper()
|
|
|
|
{
|
|
|
|
BrotliEncoderDestroyInstance(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
BrotliEncoderState * state;
|
|
|
|
};
|
|
|
|
|
|
|
|
BrotliWriteBuffer::BrotliWriteBuffer(WriteBuffer & out_, int compression_level, size_t buf_size, char * existing_memory, size_t alignment)
|
2019-02-13 20:31:54 +00:00
|
|
|
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment)
|
2019-02-13 22:42:38 +00:00
|
|
|
, brotli(new BrotliStateWrapper())
|
|
|
|
, in_available(0)
|
|
|
|
, in_data(nullptr)
|
|
|
|
, out_capacity(0)
|
|
|
|
, out_data(nullptr)
|
2019-02-13 20:31:54 +00:00
|
|
|
, out(out_)
|
|
|
|
{
|
2019-02-13 22:42:38 +00:00
|
|
|
BrotliEncoderSetParameter(brotli->state, BROTLI_PARAM_QUALITY, static_cast<uint32_t>(compression_level));
|
|
|
|
BrotliEncoderSetParameter(brotli->state, BROTLI_PARAM_LGWIN, 24);
|
2019-02-13 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BrotliWriteBuffer::~BrotliWriteBuffer()
|
|
|
|
{
|
2019-03-19 20:18:41 +00:00
|
|
|
finish();
|
|
|
|
}
|
2019-02-13 22:42:38 +00:00
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
void BrotliWriteBuffer::nextImpl()
|
|
|
|
{
|
|
|
|
if (!offset())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-02-13 22:42:38 +00:00
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
in_data = reinterpret_cast<unsigned char *>(working_buffer.begin());
|
|
|
|
in_available = offset();
|
|
|
|
|
|
|
|
do
|
2019-02-13 22:42:38 +00:00
|
|
|
{
|
|
|
|
out.nextIfAtEnd();
|
|
|
|
out_data = reinterpret_cast<unsigned char *>(out.position());
|
|
|
|
out_capacity = out.buffer().end() - out.position();
|
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
int result = BrotliEncoderCompressStream(
|
2019-02-13 22:42:38 +00:00
|
|
|
brotli->state,
|
2019-03-19 20:18:41 +00:00
|
|
|
in_available ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH,
|
2019-02-13 22:42:38 +00:00
|
|
|
&in_available,
|
|
|
|
&in_data,
|
|
|
|
&out_capacity,
|
|
|
|
&out_data,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
out.position() = out.buffer().end() - out_capacity;
|
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
throw Exception(std::string("brotli compress failed: "), ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
|
|
|
|
}
|
2019-02-13 22:42:38 +00:00
|
|
|
}
|
2019-03-19 20:18:41 +00:00
|
|
|
while (in_available > 0 || out_capacity == 0);
|
2019-02-13 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
void BrotliWriteBuffer::finish()
|
2019-02-13 20:31:54 +00:00
|
|
|
{
|
2019-03-19 20:18:41 +00:00
|
|
|
if (finished)
|
2019-02-13 22:42:38 +00:00
|
|
|
return;
|
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
next();
|
2019-02-13 22:42:38 +00:00
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
while (true)
|
2019-02-13 22:42:38 +00:00
|
|
|
{
|
|
|
|
out.nextIfAtEnd();
|
|
|
|
out_data = reinterpret_cast<unsigned char *>(out.position());
|
|
|
|
out_capacity = out.buffer().end() - out.position();
|
|
|
|
|
|
|
|
int result = BrotliEncoderCompressStream(
|
|
|
|
brotli->state,
|
2019-03-19 20:18:41 +00:00
|
|
|
BROTLI_OPERATION_FINISH,
|
2019-02-13 22:42:38 +00:00
|
|
|
&in_available,
|
|
|
|
&in_data,
|
|
|
|
&out_capacity,
|
|
|
|
&out_data,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
out.position() = out.buffer().end() - out_capacity;
|
|
|
|
|
2019-03-19 20:18:41 +00:00
|
|
|
if (BrotliEncoderIsFinished(brotli->state))
|
|
|
|
{
|
|
|
|
finished = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:42:38 +00:00
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
throw Exception(std::string("brotli compress failed: "), ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 06:05:12 +00:00
|
|
|
#endif
|