From df8c1abf7dfede6f718655833f5be3a3a41737a2 Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Mon, 29 Feb 2016 14:05:17 -0600 Subject: [PATCH] fast path for mongodb batches (otherwise it's O(n^2) memory, which gets ugly fast) --- src/batch.js | 7 ++++++- src/database/mongo/sorted.js | 38 +++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/batch.js b/src/batch.js index 70ccd8df01..1a425e1a21 100644 --- a/src/batch.js +++ b/src/batch.js @@ -23,6 +23,11 @@ var async = require('async'), return callback(new Error('[[error:process-not-a-function]]')); } + // use the fast path if possible + if (db.processSortedSet && typeof options.doneIf !== 'function' && !utils.isNumber(options.alwaysStartAt)) { + return db.processSortedSet(setKey, process, options.batch || DEFAULT_BATCH_SIZE, callback); + } + // custom done condition options.doneIf = typeof options.doneIf === 'function' ? options.doneIf : function(){}; @@ -58,4 +63,4 @@ var async = require('async'), ); }; -}(exports)); \ No newline at end of file +}(exports)); diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index ba392fa42a..08d7310a3e 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -533,4 +533,40 @@ module.exports = function(db, module) { callback(err, data); }); }; -}; \ No newline at end of file + + module.processSortedSet = function(setKey, process, batch, callback) { + var done = false, ids = [], cursor = db.collection('objects'). + find({_key: setKey}). + sort({score: 1}). + project({_id: 0, value: 1}). + batchSize(batch); + + async.whilst( + function() { + return !done; + }, + function(next) { + cursor.next(function(err, item) { + if (err) { + return next(err); + } + if (item === null) { + done = true; + } else { + ids.push(item.value); + } + + if (ids.length < batch && (!done || ids.length === 0)) { + return next(null); + } + + process(ids, function(err) { + ids = []; + return next(err); + }); + }); + }, + callback + ); + }; +};