summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Evans <jacob.evans@mongodb.com>2019-10-21 23:51:42 +0000
committerevergreen <evergreen@mongodb.com>2019-10-21 23:51:42 +0000
commitdb9291bfafb17427adb031ba0326d1acfc52bde0 (patch)
tree1e2e4dc2fbd7eec88237270d9a1dbea82ccb2d9b
parent39bfc190446acd0b049c5cb29dfbbc6574a40ce1 (diff)
downloadmongo-db9291bfafb17427adb031ba0326d1acfc52bde0.tar.gz
Revert "SERVER-42748 Support using stored procedures (system.js) in map/reduce"
This reverts commit 6eac52a928f7a949713f7e7c20468ea0ecba8946.
-rw-r--r--buildscripts/resmokeconfig/suites/core_map_reduce_agg.yaml1
-rw-r--r--jstests/core/mr_stored.js91
-rw-r--r--src/mongo/db/pipeline/expression_context.h2
-rw-r--r--src/mongo/db/pipeline/javascript_execution.cpp3
-rw-r--r--src/mongo/db/pipeline/javascript_execution.h3
-rw-r--r--src/mongo/scripting/engine.cpp2
-rw-r--r--src/mongo/scripting/engine.h5
7 files changed, 33 insertions, 74 deletions
diff --git a/buildscripts/resmokeconfig/suites/core_map_reduce_agg.yaml b/buildscripts/resmokeconfig/suites/core_map_reduce_agg.yaml
index 7348dda6682..aa403c4b48d 100644
--- a/buildscripts/resmokeconfig/suites/core_map_reduce_agg.yaml
+++ b/buildscripts/resmokeconfig/suites/core_map_reduce_agg.yaml
@@ -13,7 +13,6 @@ selector:
- jstests/core/mr_outreduce.js
- jstests/core/mr_outreduce2.js
- jstests/core/mr_sort.js
- - jstests/core/mr_stored.js
- jstests/core/mr_tolerates_js_exception.js
- jstests/core/mr_undef.js
- jstests/core/mr4.js
diff --git a/jstests/core/mr_stored.js b/jstests/core/mr_stored.js
index b1da36c1784..d3ee89a5cf5 100644
--- a/jstests/core/mr_stored.js
+++ b/jstests/core/mr_stored.js
@@ -8,9 +8,6 @@
// requires_non_retryable_writes,
// uses_map_reduce_with_temp_collections,
// ]
-/**
- * Tests that map reduce works with stored javascript.
- */
(function() {
"use strict";
@@ -31,9 +28,7 @@ let map = function(obj) {
emit(obj.partner, {stats: [obj.visits]});
};
-let notStoredMap = `function() {(${map.toString()})(this);}`;
-
-const reduce = function(k, v) {
+let reduce = function(k, v) {
var stats = [];
var total = 0;
for (var i = 0; i < v.length; i++) {
@@ -45,84 +40,52 @@ const reduce = function(k, v) {
return {stats: stats, total: total};
};
-const finalize = function(key, reducedValue) {
- reducedValue.avg = reducedValue.total / reducedValue.stats.length;
- return reducedValue;
-};
-
+// Test that map reduce works with stored javascript
assert.commandWorked(testDB.system.js.insert({_id: "mr_stored_map", value: map}));
assert.commandWorked(testDB.system.js.insert({_id: "mr_stored_reduce", value: reduce}));
-assert.commandWorked(testDB.system.js.insert({_id: "mr_stored_finalize", value: finalize}));
const out = testDB.mr_stored_out;
-function assertCorrect(results) {
- assert.eq(2, Object.keySet(results).length);
- assert.eq([9, 11, 30], results["1"].stats);
- assert.eq([9, 41, 41], results["2"].stats);
-}
-
-// Stored Map.
-assert.commandWorked(testDB.runCommand({
- mapReduce: coll.getName(),
- map: function() {
+assert.commandWorked(coll.mapReduce(
+ function() {
mr_stored_map(this);
},
- reduce: reduce,
- finalize: finalize,
- out: "mr_stored_out"
-}));
-
-assertCorrect(out.convertToSingleObject("value"));
-out.drop();
-
-// Stored Reduce.
-assert.commandWorked(testDB.runCommand({
- mapReduce: coll.getName(),
- map: notStoredMap,
- reduce: function(k, v) {
+ function(k, v) {
return mr_stored_reduce(k, v);
},
- finalize: finalize,
- out: "mr_stored_out"
-}));
+ {out: "mr_stored_out", scope: {xx: 1}}));
+
+let z = out.convertToSingleObject("value");
+assert.eq(2, Object.keySet(z).length);
+assert.eq([9, 11, 30], z["1"].stats);
+assert.eq([9, 41, 41], z["2"].stats);
-assertCorrect(out.convertToSingleObject("value"));
out.drop();
-// Stored Finalize.
-assert.commandWorked(testDB.runCommand({
- mapReduce: coll.getName(),
- map: notStoredMap,
- reduce: reduce,
- finalize: function(key, reducedValue) {
- return mr_stored_finalize(key, reducedValue);
- },
- out: "mr_stored_out"
-}));
+map = function(obj) {
+ var x = "partner";
+ var y = "visits";
+ emit(obj[x], {stats: [obj[y]]});
+};
-assertCorrect(out.convertToSingleObject("value"));
-out.drop();
+assert.commandWorked(testDB.system.js.save({_id: "mr_stored_map", value: map}));
-// All Stored.
-assert.commandWorked(testDB.runCommand({
- mapReduce: coll.getName(),
- map: function() {
+assert.commandWorked(coll.mapReduce(
+ function() {
mr_stored_map(this);
},
- reduce: function(k, v) {
+ function(k, v) {
return mr_stored_reduce(k, v);
},
- finalize: function(key, reducedValue) {
- return mr_stored_finalize(key, reducedValue);
- },
- out: "mr_stored_out"
-}));
+ {out: "mr_stored_out", scope: {xx: 1}}));
-assertCorrect(out.convertToSingleObject("value"));
-out.drop();
+z = out.convertToSingleObject("value");
+assert.eq(2, Object.keySet(z).length);
+assert.eq([9, 11, 30], z["1"].stats);
+assert.eq([9, 41, 41], z["2"].stats);
assert.commandWorked(testDB.system.js.remove({_id: "mr_stored_map"}));
assert.commandWorked(testDB.system.js.remove({_id: "mr_stored_reduce"}));
-assert.commandWorked(testDB.system.js.remove({_id: "mr_stored_finalize"}));
+
+out.drop();
}());
diff --git a/src/mongo/db/pipeline/expression_context.h b/src/mongo/db/pipeline/expression_context.h
index e82d7bef9c5..c0b44e88da6 100644
--- a/src/mongo/db/pipeline/expression_context.h
+++ b/src/mongo/db/pipeline/expression_context.h
@@ -242,7 +242,7 @@ public:
auto getJsExecWithScope() const {
RuntimeConstants runtimeConstants = getRuntimeConstants();
const boost::optional<mongo::BSONObj>& scope = runtimeConstants.getJsScope();
- return JsExecution::get(opCtx, scope.get_value_or(BSONObj()), ns.db());
+ return JsExecution::get(opCtx, scope.get_value_or(BSONObj()));
}
// The explain verbosity requested by the user, or boost::none if no explain was requested.
diff --git a/src/mongo/db/pipeline/javascript_execution.cpp b/src/mongo/db/pipeline/javascript_execution.cpp
index 720fdad7f8e..ac8e474fb9c 100644
--- a/src/mongo/db/pipeline/javascript_execution.cpp
+++ b/src/mongo/db/pipeline/javascript_execution.cpp
@@ -37,11 +37,10 @@ namespace {
const auto getExec = OperationContext::declareDecoration<std::unique_ptr<JsExecution>>();
} // namespace
-JsExecution* JsExecution::get(OperationContext* opCtx, const BSONObj& scope, StringData database) {
+JsExecution* JsExecution::get(OperationContext* opCtx, const BSONObj& scope) {
auto& exec = getExec(opCtx);
if (!exec) {
exec = std::make_unique<JsExecution>(scope);
- exec->getScope()->setLocalDB(database);
exec->getScope()->loadStored(opCtx, true);
}
return exec.get();
diff --git a/src/mongo/db/pipeline/javascript_execution.h b/src/mongo/db/pipeline/javascript_execution.h
index 0a2d6f1219c..b62a2d2df12 100644
--- a/src/mongo/db/pipeline/javascript_execution.h
+++ b/src/mongo/db/pipeline/javascript_execution.h
@@ -29,7 +29,6 @@
#pragma once
-#include "mongo/base/string_data.h"
#include "mongo/db/client.h"
#include "mongo/db/exec/document_value/value.h"
#include "mongo/db/operation_context.h"
@@ -50,7 +49,7 @@ public:
* Create or get a pointer to a JsExecution instance, capable of invoking Javascript functions
* and reading the return value.
*/
- static JsExecution* get(OperationContext* opCtx, const BSONObj& scope, StringData database);
+ static JsExecution* get(OperationContext* opCtx, const BSONObj& scope);
/**
* Construct with a thread-local scope and initialize with the given scope variables.
diff --git a/src/mongo/scripting/engine.cpp b/src/mongo/scripting/engine.cpp
index eba9c50c9be..149da7f5969 100644
--- a/src/mongo/scripting/engine.cpp
+++ b/src/mongo/scripting/engine.cpp
@@ -421,7 +421,7 @@ public:
void init(const BSONObj* data) {
_real->init(data);
}
- void setLocalDB(StringData dbName) {
+ void setLocalDB(const string& dbName) {
_real->setLocalDB(dbName);
}
void loadStored(OperationContext* opCtx, bool ignoreNotConnected = false) {
diff --git a/src/mongo/scripting/engine.h b/src/mongo/scripting/engine.h
index 4f572cad026..b50c75baed6 100644
--- a/src/mongo/scripting/engine.h
+++ b/src/mongo/scripting/engine.h
@@ -29,7 +29,6 @@
#pragma once
-#include "mongo/base/string_data.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/service_context.h"
#include "mongo/platform/atomic_word.h"
@@ -66,8 +65,8 @@ public:
}
virtual void externalSetup() = 0;
- virtual void setLocalDB(StringData localDBName) {
- _localDBName = localDBName.toString();
+ virtual void setLocalDB(const std::string& localDBName) {
+ _localDBName = localDBName;
}
virtual BSONObj getObject(const char* field) = 0;