aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/MemoryReadWriteBuffer.h
blob: d6bf231c22b0c5372522f6291ef1d57771df4654 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include <forward_list>

#include <IO/WriteBuffer.h>
#include <IO/IReadableWriteBuffer.h>
#include <Common/Allocator.h>
#include <Core/Defines.h>
#include <boost/noncopyable.hpp>


namespace DB
{

/// Stores data in memory chunks, size of chunks are exponentially increasing during write
/// Written data could be reread after write
class MemoryWriteBuffer : public WriteBuffer, public IReadableWriteBuffer, boost::noncopyable, private Allocator<false>
{
public:
    /// 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"; }
    };

    /// Use max_total_size_ = 0 for unlimited storage
    explicit MemoryWriteBuffer(
        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);

    ~MemoryWriteBuffer() override;

protected:

    void nextImpl() override;

    void finalizeImpl() override { /* no op */ }

    std::shared_ptr<ReadBuffer> getReadBufferImpl() override;

    const size_t max_total_size;
    const size_t initial_chunk_size;
    const size_t max_chunk_size;
    const double growth_rate;

    using Container = std::forward_list<BufferBase::Buffer>;

    Container chunk_list;
    Container::iterator chunk_tail;
    size_t total_chunks_size = 0;

    void addChunk();

    friend class ReadBufferFromMemoryWriteBuffer;
};


}