summaryrefslogtreecommitdiff
path: root/jstests/libs/clustered_collections/clustered_collection_create_index_clustered_common.js
blob: 83b95160714aaf31e6e13cf810d02ae8128844cd (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * Encapsulates testing that verifies the behavior of createIndexes with the 'clustered' option. The
 * 'clustered' option may not be used to create a clustered collection impicitly via createIndexes.
 */
const CreateIndexesClusteredTest = (function() {
    "use strict";

    /**
     * Tests that createIndex with the 'clustered' option fails when a collection exists and is not
     * clustered.
     */
    const runNonClusteredCollectionTest = function(testDB, collName) {
        assertDropCollection(testDB, collName);
        const testColl = testDB[collName];
        assert.commandWorked(testDB.createCollection(collName));

        // Start with the collection empty.
        assert.commandFailedWithCode(
            testColl.createIndex({_id: 1}, {clustered: true, unique: true}), 6243700);

        // Pass non-boolean value to safeBool 'clustered' option. Should be equivalent to next
        // command.
        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            "indexes": [{key: {"newKey": 1}, name: "anyName", clustered: 2, unique: true}],
        }),
                                     6243700);

        assert.commandFailedWithCode(testColl.createIndex({a: 1}, {clustered: true, unique: true}),
                                     6243700);

        // Insert some docs. Sometimes empty collections are treated as special when it comes to
        // index builds.
        const batchSize = 100;
        const bulk = testColl.initializeUnorderedBulkOp();
        for (let i = 0; i < batchSize; i++) {
            bulk.insert({_id: i, a: -i});
        }
        assert.commandWorked(bulk.execute());
        assert.commandFailedWithCode(
            testColl.createIndex({_id: 1}, {clustered: true, unique: true}), 6243700);

        // Pass non-boolean value to safeBool 'clustered' option. Should be equivalent to next
        // command.
        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            "indexes": [{key: {"newKey2": 1}, name: "anyName2", clustered: 2, unique: true}],
        }),
                                     6243700);

        assert.commandFailedWithCode(testColl.createIndex({a: 1}, {clustered: true, unique: true}),
                                     6243700);
    };

    /**
     * Tests running createIndex on a clustered collection.
     */
    const runClusteredCollectionTest = function(testDB, collName) {
        assertDropCollection(testDB, collName);

        // The createIndex 'clustered' option is disallowed for implicit collection creation.
        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            indexes: [{key: {_id: 1}, name: "_id_", clustered: true, unique: true}],
        }),
                                     6100900);

        // Create the clustered collection.
        const createOptions = {
            clusteredIndex: {key: {_id: 1}, name: "theClusterKeyName", unique: true}
        };
        assert.commandWorked(testDB.createCollection(collName, createOptions));

        // Confirm we start out with a valid clustered collection.
        const fullCreateOptions = ClusteredCollectionUtil.constructFullCreateOptions(createOptions);
        ClusteredCollectionUtil.validateListCollections(testDB, collName, fullCreateOptions);
        ClusteredCollectionUtil.validateListIndexes(testDB, collName, fullCreateOptions);

        const testColl = testDB[collName];

        // createIndex on the cluster key is a no-op.
        assert.commandWorked(testColl.createIndex({_id: 1}));
        ClusteredCollectionUtil.validateListIndexes(testDB, collName, fullCreateOptions);

        // createIndex on the cluster key with the 'clustered' option is a no-op.
        assert.commandWorked(testColl.createIndex({_id: 1}, {clustered: true, unique: true}));

        // Pass non-boolean value to safeBool 'clustered' option. Should be equivalent to next
        // command.
        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            "indexes": [{key: {"newKey": 1}, name: "anyName", clustered: 2, unique: true}],
        }),
                                     6243700);

        // 'clustered' is not a valid option for an index not on the cluster key.
        assert.commandFailedWithCode(
            testColl.createIndex({notMyIndex: 1}, {clustered: true, unique: true}), 6243700);

        // Insert some docs. Empty collections are treated as special (single phase) when
        // it comes to index builds.
        const batchSize = 100;
        const bulk = testColl.initializeUnorderedBulkOp();
        for (let i = 0; i < batchSize; i++) {
            bulk.insert({_id: i, a: -i});
        }
        assert.commandWorked(bulk.execute());

        assert.commandWorked(testColl.createIndex({_id: 1}));
        assert.commandWorked(testColl.createIndex({_id: 1}, {clustered: true, unique: true}));

        // Pass non-boolean value to safeBool 'clustered' option. Should be equivalent to next
        // command.
        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            "indexes": [{key: {"newKey2": 1}, name: "anyName2", clustered: 2, unique: true}],
        }),
                                     6243700);

        // 'clustered' is still not a valid option for an index not on the cluster key.
        assert.commandFailedWithCode(testColl.createIndex({a: 1}, {clustered: true, unique: true}),
                                     6243700);

        assert.commandFailedWithCode(testDB.runCommand({
            createIndexes: collName,
            indexes: [
                {key: {_id: 1}, name: "_id_", clustered: true, unique: true},
                {key: {a: 1}, name: "a_1", clustered: true, unique: true}
            ],
        }),
                                     6243700);

        // Note: this a quirk of how we handle the 'name' field for indexes of {_id: 1}. The
        // createIndex is still a no-op, and the specified name is discarded.
        //
        // Only in implicit collection creation on a non-existent collection can createIndex create
        // a clusteredIndex with a custom name.
        assert.commandWorked(testColl.createIndex({_id: 1}, {name: "notTheClusterKeyName"}));

        ClusteredCollectionUtil.validateListIndexes(testDB, collName, fullCreateOptions);
    };

    /**
     * Runs test cases that are agnostic to whether the database is replicated or not.
     */
    const runBaseTests = function(testDB, collName) {
        runNonClusteredCollectionTest(testDB, collName);
        runClusteredCollectionTest(testDB, collName);
    };

    return {
        runBaseTests: runBaseTests,
    };
})();