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
|
/**
* 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 <boost/optional.hpp>
#include <memory>
#include "mongo/bson/bsonelement_comparator.h"
#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/db/exec/document_value/value.h"
#include "mongo/db/matcher/expression.h"
#include "mongo/db/matcher/expression_path.h"
#include "mongo/db/query/collation/collator_interface.h"
#include "mongo/db/query/util/make_data_structure.h"
#include "mongo/stdx/unordered_map.h"
namespace pcrecpp {
class RE;
} // namespace pcrecpp
namespace mongo {
class CollatorInterface;
/**
* A struct primarily used to make input parameters for makePredicate() function. The
* 'MatchExprType' helps in determining which MatchExpression to make in makePredicate() function.
*/
template <typename MatchExprType, typename ValueType = BSONElement>
struct MatchExprPredicate {
MatchExprPredicate(StringData path_, ValueType value_) : path(path_), value(value_){};
StringData path;
ValueType value;
};
/**
* Helper function to make $and predicates based on the set of predicates passed as parameters.
*/
template <typename T, typename ValueType, typename... Targs, typename... ValueTypeArgs>
std::unique_ptr<MatchExpression> makePredicate(
MatchExprPredicate<T, ValueType> predicate,
MatchExprPredicate<Targs, ValueTypeArgs>... predicates) {
if constexpr (sizeof...(predicates) > 0) {
return std::make_unique<AndMatchExpression>(makeVector<std::unique_ptr<MatchExpression>>(
std::make_unique<T>(predicate.path, predicate.value),
(std::make_unique<Targs>(predicates.path, predicates.value))...));
} else {
return std::make_unique<T>(predicate.path, predicate.value);
}
}
class LeafMatchExpression : public PathMatchExpression {
public:
LeafMatchExpression(MatchType matchType,
StringData path,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: LeafMatchExpression(matchType,
path,
ElementPath::LeafArrayBehavior::kTraverse,
ElementPath::NonLeafArrayBehavior::kTraverse,
std::move(annotation)) {}
LeafMatchExpression(MatchType matchType,
StringData path,
ElementPath::LeafArrayBehavior leafArrBehavior,
ElementPath::NonLeafArrayBehavior nonLeafArrBehavior,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: PathMatchExpression(
matchType, path, leafArrBehavior, nonLeafArrBehavior, std::move(annotation)) {}
virtual ~LeafMatchExpression() = default;
size_t numChildren() const override {
return 0;
}
MatchExpression* getChild(size_t i) const override {
MONGO_UNREACHABLE;
}
std::vector<std::unique_ptr<MatchExpression>>* getChildVector() final {
return nullptr;
}
MatchCategory getCategory() const override {
return MatchCategory::kLeaf;
}
};
/**
* Base class for comparison-like match expression nodes. This includes both the comparison nodes in
* the match language ($eq, $gt, $gte, $lt, and $lte), as well as internal comparison nodes like
* $_internalExprEq.
*/
class ComparisonMatchExpressionBase : public LeafMatchExpression {
public:
static bool isEquality(MatchType matchType) {
switch (matchType) {
case MatchExpression::EQ:
case MatchExpression::INTERNAL_EXPR_EQ:
return true;
default:
return false;
}
}
static bool isInternalExprComparison(MatchType matchType) {
switch (matchType) {
case MatchExpression::INTERNAL_EXPR_EQ:
case MatchExpression::INTERNAL_EXPR_GT:
case MatchExpression::INTERNAL_EXPR_GTE:
case MatchExpression::INTERNAL_EXPR_LT:
case MatchExpression::INTERNAL_EXPR_LTE:
return true;
default:
return false;
}
}
ComparisonMatchExpressionBase(MatchType type,
StringData path,
Value rhs,
ElementPath::LeafArrayBehavior,
ElementPath::NonLeafArrayBehavior,
clonable_ptr<ErrorAnnotation> annotation = nullptr,
const CollatorInterface* collator = nullptr);
virtual ~ComparisonMatchExpressionBase() = default;
virtual void debugString(StringBuilder& debug, int indentationLevel = 0) const;
BSONObj getSerializedRightHandSide() const final;
virtual bool equivalent(const MatchExpression* other) const;
/**
* Returns the name of this MatchExpression.
*/
virtual StringData name() const = 0;
const BSONElement& getData() const {
return _rhs;
}
/**
* Replaces the RHS element of this expression. The caller is responsible for ensuring that the
* BSONObj backing 'elem' outlives this MatchExpression.
*/
void setData(BSONElement elem) {
// TODO SERVER-50629: Ensure that the _backingBSON is consistent with the new element.
_rhs = elem;
}
const CollatorInterface* getCollator() const {
return _collator;
}
protected:
/**
* 'collator' must outlive the ComparisonMatchExpression and any clones made of it.
*/
void _doSetCollator(const CollatorInterface* collator) final {
_collator = collator;
}
// BSON which holds the data referenced by _rhs.
BSONObj _backingBSON;
BSONElement _rhs;
// Collator used to compare elements. By default, simple binary comparison will be used.
const CollatorInterface* _collator = nullptr;
private:
ExpressionOptimizerFunc getOptimizer() const final {
return [](std::unique_ptr<MatchExpression> expression) { return expression; };
}
};
/**
* EQ, LTE, LT, GT, GTE subclass from ComparisonMatchExpression.
*/
class ComparisonMatchExpression : public ComparisonMatchExpressionBase {
public:
/**
* Returns true if the MatchExpression is a ComparisonMatchExpression.
*/
static bool isComparisonMatchExpression(const MatchExpression* expr) {
switch (expr->matchType()) {
case MatchExpression::LT:
case MatchExpression::LTE:
case MatchExpression::EQ:
case MatchExpression::GTE:
case MatchExpression::GT:
return true;
default:
return false;
}
}
ComparisonMatchExpression(MatchType type,
StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr,
const CollatorInterface* collator = nullptr);
virtual ~ComparisonMatchExpression() = default;
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
};
class EqualityMatchExpression final : public ComparisonMatchExpression {
public:
static constexpr StringData kName = "$eq"_sd;
EqualityMatchExpression(StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr,
const CollatorInterface* collator = nullptr)
: ComparisonMatchExpression(EQ, path, std::move(rhs), std::move(annotation), collator) {}
EqualityMatchExpression(StringData path,
const BSONElement& rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr,
const CollatorInterface* collator = nullptr)
: ComparisonMatchExpression(EQ, path, Value(rhs), std::move(annotation), collator) {}
StringData name() const final {
return kName;
}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ComparisonMatchExpression> e =
std::make_unique<EqualityMatchExpression>(path(), Value(getData()), _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
e->setCollator(_collator);
return e;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class LTEMatchExpression final : public ComparisonMatchExpression {
public:
static constexpr StringData kName = "$lte"_sd;
LTEMatchExpression(StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(LTE, path, std::move(rhs), std::move(annotation)) {}
LTEMatchExpression(StringData path,
const BSONElement& rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(LTE, path, Value(rhs), std::move(annotation)) {}
StringData name() const final {
return kName;
}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ComparisonMatchExpression> e =
std::make_unique<LTEMatchExpression>(path(), _rhs, _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
e->setCollator(_collator);
return e;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class LTMatchExpression final : public ComparisonMatchExpression {
public:
static constexpr StringData kName = "$lt"_sd;
LTMatchExpression(StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(LT, path, std::move(rhs), std::move(annotation)) {}
LTMatchExpression(StringData path,
const BSONElement& rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(LT, path, Value(rhs), std::move(annotation)) {}
StringData name() const final {
return kName;
}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ComparisonMatchExpression> e =
std::make_unique<LTMatchExpression>(path(), _rhs, _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
e->setCollator(_collator);
return e;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
bool isLTMaxKey() const final {
return _rhs.type() == BSONType::MaxKey;
}
};
class GTMatchExpression final : public ComparisonMatchExpression {
public:
static constexpr StringData kName = "$gt"_sd;
GTMatchExpression(StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(GT, path, std::move(rhs), std::move(annotation)) {}
GTMatchExpression(StringData path,
const BSONElement& rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(GT, path, Value(rhs), std::move(annotation)) {}
StringData name() const final {
return kName;
}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ComparisonMatchExpression> e =
std::make_unique<GTMatchExpression>(path(), _rhs, _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
e->setCollator(_collator);
return e;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
bool isGTMinKey() const final {
return _rhs.type() == BSONType::MinKey;
}
};
class GTEMatchExpression final : public ComparisonMatchExpression {
public:
static constexpr StringData kName = "$gte"_sd;
GTEMatchExpression(StringData path,
Value rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(GTE, path, std::move(rhs), std::move(annotation)) {}
GTEMatchExpression(StringData path,
const BSONElement& rhs,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: ComparisonMatchExpression(GTE, path, Value(rhs), std::move(annotation)) {}
StringData name() const final {
return kName;
}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ComparisonMatchExpression> e =
std::make_unique<GTEMatchExpression>(path(), _rhs, _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
e->setCollator(_collator);
return e;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class RegexMatchExpression : public LeafMatchExpression {
public:
static const std::set<char> kValidRegexFlags;
static std::unique_ptr<pcrecpp::RE> makeRegex(const std::string& regex,
const std::string& flags);
RegexMatchExpression(StringData path, Value e, clonable_ptr<ErrorAnnotation> annotation)
: RegexMatchExpression(path, e.getRegex(), e.getRegexFlags(), std::move(annotation)) {}
RegexMatchExpression(StringData path,
const BSONElement& e,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: RegexMatchExpression(path, Value(e), annotation) {}
RegexMatchExpression(StringData path,
StringData regex,
StringData options,
clonable_ptr<ErrorAnnotation> annotation = nullptr);
~RegexMatchExpression();
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<RegexMatchExpression> e =
std::make_unique<RegexMatchExpression>(path(), _regex, _flags, _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
return e;
}
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
virtual void debugString(StringBuilder& debug, int indentationLevel) const;
BSONObj getSerializedRightHandSide() const final;
void serializeToBSONTypeRegex(BSONObjBuilder* out) const;
void shortDebugString(StringBuilder& debug) const;
virtual bool equivalent(const MatchExpression* other) const;
const std::string& getString() const {
return _regex;
}
const std::string& getFlags() const {
return _flags;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
private:
ExpressionOptimizerFunc getOptimizer() const final {
return [](std::unique_ptr<MatchExpression> expression) { return expression; };
}
void _init();
std::string _regex;
std::string _flags;
std::unique_ptr<pcrecpp::RE> _re;
};
class ModMatchExpression : public LeafMatchExpression {
public:
ModMatchExpression(StringData path,
long long divisor,
long long remainder,
clonable_ptr<ErrorAnnotation> annotation = nullptr);
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ModMatchExpression> m =
std::make_unique<ModMatchExpression>(path(), _divisor, _remainder, _errorAnnotation);
if (getTag()) {
m->setTag(getTag()->clone());
}
return m;
}
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
virtual void debugString(StringBuilder& debug, int indentationLevel) const;
BSONObj getSerializedRightHandSide() const final;
virtual bool equivalent(const MatchExpression* other) const;
long long getDivisor() const {
return _divisor;
}
long long getRemainder() const {
return _remainder;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
private:
ExpressionOptimizerFunc getOptimizer() const final {
return [](std::unique_ptr<MatchExpression> expression) { return expression; };
}
long long _divisor;
long long _remainder;
};
class ExistsMatchExpression : public LeafMatchExpression {
public:
explicit ExistsMatchExpression(StringData path,
clonable_ptr<ErrorAnnotation> annotation = nullptr);
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<ExistsMatchExpression> e =
std::make_unique<ExistsMatchExpression>(path(), _errorAnnotation);
if (getTag()) {
e->setTag(getTag()->clone());
}
return e;
}
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
virtual void debugString(StringBuilder& debug, int indentationLevel) const;
BSONObj getSerializedRightHandSide() const final;
virtual bool equivalent(const MatchExpression* other) const;
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
private:
ExpressionOptimizerFunc getOptimizer() const final {
return [](std::unique_ptr<MatchExpression> expression) { return expression; };
}
};
/**
* query operator: $in
*/
class InMatchExpression : public LeafMatchExpression {
public:
explicit InMatchExpression(StringData path, clonable_ptr<ErrorAnnotation> annotation = nullptr);
virtual std::unique_ptr<MatchExpression> shallowClone() const;
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
virtual void debugString(StringBuilder& debug, int indentationLevel) const;
BSONObj getSerializedRightHandSide() const final;
virtual bool equivalent(const MatchExpression* other) const;
/**
* 'collator' must outlive the InMatchExpression and any clones made of it.
*/
virtual void _doSetCollator(const CollatorInterface* collator);
Status setEqualities(std::vector<BSONElement> equalities);
void setBackingBSON(BSONObj equalityStorage);
Status addRegex(std::unique_ptr<RegexMatchExpression> expr);
const std::vector<BSONElement>& getEqualities() const {
return _equalitySet;
}
bool contains(const BSONElement& e) const;
const std::vector<std::unique_ptr<RegexMatchExpression>>& getRegexes() const {
return _regexes;
}
const CollatorInterface* getCollator() const {
return _collator;
}
bool hasNull() const {
return _hasNull;
}
bool hasEmptyArray() const {
return _hasEmptyArray;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
private:
ExpressionOptimizerFunc getOptimizer() const final;
// Whether or not '_equalities' has a jstNULL element in it.
bool _hasNull = false;
// Whether or not '_equalities' has an empty array element in it.
bool _hasEmptyArray = false;
// Collator used to construct '_eltCmp';
const CollatorInterface* _collator = nullptr;
// Comparator used to compare elements. By default, simple binary comparison will be used.
BSONElementComparator _eltCmp;
// Original container of equality elements, including duplicates. Needed for re-computing
// '_equalitySet' in case '_collator' changes after elements have been added.
//
// We keep the equalities in sorted order according to the current BSON element comparator. This
// enables a fast-path to avoid re-sorting if the expression is serialized and re-parsed.
std::vector<BSONElement> _originalEqualityVector;
// Deduped set of equality elements associated with this expression. Kept in sorted order to
// support std::binary_search. Because we need to sort the elements anyway for things like index
// bounds building, using binary search avoids the overhead of inserting into a hash table which
// doesn't pay for itself in the common case where lookups are done a few times if ever.
// TODO It may be worth dynamically creating a hashset after matchesSingleElement() has been
// called "many" times.
std::vector<BSONElement> _equalitySet;
// Container of regex elements this object owns.
std::vector<std::unique_ptr<RegexMatchExpression>> _regexes;
// When this $in is generated internally, e.g. via a rewrite, this is where we store the
// data of the corresponding equality elements.
BSONObj _equalityStorage;
};
/**
* Bit test query operators include $bitsAllSet, $bitsAllClear, $bitsAnySet, and $bitsAnyClear.
*/
class BitTestMatchExpression : public LeafMatchExpression {
public:
/**
* Construct with either bit positions, a 64-bit numeric bitmask, or a char array
* bitmask.
*/
explicit BitTestMatchExpression(MatchType type,
StringData path,
std::vector<uint32_t> bitPositions,
clonable_ptr<ErrorAnnotation> annotation);
explicit BitTestMatchExpression(MatchType type,
StringData path,
uint64_t bitMask,
clonable_ptr<ErrorAnnotation> annotation);
explicit BitTestMatchExpression(MatchType type,
StringData path,
const char* bitMaskBinary,
uint32_t bitMaskLen,
clonable_ptr<ErrorAnnotation> annotation);
virtual ~BitTestMatchExpression() {}
bool matchesSingleElement(const BSONElement&, MatchDetails* details = nullptr) const final;
virtual void debugString(StringBuilder& debug, int indentationLevel) const;
BSONObj getSerializedRightHandSide() const final;
virtual bool equivalent(const MatchExpression* other) const;
size_t numBitPositions() const {
return _bitPositions.size();
}
const std::vector<uint32_t>& getBitPositions() const {
return _bitPositions;
}
uint64_t getBitMask() const {
return _bitMask;
}
std::string name() const;
private:
ExpressionOptimizerFunc getOptimizer() const final {
return [](std::unique_ptr<MatchExpression> expression) { return expression; };
}
/**
* Performs bit test using bit positions on 'eValue' and returns whether or not the bit test
* passes.
*/
bool performBitTest(long long eValue) const;
/**
* Performs bit test using bit positions on 'eBinary' with length (in bytes) 'eBinaryLen' and
* returns whether or not the bit test passes.
*/
bool performBitTest(const char* eBinary, uint32_t eBinaryLen) const;
/**
* Helper function for performBitTest(...).
*
* needFurtherBitTests() determines if the result of a bit-test ('isBitSet') is enough
* information to skip the rest of the bit tests.
**/
bool needFurtherBitTests(bool isBitSet) const;
// Vector of bit positions to test, with bit position 0 being the least significant bit.
// Used to perform bit tests against BinData.
std::vector<uint32_t> _bitPositions;
// Used to perform bit tests against numbers using a single bitwise operation.
uint64_t _bitMask = 0;
};
class BitsAllSetMatchExpression : public BitTestMatchExpression {
public:
BitsAllSetMatchExpression(StringData path,
std::vector<uint32_t> bitPositions,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ALL_SET, path, bitPositions, std::move(annotation)) {}
BitsAllSetMatchExpression(StringData path,
uint64_t bitMask,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ALL_SET, path, bitMask, std::move(annotation)) {}
BitsAllSetMatchExpression(StringData path,
const char* bitMaskBinary,
uint32_t bitMaskLen,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(
BITS_ALL_SET, path, bitMaskBinary, bitMaskLen, std::move(annotation)) {}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<BitTestMatchExpression> bitTestMatchExpression =
std::make_unique<BitsAllSetMatchExpression>(
path(), getBitPositions(), _errorAnnotation);
if (getTag()) {
bitTestMatchExpression->setTag(getTag()->clone());
}
return bitTestMatchExpression;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class BitsAllClearMatchExpression : public BitTestMatchExpression {
public:
BitsAllClearMatchExpression(StringData path,
std::vector<uint32_t> bitPositions,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ALL_CLEAR, path, bitPositions, std::move(annotation)) {}
BitsAllClearMatchExpression(StringData path,
uint64_t bitMask,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ALL_CLEAR, path, bitMask, std::move(annotation)) {}
BitsAllClearMatchExpression(StringData path,
const char* bitMaskBinary,
uint32_t bitMaskLen,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(
BITS_ALL_CLEAR, path, bitMaskBinary, bitMaskLen, std::move(annotation)) {}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<BitTestMatchExpression> bitTestMatchExpression =
std::make_unique<BitsAllClearMatchExpression>(
path(), getBitPositions(), _errorAnnotation);
if (getTag()) {
bitTestMatchExpression->setTag(getTag()->clone());
}
return bitTestMatchExpression;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class BitsAnySetMatchExpression : public BitTestMatchExpression {
public:
BitsAnySetMatchExpression(StringData path,
std::vector<uint32_t> bitPositions,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ANY_SET, path, bitPositions, std::move(annotation)) {}
BitsAnySetMatchExpression(StringData path,
uint64_t bitMask,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ANY_SET, path, bitMask, std::move(annotation)) {}
BitsAnySetMatchExpression(StringData path,
const char* bitMaskBinary,
uint32_t bitMaskLen,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(
BITS_ANY_SET, path, bitMaskBinary, bitMaskLen, std::move(annotation)) {}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<BitTestMatchExpression> bitTestMatchExpression =
std::make_unique<BitsAnySetMatchExpression>(
path(), getBitPositions(), _errorAnnotation);
if (getTag()) {
bitTestMatchExpression->setTag(getTag()->clone());
}
return bitTestMatchExpression;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
class BitsAnyClearMatchExpression : public BitTestMatchExpression {
public:
BitsAnyClearMatchExpression(StringData path,
std::vector<uint32_t> bitPositions,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ANY_CLEAR, path, bitPositions, std::move(annotation)) {}
BitsAnyClearMatchExpression(StringData path,
uint64_t bitMask,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(BITS_ANY_CLEAR, path, bitMask, std::move(annotation)) {}
BitsAnyClearMatchExpression(StringData path,
const char* bitMaskBinary,
uint32_t bitMaskLen,
clonable_ptr<ErrorAnnotation> annotation = nullptr)
: BitTestMatchExpression(
BITS_ANY_CLEAR, path, bitMaskBinary, bitMaskLen, std::move(annotation)) {}
virtual std::unique_ptr<MatchExpression> shallowClone() const {
std::unique_ptr<BitTestMatchExpression> bitTestMatchExpression =
std::make_unique<BitsAnyClearMatchExpression>(
path(), getBitPositions(), _errorAnnotation);
if (getTag()) {
bitTestMatchExpression->setTag(getTag()->clone());
}
return bitTestMatchExpression;
}
void acceptVisitor(MatchExpressionMutableVisitor* visitor) final {
visitor->visit(this);
}
void acceptVisitor(MatchExpressionConstVisitor* visitor) const final {
visitor->visit(this);
}
};
} // namespace mongo
|