summaryrefslogtreecommitdiff
path: root/jstests/core/queryoptimizera.js
blob: f6950c81113792fd47fc8c85ff4b793dd25fa5b2 (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
// @tags: [does_not_support_stepdowns, requires_capped]

// Check that a warning message about doing a capped collection scan for a query with an _id
// constraint is printed at appropriate times.  SERVER-5353

function numWarnings() {
    logs = db.adminCommand({getLog: "global"}).log;
    ret = 0;
    logs.forEach(function(x) {
        if (x.match(warningMatchRegexp)) {
            ++ret;
        }
    });
    return ret;
}

collectionNameIndex = 0;

// Generate a collection name not already present in the log.
do {
    testCollectionName = 'jstests_queryoptimizera__' + collectionNameIndex++;
    warningMatchString =
        'unindexed _id query on capped collection.*collection: test.' + testCollectionName;
    warningMatchRegexp = new RegExp(warningMatchString);

} while (numWarnings() > 0);

t = db[testCollectionName];
t.drop();

notCappedCollectionName = testCollectionName + '_notCapped';

notCapped = db.getSiblingDB("local").getCollection(notCappedCollectionName);
notCapped.drop();

assert.commandWorked(db.createCollection(testCollectionName, {capped: true, size: 1000}));
assert.commandWorked(
    notCapped.getDB().createCollection(notCappedCollectionName, {autoIndexId: false}));

t.insert({});
notCapped.insert({});

oldNumWarnings = 0;

function assertNoNewWarnings() {
    assert.eq(oldNumWarnings, numWarnings());
}

function assertNewWarning() {
    newNumWarnings = numWarnings();
    // Ensure that newNumWarnings > oldNumWarnings.  It's not safe to test that oldNumWarnings + 1
    // == newNumWarnings, because a (simulated) page fault exception may cause multiple messages to
    // be logged instead of only one.
    assert.lt(oldNumWarnings, newNumWarnings);
    oldNumWarnings = newNumWarnings;
}

// Simple _id query
t.find({_id: 0}).itcount();
assertNoNewWarnings();

// Simple _id query without an _id index, on a non capped collection.
notCapped.find({_id: 0}).itcount();
assertNoNewWarnings();

// A multi field query, including _id.
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();

// An unsatisfiable query.
t.find({_id: 0, a: {$in: []}}).itcount();
assertNoNewWarnings();

// An hinted query.
t.find({_id: 0}).hint({$natural: 1}).itcount();
assertNoNewWarnings();

// Retry a multi field query.
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();

// Warnings should not be printed when an index is added on _id.
t.ensureIndex({_id: 1});

t.find({_id: 0}).itcount();
assertNoNewWarnings();

t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();

t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();

t.drop();  // cleanup
notCapped.drop();