2011-10-30 05:19:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#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());
|
|
|
|
}
|
|
|
|
|
2022-01-31 06:35:07 +00:00
|
|
|
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
|
|
|
}
|
2015-02-07 23:13:04 +00:00
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
protected:
|
2021-03-04 11:10:21 +00:00
|
|
|
Buffers buffers;
|
|
|
|
Buffers::iterator current;
|
2015-02-07 23:13:04 +00:00
|
|
|
|
2016-08-19 01:54:23 +00:00
|
|
|
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;
|
2015-02-07 23:13:04 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// First reading
|
2021-02-03 12:31:45 +00:00
|
|
|
if (working_buffer.empty())
|
2011-10-31 06:37:12 +00:00
|
|
|
{
|
2021-02-03 12:31:45 +00:00
|
|
|
if ((*current)->hasPendingData())
|
|
|
|
{
|
|
|
|
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-31 06:37:12 +00:00
|
|
|
}
|
2021-02-03 12:31:45 +00:00
|
|
|
else
|
|
|
|
(*current)->position() = position();
|
2015-02-07 23:13:04 +00:00
|
|
|
|
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
|
|
|
}
|
2015-02-07 23:13:04 +00:00
|
|
|
|
2014-04-24 20:18:00 +00:00
|
|
|
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
|
2011-10-30 05:19:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|