| 1 | #pragma once |
|---|---|
| 2 | #include <forward_list> |
| 3 | |
| 4 | #include <IO/WriteBuffer.h> |
| 5 | #include <IO/IReadableWriteBuffer.h> |
| 6 | #include <Common/Allocator.h> |
| 7 | #include <Core/Defines.h> |
| 8 | #include <boost/noncopyable.hpp> |
| 9 | |
| 10 | |
| 11 | namespace DB |
| 12 | { |
| 13 | |
| 14 | /// Stores data in memory chunks, size of chunks are exponentially increasing during write |
| 15 | /// Written data could be reread after write |
| 16 | class MemoryWriteBuffer : public WriteBuffer, public IReadableWriteBuffer, boost::noncopyable, private Allocator<false> |
| 17 | { |
| 18 | public: |
| 19 | |
| 20 | /// Use max_total_size_ = 0 for unlimited storage |
| 21 | MemoryWriteBuffer( |
| 22 | size_t max_total_size_ = 0, |
| 23 | size_t initial_chunk_size_ = DBMS_DEFAULT_BUFFER_SIZE, |
| 24 | double growth_rate_ = 2.0, |
| 25 | size_t max_chunk_size_ = 128 * DBMS_DEFAULT_BUFFER_SIZE); |
| 26 | |
| 27 | void nextImpl() override; |
| 28 | |
| 29 | ~MemoryWriteBuffer() override; |
| 30 | |
| 31 | protected: |
| 32 | |
| 33 | std::shared_ptr<ReadBuffer> getReadBufferImpl() override; |
| 34 | |
| 35 | const size_t max_total_size; |
| 36 | const size_t initial_chunk_size; |
| 37 | const size_t max_chunk_size; |
| 38 | const double growth_rate; |
| 39 | |
| 40 | using Container = std::forward_list<BufferBase::Buffer>; |
| 41 | |
| 42 | Container chunk_list; |
| 43 | Container::iterator chunk_tail; |
| 44 | size_t total_chunks_size = 0; |
| 45 | |
| 46 | void addChunk(); |
| 47 | |
| 48 | friend class ReadBufferFromMemoryWriteBuffer; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | } |
| 53 |