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/db/query/collation | |
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/db/query/collation')
8 files changed, 38 insertions, 20 deletions
diff --git a/src/mongo/db/query/collation/collation_index_key_test.cpp b/src/mongo/db/query/collation/collation_index_key_test.cpp index bcd341c9488..0b92c5d7cae 100644 --- a/src/mongo/db/query/collation/collation_index_key_test.cpp +++ b/src/mongo/db/query/collation/collation_index_key_test.cpp @@ -70,7 +70,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyAppendsElementWithNullC BSONObj dataObj = BSON("test" << 1); BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), nullptr, &out); - ASSERT_EQ(out.obj(), BSON("" << 1)); + ASSERT_BSONOBJ_EQ(out.obj(), BSON("" << 1)); } TEST(CollationIndexKeyTest, CollationAwareAppendReversesStringWithReverseMockCollator) { @@ -79,9 +79,9 @@ TEST(CollationIndexKeyTest, CollationAwareAppendReversesStringWithReverseMockCol << "string"); BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), - BSON("" - << "gnirts")); + ASSERT_BSONOBJ_EQ(out.obj(), + BSON("" + << "gnirts")); } TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlySerializesEmptyComparisonKey) { @@ -96,7 +96,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlySerializesEmptyComparis BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), expectedObj); + ASSERT_BSONOBJ_EQ(out.obj(), expectedObj); } TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlySerializesWithEmbeddedNullByte) { @@ -111,7 +111,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlySerializesWithEmbeddedN BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), expectedObj); + ASSERT_BSONOBJ_EQ(out.obj(), expectedObj); } TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesSimpleEmbeddedObject) { @@ -123,7 +123,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesSimpleEmbeddedO BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), expected); + ASSERT_BSONOBJ_EQ(out.obj(), expected); } TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesSimpleEmbeddedArray) { @@ -135,7 +135,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesSimpleEmbeddedA BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), expected); + ASSERT_BSONOBJ_EQ(out.obj(), expected); } TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesComplexNesting) { @@ -151,7 +151,7 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesComplexNesting) BSONObjBuilder out; CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out); - ASSERT_EQ(out.obj(), expected); + ASSERT_BSONOBJ_EQ(out.obj(), expected); } } // namespace diff --git a/src/mongo/db/query/collation/collation_spec_test.cpp b/src/mongo/db/query/collation/collation_spec_test.cpp index cd8553c7305..29ab7f99473 100644 --- a/src/mongo/db/query/collation/collation_spec_test.cpp +++ b/src/mongo/db/query/collation/collation_spec_test.cpp @@ -203,7 +203,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesDefaults) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesCaseFirstUpper) { @@ -233,7 +233,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesCaseFirstUpper) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesCaseFirstLower) { @@ -263,7 +263,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesCaseFirstLower) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesPrimaryStrength) { @@ -293,7 +293,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesPrimaryStrength) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesSecondaryStrength) { @@ -323,7 +323,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesSecondaryStrength) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesQuaternaryStrength) { @@ -353,7 +353,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesQuaternaryStrength) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesIdenticalStrength) { @@ -383,7 +383,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesIdenticalStrength) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesAlternateShifted) { @@ -413,7 +413,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesAlternateShifted) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } TEST(CollationSpecTest, ToBSONCorrectlySerializesMaxVariableSpace) { @@ -443,7 +443,7 @@ TEST(CollationSpecTest, ToBSONCorrectlySerializesMaxVariableSpace) { << "version" << "myVersion"); - ASSERT_EQ(expectedObj, collationSpec.toBSON()); + ASSERT_BSONOBJ_EQ(expectedObj, collationSpec.toBSON()); } } // namespace diff --git a/src/mongo/db/query/collation/collator_factory_mock.cpp b/src/mongo/db/query/collation/collator_factory_mock.cpp index ac606847131..13784517d1d 100644 --- a/src/mongo/db/query/collation/collator_factory_mock.cpp +++ b/src/mongo/db/query/collation/collator_factory_mock.cpp @@ -33,6 +33,7 @@ #include "mongo/base/status_with.h" #include "mongo/bson/bsonobj.h" #include "mongo/bson/bsonobjbuilder.h" +#include "mongo/bson/simple_bsonobj_comparator.h" #include "mongo/db/query/collation/collator_interface_mock.h" #include "mongo/stdx/memory.h" @@ -40,7 +41,8 @@ namespace mongo { StatusWith<std::unique_ptr<CollatorInterface>> CollatorFactoryMock::makeFromBSON( const BSONObj& spec) { - if (spec == BSON(CollationSpec::kLocaleField << CollationSpec::kSimpleBinaryComparison)) { + if (SimpleBSONObjComparator::kInstance.evaluate( + spec == BSON(CollationSpec::kLocaleField << CollationSpec::kSimpleBinaryComparison))) { return {nullptr}; } auto collator = diff --git a/src/mongo/db/query/collation/collator_interface.h b/src/mongo/db/query/collation/collator_interface.h index 76f236c7413..7d44a4c63ed 100644 --- a/src/mongo/db/query/collation/collator_interface.h +++ b/src/mongo/db/query/collation/collator_interface.h @@ -34,6 +34,7 @@ #include "mongo/base/disallow_copying.h" #include "mongo/base/string_data.h" #include "mongo/base/string_data_comparator_interface.h" +#include "mongo/bson/bsonobj_comparator_interface.h" #include "mongo/db/query/collation/collation_spec.h" namespace mongo { @@ -46,7 +47,8 @@ namespace mongo { * * Does not throw exceptions. */ -class CollatorInterface : public StringData::ComparatorInterface { +class CollatorInterface : public StringData::ComparatorInterface, + public BSONObj::ComparatorInterface { MONGO_DISALLOW_COPYING(CollatorInterface); public: @@ -100,6 +102,8 @@ public: */ virtual int compare(StringData left, StringData right) const = 0; + virtual int compare(const BSONObj& left, const BSONObj& right) const = 0; + /** * Hashes the string such that strings which are equal under this collation also have equal * hashes. diff --git a/src/mongo/db/query/collation/collator_interface_icu.cpp b/src/mongo/db/query/collation/collator_interface_icu.cpp index 12498d12712..1040591fef5 100644 --- a/src/mongo/db/query/collation/collator_interface_icu.cpp +++ b/src/mongo/db/query/collation/collator_interface_icu.cpp @@ -71,6 +71,10 @@ int CollatorInterfaceICU::compare(StringData left, StringData right) const { MONGO_UNREACHABLE; } +int CollatorInterfaceICU::compare(const BSONObj& left, const BSONObj& right) const { + return left.woCompare(right, BSONObj(), true, this); +} + CollatorInterface::ComparisonKey CollatorInterfaceICU::getComparisonKey( StringData stringData) const { // A StringPiece is ICU's StringData. They are logically the same abstraction. diff --git a/src/mongo/db/query/collation/collator_interface_icu.h b/src/mongo/db/query/collation/collator_interface_icu.h index 9bb83824891..c35cc898c37 100644 --- a/src/mongo/db/query/collation/collator_interface_icu.h +++ b/src/mongo/db/query/collation/collator_interface_icu.h @@ -50,6 +50,8 @@ public: int compare(StringData left, StringData right) const final; + int compare(const BSONObj& left, const BSONObj& right) const final; + ComparisonKey getComparisonKey(StringData stringData) const final; private: diff --git a/src/mongo/db/query/collation/collator_interface_mock.cpp b/src/mongo/db/query/collation/collator_interface_mock.cpp index 638427bcd99..74fc7a26f4f 100644 --- a/src/mongo/db/query/collation/collator_interface_mock.cpp +++ b/src/mongo/db/query/collation/collator_interface_mock.cpp @@ -92,6 +92,10 @@ int CollatorInterfaceMock::compare(StringData left, StringData right) const { MONGO_UNREACHABLE; } +int CollatorInterfaceMock::compare(const BSONObj& left, const BSONObj& right) const { + return left.woCompare(right, BSONObj(), true, this); +} + CollatorInterface::ComparisonKey CollatorInterfaceMock::getComparisonKey( StringData stringData) const { switch (_mockType) { diff --git a/src/mongo/db/query/collation/collator_interface_mock.h b/src/mongo/db/query/collation/collator_interface_mock.h index 8ef8d63fb83..6ab8a3d072b 100644 --- a/src/mongo/db/query/collation/collator_interface_mock.h +++ b/src/mongo/db/query/collation/collator_interface_mock.h @@ -65,6 +65,8 @@ public: int compare(StringData left, StringData right) const final; + int compare(const BSONObj& left, const BSONObj& right) const final; + ComparisonKey getComparisonKey(StringData stringData) const final; private: |