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