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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <limits>
#include <absl/hash/hash.h>
#include "mongo/base/static_assert.h"
#include "mongo/bson/bsonelement_comparator_interface.h"
#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/bson/ordering.h"
#include "mongo/bson/timestamp.h"
#include "mongo/db/record_id.h"
#include "mongo/db/storage/key_format.h"
#include "mongo/platform/decimal128.h"
#include "mongo/util/assert_util.h"
#include <boost/container/flat_set.hpp>
namespace mongo {
namespace sbe::value {
class ValueBuilder;
}
namespace KeyString {
enum class Version : uint8_t { V0 = 0, V1 = 1, kLatestVersion = V1 };
static StringData keyStringVersionToString(Version version) {
return version == Version::V0 ? "V0" : "V1";
}
static const Ordering ALL_ASCENDING = Ordering::make(BSONObj());
// Encode the size of a RecordId binary string using up to 4 bytes, 7 bits per byte.
// This supports encoding sizes that fit into 28 bits, which largely covers the
// maximum BSON size.
static const int kRecordIdStrEncodedSizeMaxBytes = 4;
MONGO_STATIC_ASSERT(RecordId::kBigStrMaxSize < 1 << (7 * kRecordIdStrEncodedSizeMaxBytes));
/**
* Encodes info needed to restore the original BSONTypes from a KeyString. They cannot be
* stored in place since we don't want them to affect the ordering (1 and 1.0 compare as
* equal).
*/
class TypeBits {
public:
// See comments in getBuffer() about short/long encoding schemes.
static const uint8_t kMaxBytesForShortEncoding = 127;
static const uint8_t kPrefixBytes = 5;
static const uint8_t kStoredDecimalExponentBits = 6;
static const uint32_t kStoredDecimalExponentMask = (1U << kStoredDecimalExponentBits) - 1;
explicit TypeBits(Version version) : version(version) {
reset();
}
TypeBits(const TypeBits& tb)
: version(tb.version), _curBit(tb._curBit), _isAllZeros(tb._isAllZeros) {
_buf.reset();
_buf.appendBuf(tb._buf.buf(), tb._buf.len());
}
TypeBits& operator=(const TypeBits& tb);
TypeBits(TypeBits&&) = default;
TypeBits& operator=(TypeBits&&) = default;
/**
* If there are no bytes remaining, assumes AllZeros. Otherwise, reads bytes out of the
* BufReader in the format described on the getBuffer() method.
*/
void resetFromBuffer(BufReader* reader);
static TypeBits fromBuffer(Version version, BufReader* reader) {
TypeBits out(version);
out.resetFromBuffer(reader);
return out;
}
/**
* If true, no bits have been set to one. This is true if no bits have been set at all.
*/
bool isAllZeros() const {
return _isAllZeros;
}
/**
* These methods return a buffer and size which encodes all of the type bits in this
* instance.
*
* Encoded format:
* Case 1 (first byte is 0x0):
* This encodes the "AllZeros" state which represents an infinite stream of bits set
* to 0. Callers may optionally encode this case as an empty buffer if they have
* another way to mark the end of the buffer. There are no follow-up bytes.
*
* Case 2 (first byte isn't 0x0 but has high bit set to 0):
* The first byte is the only data byte. This can represent any 7-bit sequence or an
* 8-bit sequence if the 8th bit is 0, since the 8th bit is the same as the bit that
* is 1 if the first byte is the size byte. There are no follow-up bytes.
*
* Case 3 (first byte has high bit set to 1 but it's not 0x80):
* Remaining bits of first byte encode number of follow-up bytes that are data
* bytes.
*
* Case 4 (first byte is 0x80)
* The first byte is the signal byte indicating that this TypeBits is encoded with long
* encoding scheme: the next four bytes (in little endian order) represent the number of
* data bytes.
*
* Within data bytes (ie everything excluding the size byte if there is one), bits are
* packed in from low to high.
*/
const char* getBuffer() const {
if (_isAllZeros)
return ""; // Case 1: pointer to a zero byte.
if (getSize() == 1)
return getDataBuffer(); // Case 2: all bits in one byte; no size byte.
// Case 3 & 4: size byte(s) + data bytes.
return isLongEncoding() ? _buf.buf() : (getDataBuffer() - 1);
}
size_t getSize() const {
if (_isAllZeros) { // Case 1
dassert(getDataBufferLen() == 0 || getDataBuffer()[0] == 0);
return 1;
}
uint32_t rawSize = getDataBufferLen();
dassert(rawSize >= 1); // 0 should be handled as isAllZeros.
if (rawSize > kMaxBytesForShortEncoding) { // Case 4
return rawSize + kPrefixBytes;
}
if (rawSize == 1 && !(getDataBuffer()[0] & 0x80)) { // Case 2
return 1;
}
return rawSize + 1; // Case 3
}
bool isLongEncoding() const {
// TypeBits with all zeros is in short encoding regardless of the data buffer length.
return !_isAllZeros && getDataBufferLen() > kMaxBytesForShortEncoding;
}
//
// Everything below is only for use by KeyString::Builder.
//
// Note: No space is used if all bits are 0 so the most common cases should be 0x0.
static const uint8_t kString = 0x0;
static const uint8_t kSymbol = 0x1;
static const uint8_t kInt = 0x0;
static const uint8_t kLong = 0x1;
static const uint8_t kDouble = 0x2;
static const uint8_t kDecimal = 0x3; // indicates 6 more bits of typeinfo follow.
static const uint8_t kSpecialZeroPrefix = 0x3; // kNumericZero case, 3 more bits follow.
static const uint8_t kNegativeDoubleZero = 0x3; // normalized -0.0 double, either V0 or V1.
static const uint8_t kV0NegativeDoubleZero = 0x3; // legacy encoding for V0
// The following describe the initial 5 type bits for kNegativeOrDecimalZero. These bits
// encode double -0 or a 3-bit prefix (range 0 to 5) of the 15-bit decimal zero type.
static const uint8_t kV1NegativeDoubleZero = 0x18; // 0b11000
static const uint8_t kUnusedEncoding = 0x19; // 0b11001
// There are 6 * (1<<12) == 2 * (kMaxBiasedExponent + 1) == 24576 decimal zeros.
static const uint8_t kDecimalZero0xxx = 0x1a; // 0b11010 12 more exponent bits follow
static const uint8_t kDecimalZero1xxx = 0x1b; // 0b11011
static const uint8_t kDecimalZero2xxx = 0x1c; // 0b11100
static const uint8_t kDecimalZero3xxx = 0x1d; // 0b11101
static const uint8_t kDecimalZero4xxx = 0x1e; // 0b11110
static const uint8_t kDecimalZero5xxx = 0x1f; // 0b11111
void reset() {
_curBit = 0;
_isAllZeros = true;
_buf.setlen(kPrefixBytes);
}
void appendString() {
appendBit(kString);
}
void appendSymbol() {
appendBit(kSymbol);
}
void appendNumberDouble() {
appendBit(kDouble >> 1);
appendBit(kDouble & 1);
}
void appendNumberInt() {
appendBit(kInt >> 1);
appendBit(kInt & 1);
}
void appendNumberLong() {
appendBit(kLong >> 1);
appendBit(kLong & 1);
}
void appendNumberDecimal() {
appendBit(kDecimal >> 1);
appendBit(kDecimal & 1);
}
void appendZero(uint8_t zeroType);
void appendDecimalZero(uint32_t whichZero);
void appendDecimalExponent(uint8_t storedExponentBits);
class ReaderBase {
public:
virtual ~ReaderBase(){};
virtual uint8_t readStringLike() = 0;
virtual uint8_t readNumeric() = 0;
virtual uint8_t readZero() = 0;
// Given a decimal zero type between kDecimalZero0xxx and kDecimal5xxx, read the
// remaining 12 bits and return which of the 24576 decimal zeros to produce.
virtual uint32_t readDecimalZero(uint8_t zeroType) = 0;
// Reads the stored exponent bits of a non-zero decimal number.
virtual uint8_t readDecimalExponent() = 0;
protected:
ReaderBase() = default;
virtual uint8_t readBit() = 0;
};
class Reader : public ReaderBase {
public:
/**
* Passed in TypeBits must outlive this Reader instance.
*/
explicit Reader(const TypeBits& typeBits) : _curBit(0), _typeBits(typeBits) {}
~Reader() = default;
uint8_t readStringLike() final;
uint8_t readNumeric() final;
uint8_t readZero() final;
uint32_t readDecimalZero(uint8_t zeroType) final;
uint8_t readDecimalExponent() final;
protected:
uint8_t readBit() final;
private:
uint32_t _curBit;
const TypeBits& _typeBits;
};
/**
* An ExplainReader wraps a TypeBits::Reader and stores a human-readable description an about
* the TypeBits that have been retrieved. The explanation may be retrieved with getExplain().
* Note that this class is only designed to generate an explanation for a single field. To
* generate explanations for multiple fields, use multiple ExplainReaders.
*
* For diagnostic purposes only.
*/
class ExplainReader : public ReaderBase {
public:
explicit ExplainReader(ReaderBase& reader) : _reader(reader){};
~ExplainReader() = default;
uint8_t readStringLike() final;
uint8_t readNumeric() final;
uint8_t readZero() final;
uint32_t readDecimalZero(uint8_t zeroType) final;
uint8_t readDecimalExponent() final;
std::string getExplain() const {
return _explain.ss.str();
}
protected:
uint8_t readBit() final {
MONGO_UNREACHABLE;
}
private:
ReaderBase& _reader;
str::stream _explain;
};
Version version;
private:
static uint32_t readSizeFromBuffer(BufReader* reader);
void setRawSize(uint32_t size);
const char* getDataBuffer() const {
return _buf.buf() + kPrefixBytes;
}
char* getDataBuffer() {
return _buf.buf() + kPrefixBytes;
}
uint32_t getDataBufferLen() const {
return _buf.len() - kPrefixBytes;
}
void appendBit(uint8_t oneOrZero);
uint32_t _curBit;
bool _isAllZeros;
/**
* See getBuffer()/getSize() documentation for a description of how data is encoded. When
* the TypeBits size is in short encoding range(<=127), the bytes starting from the fifth
* byte are the complete TypeBits in short encoding scheme (1 size byte + data bytes). When
* the TypeBits size is in long encoding range(>127), all the bytes are used for the long
* encoding format (first byte + 4 size bytes + data bytes).
*/
// TypeBits buffers are often small and at least 5 bytes. Only pre-allocate a small amount of
// memory despite using a StackBufBuilder, which can use cheap stack space. Because TypeBits is
// allowed to be allocated dynamically on the heap, so is the owned StackBufBuilder. Lower the
// initial buffer size so that we do not pre-allocate excessively large buffers on the heap when
// TypeBits is not a stack variable.
enum { SmallStackSize = 8 };
StackBufBuilderBase<SmallStackSize> _buf;
};
/**
* Value owns a buffer that corresponds to a completely generated KeyString::Builder with the
* TypeBits appended.
*
* To optimize copy performance and space requirements of this structure, the buffer will contain
* the full KeyString with the TypeBits appended at the end.
*/
class Value {
public:
Value() : _version(Version::kLatestVersion), _ksSize(0) {}
Value(Version version, int32_t ksSize, SharedBufferFragment buffer)
: _version(version), _ksSize(ksSize), _buffer(std::move(buffer)) {
invariant(ksSize >= 0);
invariant(ksSize <= static_cast<int32_t>(_buffer.size()));
}
Value(const Value&) = default;
Value(Value&&) = default;
// Use a copy-and-swap, which prevents unnecessary allocation and deallocations.
Value& operator=(Value copy) noexcept {
_version = copy._version;
_ksSize = copy._ksSize;
std::swap(_buffer, copy._buffer);
return *this;
}
/**
* Compare with another KeyString::Value or Builder.
*/
template <class T>
int compare(const T& other) const;
int compareWithTypeBits(const Value& other) const;
/**
* Compare with another KeyString::Value or Builder, ignoring the RecordId part of both.
*/
template <class T>
int compareWithoutRecordIdLong(const T& other) const;
template <class T>
int compareWithoutRecordIdStr(const T& other) const;
// Returns the size of the stored KeyString.
size_t getSize() const {
return _ksSize;
}
// Returns whether the size of the stored KeyString is 0.
bool isEmpty() const {
return _ksSize == 0;
}
const char* getBuffer() const {
return _buffer.get();
}
// Returns the stored TypeBits.
TypeBits getTypeBits() const {
const char* buf = _buffer.get() + _ksSize;
BufReader reader(buf, _buffer.size() - _ksSize);
return TypeBits::fromBuffer(_version, &reader);
}
// Compute hash over key
uint64_t hash(uint64_t seed = 0) const {
return absl::hash_internal::CityHash64WithSeed(_buffer.get(), _buffer.size(), seed);
}
/**
* Returns a hex encoding of this key.
*/
std::string toString() const;
// Serializes this Value into a storable format with TypeBits information. The serialized
// format takes the following form:
// [keystring size][keystring encoding][typebits encoding]
void serialize(BufBuilder& buf) const {
buf.appendNum(_ksSize); // Serialize size of Keystring
buf.appendBuf(_buffer.get(), _buffer.size()); // Serialize Keystring + Typebits
}
/**
* Serializes this Value, excluding the RecordId, into a storable format with TypeBits
* information. The serialized format takes the following form:
* [keystring size][keystring encoding][typebits encoding]
*/
void serializeWithoutRecordIdLong(BufBuilder& buf) const;
void serializeWithoutRecordIdStr(BufBuilder& buf) const;
// Deserialize the Value from a serialized format.
static Value deserialize(BufReader& buf, KeyString::Version version) {
const int32_t sizeOfKeystring = buf.read<LittleEndian<int32_t>>();
const void* keystringPtr = buf.skip(sizeOfKeystring);
BufBuilder newBuf;
newBuf.appendBuf(keystringPtr, sizeOfKeystring);
auto typeBits = TypeBits::fromBuffer(version, &buf); // advances the buf
if (typeBits.isAllZeros()) {
newBuf.appendChar(0);
} else {
newBuf.appendBuf(typeBits.getBuffer(), typeBits.getSize());
}
// Note: this variable is needed to make sure that no method is called on 'newBuf'
// after a call on its 'release' method.
const size_t newBufLen = newBuf.len();
return {version, sizeOfKeystring, SharedBufferFragment(newBuf.release(), newBufLen)};
}
/// Members for Sorter
struct SorterDeserializeSettings {
SorterDeserializeSettings(Version version) : keyStringVersion(version) {}
Version keyStringVersion;
};
void serializeForSorter(BufBuilder& buf) const {
serialize(buf);
}
static Value deserializeForSorter(BufReader& buf, const SorterDeserializeSettings& settings) {
return deserialize(buf, settings.keyStringVersion);
}
// It is illegal to call this function on a value that is backed by a buffer that is shared
// elsewhere. The SharedBufferFragment cannot accurately report memory usage per individual
// Value, so we require the sorter to look at the SharedBufferFragmentBuilder's memory usage in
// aggregate and free unused memory periodically.
int memUsageForSorter() const {
invariant(!_buffer.isShared(),
"Cannot obtain memory usage from shared buffer on KeyString::Value");
return sizeof(Value) + _buffer.underlyingCapacity();
}
Value getOwned() const {
return *this;
}
void makeOwned() {}
Version getVersion() const {
return _version;
}
size_t getApproximateSize() const;
private:
Version _version;
// _ksSize is the total length that the KeyString takes up in the buffer.
int32_t _ksSize;
SharedBufferFragment _buffer;
};
enum class Discriminator {
kInclusive, // Anything to be stored in an index must use this.
kExclusiveBefore,
kExclusiveAfter,
};
enum class BuildState {
kEmpty, // Buffer is empty.
kAppendingBSONElements, // In the process of appending BSON Elements
kEndAdded, // Finished appedning BSON Elements.
kAppendedRecordID, // Finished appending a RecordID.
kAppendedTypeBits, // Finished appending a TypeBits.
kReleased // Released the buffer and so the buffer is no longer valid.
};
/**
* Encodes the kind of NumberDecimal that is stored.
*/
enum DecimalContinuationMarker {
kDCMEqualToDouble = 0x0,
kDCMHasContinuationLessThanDoubleRoundedUpTo15Digits = 0x1,
kDCMEqualToDoubleRoundedUpTo15Digits = 0x2,
kDCMHasContinuationLargerThanDoubleRoundedUpTo15Digits = 0x3
};
using StringTransformFn = std::function<std::string(StringData)>;
template <class BuilderT>
class BuilderBase {
public:
BuilderBase(Version version, Ordering ord, Discriminator discriminator)
: version(version),
_typeBits(version),
_state(BuildState::kEmpty),
_elemCount(0),
_ordering(ord),
_discriminator(discriminator) {}
BuilderBase(Version version, Ordering ord)
: BuilderBase(version, ord, Discriminator::kInclusive) {}
explicit BuilderBase(Version version)
: BuilderBase(version, ALL_ASCENDING, Discriminator::kInclusive) {}
BuilderBase(Version version, const BSONObj& obj, Ordering ord, const RecordId& recordId)
: BuilderBase(version, ord) {
resetToKey(obj, ord, recordId);
}
BuilderBase(Version version,
const BSONObj& obj,
Ordering ord,
Discriminator discriminator = Discriminator::kInclusive)
: BuilderBase(version, ord) {
resetToKey(obj, ord, discriminator);
}
BuilderBase(const BuilderBase& other)
: version(other.version),
_typeBits(other.getTypeBits()),
_state(other._state),
_elemCount(other._elemCount),
_ordering(other._ordering),
_discriminator(other._discriminator) {
resetFromBuffer(other.getBuffer(), other.getSize());
}
BuilderBase(Version version, const RecordId& rid) : BuilderBase(version) {
appendRecordId(rid);
}
/**
* Copies the data held in this buffer into a Value type that holds and owns a copy of the
* buffer.
*/
Value getValueCopy() {
_doneAppending();
// Create a new buffer that is a concatenation of the KeyString and its TypeBits.
BufBuilder newBuf(_buffer().len() + _typeBits.getSize());
newBuf.appendBuf(_buffer().buf(), _buffer().len());
if (_typeBits.isAllZeros()) {
newBuf.appendChar(0);
} else {
newBuf.appendBuf(_typeBits.getBuffer(), _typeBits.getSize());
}
// Note: this variable is needed to make sure that no method is called on 'newBuf'
// after a call on its 'release' method.
const size_t newBufLen = newBuf.len();
return {version, _buffer().len(), SharedBufferFragment(newBuf.release(), newBufLen)};
}
void appendRecordId(const RecordId& loc);
void appendTypeBits(const TypeBits& bits);
/*
* Function 'f' will be applied to all string elements contained in 'elem'.
*/
void appendBSONElement(const BSONElement& elem, const StringTransformFn& f = nullptr);
void appendBool(bool val);
void appendString(StringData val, const StringTransformFn& f = nullptr);
void appendSymbol(StringData val);
void appendNumberDouble(double num);
void appendNumberLong(long long num);
void appendNumberInt(int num);
void appendNumberDecimal(Decimal128 num);
void appendNull();
void appendUndefined();
void appendCodeWString(const BSONCodeWScope& val);
void appendBinData(const BSONBinData& data);
void appendRegex(const BSONRegEx& val);
void appendSetAsArray(const BSONElementSet& set, const StringTransformFn& f = nullptr);
void appendOID(OID oid);
void appendDate(Date_t date);
void appendTimestamp(Timestamp val);
void appendBytes(const void* source, size_t bytes);
void appendDBRef(const BSONDBRef& val);
void appendObject(const BSONObj& val, const StringTransformFn& f = nullptr);
void appendArray(const BSONArray& val, const StringTransformFn& f = nullptr);
void appendCode(StringData val);
/**
* Appends a Discriminator byte and kEnd byte to a key string.
*/
void appendDiscriminator(Discriminator discriminator);
/**
* Resets to an empty state.
* Equivalent to but faster than *this = Builder(ord, discriminator)
*/
void resetToEmpty(Ordering ord = ALL_ASCENDING,
Discriminator discriminator = Discriminator::kInclusive) {
_reinstantiateBufferIfNeeded();
_buffer().reset();
_typeBits.reset();
_elemCount = 0;
_ordering = ord;
_discriminator = discriminator;
_transition(BuildState::kEmpty);
}
void resetToKey(const BSONObj& obj, Ordering ord, const RecordId& recordId);
void resetToKey(const BSONObj& obj,
Ordering ord,
Discriminator discriminator = Discriminator::kInclusive);
void resetFromBuffer(const void* buffer, size_t size) {
_buffer().reset();
memcpy(_buffer().skip(size), buffer, size);
}
const char* getBuffer() const {
invariant(_state != BuildState::kReleased);
return _buffer().buf();
}
size_t getSize() const {
invariant(_state != BuildState::kReleased);
return _buffer().len();
}
bool isEmpty() const {
invariant(_state != BuildState::kReleased);
return _buffer().len() == 0;
}
void setTypeBits(const TypeBits& typeBits) {
invariant(_state != BuildState::kReleased);
_typeBits = typeBits;
}
const TypeBits& getTypeBits() const {
invariant(_state != BuildState::kReleased);
return _typeBits;
}
/**
* Compare with another KeyString::Value or Builder.
*/
template <class T>
int compare(const T& other) const;
/**
* Compare with another KeyString::Value or Builder, ignoring the RecordId part of both.
*/
template <class T>
int compareWithoutRecordIdLong(const T& other) const;
template <class T>
int compareWithoutRecordIdStr(const T& other) const;
/**
* @return a hex encoding of this key
*/
std::string toString() const;
/**
* Version to use for conversion to/from KeyString. V1 has different encodings for numeric
* values.
*/
const Version version;
protected:
void _appendAllElementsForIndexing(const BSONObj& obj, Discriminator discriminator);
void _appendBool(bool val, bool invert);
void _appendDate(Date_t val, bool invert);
void _appendTimestamp(Timestamp val, bool invert);
void _appendOID(OID val, bool invert);
void _appendString(StringData val, bool invert, const StringTransformFn& f);
void _appendSymbol(StringData val, bool invert);
void _appendCode(StringData val, bool invert);
void _appendCodeWString(const BSONCodeWScope& val, bool invert);
void _appendBinData(const BSONBinData& val, bool invert);
void _appendRegex(const BSONRegEx& val, bool invert);
void _appendDBRef(const BSONDBRef& val, bool invert);
void _appendArray(const BSONArray& val, bool invert, const StringTransformFn& f);
void _appendSetAsArray(const BSONElementSet& val, bool invert, const StringTransformFn& f);
void _appendObject(const BSONObj& val, bool invert, const StringTransformFn& f);
void _appendNumberDouble(double num, bool invert);
void _appendNumberLong(long long num, bool invert);
void _appendNumberInt(int num, bool invert);
void _appendNumberDecimal(Decimal128 num, bool invert);
void _appendRecordIdLong(int64_t val);
void _appendRecordIdStr(const char* val, int size);
/**
* @param name - optional, can be NULL
* if NULL, not included in encoding
* if not NULL, put in after type, before value
*/
void _appendBsonValue(const BSONElement& elem,
bool invert,
const StringData* name,
const StringTransformFn& f);
void _appendStringLike(StringData str, bool invert);
void _appendBson(const BSONObj& obj, bool invert, const StringTransformFn& f);
void _appendSmallDouble(double value, DecimalContinuationMarker dcm, bool invert);
void _appendLargeDouble(double value, DecimalContinuationMarker dcm, bool invert);
void _appendInteger(long long num, bool invert);
void _appendPreshiftedIntegerPortion(uint64_t value, bool isNegative, bool invert);
void _appendDoubleWithoutTypeBits(double num, DecimalContinuationMarker dcm, bool invert);
void _appendHugeDecimalWithoutTypeBits(Decimal128 dec, bool invert);
void _appendTinyDecimalWithoutTypeBits(Decimal128 dec, double bin, bool invert);
void _appendEnd();
template <typename T>
void _append(const T& thing, bool invert) {
_appendBytes(&thing, sizeof(thing), invert);
}
void _appendBytes(const void* source, size_t bytes, bool invert);
void _doneAppending() {
if (_state == BuildState::kAppendingBSONElements) {
appendDiscriminator(_discriminator);
}
}
void _verifyAppendingState() {
invariant(_state == BuildState::kEmpty || _state == BuildState::kAppendingBSONElements);
if (_state == BuildState::kEmpty) {
_transition(BuildState::kAppendingBSONElements);
}
}
void _transition(BuildState to) {
// We can empty at any point since it just means that we are clearing the buffer.
if (to == BuildState::kEmpty) {
_state = to;
return;
}
switch (_state) {
case BuildState::kEmpty:
invariant(to == BuildState::kAppendingBSONElements || to == BuildState::kEndAdded ||
to == BuildState::kAppendedRecordID);
break;
case BuildState::kAppendingBSONElements:
invariant(to == BuildState::kEndAdded);
break;
case BuildState::kEndAdded:
invariant(to == BuildState::kAppendedRecordID || to == BuildState::kReleased);
break;
case BuildState::kAppendedRecordID:
invariant(to == BuildState::kAppendedTypeBits || to == BuildState::kReleased ||
to == BuildState::kAppendedRecordID);
break;
case BuildState::kAppendedTypeBits:
invariant(to == BuildState::kAppendedRecordID || to == BuildState::kReleased);
break;
case BuildState::kReleased:
invariant(to == BuildState::kEmpty);
break;
default:
MONGO_UNREACHABLE;
}
_state = to;
}
bool _shouldInvertOnAppend() const {
return _ordering.get(_elemCount) == -1;
}
// Appends the TypeBits buffer to the main buffer and returns the offset of where the TypeBits
// begin
int32_t _appendTypeBits() {
_doneAppending();
// append the TypeBits.
int32_t ksSize = _buffer().len();
if (_typeBits.isAllZeros()) {
_buffer().appendChar(0);
} else {
_buffer().appendBuf(_typeBits.getBuffer(), _typeBits.getSize());
}
return ksSize;
}
auto& _buffer() {
return static_cast<BuilderT*>(this)->_buffer();
}
const auto& _buffer() const {
return static_cast<const BuilderT*>(this)->_buffer();
}
void _reinstantiateBufferIfNeeded() {
static_cast<BuilderT*>(this)->_reinstantiateBufferIfNeeded();
}
TypeBits _typeBits;
BuildState _state;
int _elemCount;
Ordering _ordering;
Discriminator _discriminator;
};
// Helper class to hold a buffer builder. This class needs to be before BuilderBase when inheriting
// to ensure the buffer is constructed first
template <typename BufferBuilderT>
class BufferHolder {
protected:
template <typename... Args>
BufferHolder(Args&&... args) : _bufferBuilder(std::forward<Args>(args)...) {}
BufferBuilderT _bufferBuilder;
};
class Builder : private BufferHolder<StackBufBuilder>, public BuilderBase<Builder> {
public:
using BuilderBase::BuilderBase;
Builder(const Builder& other) : BuilderBase(other) {}
public:
friend class BuilderBase;
StackBufBuilder& _buffer() {
return _bufferBuilder;
}
const StackBufBuilder& _buffer() const {
return _bufferBuilder;
}
void _reinstantiateBufferIfNeeded() {}
};
class HeapBuilder : private BufferHolder<BufBuilder>, public BuilderBase<HeapBuilder> {
public:
static constexpr uint8_t kHeapAllocatorDefaultBytes = 32;
// Forwarding constructor to BuilderBase
template <typename... Args>
HeapBuilder(Args&&... args)
: BufferHolder(kHeapAllocatorDefaultBytes), BuilderBase(std::forward<Args>(args)...) {}
// When copying don't allocate memory by default. Copy-constructor will request the right amount
// of memory
HeapBuilder(const HeapBuilder& other) : BufferHolder(0), BuilderBase(other) {}
/**
* Releases the data held in this buffer into a Value type, releasing and transfering ownership
* of the buffer _buffer and TypeBits _typeBits to the returned Value object from the current
* Builder.
*/
Value release() {
int32_t ksSize = _appendTypeBits();
_transition(BuildState::kReleased);
// Note: this variable is needed to make sure that no method is called on '_bufferBuilder'
// after a call on its 'release' method.
const size_t bufLen = _bufferBuilder.len();
return {version, ksSize, SharedBufferFragment(_bufferBuilder.release(), bufLen)};
}
protected:
friend class BuilderBase;
BufBuilder& _buffer() {
return _bufferBuilder;
}
const BufBuilder& _buffer() const {
return _bufferBuilder;
}
void _reinstantiateBufferIfNeeded() {
if (_state == BuildState::kReleased) {
_bufferBuilder = BufBuilder(kHeapAllocatorDefaultBytes);
}
}
};
class PooledBuilder : private BufferHolder<PooledFragmentBuilder>,
public BuilderBase<PooledBuilder> {
public:
template <typename... Args>
PooledBuilder(SharedBufferFragmentBuilder& memoryPool, Args&&... args)
: BufferHolder(memoryPool), BuilderBase(std::forward<Args>(args)...) {}
// Underlying SharedBufferFragmentBuilder can only build one buffer at the time, so copy does
// not work for the PooledBuilder.
PooledBuilder(const PooledBuilder&) = delete;
Value release() {
int32_t ksSize = _appendTypeBits();
_transition(BuildState::kReleased);
return {version, ksSize, _bufferBuilder.done()};
}
public:
friend class BuilderBase;
PooledFragmentBuilder& _buffer() {
return _bufferBuilder;
}
const PooledFragmentBuilder& _buffer() const {
return _bufferBuilder;
}
void _reinstantiateBufferIfNeeded() {}
};
/*
* The isKeyString struct allows the operators below to only be enabled if the types being operated
* on are KeyStrings.
*/
template <class T>
struct isKeyString : public std::false_type {};
template <>
struct isKeyString<Builder> : public std::true_type {};
template <>
struct isKeyString<HeapBuilder> : public std::true_type {};
template <>
struct isKeyString<PooledBuilder> : public std::true_type {};
template <>
struct isKeyString<Value> : public std::true_type {};
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator<(const T& lhs,
const U& rhs) {
return lhs.compare(rhs) < 0;
}
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator<=(const T& lhs,
const U& rhs) {
return lhs.compare(rhs) <= 0;
}
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator==(const T& lhs,
const U& rhs) {
return lhs.compare(rhs) == 0;
}
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator>(const T& lhs,
const U& rhs) {
return lhs.compare(rhs) > 0;
}
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator>=(const T& lhs,
const U& rhs) {
return lhs.compare(rhs) >= 0;
}
template <class T, class U>
inline typename std::enable_if<isKeyString<T>::value, bool>::type operator!=(const T& lhs,
const U& rhs) {
return !(lhs == rhs);
}
template <class T>
inline typename std::enable_if<isKeyString<T>::value, std::ostream&>::type operator<<(
std::ostream& stream, const T& value) {
return stream << value.toString();
}
/**
* Given a KeyString which may or may not have a RecordId, returns the length of the section without
* the RecordId. More expensive than sizeWithoutRecordId(Long|Str)AtEnd
*/
size_t getKeySize(const char* buffer, size_t len, Ordering ord, const TypeBits& typeBits);
/**
* Decodes the given KeyString buffer into it's BSONObj representation. This is marked as
* noexcept since the assumption is that 'buffer' is a valid KeyString buffer and this method
* is not expected to throw.
*
* If the buffer provided may not be valid, use the 'safe' version instead.
*/
BSONObj toBson(StringData data, Ordering ord, const TypeBits& types);
BSONObj toBson(const char* buffer, size_t len, Ordering ord, const TypeBits& types) noexcept;
BSONObj toBsonSafe(const char* buffer, size_t len, Ordering ord, const TypeBits& types);
void toBsonSafe(
const char* buffer, size_t len, Ordering ord, const TypeBits& types, BSONObjBuilder& builder);
Discriminator decodeDiscriminator(const char* buffer,
size_t len,
Ordering ord,
const TypeBits& typeBits);
template <class T>
BSONObj toBson(const T& keyString, Ordering ord) noexcept {
return toBson(keyString.getBuffer(), keyString.getSize(), ord, keyString.getTypeBits());
}
/**
* Decodes a RecordId long from the end of a buffer.
*/
RecordId decodeRecordIdLongAtEnd(const void* buf, size_t size);
/**
* Decodes a RecordId string from the end of a buffer.
* The RecordId string length cannot be determined by looking at the start of the string.
*/
RecordId decodeRecordIdStrAtEnd(const void* buf, size_t size);
/**
* Given a KeyString with a RecordId in the long format, returns the length of the section without
* the RecordId.
*/
size_t sizeWithoutRecordIdLongAtEnd(const void* bufferRaw, size_t bufSize);
/**
* Given a KeyString with a RecordId in the string format, returns the length of the section without
* the RecordId.
*/
size_t sizeWithoutRecordIdStrAtEnd(const void* bufferRaw, size_t bufSize);
/**
* Decodes a RecordId, consuming all bytes needed from reader.
*/
RecordId decodeRecordIdLong(BufReader* reader);
int compare(const char* leftBuf, const char* rightBuf, size_t leftSize, size_t rightSize);
/**
* Read one KeyString component from the given 'reader' and 'typeBits' inputs and stream it to the
* 'valueBuilder' object, which converts it to a "Slot-Based Execution" (SBE) representation. When
* no components remain in the KeyString, this function returns false and leaves 'valueBuilder'
* unmodified.
*/
bool readSBEValue(BufReader* reader,
TypeBits::ReaderBase* typeBits,
bool inverted,
Version version,
sbe::value::ValueBuilder* valueBuilder);
/*
* Appends the first field of a key string to a BSON object.
* This does not accept TypeBits because callers of this function discard TypeBits.
*/
void appendSingleFieldToBSONAs(const char* buf,
int len,
StringData fieldName,
BSONObjBuilder* builder,
Version version = KeyString::Version::kLatestVersion);
template <class BufferT>
template <class T>
int BuilderBase<BufferT>::compare(const T& other) const {
return KeyString::compare(getBuffer(), other.getBuffer(), getSize(), other.getSize());
}
template <class BufferT>
template <class T>
int BuilderBase<BufferT>::compareWithoutRecordIdLong(const T& other) const {
return KeyString::compare(
getBuffer(),
other.getBuffer(),
!isEmpty() ? sizeWithoutRecordIdLongAtEnd(getBuffer(), getSize()) : 0,
!other.isEmpty() ? sizeWithoutRecordIdLongAtEnd(other.getBuffer(), other.getSize()) : 0);
}
template <class BufferT>
template <class T>
int BuilderBase<BufferT>::compareWithoutRecordIdStr(const T& other) const {
return KeyString::compare(
getBuffer(),
other.getBuffer(),
!isEmpty() ? sizeWithoutRecordIdStrAtEnd(getBuffer(), getSize()) : 0,
!other.isEmpty() ? sizeWithoutRecordIdStrAtEnd(other.getBuffer(), other.getSize()) : 0);
}
template <class T>
int Value::compare(const T& other) const {
return KeyString::compare(getBuffer(), other.getBuffer(), getSize(), other.getSize());
}
template <class T>
int Value::compareWithoutRecordIdLong(const T& other) const {
return KeyString::compare(
getBuffer(),
other.getBuffer(),
!isEmpty() ? sizeWithoutRecordIdLongAtEnd(getBuffer(), getSize()) : 0,
!other.isEmpty() ? sizeWithoutRecordIdLongAtEnd(other.getBuffer(), other.getSize()) : 0);
}
template <class T>
int Value::compareWithoutRecordIdStr(const T& other) const {
return KeyString::compare(
getBuffer(),
other.getBuffer(),
!isEmpty() ? sizeWithoutRecordIdStrAtEnd(getBuffer(), getSize()) : 0,
!other.isEmpty() ? sizeWithoutRecordIdStrAtEnd(other.getBuffer(), other.getSize()) : 0);
}
/**
* Takes key string and key pattern information and uses it to present human-readable information
* about an index or collection entry.
*
* 'logPrefix' addes a logging prefix. Useful for differentiating callers.
*/
void logKeyString(const RecordId& recordId,
const Value& keyStringValue,
const BSONObj& keyPatternBson,
const BSONObj& keyStringBson,
std::string callerLogPrefix);
BSONObj rehydrateKey(const BSONObj& keyPatternBson, const BSONObj& keyStringBson);
/**
* Returns a human-readable output that explains each byte within the key string. For diagnostic
* purposes only.
*
* If 'keyPattern' is empty or does not have as many fields as there are in the key string, fields
* will be assumed to be ascending and will be assigned field names as empty string.
* 'keyFormat' may be provided if the caller knows the RecordId format of this key string, if
* any.
*/
std::string explain(const char* buffer,
int len,
const BSONObj& keyPattern,
const TypeBits& typeBits,
boost::optional<KeyFormat> keyFormat);
} // namespace KeyString
using KeyStringSet = boost::container::flat_set<KeyString::Value>;
} // namespace mongo
|