summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/map_reduce_inline.js
blob: ade5a8aa3691b1a1a74cd07b83cb517a577143d4 (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
'use strict';

/**
 * map_reduce_inline.js
 *
 * Generates some random data and inserts it into a collection. Runs a
 * map-reduce command over the collection that computes the frequency
 * counts of the 'value' field in memory.
 *
 * Used as the base workload for the other map-reduce workloads.
 */
var $config = (function() {

    function mapper() {
        if (this.hasOwnProperty('key') && this.hasOwnProperty('value')) {
            var obj = {};
            obj[this.value] = 1;
            emit(this.key, obj);
        }
    }

    function reducer(key, values) {
        var res = {};

        values.forEach(function(obj) {
            Object.keys(obj).forEach(function(value) {
                if (!res.hasOwnProperty(value)) {
                    res[value] = 0;
                }
                res[value] += obj[value];
            });
        });

        return res;
    }

    function finalizer(key, reducedValue) {
        return reducedValue;
    }

    var data = {numDocs: 2000, mapper: mapper, reducer: reducer, finalizer: finalizer};

    var states = (function() {

        function init(db, collName) {
            // no-op
            // other workloads that extend this workload use this method
        }

        function mapReduce(db, collName) {
            var options = {finalize: this.finalizer, out: {inline: 1}};

            var res = db[collName].mapReduce(this.mapper, this.reducer, options);
            assertAlways.commandWorked(res);
        }

        return {init: init, mapReduce: mapReduce};

    })();

    var transitions = {init: {mapReduce: 1}, mapReduce: {mapReduce: 1}};

    function makeDoc(keyLimit, valueLimit) {
        return {
            _id: new ObjectId(),
            key: Random.randInt(keyLimit),
            value: Random.randInt(valueLimit)
        };
    }

    function setup(db, collName, cluster) {
        var bulk = db[collName].initializeUnorderedBulkOp();
        for (var i = 0; i < this.numDocs; ++i) {
            // TODO: this actually does assume that there are no unique indexes
            var doc = makeDoc(this.numDocs / 100, this.numDocs / 10);
            bulk.insert(doc);
        }

        var res = bulk.execute();
        assertAlways.writeOK(res);
        assertAlways.eq(this.numDocs, res.nInserted);
    }

    return {
        threadCount: 5,
        iterations: 10,
        data: data,
        states: states,
        transitions: transitions,
        setup: setup
    };

})();