summaryrefslogtreecommitdiff
path: root/jstests/libs/get_index_helpers.js
blob: 77468ab17cb986217b9e070708864b794d6ac5e1 (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
"use strict";

/**
 * Helpers for filtering the index specifications returned by DBCollection.prototype.getIndexes().
 */
var GetIndexHelpers = (function() {

    /**
     * Returns the index specification with the name 'indexName' if it is present in the
     * 'indexSpecs' array, and returns null otherwise.
     */
    function getIndexSpecByName(indexSpecs, indexName) {
        if (typeof indexName !== "string") {
            throw new Error("'indexName' parameter must be a string, but got " + tojson(indexName));
        }

        const found = indexSpecs.filter(spec => spec.name === indexName);

        if (found.length > 1) {
            throw new Error("Found multiple indexes with name '" + indexName + "': " +
                            tojson(indexSpecs));
        }
        return (found.length === 1) ? found[0] : null;
    }

    /**
     * Returns the index specification with the key pattern 'keyPattern' and the collation
     * 'collation' if it is present in the 'indexSpecs' array, and returns null otherwise.
     *
     * The 'collation' parameter is optional and is only required to be specified when multiple
     * indexes with the same key pattern exist.
     */
    function getIndexSpecByKeyPattern(indexSpecs, keyPattern, collation) {
        const collationWasSpecified = arguments.length >= 3;
        const foundByKeyPattern = indexSpecs.filter(spec => {
            return bsonWoCompare(spec.key, keyPattern) === 0;
        });

        if (!collationWasSpecified) {
            if (foundByKeyPattern.length > 1) {
                throw new Error("Found multiple indexes with key pattern " + tojson(keyPattern) +
                                " and 'collation' parameter was not specified: " +
                                tojson(indexSpecs));
            }
            return (foundByKeyPattern.length === 1) ? foundByKeyPattern[0] : null;
        }

        const foundByKeyPatternAndCollation = foundByKeyPattern.filter(spec => {
            if (collation.locale === "simple") {
                // The simple collation is not explicitly stored in the index catalog, so we expect
                // the "collation" field to be absent.
                return !spec.hasOwnProperty("collation");
            }
            return bsonWoCompare(spec.collation, collation) === 0;
        });

        if (foundByKeyPatternAndCollation.length > 1) {
            throw new Error("Found multiple indexes with key pattern" + tojson(keyPattern) +
                            " and collation " + tojson(collation) + ": " + tojson(indexSpecs));
        }
        return (foundByKeyPatternAndCollation.length === 1) ? foundByKeyPatternAndCollation[0]
                                                            : null;
    }

    return {
        findByName: getIndexSpecByName,
        findByKeyPattern: getIndexSpecByKeyPattern,
    };
})();