aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/chunked_buffer.cpp
blob: 7ae66d04efc9982ccfa89718fd06b9b9ea830445 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "chunked_buffer.h"

#include <yql/essentials/utils/yql_panic.h>

namespace NYql {

TChunkedBuffer::TChunkedBuffer(TChunkedBuffer&& other) {
    Items_ = std::move(other.Items_);
    Size_ = other.Size_;
    other.Size_ = 0;
}

TChunkedBuffer& TChunkedBuffer::operator=(TChunkedBuffer&& other) {
    Items_ = std::move(other.Items_);
    Size_ = other.Size_;
    other.Size_ = 0;
    return *this;
}

TChunkedBuffer::TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner) {
    Append(buf, owner);
}

TChunkedBuffer::TChunkedBuffer(TString&& str) {
    Append(std::move(str));
}

const TChunkedBuffer::TChunk& TChunkedBuffer::Front() const {
    YQL_ENSURE(!Items_.empty());
    return Items_.front();
}

size_t TChunkedBuffer::CopyTo(IOutputStream& dst, size_t toCopy) const {
    size_t copied = 0;
    for (auto& chunk : Items_) {
        if (!toCopy) {
            break;
        }
        size_t copyChunk = std::min(chunk.Buf.size(), toCopy);
        dst.Write(chunk.Buf.data(), copyChunk);
        toCopy -= copyChunk;
        copied += copyChunk;
    }
    return copied;
}

TChunkedBuffer& TChunkedBuffer::Append(TStringBuf buf, const std::shared_ptr<const void>& owner) {
    if (!buf.empty()) {
        Items_.emplace_back(TChunk{buf, owner});
        Size_ += buf.size();
    }
    return *this;
}

TChunkedBuffer& TChunkedBuffer::Append(TString&& str) {
    if (!str.empty()) {
        auto owner = std::make_shared<TString>(std::move(str));
        Items_.emplace_back(TChunk{*owner, owner});
        Size_ += owner->size();
    }
    return *this;
}

TChunkedBuffer& TChunkedBuffer::Append(TChunkedBuffer&& other) {
    while (!other.Items_.empty()) {
        Items_.emplace_back(std::move(other.Items_.front()));
        Size_ += Items_.back().Buf.size();
        other.Items_.pop_front();
    }
    other.Size_ = 0;
    return *this;
}

TChunkedBuffer& TChunkedBuffer::Clear() {
    Items_.clear();
    Size_ = 0;
    return *this;
}

TChunkedBuffer& TChunkedBuffer::Erase(size_t size) {
    while (size && !Items_.empty()) {
        TStringBuf& buf = Items_.front().Buf;
        size_t toErase = std::min(buf.size(), size);
        buf.Skip(toErase);
        size -= toErase;
        Size_ -= toErase;
        if (buf.empty()) {
            Items_.pop_front();
        }
    }
    return *this;
}

TChunkedBufferOutput::TChunkedBufferOutput(TChunkedBuffer& dst)
    : Dst_(dst)
{
}

void TChunkedBufferOutput::DoWrite(const void* buf, size_t len) {
    TString str(static_cast<const char*>(buf), len);
    Dst_.Append(std::move(str));
}

TChunkedBuffer CopyData(const TChunkedBuffer& src) {
    TChunkedBuffer result;
    TChunkedBufferOutput out(result);
    src.CopyTo(out);
    return result;
}

TChunkedBuffer CopyData(TChunkedBuffer&& src) {
    TChunkedBuffer result = CopyData(src);
    src.Clear();
    return result;
}

}