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

/**
 * indexed_insert_where.js
 *
 * Bulk inserts documents in batches of 100, and then queries for documents inserted by the thread.
 * Note: This workload is extended by remove_where.js, touch_base.js, update_where.js,
 * and upsert_where.js. data.insertedDocuments is used as a counter by all of those workloads
 * for their own checks.
 */

var $config = (function() {

    var data = {
        documentsToInsert: 100,
        insertedDocuments: 0,
        generateDocumentToInsert: function generateDocumentToInsert() {
            return {
                tid: this.tid
            };
        },
        shardKey: {tid: 1}
    };

    var states = {
        insert: function insert(db, collName) {
            var bulk = db[collName].initializeUnorderedBulkOp();
            for (var i = 0; i < this.documentsToInsert; ++i) {
                bulk.insert(this.generateDocumentToInsert());
            }
            var res = bulk.execute();
            assertAlways.writeOK(res);
            assertAlways.eq(this.documentsToInsert, res.nInserted);
            this.insertedDocuments += this.documentsToInsert;
        },

        query: function query(db, collName) {
            var count = db[collName].find({$where: 'this.tid === ' + this.tid}).itcount();
            assertWhenOwnColl.eq(count,
                                 this.insertedDocuments,
                                 '$where query should return the number of documents this ' +
                                     'thread inserted');
        }
    };

    var transitions = {
        insert: {insert: 0.2, query: 0.8},
        query: {insert: 0.8, query: 0.2}
    };

    var setup = function setup(db, collName, cluster) {
        assertAlways.commandWorked(db[collName].ensureIndex({tid: 1}));
    };

    return {
        threadCount: 10,
        iterations: 10,
        data: data,
        states: states,
        startState: 'insert',
        setup: setup,
        transitions: transitions
    };
})();