2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/MemoryReadWriteBuffer.h>
|
2017-02-09 10:10:13 +00:00
|
|
|
#include <common/likely.h>
|
2017-04-08 01:32:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CURRENT_WRITE_BUFFER_IS_EXHAUSTED;
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-27 12:40:08 +00:00
|
|
|
class ReadBufferFromMemoryWriteBuffer : public ReadBuffer, boost::noncopyable, private Allocator<false>
|
2017-02-09 10:10:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-09-07 21:04:48 +00:00
|
|
|
explicit ReadBufferFromMemoryWriteBuffer(MemoryWriteBuffer && origin)
|
|
|
|
: ReadBuffer(nullptr, 0),
|
|
|
|
chunk_list(std::move(origin.chunk_list)),
|
|
|
|
end_pos(origin.position())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
chunk_head = chunk_list.begin();
|
|
|
|
setChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nextImpl() override
|
|
|
|
{
|
|
|
|
if (chunk_head == chunk_list.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++chunk_head;
|
|
|
|
return setChunk();
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:02:56 +00:00
|
|
|
~ReadBufferFromMemoryWriteBuffer() override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (const auto & range : chunk_list)
|
|
|
|
free(range.begin(), range.size());
|
|
|
|
}
|
2017-02-09 10:10:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// update buffers and position according to chunk_head pointer
|
|
|
|
bool setChunk()
|
|
|
|
{
|
|
|
|
if (chunk_head != chunk_list.end())
|
|
|
|
{
|
|
|
|
internalBuffer() = *chunk_head;
|
|
|
|
|
|
|
|
/// It is last chunk, it should be truncated
|
|
|
|
if (std::next(chunk_head) != chunk_list.end())
|
|
|
|
buffer() = internalBuffer();
|
|
|
|
else
|
|
|
|
buffer() = Buffer(internalBuffer().begin(), end_pos);
|
|
|
|
|
|
|
|
position() = buffer().begin();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer() = internalBuffer() = Buffer(nullptr, nullptr);
|
|
|
|
position() = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer().size() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
using Container = std::forward_list<BufferBase::Buffer>;
|
|
|
|
|
|
|
|
Container chunk_list;
|
|
|
|
Container::iterator chunk_head;
|
|
|
|
Position end_pos;
|
2017-02-09 10:10:13 +00:00
|
|
|
};
|
|
|
|
|
2017-03-02 09:35:17 +00:00
|
|
|
|
2017-03-27 12:40:08 +00:00
|
|
|
MemoryWriteBuffer::MemoryWriteBuffer(size_t max_total_size_, size_t initial_chunk_size_, double growth_rate_, size_t max_chunk_size_)
|
2017-09-07 21:04:48 +00:00
|
|
|
: WriteBuffer(nullptr, 0),
|
|
|
|
max_total_size(max_total_size_),
|
|
|
|
initial_chunk_size(initial_chunk_size_),
|
|
|
|
max_chunk_size(max_chunk_size_),
|
|
|
|
growth_rate(growth_rate_)
|
2017-02-09 10:10:13 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
addChunk();
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
void MemoryWriteBuffer::nextImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely(hasPendingData()))
|
|
|
|
{
|
|
|
|
/// ignore flush
|
|
|
|
buffer() = Buffer(pos, buffer().end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addChunk();
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
void MemoryWriteBuffer::addChunk()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t next_chunk_size;
|
|
|
|
if (chunk_list.empty())
|
|
|
|
{
|
|
|
|
chunk_tail = chunk_list.before_begin();
|
|
|
|
next_chunk_size = initial_chunk_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-01 17:21:03 +00:00
|
|
|
next_chunk_size = std::max(static_cast<size_t>(1), static_cast<size_t>(chunk_tail->size() * growth_rate));
|
2017-04-01 07:20:54 +00:00
|
|
|
next_chunk_size = std::min(next_chunk_size, max_chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_total_size)
|
|
|
|
{
|
|
|
|
if (total_chunks_size + next_chunk_size > max_total_size)
|
|
|
|
next_chunk_size = max_total_size - total_chunks_size;
|
|
|
|
|
|
|
|
if (0 == next_chunk_size)
|
|
|
|
{
|
|
|
|
set(position(), 0);
|
|
|
|
throw Exception("MemoryWriteBuffer limit is exhausted", ErrorCodes::CURRENT_WRITE_BUFFER_IS_EXHAUSTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Position begin = reinterpret_cast<Position>(alloc(next_chunk_size));
|
|
|
|
chunk_tail = chunk_list.emplace_after(chunk_tail, begin, begin + next_chunk_size);
|
|
|
|
total_chunks_size += next_chunk_size;
|
|
|
|
|
|
|
|
set(chunk_tail->begin(), chunk_tail->size());
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2017-02-28 14:15:13 +00:00
|
|
|
std::shared_ptr<ReadBuffer> MemoryWriteBuffer::getReadBufferImpl()
|
2017-02-09 10:10:13 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = std::make_shared<ReadBufferFromMemoryWriteBuffer>(std::move(*this));
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// invalidate members
|
|
|
|
chunk_list.clear();
|
|
|
|
chunk_tail = chunk_list.begin();
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
MemoryWriteBuffer::~MemoryWriteBuffer()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & range : chunk_list)
|
|
|
|
free(range.begin(), range.size());
|
2017-02-09 10:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|