ClickHouse/src/IO/ConcatReadBuffer.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.1 KiB
C++
Raw Normal View History

2011-10-30 05:19:41 +00:00
#pragma once
2011-10-31 06:37:12 +00:00
#include <vector>
#include <IO/ReadBuffer.h>
2011-10-30 05:19:41 +00:00
namespace DB
{
2021-03-04 11:10:21 +00:00
/// Reads from the concatenation of multiple ReadBuffer's
2011-10-30 05:19:41 +00:00
class ConcatReadBuffer : public ReadBuffer
{
2011-10-31 06:37:12 +00:00
public:
2021-03-04 11:10:21 +00:00
using Buffers = std::vector<std::unique_ptr<ReadBuffer>>;
2021-03-17 14:11:47 +00:00
ConcatReadBuffer() : ReadBuffer(nullptr, 0), current(buffers.end())
2021-03-04 11:10:21 +00:00
{
}
explicit ConcatReadBuffer(Buffers && buffers_) : ReadBuffer(nullptr, 0), buffers(std::move(buffers_)), current(buffers.begin())
{
assert(!buffers.empty());
}
ConcatReadBuffer(std::unique_ptr<ReadBuffer> buf1, std::unique_ptr<ReadBuffer> buf2) : ConcatReadBuffer()
{
appendBuffer(std::move(buf1));
appendBuffer(std::move(buf2));
}
2021-03-04 11:10:21 +00:00
ConcatReadBuffer(ReadBuffer & buf1, ReadBuffer & buf2) : ConcatReadBuffer()
{
appendBuffer(wrapReadBufferReference(buf1));
appendBuffer(wrapReadBufferReference(buf2));
}
void appendBuffer(std::unique_ptr<ReadBuffer> buffer)
{
2021-03-17 14:11:47 +00:00
assert(!count());
2021-03-04 11:10:21 +00:00
buffers.push_back(std::move(buffer));
2021-03-17 14:11:47 +00:00
current = buffers.begin();
2021-03-04 11:10:21 +00:00
}
2011-10-31 06:37:12 +00:00
protected:
2021-03-04 11:10:21 +00:00
Buffers buffers;
Buffers::iterator current;
bool nextImpl() override
2011-10-30 05:19:41 +00:00
{
2011-10-31 06:37:12 +00:00
if (buffers.end() == current)
return false;
2017-05-28 14:29:40 +00:00
/// First reading
if (working_buffer.empty())
2011-10-31 06:37:12 +00:00
{
if ((*current)->hasPendingData())
{
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
return true;
}
2011-10-31 06:37:12 +00:00
}
else
(*current)->position() = position();
2011-10-30 05:19:41 +00:00
if (!(*current)->next())
{
++current;
if (buffers.end() == current)
return false;
2012-03-05 04:29:16 +00:00
2017-05-28 14:29:40 +00:00
/// We skip the filled up buffers; if the buffer is not filled in, but the cursor is at the end, then read the next piece of data.
2012-03-05 04:29:16 +00:00
while ((*current)->eof())
{
++current;
if (buffers.end() == current)
return false;
}
2011-10-30 05:19:41 +00:00
}
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
2011-10-30 05:19:41 +00:00
return true;
}
};
}