summaryrefslogtreecommitdiff
path: root/jstests/core/find_getmore_bsonsize.js
blob: 904a9c33ab03aa9cab171c851f88853bf919509b (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
// Ensure that the find and getMore commands can handle documents nearing the 16 MB size limit for
// user-stored BSON documents.
(function() {
    'use strict';

    var cmdRes;
    var collName = 'find_getmore_bsonsize';
    var coll = db[collName];

    coll.drop();

    var oneKB = 1024;
    var oneMB = 1024 * oneKB;

    // Build a (1 MB - 1 KB) string.
    var smallStr = 'x';
    while (smallStr.length < oneMB) {
        smallStr += smallStr;
    }
    assert.eq(smallStr.length, oneMB);
    smallStr = smallStr.substring(0, oneMB - oneKB);

    // Build a (16 MB - 1 KB) string.
    var bigStr = 'y';
    while (bigStr.length < (16 * oneMB)) {
        bigStr += bigStr;
    }
    assert.eq(bigStr.length, 16 * oneMB);
    bigStr = bigStr.substring(0, (16 * oneMB) - oneKB);

    // Collection has one ~1 MB doc followed by one ~16 MB doc.
    assert.writeOK(coll.insert({_id: 0, padding: smallStr}));
    assert.writeOK(coll.insert({_id: 1, padding: bigStr}));

    // Find command should just return the first doc, as adding the last would create an invalid
    // command response document.
    cmdRes = db.runCommand({find: collName});
    assert.commandWorked(cmdRes);
    assert.gt(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.firstBatch.length, 1);

    // The 16 MB doc should be returned alone on getMore. This is the last document in the
    // collection, so the server should close the cursor.
    cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName});
    assert.eq(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.nextBatch.length, 1);

    // Setup a cursor without returning any results (batchSize of zero).
    cmdRes = db.runCommand({find: collName, batchSize: 0});
    assert.commandWorked(cmdRes);
    assert.gt(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.firstBatch.length, 0);

    // First getMore should only return one doc, since both don't fit in the response.
    cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName});
    assert.gt(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.nextBatch.length, 1);

    // Second getMore should return the second doc and a third will close the cursor.
    cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName});
    assert.eq(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.nextBatch.length, 1);

    coll.drop();

    // Insert a document of exactly 16MB and make sure the find command can return it.
    bigStr = 'y';
    while (bigStr.length < (16 * oneMB)) {
        bigStr += bigStr;
    }
    bigStr = bigStr.substring(0, (16 * oneMB) - 32);
    var maxSizeDoc = {
        _id: 0,
        padding: bigStr
    };
    assert.eq(Object.bsonsize(maxSizeDoc), 16 * oneMB);
    assert.writeOK(coll.insert(maxSizeDoc));

    cmdRes = db.runCommand({find: collName});
    assert.commandWorked(cmdRes);
    assert.eq(cmdRes.cursor.id, NumberLong(0));
    assert.eq(cmdRes.cursor.ns, coll.getFullName());
    assert.eq(cmdRes.cursor.firstBatch.length, 1);
})();