aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/chunked_buffer.h
blob: 684e185fbee3423fa9a95655df97ecf811eff032 (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
61
62
63
64
65
#pragma once

#include <util/generic/strbuf.h>
#include <util/generic/string.h>
#include <util/stream/output.h>

#include <memory>
#include <deque>

namespace NYql {

class TChunkedBuffer {
public:
    TChunkedBuffer() = default;
    TChunkedBuffer(const TChunkedBuffer&) = default;
    TChunkedBuffer(TChunkedBuffer&& other);
    TChunkedBuffer& operator=(TChunkedBuffer&& other);
    explicit TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner);
    explicit TChunkedBuffer(TString&& str);

    inline size_t ContigousSize() const {
        return Items_.empty() ? 0 : Front().Buf.size();
    }

    inline size_t Size() const {
        return Size_;
    }

    inline bool Empty() const {
        return Size_ == 0;
    }

    struct TChunk {
        TStringBuf Buf;
        std::shared_ptr<const void> Owner;
    };

    const TChunk& Front() const;
    size_t CopyTo(IOutputStream& dst, size_t toCopy = std::numeric_limits<size_t>::max()) const;

    TChunkedBuffer& Append(TStringBuf buf, const std::shared_ptr<const void>& owner);
    TChunkedBuffer& Append(TString&& str);
    TChunkedBuffer& Append(TChunkedBuffer&& other);

    TChunkedBuffer& Clear();
    TChunkedBuffer& Erase(size_t size);

private:
    std::deque<TChunk> Items_;
    size_t Size_ = 0;
};

class TChunkedBufferOutput : public IOutputStream {
public:
    explicit TChunkedBufferOutput(TChunkedBuffer& dst);
private:
    virtual void DoWrite(const void *buf, size_t len) override;

    TChunkedBuffer& Dst_;
};

TChunkedBuffer CopyData(const TChunkedBuffer& src);
TChunkedBuffer CopyData(TChunkedBuffer&& src);

}