summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/hybrid_index_timeseries.js
blob: 4781272217bbda17843de690a0e88de68626a7f8 (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
/**
 * Tests that hybrid index builds on timeseries buckets collections behave correctly when they
 * receive concurrent writes.
 */
load("jstests/noPassthrough/libs/index_build.js");

(function() {
"use strict";

const conn = MongoRunner.runMongod();

const dbName = jsTestName();
const collName = 'ts';
const testDB = conn.getDB(dbName);
const tsColl = testDB[collName];
const bucketsColl = testDB.getCollection('system.buckets.' + collName);

const timeField = 'time';
const metaField = 'meta';

const runTest = (config) => {
    // Populate the collection.
    tsColl.drop();

    assert.commandWorked(testDB.createCollection(
        collName, {timeseries: {timeField: timeField, metaField: metaField}}));

    let nDocs = 10;
    for (let i = 0; i < nDocs; i++) {
        assert.commandWorked(tsColl.insert({
            _id: i,
            [timeField]: new Date(),
        }));
    }

    jsTestLog("Testing: " + tojson(config));

    // Prevent the index build from completing.
    IndexBuildTest.pauseIndexBuilds(conn);

    // Build an index on the buckets collection, not the time-series view, and wait for it to start.
    const indexName = "test_index";
    let indexOptions = config.indexOptions || {};
    indexOptions.name = indexName;
    const awaitIndex = IndexBuildTest.startIndexBuild(
        conn, bucketsColl.getFullName(), config.indexSpec, indexOptions);
    IndexBuildTest.waitForIndexBuildToStart(testDB, bucketsColl.getName(), indexName);

    // Perform writes while the index build is in progress.
    assert.commandWorked(tsColl.insert({
        _id: nDocs++,
        [timeField]: new Date(),
    }));

    let extraDocs = config.extraDocs || [];
    extraDocs.forEach((doc) => {
        const template = {_id: nDocs++, [timeField]: new Date()};
        const newObj = Object.assign({}, doc, template);
        assert.commandWorked(tsColl.insert(newObj));
    });

    // Allow the index build to complete.
    IndexBuildTest.resumeIndexBuilds(conn);
    awaitIndex();

    // Due to the nature of bucketing, we can't reliably make assertions about the contents of the
    // buckets collection, so we rely on validate to ensure the index is built correctly.
    const validateRes = assert.commandWorked(bucketsColl.validate());
    assert(validateRes.valid, validateRes);
};

const basicOps = [
    {[metaField]: -Math.pow(-2147483648, 34)},  // -Inf
    {[metaField]: 0},
    {[metaField]: 0},
    {[metaField]: {foo: 'bar'}},
    {[metaField]: {foo: 1}},
    {[metaField]: 'hello world'},
    {[metaField]: 1},
    {},
    {[metaField]: Math.pow(-2147483648, 34)},  // Inf
];

runTest({
    indexSpec: {[metaField]: 1},
    extraDocs: basicOps,
});

runTest({
    indexSpec: {[metaField]: -1},
    extraDocs: basicOps,
});

runTest({
    indexSpec: {[metaField]: 'hashed'},
    extraDocs: basicOps,

});

runTest({
    indexSpec: {[metaField + '.$**']: 1},
    extraDocs: basicOps,
});
runTest({
    indexSpec: {[metaField + '$**']: 1},
    extraDocs: basicOps,
});

runTest({
    indexSpec: {[metaField]: '2dsphere'},
    extraDocs: [
        {[metaField]: {type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]}},
        {[metaField]: {type: 'Point', coordinates: [0, 1]}},
    ]
});

runTest({
    indexSpec: {[metaField]: '2dsphere'},
    indexOptions: {sparse: true},
    extraDocs: [
        {[metaField]: {type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]}},
        {[metaField]: {type: 'Point', coordinates: [0, 1]}},
        {},
    ]
});

runTest({
    indexSpec: {'control.min.time': 1},
    extraDocs: basicOps,
});

runTest({
    indexSpec: {'control.max.time': -1},
    extraDocs: basicOps,
});

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