summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTess Avitabile <tess.avitabile@mongodb.com>2016-03-22 11:58:21 -0400
committerTess Avitabile <tess.avitabile@mongodb.com>2016-03-24 10:03:42 -0400
commitee98cdb5f498081d82f5f12737be7ca84e1cf4a3 (patch)
treec1cd58a39448aa9d662f8699db81cffb058b551a
parentacecea6df3326f6e94063bad2a0f900329f7af4e (diff)
downloadmongo-ee98cdb5f498081d82f5f12737be7ca84e1cf4a3.tar.gz
SERVER-23218 Decorate ServiceContext with a CollatorFactoryInterface
-rw-r--r--src/mongo/db/query/collation/SConscript33
-rw-r--r--src/mongo/db/query/collation/collator_factory_interface.cpp54
-rw-r--r--src/mongo/db/query/collation/collator_factory_interface.h14
-rw-r--r--src/mongo/db/query/collation/collator_factory_mock.cpp59
-rw-r--r--src/mongo/db/query/collation/collator_factory_mock.h44
-rw-r--r--src/mongo/db/query/collation/collator_factory_mock_test.cpp63
6 files changed, 267 insertions, 0 deletions
diff --git a/src/mongo/db/query/collation/SConscript b/src/mongo/db/query/collation/SConscript
index 0ea9a1df513..d5bf4bb70ed 100644
--- a/src/mongo/db/query/collation/SConscript
+++ b/src/mongo/db/query/collation/SConscript
@@ -97,6 +97,39 @@ env.CppUnitTest(
],
)
+env.Library(
+ target="collator_factory_interface",
+ source=[
+ "collator_factory_interface.cpp",
+ ],
+ LIBDEPS=[
+ "$BUILD_DIR/mongo/base",
+ "$BUILD_DIR/mongo/db/service_context",
+ "collator_interface",
+ ],
+)
+
+env.Library(
+ target="collator_factory_mock",
+ source=[
+ "collator_factory_mock.cpp",
+ ],
+ LIBDEPS=[
+ "collator_factory_interface",
+ "collator_interface_mock",
+ ],
+)
+
+env.CppUnitTest(
+ target="collator_factory_mock_test",
+ source=[
+ "collator_factory_mock_test.cpp",
+ ],
+ LIBDEPS=[
+ "collator_factory_mock",
+ ],
+)
+
if icuEnabled:
env.Library(
target="collator_icu",
diff --git a/src/mongo/db/query/collation/collator_factory_interface.cpp b/src/mongo/db/query/collation/collator_factory_interface.cpp
new file mode 100644
index 00000000000..18648e4009d
--- /dev/null
+++ b/src/mongo/db/query/collation/collator_factory_interface.cpp
@@ -0,0 +1,54 @@
+/**
+ * Copyright (C) 2016 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/db/query/collation/collator_factory_interface.h"
+
+#include "mongo/db/service_context.h"
+
+namespace mongo {
+
+namespace {
+
+const auto getCollatorFactory =
+ ServiceContext::declareDecoration<std::unique_ptr<CollatorFactoryInterface>>();
+
+} // namespace
+
+CollatorFactoryInterface* CollatorFactoryInterface::get(ServiceContext* serviceContext) {
+ invariant(getCollatorFactory(serviceContext));
+ return getCollatorFactory(serviceContext).get();
+}
+
+void CollatorFactoryInterface::set(ServiceContext* serviceContext,
+ std::unique_ptr<CollatorFactoryInterface> collatorFactory) {
+ getCollatorFactory(serviceContext) = std::move(collatorFactory);
+}
+
+} // namespace mongo
diff --git a/src/mongo/db/query/collation/collator_factory_interface.h b/src/mongo/db/query/collation/collator_factory_interface.h
index 1503ef77327..31bf654bd98 100644
--- a/src/mongo/db/query/collation/collator_factory_interface.h
+++ b/src/mongo/db/query/collation/collator_factory_interface.h
@@ -36,6 +36,7 @@
namespace mongo {
class BSONObj;
+class ServiceContext;
template <typename T>
class StatusWith;
@@ -51,6 +52,19 @@ public:
virtual ~CollatorFactoryInterface() {}
/**
+ * Returns the CollatorFactoryInterface object associated with the specified service context.
+ * This method must only be called if a CollatorFactoryInterface has been set on the service
+ * context.
+ */
+ static CollatorFactoryInterface* get(ServiceContext* serviceContext);
+
+ /**
+ * Sets the CollatorFactoryInterface object associated with the specified service context.
+ */
+ static void set(ServiceContext* serviceContext,
+ std::unique_ptr<CollatorFactoryInterface> collatorFactory);
+
+ /**
* Parses 'spec' and, on success, returns the corresponding CollatorInterface.
*
* Returns a non-OK status if 'spec' is invalid or otherwise cannot be converted into a
diff --git a/src/mongo/db/query/collation/collator_factory_mock.cpp b/src/mongo/db/query/collation/collator_factory_mock.cpp
new file mode 100644
index 00000000000..0233cbcf262
--- /dev/null
+++ b/src/mongo/db/query/collation/collator_factory_mock.cpp
@@ -0,0 +1,59 @@
+/**
+ * Copyright (C) 2016 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/db/query/collation/collator_factory_mock.h"
+
+#include "mongo/base/init.h"
+#include "mongo/db/query/collation/collator_interface_mock.h"
+#include "mongo/db/service_context.h"
+#include "mongo/stdx/memory.h"
+
+namespace mongo {
+
+namespace {
+
+// TODO SERVER-22371: We should decorate with a CollatorFactoryICU instead.
+MONGO_INITIALIZER_WITH_PREREQUISITES(CreateCollatorFactory,
+ ("SetGlobalEnvironment"))(InitializerContext* context) {
+ CollatorFactoryInterface::set(getGlobalServiceContext(),
+ stdx::make_unique<CollatorFactoryMock>());
+ return Status::OK();
+}
+
+} // namespace
+
+StatusWith<std::unique_ptr<CollatorInterface>> CollatorFactoryMock::makeFromBSON(
+ const BSONObj& spec) {
+ auto collator =
+ stdx::make_unique<CollatorInterfaceMock>(CollatorInterfaceMock::MockType::kReverseString);
+ return {std::move(collator)};
+}
+
+} // namespace mongo
diff --git a/src/mongo/db/query/collation/collator_factory_mock.h b/src/mongo/db/query/collation/collator_factory_mock.h
new file mode 100644
index 00000000000..85466b2eec7
--- /dev/null
+++ b/src/mongo/db/query/collation/collator_factory_mock.h
@@ -0,0 +1,44 @@
+/**
+ * Copyright (C) 2016 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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 "mongo/db/query/collation/collator_factory_interface.h"
+
+namespace mongo {
+
+class CollatorFactoryMock : public CollatorFactoryInterface {
+public:
+ /**
+ * Returns a collator that compares strings by reversing them and performing a binary
+ * comparison.
+ */
+ StatusWith<std::unique_ptr<CollatorInterface>> makeFromBSON(const BSONObj& spec) final;
+};
+
+} // namespace mongo
diff --git a/src/mongo/db/query/collation/collator_factory_mock_test.cpp b/src/mongo/db/query/collation/collator_factory_mock_test.cpp
new file mode 100644
index 00000000000..f97a6360185
--- /dev/null
+++ b/src/mongo/db/query/collation/collator_factory_mock_test.cpp
@@ -0,0 +1,63 @@
+/**
+ * Copyright (C) 2016 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/base/init.h"
+#include "mongo/bson/bsonobj.h"
+#include "mongo/db/query/collation/collator_factory_mock.h"
+#include "mongo/db/service_context_noop.h"
+#include "mongo/stdx/memory.h"
+#include "mongo/unittest/unittest.h"
+
+namespace {
+
+using namespace mongo;
+
+// Stub to avoid including the server environment library.
+MONGO_INITIALIZER(SetGlobalEnvironment)(InitializerContext* context) {
+ setGlobalServiceContext(stdx::make_unique<ServiceContextNoop>());
+ return Status::OK();
+}
+
+TEST(CollatorFactoryMockTest, CollatorFactoryMockConstructsReverseStringCollator) {
+ CollatorFactoryMock factory;
+ auto collator = factory.makeFromBSON(BSONObj());
+ ASSERT_OK(collator.getStatus());
+ ASSERT_GT(collator.getValue()->compare("abc", "cba"), 0);
+}
+
+// TODO SERVER-22371: Remove this test. We will decorate with a CollatorFactoryICU instead.
+TEST(CollatorFactoryMockTest, ServiceContextDecoratedWithCollatorFactoryMock) {
+ auto factory = CollatorFactoryInterface::get(getGlobalServiceContext());
+ auto collator = factory->makeFromBSON(BSONObj());
+ ASSERT_OK(collator.getStatus());
+ ASSERT_GT(collator.getValue()->compare("abc", "cba"), 0);
+}
+
+} // namespace