aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/ForkWriteBuffer.cpp
blob: 8e11b9ff5903b8d4024267806fd494f74b20c1d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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())
    {
        throw Exception(ErrorCodes::CANNOT_CREATE_IO_BUFFER, "Expected non-zero number of buffers for `ForkWriteBuffer`");
    }
    set(sources.front()->buffer().begin(), sources.front()->buffer().size());
}


void ForkWriteBuffer::nextImpl()
{
    sources.front()->position() = position();

    try
    {
        auto & source_buffer = sources.front();
        for (auto it = sources.begin() + 1; it != sources.end(); ++it)
        {
            auto & buffer = *it;
            buffer->write(source_buffer->buffer().begin(), source_buffer->offset());
            buffer->next();
        }
        source_buffer->next();
    }
    catch (Exception & exception)
    {
        exception.addMessage("While writing to ForkWriteBuffer");
        throw;
    }

}

void ForkWriteBuffer::finalizeImpl()
{
    for (const WriteBufferPtr & buffer : sources)
    {
        buffer->finalize();
    }
}

ForkWriteBuffer::~ForkWriteBuffer()
{
    finalize();
}


}