|
| 1 | +// Ensure that the find and getMore commands can handle documents nearing the 16 MB size limit for |
| 2 | +// user-stored BSON documents. |
| 3 | +(function() { |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + var cmdRes; |
| 7 | + var collName = 'find_getmore_cmd'; |
| 8 | + var coll = db[collName]; |
| 9 | + |
| 10 | + coll.drop(); |
| 11 | + |
| 12 | + var oneKB = 1024; |
| 13 | + var oneMB = 1024 * oneKB; |
| 14 | + |
| 15 | + // Build a 1 MB - 1 KB) string. |
| 16 | + var smallStr = 'x'; |
| 17 | + while (smallStr.length < oneMB) { |
| 18 | + smallStr += smallStr; |
| 19 | + } |
| 20 | + assert.eq(smallStr.length, oneMB); |
| 21 | + smallStr = smallStr.substring(0, oneMB - oneKB); |
| 22 | + |
| 23 | + // Build a (16 MB - 1 KB) string. |
| 24 | + var bigStr = 'y'; |
| 25 | + while (bigStr.length < (16 * oneMB)) { |
| 26 | + bigStr += bigStr; |
| 27 | + } |
| 28 | + assert.eq(bigStr.length, 16 * oneMB); |
| 29 | + bigStr = bigStr.substring(0, (16 * oneMB) - oneKB); |
| 30 | + |
| 31 | + // Collection has one ~1 MB doc followed by one ~16 MB doc. |
| 32 | + assert.writeOK(coll.insert({_id: 0, padding: smallStr})); |
| 33 | + assert.writeOK(coll.insert({_id: 1, padding: bigStr})); |
| 34 | + |
| 35 | + // Find command should just return the first doc, as adding the last would create an invalid |
| 36 | + // command response document. |
| 37 | + cmdRes = db.runCommand({find: collName}); |
| 38 | + assert.commandWorked(cmdRes); |
| 39 | + assert.gt(cmdRes.cursor.id, NumberLong(0)); |
| 40 | + assert.eq(cmdRes.cursor.ns, coll.getFullName()); |
| 41 | + assert.eq(cmdRes.cursor.firstBatch.length, 1); |
| 42 | + |
| 43 | + // The 16 MB doc should be returned on getMore. |
| 44 | + cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName}); |
| 45 | + assert.eq(cmdRes.cursor.id, NumberLong(0)); |
| 46 | + assert.eq(cmdRes.cursor.ns, coll.getFullName()); |
| 47 | + assert.eq(cmdRes.cursor.nextBatch.length, 1); |
| 48 | + |
| 49 | + // Setup a cursor without returning any results (batchSize of zero). |
| 50 | + cmdRes = db.runCommand({find: collName, batchSize: 0}); |
| 51 | + assert.commandWorked(cmdRes); |
| 52 | + assert.gt(cmdRes.cursor.id, NumberLong(0)); |
| 53 | + assert.eq(cmdRes.cursor.ns, coll.getFullName()); |
| 54 | + assert.eq(cmdRes.cursor.firstBatch.length, 0); |
| 55 | + |
| 56 | + // First getMore should only return one doc, since both don't fit in the response. |
| 57 | + cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName}); |
| 58 | + assert.gt(cmdRes.cursor.id, NumberLong(0)); |
| 59 | + assert.eq(cmdRes.cursor.ns, coll.getFullName()); |
| 60 | + assert.eq(cmdRes.cursor.nextBatch.length, 1); |
| 61 | + |
| 62 | + // Second getMore should return the second doc and close the cursor. |
| 63 | + cmdRes = db.runCommand({getMore: cmdRes.cursor.id, collection: collName}); |
| 64 | + assert.eq(cmdRes.cursor.id, NumberLong(0)); |
| 65 | + assert.eq(cmdRes.cursor.ns, coll.getFullName()); |
| 66 | + assert.eq(cmdRes.cursor.nextBatch.length, 1); |
| 67 | +})(); |
0 commit comments