summaryrefslogtreecommitdiff
path: root/jstests/mmap_v1/capped7.js
blob: fa19c17af127518c49eee69e8e00d8bfe1bf4bf9 (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
// Test capped collection truncate via 'emptycapped' command

Random.setRandomSeed();

db.capped7.drop();
db._dbCommand({create: "capped7", capped: true, size: 1000, $nExtents: 11, autoIndexId: false});
tzz = db.capped7;

var ten = new Array(11).toString().replace(/,/g, "-");

count = 0;

/**
 * Insert new documents until the capped collection loops and the document
 * count doesn't increase on insert.
 */
function insertUntilFull() {
    count = tzz.count();
    var j = 0;
    while (1) {
        tzz.save({i: ten, j: j++});
        var newCount = tzz.count();
        if (count == newCount) {
            break;
        }
        count = newCount;
    }
}

insertUntilFull();

// oldCount == count before empty
oldCount = count;

assert.eq.automsg("11", "tzz.stats().numExtents");

// oldSize == size before empty
var oldSize = tzz.stats().storageSize;

assert.commandWorked(db._dbCommand({emptycapped: "capped7"}));

// check that collection storage parameters are the same after empty
assert.eq.automsg("11", "tzz.stats().numExtents");
assert.eq.automsg("oldSize", "tzz.stats().storageSize");

// check that the collection is empty after empty
assert.eq.automsg("0", "tzz.find().itcount()");
assert.eq.automsg("0", "tzz.count()");

// check that we can reuse the empty collection, inserting as many documents
// as we were able to the first time through.
insertUntilFull();
assert.eq.automsg("oldCount", "count");
assert.eq.automsg("oldCount", "tzz.find().itcount()");
assert.eq.automsg("oldCount", "tzz.count()");

assert.eq.automsg("11", "tzz.stats().numExtents");
var oldSize = tzz.stats().storageSize;

assert.commandWorked(db._dbCommand({emptycapped: "capped7"}));

// check that the collection storage parameters are unchanged after another empty
assert.eq.automsg("11", "tzz.stats().numExtents");
assert.eq.automsg("oldSize", "tzz.stats().storageSize");

// insert an arbitrary number of documents
var total = Random.randInt(2000);
for (var j = 1; j <= total; ++j) {
    tzz.save({i: ten, j: j});
    // occasionally check that only the oldest documents are removed to make room
    // for the newest documents
    if (Random.rand() > 0.95) {
        assert.automsg("j >= tzz.count()");
        assert.eq.automsg("tzz.count()", "tzz.find().itcount()");
        var c = tzz.find().sort({$natural: -1});
        var k = j;
        assert.automsg("c.hasNext()");
        while (c.hasNext()) {
            assert.eq.automsg("c.next().j", "k--");
        }
        // check the same thing with a reverse iterator as well
        var c = tzz.find().sort({$natural: 1});
        assert.automsg("c.hasNext()");
        while (c.hasNext()) {
            assert.eq.automsg("c.next().j", "++k");
        }
        assert.eq.automsg("j", "k");
    }
}