ClickHouse/dbms/include/DB/IO/ConcatReadBuffer.h

65 lines
1.4 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>
2011-10-30 05:19:41 +00:00
#include <DB/IO/ReadBuffer.h>
namespace DB
{
/** Читает из конкатенации нескольких ReadBuffer-ов
*/
class ConcatReadBuffer : public ReadBuffer
{
2011-10-31 06:37:12 +00:00
public:
2011-10-30 05:19:41 +00:00
typedef std::vector<ReadBuffer *> ReadBuffers;
2011-10-31 06:37:12 +00:00
protected:
2011-10-30 05:19:41 +00:00
ReadBuffers buffers;
ReadBuffers::iterator current;
bool nextImpl()
{
2011-10-31 06:37:12 +00:00
if (buffers.end() == current)
return false;
/// Первое чтение
if (working_buffer.size() == 0 && (*current)->position() != (*current)->buffer().end())
{
working_buffer = (*current)->buffer();
return true;
}
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
/// Пропускаем закончившиеся буферы; если буфер не закончился, но курсор на конце, то прочитаем следующую порцию данных.
while ((*current)->eof())
{
++current;
if (buffers.end() == current)
return false;
}
2011-10-30 05:19:41 +00:00
}
2011-10-31 06:37:12 +00:00
working_buffer = (*current)->buffer();
2011-10-30 05:19:41 +00:00
return true;
}
public:
2011-10-31 06:37:12 +00:00
ConcatReadBuffer(const ReadBuffers & buffers_) : ReadBuffer(NULL, 0), buffers(buffers_), current(buffers.begin()) {}
2011-10-30 05:19:41 +00:00
2011-10-31 06:37:12 +00:00
ConcatReadBuffer(ReadBuffer & buf1, ReadBuffer & buf2) : ReadBuffer(NULL, 0)
2011-10-30 05:19:41 +00:00
{
2011-10-31 06:37:12 +00:00
buffers.push_back(&buf1);
buffers.push_back(&buf2);
2011-10-30 05:19:41 +00:00
current = buffers.begin();
}
};
}