jstests/noPassthroughWithMongod/capped6.js
/**
 * 1. When this function is called the first time, insert new documents until 'maxDocuments'
 *    number of documents have been inserted. Note that the collection may not have
 *    'maxDocuments' number of documents since it is a capped collection.
 * 2. Remove all but one documents via one or more "captrunc" requests.
 * 3. For each subsequent call to this function, keep track of the removed documents using
 *    'valueArrayIndexes' and re-insert the removed documents each time this function is
 *    called.
 */
1. When this function is called the first time, insert new documents until 'maxDocuments'
number of documents have been inserted. Note that the collection may not have
'maxDocuments' number of documents since it is a capped collection.
2. Remove all but one documents via one or more "captrunc" requests.
3. For each subsequent call to this function, keep track of the removed documents using
'valueArrayIndexes' and re-insert the removed documents each time this function is
called.
1 . When this function is called the first time insert new documents until ' maxDocuments ' number of documents have been inserted . Note that the collection may not have ' maxDocuments ' number of documents since it is a capped collection . 2 . Remove all but one documents via one or more " captrunc " requests . 3 . For each subsequent call to this function keep track of the removed documents using ' valueArrayIndexes ' and re - insert the removed documents each time this function is called .
function runCapTrunc(valueArray, valueArrayCurIndex, n, inc) {
    assert.gt(n, 0);
    assert.gte(valueArray.length, maxDocuments);
    for (var i = valueArrayCurIndex; i < maxDocuments; ++i) {
        assert.commandWorked(coll.insert(valueArray[i]));
    }
    count = coll.count();
    valueArrayCurIndex = maxDocuments - 1;
    var iterations = Math.floor((count - 1) / (n + inc));
    for (i = 0; i < iterations; ++i) {
        assert.commandWorked(db.runCommand({captrunc: "capped6", n: n, inc: inc}));
        count -= (n + inc);
        valueArrayCurIndex -= (n + inc);
        checkOrder(valueArrayCurIndex, valueArray);
    }
    return valueArrayCurIndex + 1;
}
function runCapTrunc ( valueArray , valueArrayCurIndex , n , inc ) { assert . gt ( n , 0 ) ; assert . gte ( valueArray . length , maxDocuments ) ; for ( var i = valueArrayCurIndex ; i < maxDocuments ; ++ i ) { assert . commandWorked ( coll . insert ( valueArray [ i ] ) ) ; } count = coll . count ( ) ; valueArrayCurIndex = maxDocuments - 1 ; var iterations = Math . floor ( ( count - 1 ) / ( n + inc ) ) ; for ( i = 0 ; i < iterations ; ++ i ) { assert . commandWorked ( db . runCommand ( { captrunc : "capped6" , n : n , inc : inc } ) ) ; count -= ( n + inc ) ; valueArrayCurIndex -= ( n + inc ) ; checkOrder ( valueArrayCurIndex , valueArray ) ; } return valueArrayCurIndex + 1 ; }
1.
1 .
// If n <= 0, no documents are removed by captrunc. // The index corresponding to the last document in the collection. // Number of times to call "captrunc" so that (count - 1) documents are removed // and at least 1 document is left in the array. // We return the index of the next document that should be inserted into the capped // collection, which would be the document after valueArrayCurIndex.