forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_field_operator.cpp
More file actions
678 lines (561 loc) · 23.5 KB
/
encoder_field_operator.cpp
File metadata and controls
678 lines (561 loc) · 23.5 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Copyright (c) 2016, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
// See the file license.txt for licensing information.
#include <iterator>
#include <algorithm>
#include "../common/codec_helper.h"
#include "fast_ostream.h"
#include "encoder_presence_map.h"
#include "encoder_field_operator.h"
#include "fast_ostream_inserter.h"
namespace mfast {
void encoder_field_operator::encode(const int32_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const uint32_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const int64_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const uint64_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const ascii_string_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const unicode_string_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const byte_vector_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
void encoder_field_operator::encode(const decimal_cref & /* cref */,
fast_ostream & /* stream */,
encoder_presence_map & /* pmap */) const {}
namespace encoder_detail {
template <typename Operator> struct decimal_encoder {
void encode_decimal(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const {
const Operator *derived = static_cast<const Operator *>(this);
if (!cref.has_individual_operators())
derived->encode_impl(cref, stream, pmap);
else {
derived->encode_impl(cref.get_exponent(), stream, pmap);
if (cref.present()) {
int64_cref mantissa_cref = cref.get_mantissa();
const encoder_field_operator *mantissa_operator =
encoder_operators[mantissa_cref.instruction()->field_operator()];
mantissa_operator->encode(mantissa_cref, stream, pmap);
}
}
}
};
class no_operator : public encoder_field_operator,
public mfast::detail::codec_helper,
public decimal_encoder<no_operator> {
public:
no_operator() {}
template <typename T>
void encode_impl(const T &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const {
if (cref.absent())
{
stream.encode_null();
}
else
{
stream << cref;
}
// Fast Specification 1.1, page 22
//
// If a field is mandatory and has no field operator, it will not occupy any
// bit in the presence map and its value must always appear in the stream.
//
// If a field is optional and has no field operator, it is encoded with a
// nullable representation and the NULL is used to represent absence of a
// value. It will not occupy any bits in the presence map.
stream.save_previous_value(cref);
}
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_decimal(cref, stream, pmap);
}
};
class constant_operator : public encoder_field_operator,
public mfast::detail::codec_helper,
public decimal_encoder<constant_operator> {
public:
constant_operator() {}
template <typename T>
void encode_impl(const T &cref, fast_ostream &stream,
encoder_presence_map &pmap) const {
// A field will not occupy any bit in the presence map if it is mandatory
// and has the constant operator.
// An optional field with the constant operator will occupy a single bit. If
// the bit is set, the value
// is the initial value in the instruction context. If the bit is not set,
// the value is considered absent.
if (cref.optional()) {
pmap.set_next_bit(cref.present());
}
stream.save_previous_value(cref);
}
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_decimal(cref, stream, pmap);
}
};
template <typename Operation>
class copy_or_increment_operator_impl : public mfast::detail::codec_helper {
public:
copy_or_increment_operator_impl() {}
template <typename T>
void encode_impl(const T &cref, fast_ostream &stream,
encoder_presence_map &pmap) const {
value_storage previous = previous_value_of(cref);
if (!previous.is_defined()) {
// if the previous value is undefined – the value of the field is the
// initial value
// that also becomes the new previous value.
// If the field has optional presence and no initial value, the field is
// considered
// absent and the state of the previous value is changed to empty.
if (cref.is_initial_value()) {
stream.save_previous_value(cref);
pmap.set_next_bit(false);
return;
}
} else if (previous.is_empty()) {
// if the previous value is empty – the value of the field is empty.
// If the field is optional the value is considered absent.
if (cref.absent()) {
stream.save_previous_value(cref);
pmap.set_next_bit(false);
return;
} else if (!cref.optional()) {
// It is a dynamic error [ERR D6] if the field is mandatory.
BOOST_THROW_EXCEPTION(fast_dynamic_error("D6"));
// We need to handle this case because the previous value may have been
// modified by another instruction with the same key and that intruction
// has optional presence.
}
} else if (Operation()(cref, previous)) {
stream.save_previous_value(cref);
pmap.set_next_bit(false);
return;
}
stream.save_previous_value(cref);
pmap.set_next_bit(true);
stream << cref;
}
};
struct is_same {
template <typename T>
bool operator()(const int_cref<T> &v, const value_storage &prev) const {
return v.absent() == prev.is_empty() && v.value() == prev.get<T>();
}
bool operator()(const exponent_cref &v, const value_storage &prev) const {
return v.absent() == prev.is_empty() &&
v.value() == prev.of_decimal.exponent_;
}
bool operator()(const decimal_cref &v, const value_storage &prev) const {
return v.absent() == prev.is_empty() &&
v.mantissa() == prev.of_decimal.mantissa_ &&
v.exponent() == prev.of_decimal.exponent_;
}
template <typename T>
bool operator()(const vector_cref<T> &v, const value_storage &prev) const {
return v.size() == prev.of_array.len_ - 1 &&
memcmp(v.data(), prev.of_array.content_, v.size() * sizeof(T)) == 0;
}
};
class copy_operator : public encoder_field_operator,
public copy_or_increment_operator_impl<is_same>,
public decimal_encoder<copy_operator> {
public:
copy_operator() {}
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_decimal(cref, stream, pmap);
}
};
struct is_increment {
template <typename T>
bool operator()(const T &v, const value_storage &previous) const {
return v.value() == previous.get<const typename T::value_type>() + 1;
}
};
class increment_operator : public encoder_field_operator,
copy_or_increment_operator_impl<is_increment> {
public:
increment_operator() {}
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
};
class default_operator : public encoder_field_operator,
public mfast::detail::codec_helper,
public decimal_encoder<default_operator> {
public:
default_operator() {}
template <typename T>
void encode_impl(const T &cref, fast_ostream &stream,
encoder_presence_map &pmap) const {
// Mandatory integer, decimal, string and byte vector fields – one bit. If
// set, the value appears in the stream.
// Optional integer, decimal, string and byte vector fields – one bit. If
// set, the value appears in the stream in a nullable representation.
// The default operator specifies that the value of a field is either
// present in the stream
// or it will be the initial value.
// If the field has optional presence and no initial value, the field is
// considered absent
// when there is no value in the stream.
if (cref.is_initial_value()) {
pmap.set_next_bit(false);
stream.save_previous_value(cref);
return;
}
pmap.set_next_bit(true);
if (cref.absent()) {
// A NULL indicates that the value is absent and the state of the
// previous value is left unchanged.
stream.encode_null();
} else {
stream << cref;
stream.save_previous_value(cref);
}
}
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_decimal(cref, stream, pmap);
}
};
class delta_operator : public encoder_field_operator,
public mfast::detail::codec_helper {
template <typename T>
void encode_integer(const T &cref, fast_ostream &stream) const {
if (cref.absent()) {
// If the field has optional presence, the delta value can be NULL. In
// that case the value of the field is considered absent.
stream.encode_null();
} else {
value_storage bv = delta_base_value_of(cref);
T base(&bv, nullptr);
int64_t delta = static_cast<int64_t>(cref.value() - base.value());
stream.encode(delta, false, cref.instruction()->is_nullable());
stream.save_previous_value(cref);
}
}
template <typename T>
void encode_string(const T &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const {
if (cref.absent()) {
stream.encode_null();
return;
}
const value_storage &prev = delta_base_value_of(cref);
T prev_cref(&prev, cref.instruction());
typedef typename T::const_iterator const_iterator;
typedef typename T::const_reverse_iterator const_reverse_iterator;
typedef typename std::iterator_traits<const_iterator>::difference_type
difference_type;
std::pair<const_iterator, const_iterator> common_prefix_positions =
std::mismatch(cref.begin(), cref.end(), prev_cref.begin());
std::pair<const_reverse_iterator, const_reverse_iterator>
common_suffix_positions =
std::mismatch(cref.rbegin(), cref.rend(), prev_cref.rbegin());
int32_t substraction_len;
const_iterator delta_iterator;
uint32_t delta_len;
difference_type common_prefix_delta_len =
cref.end() - common_prefix_positions.first;
difference_type common_suffix_delta_len =
cref.rend() - common_suffix_positions.first;
if (common_prefix_delta_len <= common_suffix_delta_len) {
substraction_len = static_cast<int32_t>(prev_cref.end() -
common_prefix_positions.second);
delta_iterator = common_prefix_positions.first;
delta_len = static_cast<uint32_t>(common_prefix_delta_len);
} else {
// Characters are removed from the front when the subtraction length is
// negative.
// The subtraction length uses an excess-1 encoding: if the value is
// negative when decoding,
// it is incremented by one to get the number of characters to subtract.
// This makes it possible
// to encode negative zero as -1,
substraction_len = static_cast<int32_t>(
~(prev_cref.rend() - common_suffix_positions.second));
delta_iterator = cref.begin();
delta_len = static_cast<uint32_t>(common_suffix_delta_len);
}
stream.encode(substraction_len, false, cref.instruction()->is_nullable());
stream.encode(delta_iterator, delta_len, cref.instruction(), false);
stream.save_previous_value(cref);
}
public:
delta_operator(){};
virtual void encode(const int32_cref &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const override {
this->encode_integer(cref, stream);
}
virtual void encode(const uint32_cref &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const override {
encode_integer(cref, stream);
}
virtual void encode(const int64_cref &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const override {
encode_integer(cref, stream);
}
virtual void encode(const uint64_cref &cref, fast_ostream &stream,
encoder_presence_map & /* pmap */) const override {
encode_integer(cref, stream);
}
virtual void encode(const decimal_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
if (!cref.has_individual_operators()) {
if (cref.present()) {
value_storage bv = delta_base_value_of(cref);
value_storage delta_storage;
delta_storage.of_decimal.exponent_ =
cref.exponent() - bv.of_decimal.exponent_;
delta_storage.of_decimal.mantissa_ =
cref.mantissa() - bv.of_decimal.mantissa_;
delta_storage.of_decimal.present_ = cref.present();
decimal_cref delta(&delta_storage, cref.instruction());
stream << delta;
stream.save_previous_value(cref);
} else {
stream.encode_null();
}
} else {
encode_integer(cref.get_exponent(), stream);
if (cref.present()) {
int64_cref mantissa_cref = cref.get_mantissa();
const encoder_field_operator *mantissa_operator =
encoder_operators[mantissa_cref.instruction()->field_operator()];
mantissa_operator->encode(mantissa_cref, stream, pmap);
}
}
}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_string(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_string(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_string(cref, stream, pmap);
}
};
class tail_operator : public encoder_field_operator,
public mfast::detail::codec_helper {
private:
template <typename T>
void encode_impl(const T &cref, fast_ostream &stream,
encoder_presence_map &pmap) const {
value_storage &prev = previous_value_of(cref);
// if (cref.absent()) {
// if (!prev.is_defined() || prev.is_empty()) {
// pmap.set_next_bit(false);
// }
// else {
// pmap.set_next_bit(true);
// stream.encode_null();
// }
// }
// else
if (is_same()(cref, tail_base_value_of(cref))) {
pmap.set_next_bit(false);
} else if (cref.absent()) {
if (prev.is_defined() && prev.is_empty()) {
pmap.set_next_bit(false);
} else {
pmap.set_next_bit(true);
stream.encode_null();
}
} else {
pmap.set_next_bit(true);
uint32_t tail_len;
typedef typename T::const_iterator const_iterator;
const_iterator tail_itr;
value_storage base = tail_base_value_of(cref);
T base_cref(&base, cref.instruction());
if (cref.size() == base_cref.size()) {
std::pair<const_iterator, const_iterator> positions =
std::mismatch(cref.begin(), cref.end(), base_cref.begin());
tail_itr = positions.first;
tail_len = static_cast<uint32_t>(cref.end() - positions.first);
} else {
tail_itr = cref.begin();
tail_len = static_cast<uint32_t>(cref.size());
}
stream.encode(tail_itr, tail_len, cref.instruction(),
cref.instruction()->is_nullable());
}
stream.save_previous_value(cref);
}
public:
tail_operator() {}
virtual void encode(const ascii_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const unicode_string_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
virtual void encode(const byte_vector_cref &cref, fast_ostream &stream,
encoder_presence_map &pmap) const override {
encode_impl(cref, stream, pmap);
}
};
static const no_operator no_operator_instance;
static const constant_operator constant_operator_instance;
static const copy_operator copy_operator_instance;
static const default_operator default_operator_instance;
static const delta_operator delta_operator_instance;
static const increment_operator increment_operator_instance;
static const tail_operator tail_operator_instance;
}
const encoder_field_operator *const encoder_operators[] = {
&encoder_detail::no_operator_instance,
&encoder_detail::constant_operator_instance,
&encoder_detail::delta_operator_instance,
&encoder_detail::default_operator_instance,
&encoder_detail::copy_operator_instance,
&encoder_detail::increment_operator_instance,
&encoder_detail::tail_operator_instance};
}