diff options
author | David Storch <david.storch@10gen.com> | 2016-08-12 15:58:56 -0400 |
---|---|---|
committer | David Storch <david.storch@10gen.com> | 2016-08-18 11:14:17 -0400 |
commit | 26543060c852aac22f26143a04bf7789ec8fec53 (patch) | |
tree | df3ae49e5c4745058be29b7ec8a8e4b528b50a9a /src/mongo/bson/bsonobj.h | |
parent | 13fa28982d008568f7620d73ddec0c61fad7cbc8 (diff) | |
download | mongo-26543060c852aac22f26143a04bf7789ec8fec53.tar.gz |
SERVER-24508 BSONObj::ComparatorInterface
BSONObj instances should now be compared via the comparator
interface's evaluate() method. This preferred over using
BSONObj::woCompare() directly. If the comparison doesn't
require any database semantics (e.g. there is no collation),
there is a global instance of the SimpleBSONObjComparator
which should be used for BSONObj comparisons. If the
comparison requires special semantics, then callers must
instantiate their own comparator object.
Diffstat (limited to 'src/mongo/bson/bsonobj.h')
-rw-r--r-- | src/mongo/bson/bsonobj.h | 68 |
1 files changed, 53 insertions, 15 deletions
diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h index fa405e9f299..726a5f43be9 100644 --- a/src/mongo/bson/bsonobj.h +++ b/src/mongo/bson/bsonobj.h @@ -95,6 +95,31 @@ typedef std::multiset<BSONElement, BSONElementCmpWithoutField> BSONElementMSet; */ class BSONObj { public: + // Declared in bsonobj_comparator_interface.h. + class ComparatorInterface; + + /** + * Operator overloads for relops return a DeferredComparison which can subsequently be evaluated + * by a BSONObj::ComparatorInterface. + */ + struct DeferredComparison { + enum class Type { + kLT, + kLTE, + kEQ, + kGT, + kGTE, + kNE, + }; + + DeferredComparison(Type type, const BSONObj& lhs, const BSONObj& rhs) + : type(type), lhs(lhs), rhs(rhs) {} + + Type type; + const BSONObj& lhs; + const BSONObj& rhs; + }; + static const char kMinBSONLength = 5; /** Construct an empty BSONObj -- that is, {}. */ @@ -394,6 +419,15 @@ public: /** Alternative output format */ std::string hexDump() const; + // + // Comparison API. + // + // BSONObj instances can be compared either using woCompare() or via operator overloads. Most + // callers should prefer operator overloads. Note that the operator overloads return a + // DeferredComparison, which must be subsequently evaluated by a BSONObj::ComparatorInterface. + // See bsonobj_comparator_interface.h for details. + // + /**wo='well ordered'. fields must be in same order in each object. Ordering is with respect to the signs of the elements and allows ascending / descending key mixing. @@ -416,17 +450,28 @@ public: bool considerFieldName = true, const StringData::ComparatorInterface* comparator = nullptr) const; - bool operator<(const BSONObj& other) const { - return woCompare(other) < 0; + DeferredComparison operator<(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kLT, *this, other); + } + + DeferredComparison operator<=(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kLTE, *this, other); } - bool operator<=(const BSONObj& other) const { - return woCompare(other) <= 0; + + DeferredComparison operator>(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kGT, *this, other); + } + + DeferredComparison operator>=(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kGTE, *this, other); } - bool operator>(const BSONObj& other) const { - return woCompare(other) > 0; + + DeferredComparison operator==(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kEQ, *this, other); } - bool operator>=(const BSONObj& other) const { - return woCompare(other) >= 0; + + DeferredComparison operator!=(const BSONObj& other) const { + return DeferredComparison(DeferredComparison::Type::kNE, *this, other); } bool equal(const BSONObj& r) const; @@ -505,13 +550,6 @@ public: /** true unless corrupt */ bool valid() const; - bool operator==(const BSONObj& other) const { - return equal(other); - } - bool operator!=(const BSONObj& other) const { - return !operator==(other); - } - enum MatchType { Equality = 0, LT = 0x1, |