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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#pragma once
#include <yql/essentials/minikql/defs.h>
#include <yql/essentials/minikql/pack_num.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_pack.h>
#include <yql/essentials/minikql/mkql_string_util.h>
#include <util/generic/strbuf.h>
#include <util/generic/maybe.h>
#include <string_view>
namespace NKikimr {
namespace NMiniKQL {
Y_FORCE_INLINE void WriteByte(TString& out, ui8 value) {
out.append((char)value);
}
Y_FORCE_INLINE void WriteBool(TString& out, bool value) {
out.append((char)value);
}
Y_FORCE_INLINE void WriteUi32(TString& out, ui32 value) {
char buf[MAX_PACKED32_SIZE];
out.AppendNoAlias(buf, Pack32(value, buf));
}
Y_FORCE_INLINE void WriteUi64(TString& out, ui64 value) {
char buf[MAX_PACKED64_SIZE];
out.AppendNoAlias(buf, Pack64(value, buf));
}
Y_FORCE_INLINE bool ReadBool(TStringBuf& in) {
MKQL_ENSURE(in.size(), "Serialized state is corrupted");
bool result = (bool)*in.data();
in.Skip(1);
return result;
}
Y_FORCE_INLINE ui8 ReadByte(TStringBuf& in) {
MKQL_ENSURE(in.size(), "Serialized state is corrupted");
ui8 result = *in.data();
in.Skip(1);
return result;
}
Y_FORCE_INLINE ui32 ReadUi32(TStringBuf& in) {
ui32 result;
auto count = Unpack32(in.data(), in.size(), result);
MKQL_ENSURE(count, "Serialized state is corrupted");
in.Skip(count);
return result;
}
Y_FORCE_INLINE ui64 ReadUi64(TStringBuf& in) {
ui64 result;
auto count = Unpack64(in.data(), in.size(), result);
MKQL_ENSURE(count, "Serialized state is corrupted");
in.Skip(count);
return result;
}
Y_FORCE_INLINE std::string_view ReadString(TStringBuf& in) {
const ui32 size = ReadUi32(in);
MKQL_ENSURE(in.size() >= size, "Serialized state is corrupted");
TStringBuf head = in.Head(size);
in = in.Tail(size);
return head;
}
Y_FORCE_INLINE void WriteString(TString& out, std::string_view str) {
WriteUi32(out, str.size());
out.AppendNoAlias(str.data(), str.size());
}
template<class>
inline constexpr bool always_false_v = false;
enum class EMkqlStateType {
SIMPLE_BLOB,
SNAPSHOT,
INCREMENT
};
struct TOutputSerializer {
public:
static NUdf::TUnboxedValue MakeSimpleBlobState(const TString& blob, ui32 stateVersion) {
TString out;
WriteUi32(out, static_cast<ui32>(EMkqlStateType::SIMPLE_BLOB));
WriteUi32(out, stateVersion);
out.AppendNoAlias(blob.data(), blob.size());
auto strRef = NUdf::TStringRef(out);
return NMiniKQL::MakeString(strRef);
}
template<typename TContainer>
static NUdf::TUnboxedValue MakeSnapshotState(TContainer& items, ui32 stateVersion) {
TString out;
WriteUi32(out, static_cast<ui32>(EMkqlStateType::SNAPSHOT));
WriteUi32(out, stateVersion);
WriteUi32(out, static_cast<ui32>(items.size()));
for (const auto& [key, value] : items) {
WriteString(out, key);
WriteString(out, value);
}
auto strRef = NUdf::TStringRef(out);
return NMiniKQL::MakeString(strRef);
}
template<typename TContainer, typename TContainer2>
static NUdf::TUnboxedValue MakeIncrementState(TContainer& createdOrChanged, TContainer2& deleted, ui32 stateVersion) {
TString out;
WriteUi32(out, static_cast<ui32>(EMkqlStateType::INCREMENT));
WriteUi32(out, stateVersion);
WriteUi32(out, static_cast<ui32>(createdOrChanged.size()));
WriteUi32(out, static_cast<ui32>(deleted.size()));
for(const auto& [key, value] : createdOrChanged) {
WriteString(out, key);
WriteString(out, value);
}
for(const auto& key : deleted) {
WriteString(out, key);
}
auto strRef = NUdf::TStringRef(out);
return NMiniKQL::MakeString(strRef);
}
public:
TOutputSerializer(EMkqlStateType stateType, ui32 stateVersion, TComputationContext& ctx)
: Ctx(ctx) {
Write(static_cast<ui32>(stateType));
Write(stateVersion);
}
template <typename... Ts>
void operator()(Ts&&... args) {
(Write(std::forward<Ts>(args)), ...);
}
template<typename Type>
void Write(const Type& value ) {
if constexpr (std::is_same_v<std::remove_cv_t<Type>, TString>) {
WriteString(Buf, value);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui64>) {
WriteUi64(Buf, value);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, i64>) {
WriteUi64(Buf, value);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, bool>) {
WriteBool(Buf, value);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui8>) {
WriteByte(Buf, value);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui32>) {
WriteUi32(Buf, value);
} else if constexpr (std::is_empty_v<Type>){
// Empty struct is not saved/loaded.
} else {
static_assert(always_false_v<Type>, "Not supported type / not implemented");
}
}
template<class Type1, class Type2>
void Write(const std::pair<Type1, Type2>& value) {
Write(value.first);
Write(value.second);
}
template<class Type, class Allocator>
void Write(const std::vector<Type, Allocator>& value) {
Write(value.size());
for (size_t i = 0; i < value.size(); ++i) {
Write(value[i]);
}
}
Y_FORCE_INLINE void WriteUnboxedValue(const TValuePacker& packer, const NUdf::TUnboxedValue& value) {
auto state = packer.Pack(value);
Write<ui32>(state.size());
Buf.AppendNoAlias(state.data(), state.size());
}
static NUdf::TUnboxedValue MakeArray(TComputationContext& ctx, const TStringBuf& buf) {
const size_t MaxItemLen = 1048576;
size_t count = buf.size() / MaxItemLen + (buf.size() % MaxItemLen ? 1 : 0);
NUdf::TUnboxedValue *items = nullptr;
auto array = ctx.HolderFactory.CreateDirectArrayHolder(count, items);
size_t pos = 0;
for (size_t index = 0; index < count; ++index) {
size_t itemSize = std::min(buf.size() - pos, MaxItemLen);
NUdf::TStringValue str(itemSize);
std::memcpy(str.Data(), buf.data() + pos, itemSize);
items[index] = NUdf::TUnboxedValuePod(std::move(str));
pos += itemSize;
}
return array;
}
NUdf::TUnboxedValue MakeState() {
return MakeArray(Ctx, Buf);
}
protected:
TString Buf;
TComputationContext& Ctx;
};
struct TInputSerializer {
public:
TInputSerializer(const TStringBuf& state, TMaybe<EMkqlStateType> expectedType = Nothing())
: Buf(state) {
Type = static_cast<EMkqlStateType>(Read<ui32>());
Read(StateVersion);
if (expectedType) {
MKQL_ENSURE(Type == *expectedType, "state type is not expected");
}
}
TInputSerializer(const NUdf::TUnboxedValue& state, TMaybe<EMkqlStateType> expectedType = Nothing())
: State(StateToString(state))
, Buf(State) {
Type = static_cast<EMkqlStateType>(Read<ui32>());
Read(StateVersion);
if (expectedType) {
MKQL_ENSURE(Type == *expectedType, "state type is not expected");
}
}
ui32 GetStateVersion() {
return StateVersion;
}
EMkqlStateType GetType() {
return Type;
}
template <typename... Ts>
void operator()(Ts&... args) {
(Read(args), ...);
}
template<typename Type, typename ReturnType = Type>
ReturnType Read() {
if constexpr (std::is_same_v<std::remove_cv_t<Type>, TString>) {
return ReturnType(ReadString(Buf));
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui64>) {
return ReadUi64(Buf);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, i64>) {
return ReadUi64(Buf);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, bool>) {
return ReadBool(Buf);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui8>) {
return ReadByte(Buf);
} else if constexpr (std::is_same_v<std::remove_cv_t<Type>, ui32>) {
return ReadUi32(Buf);
} else if constexpr (std::is_empty_v<Type>){
// Empty struct is not saved/loaded.
return ReturnType{};
} else {
static_assert(always_false_v<Type>, "Not supported type / not implemented");
}
}
Y_FORCE_INLINE NUdf::TUnboxedValue ReadUnboxedValue(const TValuePacker& packer, TComputationContext& ctx) {
auto size = Read<ui32>();
MKQL_ENSURE_S(size <= Buf.size(), "Serialized state is corrupted, size " << size << ", Buf.size " << Buf.size());
auto value = packer.Unpack(TStringBuf(Buf.data(), Buf.data() + size), ctx.HolderFactory);
Buf.Skip(size);
return value;
}
template<typename Type>
void Read(Type& value) {
value = Read<Type, Type>();
}
template<class Type1, class Type2>
void Read(std::pair<Type1, Type2>& value) {
Read(value.first);
Read(value.second);
}
template<class Type, class Allocator>
void Read(std::vector<Type, Allocator>& value) {
using TVector = std::vector<Type, Allocator>;
auto size = Read<typename TVector::size_type>();
value.clear();
value.resize(size);
for (size_t i = 0; i < size; ++i) {
Read(value[i]);
}
}
template<class TCallbackUpdate, class TCallbackDelete>
void ReadItems(TCallbackUpdate updateItem, TCallbackDelete deleteKey) {
MKQL_ENSURE(Buf.size(), "Serialized state is corrupted");
ui32 itemsCount = ReadUi32(Buf);
ui32 deletedCount = 0;
if (Type == EMkqlStateType::INCREMENT) {
deletedCount = ReadUi32(Buf);
}
for (ui32 i = 0; i < itemsCount; ++i) {
auto key = ReadString(Buf);
auto value = ReadString(Buf);
updateItem(key, value);
}
if (deletedCount) {
auto key = ReadString(Buf);
deleteKey(key);
}
}
bool Empty() const {
return Buf.empty();
}
private:
TString StateToString(const NUdf::TUnboxedValue& state) {
TString result;
auto listIt = state.GetListIterator();
NUdf::TUnboxedValue str;
while (listIt.Next(str)) {
const TStringBuf strRef = str.AsStringRef();
result.AppendNoAlias(strRef.data(), strRef.size());
}
return result;
}
protected:
TString State;
TStringBuf Buf;
EMkqlStateType Type{EMkqlStateType::SIMPLE_BLOB};
ui32 StateVersion{0};
};
class TNodeStateHelper {
public:
static void AddNodeState(TString& result, const TStringBuf& state) {
WriteUi64(result, state.size());
result.AppendNoAlias(state.data(), state.size());
}
};
} // namespace NMiniKQL
} // namespace NKikimr
|