forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.hh
400 lines (336 loc) · 11.4 KB
/
serializer.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
/*
* Copyright 2016-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <seastar/core/sstring.hh>
#include <optional>
#include "utils/assert.hh"
#include "utils/managed_bytes.hh"
#include "bytes_ostream.hh"
#include <seastar/core/simple-stream.hh>
#include "boost/variant/variant.hpp"
#include "bytes_ostream.hh"
#include "utils/fragment_range.hh"
#include <variant>
#include <type_traits>
namespace ser {
/// A fragmented view of an opaque buffer in a stream of serialised data
///
/// This class allows reading large, fragmented blobs serialised by the IDL
/// infrastructure without linearising or copying them. The view remains valid
/// as long as the underlying IDL-serialised buffer is alive.
///
/// Satisfies FragmentRange concept.
template<typename FragmentIterator>
class buffer_view {
bytes_view _first;
size_t _total_size;
FragmentIterator _next;
public:
using fragment_type = bytes_view;
struct implementation {
bytes_view current;
FragmentIterator next;
size_t size;
};
class iterator {
bytes_view _current;
size_t _left = 0;
FragmentIterator _next;
public:
using iterator_category = std::input_iterator_tag;
using value_type = bytes_view;
using pointer = const bytes_view*;
using reference = const bytes_view&;
using difference_type = std::ptrdiff_t;
iterator() = default;
iterator(bytes_view current, size_t left, FragmentIterator next)
: _current(current), _left(left), _next(next) { }
bytes_view operator*() const {
return _current;
}
const bytes_view* operator->() const {
return &_current;
}
iterator& operator++() {
_left -= _current.size();
if (_left) {
auto next_view = bytes_view(reinterpret_cast<const bytes::value_type*>((*_next).begin()),
(*_next).size());
auto next_size = std::min(_left, next_view.size());
_current = bytes_view(next_view.data(), next_size);
++_next;
}
return *this;
}
iterator operator++(int) {
iterator it(*this);
operator++();
return it;
}
bool operator==(const iterator& other) const {
return _left == other._left;
}
};
using const_iterator = iterator;
explicit buffer_view(bytes_view current)
: _first(current), _total_size(current.size()) { }
buffer_view(bytes_view current, size_t size, FragmentIterator it)
: _first(current), _total_size(size), _next(it)
{
if (_first.size() > _total_size) {
_first.remove_suffix(_first.size() - _total_size);
}
}
explicit buffer_view(typename seastar::memory_input_stream<FragmentIterator>::simple stream)
: buffer_view(bytes_view(reinterpret_cast<const int8_t*>(stream.begin()), stream.size()))
{ }
explicit buffer_view(typename seastar::memory_input_stream<FragmentIterator>::fragmented stream)
: buffer_view(bytes_view(reinterpret_cast<const int8_t*>(stream.first_fragment_data()), stream.first_fragment_size()),
stream.size(), stream.fragment_iterator())
{ }
iterator begin() const {
return iterator(_first, _total_size, _next);
}
iterator end() const {
return iterator();
}
size_t size_bytes() const {
return _total_size;
}
bool empty() const {
return !_total_size;
}
// FragmentedView implementation
void remove_prefix(size_t n) {
while (n >= _first.size() && n > 0) {
n -= _first.size();
remove_current();
}
_total_size -= n;
_first.remove_prefix(n);
}
void remove_current() {
_total_size -= _first.size();
if (_total_size) {
auto next_data = reinterpret_cast<const bytes::value_type*>((*_next).begin());
size_t next_size = std::min(_total_size, (*_next).size());
_first = bytes_view(next_data, next_size);
++_next;
} else {
_first = bytes_view();
}
}
buffer_view prefix(size_t n) const {
auto tmp = *this;
tmp._total_size = std::min(tmp._total_size, n);
tmp._first = tmp._first.substr(0, n);
return tmp;
}
bytes_view current_fragment() {
return _first;
}
bytes linearize() const {
bytes b(bytes::initialized_later(), size_bytes());
auto dst = b.begin();
for (bytes_view fragment : *this) {
dst = std::copy(fragment.begin(), fragment.end(), dst);
}
return b;
}
template<typename Function>
decltype(auto) with_linearized(Function&& fn) const
{
bytes b;
bytes_view bv;
if (_first.size() != _total_size) {
b = linearize();
bv = b;
} else {
bv = _first;
}
return fn(bv);
}
implementation extract_implementation() const {
return implementation {
.current = _first,
.next = _next,
.size = _total_size,
};
}
};
static_assert(FragmentedView<buffer_view<bytes_ostream::fragment_iterator>>);
using size_type = uint32_t;
template<typename T, typename Input>
requires std::is_integral_v<T>
inline T deserialize_integral(Input& input) {
T data;
input.read(reinterpret_cast<char*>(&data), sizeof(T));
return le_to_cpu(data);
}
template<typename T, typename Output>
requires std::is_integral_v<T>
inline void serialize_integral(Output& output, T data) {
data = cpu_to_le(data);
output.write(reinterpret_cast<const char*>(&data), sizeof(T));
}
template<typename T>
struct serializer;
template<typename T>
struct integral_serializer {
template<typename Input>
static T read(Input& v) {
return deserialize_integral<T>(v);
}
template<typename Output>
static void write(Output& out, T v) {
serialize_integral(out, v);
}
template<typename Input>
static void skip(Input& v) {
read(v);
}
};
template<> struct serializer<bool> {
template <typename Input>
static bool read(Input& i) {
return deserialize_integral<uint8_t>(i);
}
template< typename Output>
static void write(Output& out, bool v) {
serialize_integral(out, uint8_t(v));
}
template <typename Input>
static void skip(Input& i) {
read(i);
}
};
template<> struct serializer<int8_t> : public integral_serializer<int8_t> {};
template<> struct serializer<uint8_t> : public integral_serializer<uint8_t> {};
template<> struct serializer<int16_t> : public integral_serializer<int16_t> {};
template<> struct serializer<uint16_t> : public integral_serializer<uint16_t> {};
template<> struct serializer<int32_t> : public integral_serializer<int32_t> {};
template<> struct serializer<uint32_t> : public integral_serializer<uint32_t> {};
template<> struct serializer<int64_t> : public integral_serializer<int64_t> {};
template<> struct serializer<uint64_t> : public integral_serializer<uint64_t> {};
template<typename Output>
void safe_serialize_as_uint32(Output& output, uint64_t data);
template<typename T, typename Output>
inline void serialize(Output& out, const T& v) {
serializer<T>::write(out, v);
};
template<typename T, typename Output>
inline void serialize(Output& out, const std::reference_wrapper<T> v) {
serializer<T>::write(out, v.get());
}
template<typename T, typename Input>
inline auto deserialize(Input& in, std::type_identity<T> t) {
return serializer<T>::read(in);
}
template<typename T, typename Input>
inline void skip(Input& v, std::type_identity<T>) {
return serializer<T>::skip(v);
}
template<typename T>
size_type get_sizeof(const T& obj);
template<typename T>
void set_size(seastar::measuring_output_stream& os, const T& obj);
template<typename Stream, typename T>
void set_size(Stream& os, const T& obj);
template<typename Buffer, typename T>
Buffer serialize_to_buffer(const T& v, size_t head_space = 0);
template<typename T, typename Buffer>
T deserialize_from_buffer(const Buffer&, std::type_identity<T>, size_t head_space = 0);
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, std::type_identity<boost::variant<T...>>);
template<typename Output, typename ...T>
void serialize(Output& out, const std::variant<T...>& v);
template<typename Input, typename ...T>
std::variant<T...> deserialize(Input& in, std::type_identity<std::variant<T...>>);
struct unknown_variant_type {
size_type index;
sstring data;
};
template<typename Output>
void serialize(Output& out, const unknown_variant_type& v);
template<typename Input>
unknown_variant_type deserialize(Input& in, std::type_identity<unknown_variant_type>);
template <typename T>
struct normalize {
using type = T;
};
template <>
struct normalize<bytes_view> {
using type = bytes;
};
template <>
struct normalize<managed_bytes> {
using type = bytes;
};
template <>
struct normalize<bytes_ostream> {
using type = bytes;
};
template <typename T, typename U>
struct is_equivalent : std::is_same<typename normalize<std::remove_const_t<std::remove_reference_t<T>>>::type, typename normalize<std::remove_const_t <std::remove_reference_t<U>>>::type> {
};
template <typename T, typename U>
struct is_equivalent<std::reference_wrapper<T>, U> : is_equivalent<T, U> {
};
template <typename T, typename U>
struct is_equivalent<T, std::reference_wrapper<U>> : is_equivalent<T, U> {
};
template <typename T, typename U>
struct is_equivalent<std::optional<T>, std::optional<U>> : is_equivalent<T, U> {
};
template <typename T, typename U, bool>
struct is_equivalent_arity;
template <typename ...T, typename ...U>
struct is_equivalent_arity<std::tuple<T...>, std::tuple<U...>, false> : std::false_type {
};
template <typename ...T, typename ...U>
struct is_equivalent_arity<std::tuple<T...>, std::tuple<U...>, true> {
static constexpr bool value = (is_equivalent<T, U>::value && ...);
};
template <typename ...T, typename ...U>
struct is_equivalent<std::tuple<T...>, std::tuple<U...>> : is_equivalent_arity<std::tuple<T...>, std::tuple<U...>, sizeof...(T) == sizeof...(U)> {
};
template <typename ...T, typename ...U>
struct is_equivalent<std::variant<T...>, std::variant<U...>> : is_equivalent<std::tuple<T...>, std::tuple<U...>> {
};
// gc_clock duration values were serialized as 32-bit prior to 3.1, and
// are serialized as 64-bit in 3.1.0.
//
// TTL values are capped to 20 years, which fits into 32 bits, so
// truncation is not a concern.
inline bool gc_clock_using_3_1_0_serialization = false;
template <typename Output>
void
serialize_gc_clock_duration_value(Output& out, int64_t v) {
if (!gc_clock_using_3_1_0_serialization) {
// This should have been caught by the CQL layer, so this is just
// for extra safety.
SCYLLA_ASSERT(int32_t(v) == v);
serializer<int32_t>::write(out, v);
} else {
serializer<int64_t>::write(out, v);
}
}
template <typename Input>
int64_t
deserialize_gc_clock_duration_value(Input& in) {
if (!gc_clock_using_3_1_0_serialization) {
return serializer<int32_t>::read(in);
} else {
return serializer<int64_t>::read(in);
}
}
}
/*
* Import the auto generated forward declaration code
*/