summaryrefslogtreecommitdiff
path: root/jstests/core/query/sort/sortg.js
blob: 7e6186af0977dba4d99ecef9509261414859505c (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
// Cannot implicitly shard accessed collections because of extra shard key index in sharded
// collection.
// @tags: [
//   assumes_no_implicit_index_creation,
//   requires_getmore,
// ]

// Test that a memory exception is triggered for in memory sorts, but not for indexed sorts.
(function() {
"use strict";

const t = db.jstests_sortg;
t.drop();

const big = new Array(1000000).toString();

let i;
for (i = 0; i < 100; ++i) {
    t.save({b: 0});
}

for (i = 0; i < 110; ++i) {
    t.save({a: 0, x: big});
}

function memoryException(sortSpec, querySpec) {
    querySpec = querySpec || {};
    var ex = assert.throwsWithCode(
        () => t.find(querySpec).sort(sortSpec).allowDiskUse(false).batchSize(1000).itcount(),
        ErrorCodes.QueryExceededMemoryLimitNoDiskUseAllowed);
    assert(ex.toString().match(/Sort/));
}

function noMemoryException(sortSpec, querySpec) {
    querySpec = querySpec || {};
    t.find(querySpec).sort(sortSpec).allowDiskUse(false).batchSize(1000).itcount();
}

// Unindexed sorts.
memoryException({a: 1});
memoryException({b: 1});

// Indexed sorts.
noMemoryException({_id: 1});
noMemoryException({$natural: 1});

assert.eq(1, t.getIndexes().length);

t.createIndex({a: 1});
t.createIndex({b: 1});
t.createIndex({c: 1});

assert.eq(4, t.getIndexes().length);

// These sorts are now indexed.
noMemoryException({a: 1});
noMemoryException({b: 1});

// A memory exception is triggered for an unindexed sort involving multiple plans.
memoryException({d: 1}, {b: null, c: null});

// With an indexed plan on _id:1 and an unindexed plan on b:1, the indexed plan should succeed
// even if the unindexed one would exhaust its memory limit.
noMemoryException({_id: 1}, {b: null});

// With an unindexed plan on b:1 recorded for a query, the query should be retried when the
// unindexed plan exhausts its memory limit.
noMemoryException({_id: 1}, {b: null});
t.drop();
})();