2022-07-14 20:14:46 +00:00
|
|
|
#include <IO/ForkWriteBuffer.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_CREATE_IO_BUFFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
ForkWriteBuffer::ForkWriteBuffer(WriteBufferPtrs && sources_)
|
|
|
|
: WriteBuffer(nullptr, 0), sources(std::move(sources_))
|
|
|
|
{
|
|
|
|
if (sources.empty())
|
|
|
|
{
|
2022-07-17 18:09:18 +00:00
|
|
|
throw Exception("Expected non-zero number of buffers for `ForkWriteBuffer`", ErrorCodes::CANNOT_CREATE_IO_BUFFER);
|
2022-07-14 20:14:46 +00:00
|
|
|
}
|
2022-07-15 14:08:53 +00:00
|
|
|
set(sources.front()->buffer().begin(), sources.front()->buffer().size());
|
2022-07-14 20:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ForkWriteBuffer::nextImpl()
|
|
|
|
{
|
2022-07-15 14:08:53 +00:00
|
|
|
sources.front()->position() = position();
|
2022-07-14 20:14:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-07-15 16:37:50 +00:00
|
|
|
auto & source_buffer = sources.front();
|
|
|
|
for (auto it = sources.begin() + 1; it != sources.end(); ++it)
|
2022-07-14 20:14:46 +00:00
|
|
|
{
|
2022-07-15 16:37:50 +00:00
|
|
|
auto & buffer = *it;
|
|
|
|
buffer->write(source_buffer->buffer().begin(), source_buffer->offset());
|
|
|
|
buffer->next();
|
2022-07-14 20:14:46 +00:00
|
|
|
}
|
2022-07-15 16:37:50 +00:00
|
|
|
source_buffer->next();
|
2022-07-14 20:14:46 +00:00
|
|
|
}
|
|
|
|
catch (Exception & exception)
|
|
|
|
{
|
|
|
|
exception.addMessage("While writing to ForkWriteBuffer");
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:02:54 +00:00
|
|
|
void ForkWriteBuffer::finalizeImpl()
|
|
|
|
{
|
|
|
|
for (const WriteBufferPtr & buffer : sources)
|
|
|
|
{
|
|
|
|
buffer->finalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:14:46 +00:00
|
|
|
ForkWriteBuffer::~ForkWriteBuffer()
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|