summaryrefslogtreecommitdiff
path: root/jstests/sortl.js
blob: 34f33639236c9d58127e6629e29634d3adb19ded (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
// When one plan among candidate plans of mixed ordering types is cached, and then replayed, and the
// remaining plans are then attempted, those remaining plans are iterated properly for their
// ordering type.  SERVER-5301

t = db.jstests_sortl;
t.drop();

t.ensureIndex( { a:1 } );
t.ensureIndex( { b:1 } );

function recordIndex( index, query, sort ) {
    // Run a query that records the desired index.
    t.find( query ).sort( sort ).explain();
    // Check that the desired index is recorded.
    assert.eq( 'BtreeCursor ' + index,
              t.find( query ).sort( sort ).explain( true ).oldPlan.cursor );
}

function checkBOrdering( result ) {
    for( i = 1; i < result.length; ++i ) {
        assert.lt( result[ i - 1 ].b, result[ i ].b );
    }
}

// An out of order plan is recorded, then an in order plan takes over.
t.save( { a:1 } );
big = new Array( 1000000 ).toString();
for( i = 0; i < 40; ++i ) {
    t.save( { a:2, b:i, c:big } );
}

recordIndex( 'a_1', { a:1 }, { b:1 } );
result = t.find( { a:2 }, { a:1, b:1 } ).sort( { b:1 } ).toArray();
assert.eq( 40, result.length );
checkBOrdering( result );

// An optimal in order plan is recorded and reused.
recordIndex( 'b_1', { b:{ $gte:0 } }, { b:1 } );
result = t.find( { b:{ $gte:0 } }, { b:1 } ).sort( { b:1 } ).toArray();
assert.eq( 40, result.length );
checkBOrdering( result );

t.remove();

// An in order plan is recorded, then an out of order plan is added.
for( i = 0; i < 20; ++i ) {
    t.save( { a:1, b:19-i } );
}

recordIndex( 'b_1', { a:1, b:{ $gte:19 } }, { b:1 } );
result = t.find( { a:1, b:{ $gte:0 } } ).sort( { b:1 } ).toArray();
assert.eq( 20, result.length );
checkBOrdering( result );