summaryrefslogtreecommitdiff
path: root/jstests/reversecursor.js
blob: bb661952fc9c909d436aa32178dd4b5d85106ee7 (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
// Test to make sure that a reverse cursor can correctly handle empty extents (SERVER-6980)

// Create a collection with three small extents
db.jstests_reversecursor.drop();
db.runCommand({"create":"jstests_reversecursor", $nExtents: [4096,4096,4096]});

// Function to check whether all three extents are non empty
function extentsSpanned() {
    var extents = db.jstests_reversecursor.validate(true).extents;
    return (extents[0].firstRecord != "null" &&
            extents[1].firstRecord != "null" &&
            extents[2].firstRecord != "null");
}

// Insert enough documents to span all three extents
a = 0;
while (!extentsSpanned()) {
    db.jstests_reversecursor.insert({a:a++});
}

// Delete all the elements in the middle
db.jstests_reversecursor.remove({a:{$gt:0,$lt:a-1}});

// Make sure the middle extent is empty and that both end extents are not empty
assert.eq(db.jstests_reversecursor.validate(true).extents[1].firstRecord, "null");
assert.eq(db.jstests_reversecursor.validate(true).extents[1].lastRecord, "null");
assert.neq(db.jstests_reversecursor.validate(true).extents[0].firstRecord, "null");
assert.neq(db.jstests_reversecursor.validate(true).extents[0].lastRecord, "null");
assert.neq(db.jstests_reversecursor.validate(true).extents[2].firstRecord, "null");
assert.neq(db.jstests_reversecursor.validate(true).extents[2].lastRecord, "null");

// Make sure that we get the same number of elements for both the forward and reverse cursors
assert.eq(db.jstests_reversecursor.find().sort({$natural:1}).toArray().length, 2);
assert.eq(db.jstests_reversecursor.find().sort({$natural:-1}).toArray().length, 2);