summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/collation
diff options
context:
space:
mode:
authorTess Avitabile <tess.avitabile@mongodb.com>2016-05-09 14:38:42 -0400
committerTess Avitabile <tess.avitabile@mongodb.com>2016-05-16 16:02:00 -0400
commitf007a7b91835b34c06401191cc879f5343023a4b (patch)
treeae30e8a570a9c006e9efc8b73f5e85976f1e04a2 /src/mongo/db/query/collation
parentfee016c605c3914c665d6643f66b3685643206ea (diff)
downloadmongo-f007a7b91835b34c06401191cc879f5343023a4b.tar.gz
SERVER-23610 CanonicalQuery should own a CollatorInterface
Diffstat (limited to 'src/mongo/db/query/collation')
-rw-r--r--src/mongo/db/query/collation/collator_interface.h3
-rw-r--r--src/mongo/db/query/collation/collator_interface_icu.cpp7
-rw-r--r--src/mongo/db/query/collation/collator_interface_icu.h2
-rw-r--r--src/mongo/db/query/collation/collator_interface_icu_test.cpp14
-rw-r--r--src/mongo/db/query/collation/collator_interface_mock.cpp6
-rw-r--r--src/mongo/db/query/collation/collator_interface_mock.h2
-rw-r--r--src/mongo/db/query/collation/collator_interface_mock_test.cpp10
7 files changed, 44 insertions, 0 deletions
diff --git a/src/mongo/db/query/collation/collator_interface.h b/src/mongo/db/query/collation/collator_interface.h
index c86e1390fa5..61bc64cd778 100644
--- a/src/mongo/db/query/collation/collator_interface.h
+++ b/src/mongo/db/query/collation/collator_interface.h
@@ -28,6 +28,7 @@
#pragma once
+#include <memory>
#include <string>
#include "mongo/base/disallow_copying.h"
@@ -90,6 +91,8 @@ public:
virtual ~CollatorInterface() {}
+ virtual std::unique_ptr<CollatorInterface> clone() const = 0;
+
/**
* Returns a number < 0 if 'left' is less than 'right' with respect to the collation, a number >
* 0 if 'left' is greater than 'right' w.r.t. the collation, and 0 if 'left' and 'right' are
diff --git a/src/mongo/db/query/collation/collator_interface_icu.cpp b/src/mongo/db/query/collation/collator_interface_icu.cpp
index 01895284cbe..08c5f74c900 100644
--- a/src/mongo/db/query/collation/collator_interface_icu.cpp
+++ b/src/mongo/db/query/collation/collator_interface_icu.cpp
@@ -32,6 +32,7 @@
#include <unicode/sortkey.h>
+#include "mongo/stdx/memory.h"
#include "mongo/util/assert_util.h"
namespace mongo {
@@ -40,6 +41,12 @@ CollatorInterfaceICU::CollatorInterfaceICU(CollationSpec spec,
std::unique_ptr<icu::Collator> collator)
: CollatorInterface(std::move(spec)), _collator(std::move(collator)) {}
+std::unique_ptr<CollatorInterface> CollatorInterfaceICU::clone() const {
+ auto clone = stdx::make_unique<CollatorInterfaceICU>(
+ getSpec(), std::unique_ptr<icu::Collator>(_collator->clone()));
+ return {std::move(clone)};
+}
+
int CollatorInterfaceICU::compare(StringData left, StringData right) const {
UErrorCode status = U_ZERO_ERROR;
auto compareResult = _collator->compareUTF8(icu::StringPiece(left.rawData(), left.size()),
diff --git a/src/mongo/db/query/collation/collator_interface_icu.h b/src/mongo/db/query/collation/collator_interface_icu.h
index a706ee1341d..76074628c87 100644
--- a/src/mongo/db/query/collation/collator_interface_icu.h
+++ b/src/mongo/db/query/collation/collator_interface_icu.h
@@ -43,6 +43,8 @@ class CollatorInterfaceICU final : public CollatorInterface {
public:
CollatorInterfaceICU(CollationSpec spec, std::unique_ptr<icu::Collator> collator);
+ std::unique_ptr<CollatorInterface> clone() const final;
+
int compare(StringData left, StringData right) const final;
ComparisonKey getComparisonKey(StringData stringData) const final;
diff --git a/src/mongo/db/query/collation/collator_interface_icu_test.cpp b/src/mongo/db/query/collation/collator_interface_icu_test.cpp
index b699cd7fc43..8b9e55a243d 100644
--- a/src/mongo/db/query/collation/collator_interface_icu_test.cpp
+++ b/src/mongo/db/query/collation/collator_interface_icu_test.cpp
@@ -98,6 +98,20 @@ void assertNotEqualEnUS(StringData left, StringData right) {
assertEnUSComparison(right, left, ExpectedComparison::NOT_EQUAL);
}
+TEST(CollatorInterfaceICUTest, ClonedCollatorMatchesOriginal) {
+ CollationSpec collationSpec;
+ collationSpec.localeID = "en_US";
+
+ UErrorCode status = U_ZERO_ERROR;
+ std::unique_ptr<icu::Collator> coll(
+ icu::Collator::createInstance(icu::Locale("en", "US"), status));
+ ASSERT(U_SUCCESS(status));
+
+ CollatorInterfaceICU icuCollator(collationSpec, std::move(coll));
+ auto clone = icuCollator.clone();
+ ASSERT_TRUE(*clone == icuCollator);
+}
+
TEST(CollatorInterfaceICUTest, ASCIIComparisonWorksForUSEnglishCollation) {
CollationSpec collationSpec;
collationSpec.localeID = "en_US";
diff --git a/src/mongo/db/query/collation/collator_interface_mock.cpp b/src/mongo/db/query/collation/collator_interface_mock.cpp
index e6bf1799f38..b10ff3637d7 100644
--- a/src/mongo/db/query/collation/collator_interface_mock.cpp
+++ b/src/mongo/db/query/collation/collator_interface_mock.cpp
@@ -32,6 +32,7 @@
#include <string>
+#include "mongo/stdx/memory.h"
#include "mongo/util/assert_util.h"
namespace mongo {
@@ -54,6 +55,11 @@ std::string mockTypeToString(CollatorInterfaceMock::MockType type) {
CollatorInterfaceMock::CollatorInterfaceMock(MockType mockType)
: CollatorInterface(CollationSpec(mockTypeToString(mockType))), _mockType(mockType) {}
+std::unique_ptr<CollatorInterface> CollatorInterfaceMock::clone() const {
+ auto clone = stdx::make_unique<CollatorInterfaceMock>(_mockType);
+ return {std::move(clone)};
+}
+
int CollatorInterfaceMock::compare(StringData left, StringData right) const {
switch (_mockType) {
case MockType::kReverseString: {
diff --git a/src/mongo/db/query/collation/collator_interface_mock.h b/src/mongo/db/query/collation/collator_interface_mock.h
index a5191502d0c..c7eaa1b11f6 100644
--- a/src/mongo/db/query/collation/collator_interface_mock.h
+++ b/src/mongo/db/query/collation/collator_interface_mock.h
@@ -57,6 +57,8 @@ public:
*/
CollatorInterfaceMock(MockType mockType);
+ std::unique_ptr<CollatorInterface> clone() const final;
+
int compare(StringData left, StringData right) const final;
ComparisonKey getComparisonKey(StringData stringData) const final;
diff --git a/src/mongo/db/query/collation/collator_interface_mock_test.cpp b/src/mongo/db/query/collation/collator_interface_mock_test.cpp
index 843685806c6..31c96900646 100644
--- a/src/mongo/db/query/collation/collator_interface_mock_test.cpp
+++ b/src/mongo/db/query/collation/collator_interface_mock_test.cpp
@@ -76,6 +76,16 @@ TEST(CollatorInterfaceMockSelfTest, PointersToMocksOfDifferentTypesDoNotMatch) {
ASSERT(!CollatorInterface::collatorsMatch(&reverseMock, &alwaysEqualMock));
}
+TEST(CollatorInterfaceMockSelfTest, ClonedMockMatchesOriginal) {
+ CollatorInterfaceMock reverseMock(CollatorInterfaceMock::MockType::kReverseString);
+ auto reverseClone = reverseMock.clone();
+ ASSERT(CollatorInterface::collatorsMatch(reverseClone.get(), &reverseMock));
+
+ CollatorInterfaceMock alwaysEqualMock(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ auto alwaysEqualClone = alwaysEqualMock.clone();
+ ASSERT(CollatorInterface::collatorsMatch(alwaysEqualClone.get(), &alwaysEqualMock));
+}
+
TEST(CollatorInterfaceMockSelfTest, ReverseMockComparesInReverse) {
CollatorInterfaceMock reverseMock(CollatorInterfaceMock::MockType::kReverseString);
ASSERT_EQ(reverseMock.compare("abc", "abc"), 0);