From 318c6da287732842463d6bd4eea565125dc22ab2 Mon Sep 17 00:00:00 2001 From: Tess Avitabile Date: Thu, 21 Apr 2016 11:51:07 -0400 Subject: SERVER-23473 Add collation parameter to the mapReduce command --- src/mongo/db/commands/mr.cpp | 10 ++++++++++ src/mongo/db/commands/mr.h | 1 + src/mongo/db/commands/mr_test.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/mongo/db/commands/mr.cpp b/src/mongo/db/commands/mr.cpp index 155525578ee..b0898b1064a 100644 --- a/src/mongo/db/commands/mr.cpp +++ b/src/mongo/db/commands/mr.cpp @@ -336,6 +336,16 @@ Config::Config(const string& _dbname, const BSONObj& cmdObj) { else uassert(13609, "sort has to be blank or an Object", !s.trueValue()); + BSONElement collationElt = cmdObj["collation"]; + if (collationElt.type() == Object) + collation = collationElt.embeddedObjectUserCheck(); + else + uassert(40082, + str::stream() + << "mapReduce 'collation' parameter must be of type Object but found type: " + << typeName(collationElt.type()), + collationElt.eoo()); + if (cmdObj["limit"].isNumber()) limit = cmdObj["limit"].numberLong(); else diff --git a/src/mongo/db/commands/mr.h b/src/mongo/db/commands/mr.h index 3d967438179..3237e6f7238 100644 --- a/src/mongo/db/commands/mr.h +++ b/src/mongo/db/commands/mr.h @@ -206,6 +206,7 @@ public: BSONObj filter; BSONObj sort; + BSONObj collation; long long limit; // functions diff --git a/src/mongo/db/commands/mr_test.cpp b/src/mongo/db/commands/mr_test.cpp index 93e90851257..d58ca5326f2 100644 --- a/src/mongo/db/commands/mr_test.cpp +++ b/src/mongo/db/commands/mr_test.cpp @@ -208,4 +208,43 @@ TEST(ConfigOutputOptionsTest, parseOutputOptions) { mr::Config::INMEMORY); } +TEST(ConfigTest, ParseCollation) { + std::string dbname = "myDB"; + BSONObj collation = BSON("locale" + << "en_US"); + BSONObjBuilder bob; + bob.append("mapReduce", "myCollection"); + bob.appendCode("map", "function() { emit(0, 1); }"); + bob.appendCode("reduce", "function(k, v) { return {count: 0}; }"); + bob.append("out", "outCollection"); + bob.append("collation", collation); + BSONObj cmdObj = bob.obj(); + mr::Config config(dbname, cmdObj); + ASSERT_EQUALS(config.collation, collation); +} + +TEST(ConfigTest, ParseNoCollation) { + std::string dbname = "myDB"; + BSONObjBuilder bob; + bob.append("mapReduce", "myCollection"); + bob.appendCode("map", "function() { emit(0, 1); }"); + bob.appendCode("reduce", "function(k, v) { return {count: 0}; }"); + bob.append("out", "outCollection"); + BSONObj cmdObj = bob.obj(); + mr::Config config(dbname, cmdObj); + ASSERT_EQUALS(config.collation, BSONObj()); +} + +TEST(ConfigTest, CollationNotAnObjectFailsToParse) { + std::string dbname = "myDB"; + BSONObjBuilder bob; + bob.append("mapReduce", "myCollection"); + bob.appendCode("map", "function() { emit(0, 1); }"); + bob.appendCode("reduce", "function(k, v) { return {count: 0}; }"); + bob.append("out", "outCollection"); + bob.append("collation", "en_US"); + BSONObj cmdObj = bob.obj(); + ASSERT_THROWS(mr::Config(dbname, cmdObj), UserException); +} + } // namespace -- cgit v1.2.1