2017-02-22 15:29:43 +00:00
|
|
|
#pragma once
|
2017-02-09 10:10:13 +00:00
|
|
|
#include <forward_list>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/IReadableWriteBuffer.h>
|
|
|
|
#include <Common/Allocator.h>
|
|
|
|
#include <Core/Defines.h>
|
2017-04-08 01:32:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-08-20 11:17:57 +00:00
|
|
|
/// Stores data in memory chunks, size of chunks are exponentially increasing during write
|
2017-03-02 09:35:17 +00:00
|
|
|
/// Written data could be reread after write
|
2017-03-27 12:40:08 +00:00
|
|
|
class MemoryWriteBuffer : public WriteBuffer, public IReadableWriteBuffer, boost::noncopyable, private Allocator<false>
|
2017-02-09 10:10:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-03-31 12:09:21 +00:00
|
|
|
/// Special exception to throw when the current WriteBuffer cannot receive data
|
|
|
|
class CurrentBufferExhausted : public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char * what() const noexcept override { return "MemoryWriteBuffer limit is exhausted"; }
|
|
|
|
};
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Use max_total_size_ = 0 for unlimited storage
|
2022-03-13 11:59:20 +00:00
|
|
|
explicit MemoryWriteBuffer(
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t max_total_size_ = 0,
|
|
|
|
size_t initial_chunk_size_ = DBMS_DEFAULT_BUFFER_SIZE,
|
|
|
|
double growth_rate_ = 2.0,
|
|
|
|
size_t max_chunk_size_ = 128 * DBMS_DEFAULT_BUFFER_SIZE);
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~MemoryWriteBuffer() override;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2023-06-18 11:56:10 +00:00
|
|
|
void nextImpl() override;
|
|
|
|
|
|
|
|
void finalizeImpl() override { /* no op */ }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::shared_ptr<ReadBuffer> getReadBufferImpl() override;
|
2017-02-28 14:15:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const size_t max_total_size;
|
|
|
|
const size_t initial_chunk_size;
|
|
|
|
const size_t max_chunk_size;
|
|
|
|
const double growth_rate;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Container = std::forward_list<BufferBase::Buffer>;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Container chunk_list;
|
|
|
|
Container::iterator chunk_tail;
|
|
|
|
size_t total_chunks_size = 0;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void addChunk();
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class ReadBufferFromMemoryWriteBuffer;
|
2017-02-09 10:10:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|