/
serializer.cc
374 lines (333 loc) · 8.04 KB
/
serializer.cc
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "tools/serializer.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <cstring>
#include "core/keyvalue/key_string.h"
#include "core/keyvalue/p_string.h"
#include "estl/chunk_buf.h"
#include "itoa/itoa.h"
#include "tools/errors.h"
#include "tools/varint.h"
namespace reindexer {
Serializer::Serializer(const void *_buf, int _len) : buf(static_cast<const uint8_t *>(_buf)), len(_len), pos(0) {}
Serializer::Serializer(const string_view &buf) : buf(reinterpret_cast<const uint8_t *>(buf.data())), len(buf.length()), pos(0) {}
bool Serializer::Eof() { return pos >= len; }
Variant Serializer::GetVariant() {
KeyValueType type = KeyValueType(GetVarUint());
switch (type) {
case KeyValueTuple: {
VariantArray compositeValues;
uint64_t count = GetVarUint();
compositeValues.reserve(count);
for (size_t i = 0; i < count; ++i) {
compositeValues.push_back(GetVariant());
}
return Variant(compositeValues);
}
default:
return GetRawVariant(type);
}
}
Variant Serializer::GetRawVariant(KeyValueType type) {
switch (type) {
case KeyValueInt:
return Variant(int(GetVarint()));
case KeyValueBool:
return Variant(bool(GetVarUint()));
case KeyValueInt64:
return Variant(int64_t(GetVarint()));
case KeyValueDouble:
return Variant(GetDouble());
case KeyValueString:
return Variant(GetPVString());
case KeyValueNull:
return Variant();
default:
throw Error(errParseBin, "Unknown type %d while parsing binary buffer", type);
}
}
inline static void checkbound(int pos, int need, int len) {
if (pos + need > len) {
throw Error(errParseBin, "Binary buffer underflow. Need more %d bytes, pos=%d,len=%d", need, pos, len);
}
}
string_view Serializer::GetSlice() {
uint32_t l = GetUInt32();
string_view b(reinterpret_cast<const char *>(buf + pos), l);
checkbound(pos, b.size(), len);
pos += b.size();
return b;
}
uint32_t Serializer::GetUInt32() {
uint32_t ret;
checkbound(pos, sizeof(ret), len);
memcpy(&ret, buf + pos, sizeof(ret));
pos += sizeof(ret);
return ret;
}
uint64_t Serializer::GetUInt64() {
uint64_t ret;
checkbound(pos, sizeof(ret), len);
memcpy(&ret, buf + pos, sizeof(ret));
pos += sizeof(ret);
return ret;
}
double Serializer::GetDouble() {
double ret;
checkbound(pos, sizeof(ret), len);
memcpy(&ret, buf + pos, sizeof(ret));
pos += sizeof(ret);
return ret;
}
int64_t Serializer::GetVarint() {
int l = scan_varint(len - pos, buf + pos);
if (l == 0) {
throw Error(errParseBin, "Binary buffer broken - scan_varint failed: pos=%d,len=%d", int(pos), int(len));
}
checkbound(pos, l, len);
pos += l;
return unzigzag64(parse_uint64(l, buf + pos - l));
}
uint64_t Serializer::GetVarUint() {
int l = scan_varint(len - pos, buf + pos);
if (l == 0) {
throw Error(errParseBin, "Binary buffer broken - scan_varint failed: pos=%d,len=%d", int(pos), int(len));
}
checkbound(pos, l, len);
pos += l;
return parse_uint64(l, buf + pos - l);
}
string_view Serializer::GetVString() {
int l = GetVarUint();
checkbound(pos, l, len);
pos += l;
return string_view(reinterpret_cast<const char *>(buf + pos - l), l);
}
p_string Serializer::GetPVString() {
auto ret = reinterpret_cast<const v_string_hdr *>(buf + pos);
int l = GetVarUint();
checkbound(pos, l, len);
pos += l;
return p_string(ret);
}
bool Serializer::GetBool() { return bool(GetVarUint()); }
WrSerializer::WrSerializer() : buf_(inBuf_), len_(0), cap_(sizeof(inBuf_)) {}
WrSerializer::WrSerializer(chunk &&ch) : buf_(ch.data_), len_(ch.len_), cap_(ch.cap_) {
if (!buf_) {
buf_ = inBuf_;
cap_ = sizeof(inBuf_);
len_ = 0;
}
ch.data_ = nullptr;
ch.len_ = 0;
ch.cap_ = 0;
ch.offset_ = 0;
}
WrSerializer::~WrSerializer() {
if (buf_ != inBuf_) delete[] buf_;
}
void WrSerializer::PutVariant(const Variant &kv) {
PutVarUint(kv.Type());
switch (kv.Type()) {
case KeyValueTuple: {
auto compositeValues = kv.getCompositeValues();
PutVarUint(compositeValues.size());
for (auto &v : compositeValues) {
PutVariant(v);
}
break;
}
default:
PutRawVariant(kv);
}
}
void WrSerializer::PutRawVariant(const Variant &kv) {
switch (kv.Type()) {
case KeyValueBool:
PutBool(bool(kv));
break;
case KeyValueInt64:
PutVarint(int64_t(kv));
break;
case KeyValueInt:
PutVarint(int(kv));
break;
case KeyValueDouble:
PutDouble(double(kv));
break;
case KeyValueString:
PutVString(string_view(kv));
break;
case KeyValueNull:
break;
default:
fprintf(stderr, "Unknown keyType %d\n", int(kv.Type()));
abort();
}
}
void WrSerializer::PutSlice(const string_view &slice) {
PutUInt32(slice.size());
grow(slice.size());
memcpy(&buf_[len_], slice.data(), slice.size());
len_ += slice.size();
}
WrSerializer::SliceHelper WrSerializer::StartSlice() {
size_t savePos = len_;
PutUInt32(0);
return SliceHelper(this, savePos);
}
WrSerializer::SliceHelper::~SliceHelper() {
if (ser_) {
uint32_t sliceSize = ser_->len_ - pos_ - sizeof(uint32_t);
memcpy(&ser_->buf_[pos_], &sliceSize, sizeof(sliceSize));
}
}
void WrSerializer::PutUInt32(uint32_t v) {
grow(sizeof v);
memcpy(&buf_[len_], &v, sizeof v);
len_ += sizeof v;
}
void WrSerializer::PutUInt64(uint64_t v) {
grow(sizeof v);
memcpy(&buf_[len_], &v, sizeof v);
len_ += sizeof v;
}
void WrSerializer::PutDouble(double v) {
grow(sizeof v);
memcpy(&buf_[len_], &v, sizeof v);
len_ += sizeof v;
}
void WrSerializer::grow(size_t sz) {
if (len_ + sz > cap_) {
Reserve((cap_ * 2) + sz + 0x1000);
}
}
void WrSerializer::Reserve(size_t cap) {
if (cap > cap_) {
cap_ = std::max(cap, cap_);
uint8_t *b = new uint8_t[cap_];
memcpy(b, buf_, len_);
if (buf_ != inBuf_) delete[] buf_;
buf_ = b;
}
}
void WrSerializer::Printf(const char *fmt, ...) {
int ret = 0, sz = 100;
va_list args;
va_start(args, fmt);
for (;;) {
grow(sz);
va_list cargs;
va_copy(cargs, args);
ret = vsnprintf(reinterpret_cast<char *>(buf_ + len_), cap_ - len_, fmt, cargs);
va_end(cargs);
if (ret < 0) {
abort();
}
if (size_t(ret) < cap_ - len_) break;
sz = ret + 1;
}
va_end(args);
len_ += ret;
}
void WrSerializer::PutBool(bool v) {
grow(1);
len_ += boolean_pack(v, buf_ + len_);
}
void WrSerializer::PutVString(const string_view &str) {
grow(str.size() + 10);
len_ += string_pack(str.data(), str.size(), buf_ + len_);
}
void WrSerializer::PrintJsonString(const string_view &str) {
const char *s = str.data();
size_t l = str.size();
grow(l * 6 + 3);
char *d = reinterpret_cast<char *>(buf_ + len_);
*d++ = '"';
while (l--) {
int c = *s++;
switch (c) {
case '\b':
*d++ = '\\';
*d++ = 'b';
break;
case '\f':
*d++ = '\\';
*d++ = 'f';
break;
case '\n':
*d++ = '\\';
*d++ = 'n';
break;
case '\r':
*d++ = '\\';
*d++ = 'r';
break;
case '\t':
*d++ = '\\';
*d++ = 't';
break;
case '\\':
*d++ = '\\';
*d++ = '\\';
break;
case '"':
*d++ = '\\';
*d++ = '"';
break;
case '&':
*d++ = '\\';
*d++ = 'u';
*d++ = '0';
*d++ = '0';
*d++ = '2';
*d++ = '6';
break;
default:
*d++ = c;
}
}
*d++ = '"';
len_ = d - reinterpret_cast<char *>(buf_);
}
const int kHexDumpBytesInRow = 16;
void WrSerializer::PrintHexDump(const string_view &str) {
for (int row = 0; row < int(str.size()); row += kHexDumpBytesInRow) {
Printf("%08x ", row);
for (int i = row; i < row + kHexDumpBytesInRow; i++) {
if (i < int(str.size())) {
Printf("%02x ", unsigned(str[i]) & 0xFF);
} else {
Write(" ");
}
}
Write(" ");
for (int i = row; i < row + kHexDumpBytesInRow; i++) {
char c = (i < int(str.size()) && unsigned(str[i]) > 0x20) ? str[i] : '.';
(*this) << c;
}
Write("\n");
}
}
uint8_t *WrSerializer::Buf() const { return buf_; }
chunk WrSerializer::DetachChunk() {
chunk ch;
if (buf_ == inBuf_) {
ch.append(Slice());
} else {
ch.data_ = buf_;
ch.cap_ = cap_;
ch.len_ = len_;
}
buf_ = inBuf_;
cap_ = sizeof(inBuf_);
len_ = 0;
return ch;
}
void WrSerializer::Write(const string_view &slice) {
grow(slice.size());
memcpy(&buf_[len_], slice.data(), slice.size());
len_ += slice.size();
}
} // namespace reindexer