-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqltypes.h
591 lines (483 loc) · 15.8 KB
/
sqltypes.h
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
// -*- C++ -*-
#ifndef SQLTYPES_H
#define SQLTYPES_H
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cassert>
#include "mem.h"
#include "varstr.h"
namespace sql {
using VarStr = felis::VarStr;
using VarStrView = felis::VarStrView;
// types that looks like a built-in C++ type, let's keep them all in lower-case!
// copied from Silo.
// equivalent to VARCHAR(N)
template <typename IntSizeType, unsigned int N>
class inline_str_base {
public:
inline_str_base() : sz(0) {}
inline_str_base(const char *s) {
assign(s);
}
inline_str_base(const char *s, size_t n) {
assign(s, n);
}
inline_str_base(const std::string &s) {
assign(s);
}
inline_str_base(const inline_str_base &that) : sz(that.sz) {
__builtin_memcpy(&buf[0], &that.buf[0], sz);
}
inline_str_base &
operator=(const inline_str_base &that) {
if (this == &that)
return *this;
sz = that.sz;
__builtin_memcpy(&buf[0], &that.buf[0], sz);
return *this;
}
size_t
max_size() const {
return N;
}
const char * c_str() const {
buf[sz] = 0;
return &buf[0];
}
inline std::string str(bool zeropad = false) const {
if (zeropad) {
assert(N >= sz);
std::string r(N, 0);
__builtin_memcpy((char *) r.data(), &buf[0], sz);
return r;
} else {
return std::string(&buf[0], sz);
}
}
const char *data() const {
return &buf[0];
}
size_t size() const {
return sz;
}
void assign(const char *s) {
assign(s, strlen(s));
}
void assign(const char *s, size_t n) {
assert(n <= N);
__builtin_memcpy(&buf[0], s, n);
sz = n;
}
void assign(const std::string &s) {
assign(s.data(), s.size());
}
void resize(size_t n, char c = 0) {
assert(n <= N);
if (n > sz)
memset(&buf[sz], c, n - sz);
sz = n;
}
void resize_junk(size_t n) {
assert(n <= N);
sz = n;
}
bool operator==(const inline_str_base &other) const {
return memcmp(buf, other.buf, sz) == 0;
}
bool operator!=(const inline_str_base &other) const {
return !operator==(other);
}
private:
IntSizeType sz;
mutable char buf[N + 1];
};
template <unsigned int N>
using inline_str_8 = inline_str_base<uint8_t, N>;
template <unsigned int N>
using inline_str_16 = inline_str_base<uint16_t, N>;
// equiavlent to CHAR(N)
template <unsigned int N, char FillChar = ' '>
class Char {
public:
Char() {
__builtin_memset(&buf[0], FillChar, N);
}
Char(const char *s) {
assign(s, strlen(s));
}
Char(const char *s, size_t n) {
assign(s, n);
}
Char(const std::string &s) {
assign(s.data(), s.size());
}
Char(const Char &that) {
__builtin_memcpy(&buf[0], &that.buf[0], N);
}
Char &
operator=(const Char &that) {
if (this == &that)
return *this;
__builtin_memcpy(&buf[0], &that.buf[0], N);
return *this;
}
std::string str() const {
return std::string(&buf[0], N);
}
const char * data() const {
return &buf[0];
}
size_t size() const {
return N;
}
void assign(const char *s) {
assign(s, strlen(s));
}
inline void assign(const char *s, size_t n) {
assert(n <= N);
__builtin_memcpy(&buf[0], s, n);
if ((N - n) > 0) // to suppress compiler warning
memset(&buf[n], FillChar, N - n); // pad with spaces
}
void assign(const std::string &s) {
assign(s.data(), s.size());
}
bool operator==(const Char &other) const {
return memcmp(buf, other.buf, N) == 0;
}
bool operator!=(const Char &other) const {
return !operator==(other);
}
private:
char buf[N];
};
// Schemas Wrapper.
// Because schemas is a POJO, we need to add less than operator to this POJO.
// However, with a combined key, we have to compare field by field. Moreover, on
// x86 architecture, memcmp() won't work for integer types at all.
template <typename T>
struct Serializer {
static size_t EncodeSize(const T *ptr) { return sizeof(T); }
static void EncodeTo(uint8_t *buf, const T *ptr) {
__builtin_memcpy(buf, ptr, EncodeSize(ptr));
}
static void DecodeFrom(T *ptr, const uint8_t *buf) {
__builtin_memcpy(ptr, buf, sizeof(T));
}
};
template <typename SizeType, unsigned int N>
struct Serializer<inline_str_base<SizeType, N>> {
typedef inline_str_base<SizeType, N> ObjectType;
static size_t EncodeSize(const ObjectType *p) {
return sizeof(SizeType) + p->size();
}
static void EncodeTo(uint8_t *buf, const ObjectType *p) {
SizeType sz = p->size();
Serializer<SizeType>::EncodeTo(buf, (const SizeType *) &sz);
__builtin_memcpy(buf + Serializer<SizeType>::EncodeSize((const SizeType *) &sz),
p->data(), p->size());
}
static void DecodeFrom(ObjectType *p, const uint8_t *buf) {
SizeType sz;
Serializer<SizeType>::DecodeFrom((SizeType *) &sz, buf);
p->assign((const char *) buf + Serializer<SizeType>::EncodeSize((const SizeType *) &sz), sz);
}
};
/* T cannot be recusively serialized, due to performance costs */
template <typename T>
struct Serializer<std::vector<T>> {
typedef std::vector<T> ObjectType;
static size_t EncodeSize(const ObjectType *p) {
return sizeof(size_t) + p->size() * sizeof(T);
}
static void EncodeTo(uint8_t *buf, const ObjectType *p) {
size_t len = p->size();
Serializer<size_t>::EncodeTo(buf, &len);
__builtin_memcpy(buf + sizeof(size_t), p->data(), len * sizeof(T));
}
static void DecodeFrom(ObjectType *p, const uint8_t *buf) {
size_t len;
__builtin_memcpy(&len, buf, sizeof(size_t));
p->resize(len);
__builtin_memcpy(p->data(), buf + sizeof(size_t), len * sizeof(T));
}
};
template <typename T>
struct KeySerializer : public Serializer<T> {};
template <typename T>
struct ValueSerializer : public Serializer<T> {};
template<>
struct KeySerializer<uint16_t> : public Serializer<uint16_t> {
static void EncodeTo(uint8_t *buf, const uint16_t *ptr) {
uint16_t be = htobe16(*ptr); // has to be BE!
__builtin_memcpy(buf, &be, EncodeSize(ptr));
}
static void DecodeFrom(uint16_t *ptr, const uint8_t *buf) {
uint16_t h = be16toh(*buf);
*ptr = h;
}
};
template <>
struct KeySerializer<uint32_t> : public Serializer<uint32_t> {
static void EncodeTo(uint8_t *buf, const uint32_t *ptr) {
uint32_t be = htobe32(*ptr); // has to be BE!
__builtin_memcpy(buf, &be, EncodeSize(ptr));
}
static void DecodeFrom(uint32_t *ptr, const uint8_t *buf) {
uint32_t h = be32toh(*(const uint32_t *) buf);
*ptr = h;
}
};
struct InheritBasePtr {
VarStr *base = nullptr;
};
template <>
struct KeySerializer<InheritBasePtr> {}; // Shouldn't call this!
template <>
struct ValueSerializer<InheritBasePtr> : public Serializer<InheritBasePtr> {
static void EncodeTo(uint8_t *buf, const InheritBasePtr *ptr) {
if (ptr->base == nullptr) {
// Encode where you are about to encode to! Also skips the VarStr header.
auto addr = (uintptr_t) (buf - sizeof(VarStr));
__builtin_memcpy(buf, &addr, sizeof(uintptr_t));
} else {
__builtin_memcpy(buf, &ptr->base, sizeof(uintptr_t));
}
}
static void DecodeFrom(InheritBasePtr *ptr, const uint8_t *buf) {
if (ptr->base != nullptr) {
__builtin_memcpy(&ptr->base, buf, sizeof(uintptr_t));
}
}
};
template <typename Base>
class Object : public Base {
public:
using Base::Base;
Object(const Base &b) : Base(b) {}
VarStr *Encode() const {
VarStr *str = VarStr::New(this->EncodeSize());
// this->EncodeTo((uint8_t *) str + sizeof(VarStr));
this->EncodeTo(str->data());
return str;
}
VarStr *EncodeToPtr(void *ptr) const {
VarStr *str = VarStr::FromPtr(ptr, this->EncodeSize());
// this->EncodeTo((uint8_t *) str + sizeof(VarStr));
this->EncodeTo(str->data());
return str;
}
VarStr *EncodeToPtrOrDefault(void *ptr) const {
if (ptr) return EncodeToPtr(ptr);
else return Encode();
}
VarStrView EncodeView(void *ptr) const {
VarStrView v(this->EncodeSize(), (uint8_t *) ptr);
this->EncodeTo((uint8_t *) ptr);
return v;
}
VarStrView EncodeViewRoutine() const {
void *base_ptr = mem::AllocFromRoutine(VarStr::NewSize(this->EncodeSize()));
return EncodeView(base_ptr);
}
void Decode(const VarStr *str) {
this->DecodeFrom(str->data());
}
void DecodeView(const VarStrView &view) {
this->DecodeFrom(view.data());
}
};
template <typename Base>
struct Serializer<Object<Base>> {
static size_t EncodeSize(const Object<Base> *ptr) {
return ptr->EncodeSize();
}
static void EncodeTo(uint8_t *buf, const Object<Base> *ptr) {
ptr->EncodeTo(buf);
}
static void DecodeFrom(Object<Base> *ptr, const uint8_t *buf) {
ptr->DecodeFrom(buf);
}
};
template <int N> class FieldValue {};
template <template <typename> class FieldSerializer, int N>
class Field : public Field<FieldSerializer, N - 1>, public FieldValue<N> {
typedef Field<FieldSerializer, N - 1> PreviousFields;
typedef typename FieldValue<N>::Type ImplType;
typedef FieldSerializer<ImplType> Impl;
ImplType *pointer() { return FieldValue<N>::ptr(); }
const ImplType *pointer() const { return FieldValue<N>::ptr(); }
protected:
Field() {}
template <int K>
using FieldType = Field<FieldSerializer, K>;
public:
static constexpr int kFieldOffset = PreviousFields::kFieldOffset + 1;
static constexpr int kOffset = N;
template <typename T>
struct FieldBuilder : public FieldValue<N>::template Builder<typename Field<FieldSerializer, N + 1>::template FieldBuilder<T>, T> {};
size_t EncodeSize() const {
return PreviousFields::EncodeSize() + Impl::EncodeSize(pointer());
}
uint8_t *EncodeTo(uint8_t *buf) const {
buf = PreviousFields::EncodeTo(buf);
Impl::EncodeTo(buf, pointer());
return buf + Impl::EncodeSize(pointer());
}
const uint8_t *DecodeFrom(const uint8_t *buf) {
buf = PreviousFields::DecodeFrom(buf);
Impl::DecodeFrom(pointer(), buf);
return buf + Impl::EncodeSize(pointer());
}
};
// Gap Fields. For example, 0 is the first gap fields
template <template <typename> class FieldSerializer>
class GapField {
protected:
static constexpr int kFieldOffset = -1;
public:
template <typename T>
struct FieldBuilder {
T *obj;
Object<T> Done() { return *obj; }
void Init() {}
};
size_t EncodeSize() const { return 0; }
uint8_t *EncodeTo(uint8_t *buf) const { return buf; }
const uint8_t *DecodeFrom(const uint8_t *buf) { return buf; }
};
// First Gap
template <template <typename> class FieldSerializer>
class Field<FieldSerializer, __COUNTER__> : public GapField<FieldSerializer> {};
#define FIELD(field_type, field_name) \
template <> struct FieldValue<__COUNTER__> { \
field_type field_name; typedef field_type Type; \
Type *ptr() { return &field_name;} \
const Type *ptr() const { return &field_name;} \
template <typename NextBuilder, typename T> struct Builder { \
T *obj; \
NextBuilder _(field_type field_name) { this->obj->field_name = field_name; NextBuilder b; b.obj = obj; return b;} \
template <typename ...Args> void Init(field_type field_name, Args... args) { _(field_name).Init(args...); } \
}; \
}; \
#define DBOBJ(name, serializer) \
using name = sql::Schemas<sql::Field<serializer, __COUNTER__ - 1>>; \
template <template <typename> class FieldSerializer> \
class Field<FieldSerializer, name::kOffset + 1> : public GapField<FieldSerializer> {}; \
#define KEYS(name) DBOBJ(name, KeySerializer)
#define VALUES(name) DBOBJ(name, ValueSerializer)
#define DERIVED(name, basename, offset) using name = sql::DerivedSchemas<sql::Field<ValueSerializer, basename::kOffset - basename::kFieldOffset + offset - 1>>;
// Serializable tuples. Tuples are different than fields, because their
// members are anonymous.
template <int N, template <typename ...> class TupleField, typename T, typename ...Types>
struct TupleFieldType {
typedef typename TupleFieldType<N - 1, TupleField, Types...>::Type Type;
typedef typename TupleFieldType<N - 1, TupleField, Types...>::ValueType ValueType;
};
template <template <typename ...> class TupleField, typename T, typename ...Types>
struct TupleFieldType<0, TupleField, T, Types...> {
typedef TupleField<T, Types...> Type;
typedef T ValueType;
};
template <typename T, typename ...Types>
struct TupleField : public TupleField<Types...> {
T value;
typedef TupleField<Types...> ParentTupleFields;
TupleField() : ParentTupleFields() {}
TupleField(const T &v, const Types&... args) : value(v), ParentTupleFields(args...) {}
size_t EncodeSize() const {
return ParentTupleFields::EncodeSize() + Serializer<T>::EncodeSize(&value);
}
uint8_t *EncodeTo(uint8_t *buf) const {
buf = ParentTupleFields::EncodeTo(buf);
Serializer<T>::EncodeTo(buf, &value);
return buf + Serializer<T>::EncodeSize(&value);
}
const uint8_t *DecodeFrom(const uint8_t *buf) {
buf = ParentTupleFields::DecodeFrom(buf);
Serializer<T>::DecodeFrom(&value, buf);
return buf + Serializer<T>::EncodeSize(&value);
}
};
template <typename T>
struct TupleField<T> {
T value;
TupleField() {}
TupleField(const T &v) : value(v) {}
size_t EncodeSize() const {
return Serializer<T>::EncodeSize(&value);
}
uint8_t *EncodeTo(uint8_t *buf) const {
Serializer<T>::EncodeTo(buf, &value);
return buf + Serializer<T>::EncodeSize(&value);
}
const uint8_t *DecodeFrom(const uint8_t *buf) {
Serializer<T>::DecodeFrom(&value, buf);
return buf + Serializer<T>::EncodeSize(&value);
}
};
template <typename ...Types>
class TupleImpl : public TupleField<Types...> {
public:
using TupleField<Types...>::TupleField;
TupleImpl(const TupleField<Types...> &rhs) : TupleField<Types...>(rhs) {}
template <size_t N>
typename TupleFieldType<N, TupleField, Types...>::ValueType _() const {
return ((const typename TupleFieldType<N, TupleField, Types...>::Type *) this)->value;
}
template <size_t N>
void set(const typename TupleFieldType<N, TupleField, Types...>::ValueType &val) {
((typename TupleFieldType<N, TupleField, Types...>::Type *) this)->value = val;
}
template <size_t N>
typename TupleFieldType<N, TupleField, Types...>::ValueType get() const {
return _<N>();
}
};
template <typename LastField>
class Schemas : public Object<LastField> {
public:
Schemas() {}
using ThisType = Schemas<LastField>;
using FirstBuilder = typename LastField::template FieldType<LastField::kOffset - LastField::kFieldOffset>::template FieldBuilder<ThisType>;
template <typename ...Args>
static ThisType New(Args... args) {
ThisType o;
o.Build().Init(args...);
return o;
}
FirstBuilder Build() {
FirstBuilder b;
b.obj = this;
return b;
}
};
template <typename LastField>
class DerivedSchemas : public Object<LastField> {
public:
static_assert(std::is_base_of<InheritBasePtr, typename FieldValue<LastField::kOffset - LastField::kFieldOffset>::Type>::value);
DerivedSchemas() {
((InheritBasePtr *) this)->base = (VarStr *) 0x01;
}
};
template <typename ...Types> using Tuple = Object<TupleImpl<Types...>>;
template <typename ...Types> Tuple<Types...> MakeTuple(Types... params) { return Tuple<Types...>(params...); }
}
namespace felis {
using sql::Tuple;
}
// C++17 destructuring
namespace std {
template <typename T, typename ...Types>
struct tuple_size<sql::Object<sql::TupleImpl<T, Types...>>> {
static constexpr size_t value = sizeof...(Types) + 1;
};
template <size_t N, typename T, typename ...Types>
struct tuple_element<N, sql::Object<sql::TupleImpl<T, Types...>>> {
using type = typename sql::TupleFieldType<int(N), sql::TupleField, T, Types...>::ValueType;
};
}
#endif /* SQLTYPES_H */