aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/codec/yql_codec_buf.h
blob: 4344730d40825d07e6777da082feaab24634eb0c (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#pragma once
#include <yql/essentials/minikql/mkql_stats_registry.h>
#include <yql/essentials/utils/yql_panic.h>

#include <library/cpp/yson/zigzag.h>

#include <util/generic/yexception.h>
#include <util/generic/maybe.h>
#ifndef LLVM_BC
#include <util/datetime/base.h>
#else
using TInstant = ui64;
#endif
#include <util/generic/vector.h>

#include <utility>
#include <functional>
#include <optional>

namespace NYql {
namespace NCommon {

class TTimeoutException : public yexception {
};

struct IBlockReader {
    virtual ~IBlockReader() = default;
    virtual void SetDeadline(TInstant deadline) = 0;

    virtual std::pair<const char*, const char*> NextFilledBlock() = 0;
    virtual void ReturnBlock() = 0;
    virtual bool Retry(const TMaybe<ui32>& rangeIndex, const TMaybe<ui64>& rowIndex, const std::exception_ptr& error) = 0;
};

struct IBlockWriter {
    virtual ~IBlockWriter() = default;

    virtual void SetRecordBoundaryCallback(std::function<void()> callback) = 0;

    virtual std::pair<char*, char*> NextEmptyBlock() = 0;
    virtual void ReturnBlock(size_t avail, std::optional<size_t> lastRecordBoundary) = 0;
    virtual void Finish() = 0;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

extern NKikimr::NMiniKQL::TStatKey InputBytes;
class TInputBuf;
extern "C" char InputBufReadSlowThunk(TInputBuf& in);
extern "C" void InputBufReadManySlowThunk(TInputBuf& in, char* buffer, size_t count);
extern "C" void InputBufSkipManySlowThunk(TInputBuf& in, size_t count);

class TInputBuf {
friend char InputBufReadSlowThunk(TInputBuf& in);
friend void InputBufReadManySlowThunk(TInputBuf& in, char* buffer, size_t count);
friend void InputBufSkipManySlowThunk(TInputBuf& in, size_t count);

public:
    TInputBuf(NKikimr::NMiniKQL::TSamplingStatTimer* readTimer)
        : ReadTimer_(readTimer)
    {}

    TInputBuf(IBlockReader& source, NKikimr::NMiniKQL::TSamplingStatTimer* readTimer)
        : TInputBuf(readTimer)
    {
        SetSource(source);
    }

    void SetSource(IBlockReader& source) {
        Source_ = &source;
        Current_ = nullptr;
        End_ = nullptr;
        String_.clear();
    }

    void SetStats(NKikimr::NMiniKQL::IStatsRegistry* jobStats) {
        JobStats_ = jobStats;
    }

    void SetNextBlockCallback(std::function<void()> cb) {
        OnNextBlockCallback_ = std::move(cb);
    }

    bool TryRead(char& value) {
        if (Y_LIKELY(Current_ < End_)) {
            value = *Current_++;
            return true;
        }

        return TryReadSlow(value);
    }

    char Read() {
        if (Y_LIKELY(Current_ < End_)) {
            return *Current_++;
        }

        return InputBufReadSlowThunk(*this);
    }

    ui32 ReadVarUI32() {
        char cmd = Read();
        if (Y_LIKELY(!(cmd & 0x80))) {
            return cmd;
        }

        return ReadVarUI32Slow(cmd);
    }

    ui32 CopyVarUI32(TVector<char>& yson);
    ui64 CopyVarUI64(TVector<char>& yson);
    ui32 ReadVarUI32Slow(char cmd);

    ui64 ReadVarUI64() {
        char cmd = Read();
        if (Y_LIKELY(!(cmd & 0x80))) {
            return cmd;
        }

        return ReadVarUI64Slow(cmd);
    }

    ui64 ReadVarUI64Slow(char cmd);

    i64 ReadVarI64() {
        return NYson::ZigZagDecode64(ReadVarUI64());
    }

    i32 ReadVarI32() {
        return NYson::ZigZagDecode32(ReadVarUI32());
    }

    i64 CopyVarI64(TVector<char>& yson) {
        return NYson::ZigZagDecode64(CopyVarUI64(yson));
    }

    i32 CopyVarI32(TVector<char>& yson) {
        return NYson::ZigZagDecode32(CopyVarUI32(yson));
    }

    TStringBuf ReadYtString(ui32 lookAhead = 0);

    TVector<char>& YsonBuffer() {
        return String_;
    }

    void ReadMany(char* buffer, size_t count) {
        if (Y_LIKELY(Current_ + count <= End_)) {
            memcpy(buffer, Current_, count);
            Current_ += count;
            return;
        }

        return InputBufReadManySlowThunk(*this, buffer, count);
    }

    void SkipMany(size_t count) {
        if (Y_LIKELY(Current_ + count <= End_)) {
            Current_ += count;
            return;
        }

        return InputBufSkipManySlowThunk(*this, count);
    }

    void CopyMany(size_t count, TVector<char>& yson) {
        auto origSize = yson.size();
        yson.resize(origSize + count);
        ReadMany(yson.data() + origSize, count);
    }

    // Call it on error
    void Reset();

private:
    void Fill();
    bool TryReadSlow(char& value);
    char ReadSlow();
    void ReadManySlow(char* buffer, size_t count);
    void SkipManySlow(size_t count);

private:
    IBlockReader* Source_ = nullptr;
    NKikimr::NMiniKQL::TSamplingStatTimer* ReadTimer_;
    NKikimr::NMiniKQL::IStatsRegistry* JobStats_ = nullptr;
    const char* Current_ = nullptr;
    const char* End_ = nullptr;
    TVector<char> String_;
    std::function<void()> OnNextBlockCallback_;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

extern NKikimr::NMiniKQL::TStatKey OutputBytes;

class TOutputBuf;
extern "C" void OutputBufFlushThunk(TOutputBuf& out);
extern "C" void OutputBufWriteManySlowThunk(TOutputBuf& out, const char* buffer, size_t count);

class TOutputBuf {
friend void OutputBufWriteManySlowThunk(TOutputBuf& out, const char* buffer, size_t count);

public:
    TOutputBuf(IBlockWriter& target, NKikimr::NMiniKQL::TStatTimer* writeTimer);

    ui64 GetWrittenBytes() const {
        return WrittenBytes_;
    }

    void SetStats(NKikimr::NMiniKQL::IStatsRegistry* jobStats) {
        JobStats_ = jobStats;
    }

    void Write(char value) {
        if (Y_LIKELY(Current_ < End_)) {
            *Current_++ = value;
            return;
        }

        WriteSlow(value);
    }

    void WriteMany(TStringBuf str) {
        WriteMany(str.data(), str.size());
    }

    void WriteMany(const char* buffer, size_t count) {
        if (Y_LIKELY(Current_ + count <= End_)) {
            memcpy(Current_, buffer, count);
            Current_ += count;
            return;
        }

        OutputBufWriteManySlowThunk(*this, buffer, count);
    }

    void WriteVarI32(i32 value) {
        WriteVarUI32(NYson::ZigZagEncode32(value));
    }

    void WriteVarI64(i64 value) {
        WriteVarUI64(NYson::ZigZagEncode64(value));
    }

    void WriteVarUI32(ui32 value);
    void WriteVarUI64(ui64 value);

    void Flush();

    void Finish() {
        Flush();
        if (WriteTimer_) {
            WriteTimer_->Acquire();
        }

        Target_.Finish();
        if (WriteTimer_) {
            WriteTimer_->Release();
        }
    }

    void OnRecordBoundary() {
        RecordBoundary_ = Current_;
    }

private:
    void WriteSlow(char value) {
        OutputBufFlushThunk(*this);
        *Current_++ = value;
    }

    void WriteManySlow(const char* buffer, size_t count);

private:
    IBlockWriter& Target_;
    NKikimr::NMiniKQL::TStatTimer* WriteTimer_;
    NKikimr::NMiniKQL::IStatsRegistry* JobStats_ = nullptr;
    char* Begin_ = nullptr;
    char* Current_ = nullptr;
    char* End_ = nullptr;
    char* RecordBoundary_ = nullptr;
    ui64 WrittenBytes_ = 0;
};

#define CHECK_EXPECTED(read, expected) \
    YQL_ENSURE(read == expected, "Expected char: " << TString(1, expected).Quote() << ", but read: " << TString(1, read).Quote());

#define EXPECTED(buf, expected) \
    { char read = buf.Read(); CHECK_EXPECTED(read, expected); }

#define EXPECTED_COPY(buf, expected, yson) \
    { char read = buf.Read(); CHECK_EXPECTED(read, expected); yson.push_back(read); }

#define CHECK_STRING_LENGTH(length) \
    YQL_ENSURE(length >= 0 && length < (1 << 30), "Bad string length: " << length);

#define CHECK_STRING_LENGTH_UNSIGNED(length) \
    YQL_ENSURE(length < (1 << 30), "Bad string length: " << length);

} // NCommon
} // NYql