-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathserializer_impl.hh
616 lines (561 loc) · 19 KB
/
serializer_impl.hh
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* Copyright 2016 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "serializer.hh"
#include <seastar/util/bool_class.hh>
#include <boost/range/algorithm/for_each.hpp>
namespace ser {
template<typename T>
void set_size(seastar::measuring_output_stream& os, const T& obj) {
serialize(os, uint32_t(0));
}
template<typename Stream, typename T>
void set_size(Stream& os, const T& obj) {
serialize(os, get_sizeof(obj));
}
template<typename Output>
void safe_serialize_as_uint32(Output& out, uint64_t data) {
if (data > std::numeric_limits<uint32_t>::max()) {
throw std::runtime_error("Size is too big for serialization");
}
serialize(out, uint32_t(data));
}
template<typename T>
constexpr bool can_serialize_fast() {
return !std::is_same<T, bool>::value && std::is_integral<T>::value && (sizeof(T) == 1 || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
}
template<bool Fast, typename T>
struct serialize_array_helper;
template<typename T>
struct serialize_array_helper<true, T> {
template<typename Container, typename Output>
static void doit(Output& out, const Container& v) {
out.write(reinterpret_cast<const char*>(v.data()), v.size() * sizeof(T));
}
};
template<typename T>
struct serialize_array_helper<false, T> {
template<typename Container, typename Output>
static void doit(Output& out, const Container& v) {
for (auto&& e : v) {
serialize(out, e);
}
}
};
template<typename T, typename Container, typename Output>
static inline void serialize_array(Output& out, const Container& v) {
serialize_array_helper<can_serialize_fast<T>(), T>::doit(out, v);
}
template<typename Container>
struct container_traits;
template<typename T>
struct container_traits<std::vector<T>> {
struct back_emplacer {
std::vector<T>& c;
back_emplacer(std::vector<T>& c_) : c(c_) {}
void operator()(T&& v) {
c.emplace_back(std::move(v));
}
};
void resize(std::vector<T>& c, size_t size) {
c.resize(size);
}
};
template<typename T>
struct container_traits<utils::chunked_vector<T>> {
struct back_emplacer {
utils::chunked_vector<T>& c;
back_emplacer(utils::chunked_vector<T>& c_) : c(c_) {}
void operator()(T&& v) {
c.emplace_back(std::move(v));
}
};
void resize(utils::chunked_vector<T>& c, size_t size) {
c.resize(size);
}
};
template<typename T, size_t N>
struct container_traits<std::array<T, N>> {
struct back_emplacer {
std::array<T, N>& c;
size_t idx = 0;
back_emplacer(std::array<T, N>& c_) : c(c_) {}
void operator()(T&& v) {
c[idx++] = std::move(v);
}
};
void resize(std::array<T, N>& c, size_t size) {}
};
template<bool Fast, typename T>
struct deserialize_array_helper;
template<typename T>
struct deserialize_array_helper<true, T> {
template<typename Input, typename Container>
static void doit(Input& in, Container& v, size_t sz) {
container_traits<Container> t;
t.resize(v, sz);
in.read(reinterpret_cast<char*>(v.data()), v.size() * sizeof(T));
}
template<typename Input>
static void skip(Input& in, size_t sz) {
in.skip(sz * sizeof(T));
}
};
template<typename T>
struct deserialize_array_helper<false, T> {
template<typename Input, typename Container>
static void doit(Input& in, Container& v, size_t sz) {
typename container_traits<Container>::back_emplacer be(v);
while (sz--) {
be(deserialize(in, boost::type<T>()));
}
}
template<typename Input>
static void skip(Input& in, size_t sz) {
while (sz--) {
serializer<T>::skip(in);
}
}
};
template<typename T, typename Input, typename Container>
static inline void deserialize_array(Input& in, Container& v, size_t sz) {
deserialize_array_helper<can_serialize_fast<T>(), T>::doit(in, v, sz);
}
template<typename T, typename Input>
static inline void skip_array(Input& in, size_t sz) {
deserialize_array_helper<can_serialize_fast<T>(), T>::skip(in, sz);
}
template<typename T>
struct serializer<std::vector<T>> {
template<typename Input>
static std::vector<T> read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
std::vector<T> v;
v.reserve(sz);
deserialize_array<T>(in, v, sz);
return v;
}
template<typename Output>
static void write(Output& out, const std::vector<T>& v) {
safe_serialize_as_uint32(out, v.size());
serialize_array<T>(out, v);
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
skip_array<T>(in, sz);
}
};
template<typename T>
struct serializer<utils::chunked_vector<T>> {
template<typename Input>
static utils::chunked_vector<T> read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
utils::chunked_vector<T> v;
v.reserve(sz);
deserialize_array<T>(in, v, sz);
return v;
}
template<typename Output>
static void write(Output& out, const utils::chunked_vector<T>& v) {
safe_serialize_as_uint32(out, v.size());
serialize_array<T>(out, v);
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
skip_array<T>(in, sz);
}
};
template<typename T, typename Ratio>
struct serializer<std::chrono::duration<T, Ratio>> {
template<typename Input>
static std::chrono::duration<T, Ratio> read(Input& in) {
return std::chrono::duration<T, Ratio>(deserialize(in, boost::type<T>()));
}
template<typename Output>
static void write(Output& out, const std::chrono::duration<T, Ratio>& d) {
serialize(out, d.count());
}
template<typename Input>
static void skip(Input& in) {
read(in);
}
};
template<typename Clock, typename Duration>
struct serializer<std::chrono::time_point<Clock, Duration>> {
using value_type = std::chrono::time_point<Clock, Duration>;
template<typename Input>
static value_type read(Input& in) {
return typename Clock::time_point(Duration(deserialize(in, boost::type<uint64_t>())));
}
template<typename Output>
static void write(Output& out, const value_type& v) {
serialize(out, uint64_t(v.time_since_epoch().count()));
}
template<typename Input>
static void skip(Input& in) {
read(in);
}
};
template<size_t N, typename T>
struct serializer<std::array<T, N>> {
template<typename Input>
static std::array<T, N> read(Input& in) {
std::array<T, N> v;
deserialize_array<T>(in, v, N);
return v;
}
template<typename Output>
static void write(Output& out, const std::array<T, N>& v) {
serialize_array<T>(out, v);
}
template<typename Input>
static void skip(Input& in) {
skip_array<T>(in, N);
}
};
template<typename K, typename V>
struct serializer<std::map<K, V>> {
template<typename Input>
static std::map<K, V> read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
std::map<K, V> m;
while (sz--) {
K k = deserialize(in, boost::type<K>());
V v = deserialize(in, boost::type<V>());
m[k] = v;
}
return m;
}
template<typename Output>
static void write(Output& out, const std::map<K, V>& v) {
safe_serialize_as_uint32(out, v.size());
for (auto&& e : v) {
serialize(out, e.first);
serialize(out, e.second);
}
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
while (sz--) {
serializer<K>::skip(in);
serializer<V>::skip(in);
}
}
};
template<typename K, typename V>
struct serializer<std::unordered_map<K, V>> {
template<typename Input>
static std::unordered_map<K, V> read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
std::unordered_map<K, V> m;
m.reserve(sz);
while (sz--) {
auto k = deserialize(in, boost::type<K>());
auto v = deserialize(in, boost::type<V>());
m.emplace(std::move(k), std::move(v));
}
return m;
}
template<typename Output>
static void write(Output& out, const std::unordered_map<K, V>& v) {
safe_serialize_as_uint32(out, v.size());
for (auto&& e : v) {
serialize(out, e.first);
serialize(out, e.second);
}
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
while (sz--) {
serializer<K>::skip(in);
serializer<V>::skip(in);
}
}
};
template<typename Tag>
struct serializer<bool_class<Tag>> {
template<typename Input>
static bool_class<Tag> read(Input& in) {
return bool_class<Tag>(deserialize(in, boost::type<bool>()));
}
template<typename Output>
static void write(Output& out, bool_class<Tag> v) {
serialize(out, bool(v));
}
template<typename Input>
static void skip(Input& in) {
read(in);
}
};
template<typename Stream>
class deserialized_bytes_proxy {
Stream _stream;
template<typename OtherStream>
friend class deserialized_bytes_proxy;
public:
explicit deserialized_bytes_proxy(Stream stream)
: _stream(std::move(stream)) { }
template<typename OtherStream, typename = std::enable_if_t<std::is_convertible_v<OtherStream, Stream>>>
deserialized_bytes_proxy(deserialized_bytes_proxy<OtherStream> proxy)
: _stream(std::move(proxy._stream)) { }
auto view() const {
if constexpr (std::is_same_v<Stream, simple_input_stream>) {
return bytes_view(reinterpret_cast<const int8_t*>(_stream.begin()), _stream.size());
} else {
using iterator_type = typename Stream::iterator_type ;
GCC6_CONCEPT(static_assert(FragmentRange<buffer_view<iterator_type>>));
return seastar::with_serialized_stream(_stream, seastar::make_visitor(
[&] (typename seastar::memory_input_stream<iterator_type >::simple stream) {
return buffer_view<iterator_type>(bytes_view(reinterpret_cast<const int8_t*>(stream.begin()),
stream.size()));
},
[&] (typename seastar::memory_input_stream<iterator_type >::fragmented stream) {
return buffer_view<iterator_type>(bytes_view(reinterpret_cast<const int8_t*>(stream.first_fragment_data()),
stream.first_fragment_size()),
stream.size(), stream.fragment_iterator());
}
));
}
}
[[gnu::always_inline]]
operator bytes() && {
bytes v(bytes::initialized_later(), _stream.size());
_stream.read(reinterpret_cast<char*>(v.begin()), _stream.size());
return v;
}
[[gnu::always_inline]]
operator managed_bytes() && {
managed_bytes v(managed_bytes::initialized_later(), _stream.size());
_stream.read(reinterpret_cast<char*>(v.begin()), _stream.size());
return v;
}
[[gnu::always_inline]]
operator bytes_ostream() && {
bytes_ostream v;
_stream.copy_to(v);
return v;
}
};
template<>
struct serializer<bytes> {
template<typename Input>
static deserialized_bytes_proxy<Input> read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
return deserialized_bytes_proxy<Input>(in.read_substream(sz));
}
template<typename Output>
static void write(Output& out, bytes_view v) {
safe_serialize_as_uint32(out, uint32_t(v.size()));
out.write(reinterpret_cast<const char*>(v.begin()), v.size());
}
template<typename Output>
static void write(Output& out, const bytes& v) {
write(out, static_cast<bytes_view>(v));
}
template<typename Output>
static void write(Output& out, const managed_bytes& v) {
write(out, static_cast<bytes_view>(v));
}
template<typename Output>
static void write(Output& out, const bytes_ostream& v) {
safe_serialize_as_uint32(out, uint32_t(v.size()));
for (bytes_view frag : v.fragments()) {
out.write(reinterpret_cast<const char*>(frag.begin()), frag.size());
}
}
template<typename Output, typename FragmentedBuffer>
GCC6_CONCEPT(requires FragmentRange<FragmentedBuffer>)
static void write_fragmented(Output& out, FragmentedBuffer&& fragments) {
safe_serialize_as_uint32(out, uint32_t(fragments.size_bytes()));
using boost::range::for_each;
for_each(fragments, [&out] (bytes_view frag) {
out.write(reinterpret_cast<const char*>(frag.begin()), frag.size());
});
}
template<typename Input>
static void skip(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
in.skip(sz);
}
};
template<typename Output>
void serialize(Output& out, const bytes_view& v) {
serializer<bytes>::write(out, v);
}
template<typename Output>
void serialize(Output& out, const managed_bytes& v) {
serializer<bytes>::write(out, v);
}
template<typename Output>
void serialize(Output& out, const bytes_ostream& v) {
serializer<bytes>::write(out, v);
}
template<typename Output, typename FragmentedBuffer>
GCC6_CONCEPT(requires FragmentRange<FragmentedBuffer>)
void serialize_fragmented(Output& out, FragmentedBuffer&& v) {
serializer<bytes>::write_fragmented(out, std::forward<FragmentedBuffer>(v));
}
template<typename T>
struct serializer<std::experimental::optional<T>> {
template<typename Input>
static std::experimental::optional<T> read(Input& in) {
std::experimental::optional<T> v;
auto b = deserialize(in, boost::type<bool>());
if (b) {
v = deserialize(in, boost::type<T>());
}
return v;
}
template<typename Output>
static void write(Output& out, const std::experimental::optional<T>& v) {
serialize(out, bool(v));
if (v) {
serialize(out, v.value());
}
}
template<typename Input>
static void skip(Input& in) {
auto present = deserialize(in, boost::type<bool>());
if (present) {
serializer<T>::skip(in);
}
}
};
template<>
struct serializer<sstring> {
template<typename Input>
static sstring read(Input& in) {
auto sz = deserialize(in, boost::type<uint32_t>());
sstring v(sstring::initialized_later(), sz);
in.read(v.begin(), sz);
return v;
}
template<typename Output>
static void write(Output& out, const sstring& v) {
safe_serialize_as_uint32(out, uint32_t(v.size()));
out.write(v.begin(), v.size());
}
template<typename Input>
static void skip(Input& in) {
in.skip(deserialize(in, boost::type<size_type>()));
}
};
template<typename T>
struct serializer<std::unique_ptr<T>> {
template<typename Input>
static std::unique_ptr<T> read(Input& in) {
std::unique_ptr<T> v;
auto b = deserialize(in, boost::type<bool>());
if (b) {
v = std::make_unique<T>(deserialize(in, boost::type<T>()));
}
return v;
}
template<typename Output>
static void write(Output& out, const std::unique_ptr<T>& v) {
serialize(out, bool(v));
if (v) {
serialize(out, *v);
}
}
template<typename Input>
static void skip(Input& in) {
auto present = deserialize(in, boost::type<bool>());
if (present) {
serializer<T>::skip(in);
}
}
};
template<typename Enum>
struct serializer<enum_set<Enum>> {
template<typename Input>
static enum_set<Enum> read(Input& in) {
return enum_set<Enum>::from_mask(deserialize(in, boost::type<uint64_t>()));
}
template<typename Output>
static void write(Output& out, enum_set<Enum> v) {
serialize(out, uint64_t(v.mask()));
}
template<typename Input>
static void skip(Input& in) {
read(in);
}
};
template<typename T>
size_type get_sizeof(const T& obj) {
seastar::measuring_output_stream ms;
serialize(ms, obj);
auto size = ms.size();
if (size > std::numeric_limits<size_type>::max()) {
throw std::runtime_error("Object is too big for get_sizeof");
}
return size;
}
template<typename Buffer, typename T>
Buffer serialize_to_buffer(const T& v, size_t head_space) {
seastar::measuring_output_stream measure;
ser::serialize(measure, v);
Buffer ret(typename Buffer::initialized_later(), measure.size() + head_space);
seastar::simple_output_stream out(reinterpret_cast<char*>(ret.begin()), ret.size(), head_space);
ser::serialize(out, v);
return ret;
}
template<typename T, typename Buffer>
T deserialize_from_buffer(const Buffer& buf, boost::type<T> type, size_t head_space) {
seastar::simple_input_stream in(reinterpret_cast<const char*>(buf.begin() + head_space), buf.size() - head_space);
return deserialize(in, std::move(type));
}
inline
utils::input_stream as_input_stream(bytes_view b) {
return utils::input_stream::simple(reinterpret_cast<const char*>(b.begin()), b.size());
}
inline
utils::input_stream as_input_stream(const bytes_ostream& b) {
if (b.is_linearized()) {
return as_input_stream(b.view());
}
return utils::input_stream::fragmented(b.fragments().begin(), b.size());
}
template<typename Output, typename ...T>
void serialize(Output& out, const boost::variant<T...>& v) {}
template<typename Input, typename ...T>
boost::variant<T...> deserialize(Input& in, boost::type<boost::variant<T...>>) {
return boost::variant<T...>();
}
template<typename Output>
void serialize(Output& out, const unknown_variant_type& v) {
out.write(v.data.begin(), v.data.size());
}
template<typename Input>
unknown_variant_type deserialize(Input& in, boost::type<unknown_variant_type>) {
return seastar::with_serialized_stream(in, [] (auto& in) {
auto size = deserialize(in, boost::type<size_type>());
auto index = deserialize(in, boost::type<size_type>());
auto sz = size - sizeof(size_type) * 2;
sstring v(sstring::initialized_later(), sz);
in.read(v.begin(), sz);
return unknown_variant_type{ index, std::move(v) };
});
}
}