summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/validate_adjust_multikey.js
blob: ad36ac48f352ce124ddcfceb5b9d91ade3dfb444 (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
/**
 * Tests foreground validation's ability to fix up allowable multikey metadata problems.
 */
(function() {
load("jstests/libs/analyze_plan.js");  // For getWinningPlan to analyze explain() output.

const conn = MongoRunner.runMongod();
const dbName = jsTestName();
const collName = 'test';
const db = conn.getDB(dbName);

const assertValidate = (coll, assertFn) => {
    let res = assert.commandWorked(coll.validate());
    assertFn(res);
};

const assertIndexMultikey = (coll, hint, expectMultikey) => {
    const explain = coll.find().hint(hint).explain();
    const plan = getWinningPlan(explain.queryPlanner);
    assert.eq("FETCH", plan.stage, explain);
    assert.eq("IXSCAN", plan.inputStage.stage, explain);
    assert.eq(expectMultikey,
              plan.inputStage.isMultiKey,
              `Index multikey state "${plan.inputStage.isMultiKey}" was not "${expectMultikey}"`);
};

const runTest = (testCase) => {
    db[collName].drop();
    db.createCollection(collName);
    testCase(db[collName]);
};

// Test that validate will modify an index's multikey paths if they change.
runTest((coll) => {
    // Create an index, make the index multikey on 'a', and expect normal validation behavior.
    assert.commandWorked(coll.createIndex({a: 1, b: 1}));
    assert.commandWorked(coll.insert({_id: 1, a: [0, 1]}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(!res.repaired);
        assert.eq(0, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, {a: 1, b: 1}, true);

    // Insert a document that makes the index multikey on 'b', and remove the document that
    // makes the index multikey on 'a'. Expect repair to adjust the paths.
    assert.commandWorked(coll.insert({_id: 2, b: [0, 1]}));
    assert.commandWorked(coll.remove({_id: 1}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(res.repaired);
        assert.eq(1, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, {a: 1, b: 1}, true);
});

// Test that validate will unset an index's multikey flag if it no longer has multikey documents.
runTest((coll) => {
    // Create an index, make the index multikey on 'a', and expect normal validation behavior.
    assert.commandWorked(coll.createIndex({a: 1, b: 1}));
    assert.commandWorked(coll.insert({_id: 1, a: [0, 1]}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(!res.repaired);
        assert.eq(0, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, {a: 1, b: 1}, true);

    // Insert a document, remove the document that makes the index multikey. Expect repair to
    // unset the multikey flag.
    assert.commandWorked(coll.insert({_id: 2, a: 1, b: 1}));
    assert.commandWorked(coll.remove({_id: 1}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(res.repaired);
        assert.eq(1, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, {a: 1, b: 1}, false);
});

// Test that validate will unset the multikey flag for an index that doesn't track path-level
// metadata.
runTest((coll) => {
    // Create an index, make the index multikey on 'a', and expect normal validation behavior.
    assert.commandWorked(coll.createIndex({a: 'text'}));
    assert.commandWorked(coll.insert({_id: 1, a: 'hello world'}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(!res.repaired);
        assert.eq(0, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, 'a_text', true);

    // Insert a document, remove the document that makes the index multikey. Expect repair to
    // unset the multikey flag.
    assert.commandWorked(coll.insert({_id: 2, a: 'test'}));
    assert.commandWorked(coll.remove({_id: 1}));
    assertValidate(coll, (res) => {
        assert(res.valid);
        assert(res.repaired);
        assert.eq(1, res.warnings.length);
        assert.eq(0, res.errors.length);
    });
    assertIndexMultikey(coll, 'a_text', false);
});

MongoRunner.stopMongod(conn);
})();