ClickHouse/dbms/src/IO/ConcatReadBuffer.h

65 lines
1.5 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
{
2017-05-28 14:29:40 +00:00
/** Reads from the concatenation of multiple ReadBuffers
2011-10-30 05:19:41 +00:00
*/
class ConcatReadBuffer : public ReadBuffer
{
2011-10-31 06:37:12 +00:00
public:
using ReadBuffers = std::vector<ReadBuffer *>;
2011-10-31 06:37:12 +00:00
protected:
ReadBuffers buffers;
ReadBuffers::iterator current;
bool nextImpl() override
{
if (buffers.end() == current)
return false;
2017-05-28 14:29:40 +00:00
/// First reading
if (working_buffer.size() == 0 && (*current)->hasPendingData())
{
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
return true;
}
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.
while ((*current)->eof())
{
++current;
if (buffers.end() == current)
return false;
}
}
working_buffer = Buffer((*current)->position(), (*current)->buffer().end());
return true;
}
2011-10-30 05:19:41 +00:00
public:
ConcatReadBuffer(const ReadBuffers & buffers_) : ReadBuffer(nullptr, 0), buffers(buffers_), current(buffers.begin()) {}
2011-10-30 05:19:41 +00:00
ConcatReadBuffer(ReadBuffer & buf1, ReadBuffer & buf2) : ReadBuffer(nullptr, 0)
{
buffers.push_back(&buf1);
buffers.push_back(&buf2);
current = buffers.begin();
}
2011-10-30 05:19:41 +00:00
};
}