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

/*
 * yield_sort_merge.js (extends yield_fetch.js)
 *
 * Intersperse queries which use the SORT_MERGE stage with updates and deletes of documents they
 * may match.
 * Other workloads that need an index { a: 1, b: 1 } can extend this
 */
load('jstests/concurrency/fsm_libs/extend_workload.js'); // for extendWorkload
load('jstests/concurrency/fsm_workloads/yield.js'); // for $config

var $config = extendWorkload($config, function($config, $super) {

    /*
     * Execute a query that will use the SORT_MERGE stage.
     */
    $config.states.query = function sortMerge(db, collName) {
        var nMatches = 50;  // Don't push this too high, or SORT_MERGE stage won't be selected.

        // Build an array [0, nMatches).
        var matches = [];
        for (var i = 0; i < nMatches; i++) {
            matches.push(i);
        }

        var cursor = db[collName].find({ a: { $in: matches } })
                                 .sort({ b: -1 })
                                 .batchSize(this.batchSize);

        var verifier = function sortMergeVerifier(doc, prevDoc) {
            var correctOrder = true;
            if (prevDoc !== null) {
                correctOrder = (doc.b <= prevDoc.b);
            }
            return doc.a < nMatches && correctOrder;
        };

        this.advanceCursor(cursor, verifier);
    };

    $config.data.genUpdateDoc = function genUpdateDoc() {
        var newA = Random.randInt(this.nDocs);
        var newB = Random.randInt(this.nDocs);
        return { $set: { a: newA, b: newB } };
    };

    $config.setup = function setup(db, collName, cluster) {
        $super.setup.apply(this, arguments);

        assertAlways.commandWorked(db[collName].ensureIndex({ a: 1, b: 1 }));
    };

    return $config;
});