2017-02-22 15:29:43 +00:00
|
|
|
#pragma once
|
|
|
|
#include <functional>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
2017-02-22 15:29:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-20 16:39:28 +00:00
|
|
|
/* The buffer is similar to ConcatReadBuffer, but writes data
|
|
|
|
*
|
|
|
|
* It has WriteBuffers sequence [prepared_sources, lazy_sources]
|
|
|
|
* (lazy_sources contains not pointers themself, but their delayed constructors)
|
|
|
|
*
|
|
|
|
* Firtly, CascadeWriteBuffer redirects data to first buffer of the sequence
|
2018-09-28 14:53:20 +00:00
|
|
|
* If current WriteBuffer cannot receive data anymore, it throws special exception CURRENT_WRITE_BUFFER_IS_EXHAUSTED in nextImpl() body,
|
2017-03-20 16:39:28 +00:00
|
|
|
* CascadeWriteBuffer prepare next buffer and continuously redirects data to it.
|
|
|
|
* If there are no buffers anymore CascadeWriteBuffer throws an exception.
|
|
|
|
*
|
|
|
|
* NOTE: If you use one of underlying WriteBuffers buffers outside, you need sync its position() with CascadeWriteBuffer's position().
|
|
|
|
* The sync is performed into nextImpl(), getResultBuffers() and destructor.
|
|
|
|
*/
|
2017-02-22 15:29:43 +00:00
|
|
|
class CascadeWriteBuffer : public WriteBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using WriteBufferPtrs = std::vector<WriteBufferPtr>;
|
|
|
|
using WriteBufferConstructor = std::function<WriteBufferPtr (const WriteBufferPtr & prev_buf)>;
|
|
|
|
using WriteBufferConstructors = std::vector<WriteBufferConstructor>;
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CascadeWriteBuffer(WriteBufferPtrs && prepared_sources_, WriteBufferConstructors && lazy_sources_ = {});
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override;
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Should be called once
|
|
|
|
void getResultBuffers(WriteBufferPtrs & res);
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const WriteBuffer * getCurrentBuffer() const
|
|
|
|
{
|
|
|
|
return curr_buffer;
|
|
|
|
}
|
2017-02-28 14:15:13 +00:00
|
|
|
|
2018-08-10 04:02:56 +00:00
|
|
|
~CascadeWriteBuffer() override;
|
2017-03-02 09:35:17 +00:00
|
|
|
|
2017-02-22 15:29:43 +00:00
|
|
|
private:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBuffer * setNextBuffer();
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferPtrs prepared_sources;
|
|
|
|
WriteBufferConstructors lazy_sources;
|
|
|
|
size_t first_lazy_source_num;
|
|
|
|
size_t num_sources;
|
2017-02-22 15:29:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBuffer * curr_buffer;
|
|
|
|
size_t curr_buffer_num;
|
2017-02-22 15:29:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|