aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/ConcatSeekableReadBuffer.h
blob: c8c16c5d8879af0e40d7377d379ce6dd2829604f (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
#pragma once

#include <IO/SeekableReadBuffer.h>
#include <vector>


namespace DB
{

/// Reads from the concatenation of multiple SeekableReadBuffer's
class ConcatSeekableReadBuffer : public SeekableReadBuffer, public WithFileSize
{
public:
    ConcatSeekableReadBuffer() : SeekableReadBuffer(nullptr, 0) { }
    ConcatSeekableReadBuffer(std::unique_ptr<SeekableReadBuffer> buf1, size_t size1, std::unique_ptr<SeekableReadBuffer> buf2, size_t size2);
    ConcatSeekableReadBuffer(SeekableReadBuffer & buf1, size_t size1, SeekableReadBuffer & buf2, size_t size2);

    void appendBuffer(std::unique_ptr<SeekableReadBuffer> buffer, size_t size);
    void appendBuffer(SeekableReadBuffer & buffer, size_t size);

    off_t seek(off_t off, int whence) override;
    off_t getPosition() override;

    size_t getFileSize() override { return total_size; }

private:
    bool nextImpl() override;
    void appendBuffer(SeekableReadBuffer * buffer, bool own, size_t size);

    struct BufferInfo
    {
        BufferInfo() = default;
        BufferInfo(BufferInfo && src) noexcept;
        ~BufferInfo();
        SeekableReadBuffer * in = nullptr;
        bool own_in = false;
        size_t size = 0;
    };

    std::vector<BufferInfo> buffers;
    size_t total_size = 0;
    size_t current = 0;
    size_t current_start_pos = 0; /// Position of the current buffer's begin.
};

}