summaryrefslogtreecommitdiff
path: root/jstests/core/list_indexes.js
blob: 96aad46e5559572b839288847d2cb892f85ae8c0 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Basic functional tests for the listIndexes command.

(function () {
    "use strict";

    var coll = db.list_indexes1;
    var cursor;
    var res;
    var specs;

    //
    // Test basic command output.
    //

    coll.drop();
    assert.commandWorked(coll.getDB().createCollection(coll.getName()));
    res = coll.runCommand("listIndexes");
    assert.commandWorked(res);
    assert.eq("object", typeof(res.cursor));
    assert.eq(0, res.cursor.id);
    assert.eq("string", typeof(res.cursor.ns));
    assert.eq(1, res.cursor.firstBatch.length);
    assert.eq("_id_", res.cursor.firstBatch[0].name);

    //
    // Test basic usage with DBCommandCursor.
    //

    var getListIndexesCursor = function(coll, options, subsequentBatchSize) {
        return new DBCommandCursor(coll.getDB().getMongo(),
                                   coll.runCommand("listIndexes", options),
                                   subsequentBatchSize);
    };

    var cursorGetIndexSpecs = function(cursor) {
        return cursor.toArray().sort(function(a, b) { return a.name > b.name; });
    };

    var cursorGetIndexNames = function(cursor) {
        return cursorGetIndexSpecs(cursor).map(function(spec) { return spec.name; });
    };

    coll.drop();
    assert.commandWorked(coll.getDB().createCollection(coll.getName()));
    assert.eq(["_id_"], cursorGetIndexNames(getListIndexesCursor(coll)));

    //
    // Test that the index metadata object is returned correctly.
    //

    coll.drop();
    assert.commandWorked(coll.getDB().createCollection(coll.getName()));
    assert.commandWorked(coll.ensureIndex({a: 1}, {unique: true}));
    specs = cursorGetIndexSpecs(getListIndexesCursor(coll));
    assert.eq(2, specs.length);
    assert.eq("_id_", specs[0].name);
    assert.eq(coll.getFullName(), specs[0].ns);
    assert.eq({_id: 1}, specs[0].key);
    assert(!specs[0].hasOwnProperty("unique"));
    assert.eq("a_1", specs[1].name);
    assert.eq(coll.getFullName(), specs[1].ns);
    assert.eq({a: 1}, specs[1].key);
    assert.eq(true, specs[1].unique);

    //
    // Test that the command does not accept invalid values for the collection.
    //

    assert.commandFailed(coll.getDB().runCommand({listIndexes: ""}));
    assert.commandFailed(coll.getDB().runCommand({listIndexes: 1}));
    assert.commandFailed(coll.getDB().runCommand({listIndexes: {}}));
    assert.commandFailed(coll.getDB().runCommand({listIndexes: []}));

    //
    // Test basic usage of "cursor.batchSize" option.
    //

    // TODO: When SERVER-19569 is done, we should be able to run this test regardless of whether we
    // are using the find/getMore commands, both against a standalone server and passed through
    // mongos.
    if (!coll.getDB().getMongo().useReadCommands()) {
        coll.drop();
        assert.commandWorked(coll.getDB().createCollection(coll.getName()));
        assert.commandWorked(coll.ensureIndex({a: 1}, {unique: true}));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: 2}});
        assert.eq(2, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: 1}});
        assert.eq(1, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: 0}});
        assert.eq(0, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: NumberInt(2)}});
        assert.eq(2, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: NumberLong(2)}});
        assert.eq(2, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: Math.pow(2, 62)}});
        assert.eq(2, cursor.objsLeftInBatch());
        assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));
    }

    // Ensure that the server accepts an empty object for "cursor".  This is equivalent to not
    // specifying "cursor" at all.
    //
    // We do not test for objsLeftInBatch() here, since the default batch size for this command is
    // not specified.
    cursor = getListIndexesCursor(coll, {cursor: {}});
    assert.eq(["_id_", "a_1"], cursorGetIndexNames(cursor));

    //
    // Test more than 2 batches of results.
    //

    // TODO: When SERVER-19569 is done, we should be able to run this test regardless of whether we
    // are using the find/getMore commands, both against a standalone server and passed through
    // mongos.
    if (!coll.getDB().getMongo().useReadCommands()) {
        coll.drop();
        assert.commandWorked(coll.getDB().createCollection(coll.getName()));
        assert.commandWorked(coll.ensureIndex({a: 1}, {unique: true}));
        assert.commandWorked(coll.ensureIndex({b: 1}, {unique: true}));
        assert.commandWorked(coll.ensureIndex({c: 1}, {unique: true}));

        cursor = getListIndexesCursor(coll, {cursor: {batchSize: 0}}, 2);
        assert.eq(0, cursor.objsLeftInBatch());
        assert(cursor.hasNext());
        assert.eq(2, cursor.objsLeftInBatch());

        cursor.next();
        assert(cursor.hasNext());
        assert.eq(1, cursor.objsLeftInBatch());

        cursor.next();
        assert(cursor.hasNext());
        assert.eq(2, cursor.objsLeftInBatch());

        cursor.next();
        assert(cursor.hasNext());
        assert.eq(1, cursor.objsLeftInBatch());

        cursor.next();
        assert(!cursor.hasNext());
    }

    //
    // Test on collection with no indexes.
    //

    coll.drop();
    assert.commandWorked(coll.getDB().createCollection(coll.getName(), {autoIndexId: false}));
    assert.eq([], cursorGetIndexNames(getListIndexesCursor(coll)));

    //
    // Test killCursors against a listCollections cursor.
    //

    coll.drop();
    assert.commandWorked(coll.getDB().createCollection(coll.getName()));
    assert.commandWorked(coll.ensureIndex({a: 1}, {unique: true}));
    assert.commandWorked(coll.ensureIndex({b: 1}, {unique: true}));
    assert.commandWorked(coll.ensureIndex({c: 1}, {unique: true}));

    res = coll.runCommand("listIndexes", {cursor: {batchSize: 0}});
    cursor = new DBCommandCursor(coll.getDB().getMongo(), res, 2);
    cursor = null;
    gc(); // Shell will send a killCursors message when cleaning up underlying cursor.
    cursor = new DBCommandCursor(coll.getDB().getMongo(), res, 2);
    assert.throws(function() { cursor.hasNext(); });
}());