summaryrefslogtreecommitdiff
path: root/jstests/core/mr_stored.js
blob: 01e821b008c5aa72a30c91927bd3f138e2c888ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// This test expects a function stored in the system.js collection to be available for a map/reduce,
// which may not be the case if it is implicitly sharded in a passthrough.
// The test runs commands that are not allowed with security token: mapReduce.
// @tags: [
//   not_allowed_with_security_token,
//   assumes_unsharded_collection,
//   # mapReduce does not support afterClusterTime.
//   does_not_support_causal_consistency,
//   does_not_support_stepdowns,
//   requires_non_retryable_writes,
//   uses_map_reduce_with_temp_collections,
// ]
/**
 * Tests that map reduce works with stored javascript.
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertArrayEq.

// Use a unique database name to avoid conflicts with other tests that directly modify
// system.js.
const testDB = db.getSiblingDB("mr_stored");
const coll = testDB.test;
coll.drop();

assert.commandWorked(coll.insert({"partner": 1, "visits": 9}));
assert.commandWorked(coll.insert({"partner": 2, "visits": 9}));
assert.commandWorked(coll.insert({"partner": 1, "visits": 11}));
assert.commandWorked(coll.insert({"partner": 1, "visits": 30}));
assert.commandWorked(coll.insert({"partner": 2, "visits": 41}));
assert.commandWorked(coll.insert({"partner": 2, "visits": 41}));

let map = function(obj) {
    emit(obj.partner, {stats: [obj.visits]});
};

let notStoredMap = `function() {(${map.toString()})(this);}`;

const reduce = function(k, v) {
    var stats = [];
    var total = 0;
    for (var i = 0; i < v.length; i++) {
        for (var j in v[i].stats) {
            stats.push(v[i].stats[j]);
            total += v[i].stats[j];
        }
    }
    return {stats: stats, total: total};
};

const finalize = function(key, reducedValue) {
    reducedValue.avg = reducedValue.total / reducedValue.stats.length;
    return reducedValue;
};

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);
    assertArrayEq({actual: [9, 11, 30], expected: results["1"].stats});
    assertArrayEq({actual: [9, 41, 41], expected: results["2"].stats});
}

// Stored Map.
assert.commandWorked(testDB.runCommand({
    mapReduce: coll.getName(),
    map: 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) {
        return mr_stored_reduce(k, v);
    },
    finalize: finalize,
    out: "mr_stored_out"
}));

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"
}));

assertCorrect(out.convertToSingleObject("value"));
out.drop();

// All Stored.
assert.commandWorked(testDB.runCommand({
    mapReduce: coll.getName(),
    map: function() {
        mr_stored_map(this);
    },
    reduce: function(k, v) {
        return mr_stored_reduce(k, v);
    },
    finalize: function(key, reducedValue) {
        return mr_stored_finalize(key, reducedValue);
    },
    out: "mr_stored_out"
}));

assertCorrect(out.convertToSingleObject("value"));
out.drop();

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"}));
}());