ClickHouse/src/IO/WriteBufferDecorator.h

56 lines
1.3 KiB
C++
Raw Normal View History

2021-11-10 22:58:56 +00:00
#pragma once
#include <IO/WriteBuffer.h>
2021-11-11 17:27:23 +00:00
#include <IO/BufferWithOwnMemory.h>
2021-11-10 22:58:56 +00:00
#include <utility>
#include <memory>
namespace DB
{
class WriteBuffer;
2021-11-22 11:19:26 +00:00
/// WriteBuffer that decorates data and delegates it to underlying buffer.
/// It's used for writing compressed and encrypted data
2021-11-10 22:58:56 +00:00
template <class Base>
class WriteBufferDecorator : public Base
{
public:
template <class ... BaseArgs>
explicit WriteBufferDecorator(std::unique_ptr<WriteBuffer> out_, BaseArgs && ... args)
: Base(std::forward<BaseArgs>(args)...), out(std::move(out_))
{
}
void finalizeImpl() override
{
try
{
2021-11-22 11:19:26 +00:00
finalizeBefore();
2021-11-10 22:58:56 +00:00
out->finalize();
2021-11-22 11:19:26 +00:00
finalizeAfter();
2021-11-10 22:58:56 +00:00
}
catch (...)
{
/// Do not try to flush next time after exception.
out->position() = out->buffer().begin();
throw;
}
}
WriteBuffer * getNestedBuffer() { return out.get(); }
protected:
2021-11-22 11:19:26 +00:00
/// Do some finalization before finalization of underlying buffer.
virtual void finalizeBefore() {}
/// Do some finalization after finalization of underlying buffer.
virtual void finalizeAfter() {}
2021-11-10 22:58:56 +00:00
std::unique_ptr<WriteBuffer> out;
};
using WriteBufferWithOwnMemoryDecorator = WriteBufferDecorator<BufferWithOwnMemory<WriteBuffer>>;
}