Skip to content

Commit

Permalink
perf: process large queries in chunks
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Jan 10, 2020
1 parent 95178d1 commit d8ca382
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,30 @@ DataAccessObject.find = function find(query, options, cb) {
cb(err, results);
}

processQueryResults(data, done);
// FIXME(bajtos) read this value from Model settings
const CHUNK_SIZE = 500;
if (data.length <= CHUNK_SIZE) {
processQueryResults(data, done);
} else {
const chunks = [];
while (data.length)
chunks.push(data.splice(0, CHUNK_SIZE));
async.mapSeries(chunks, function processChunkOfQueryResults(data, next) {
let sync = true;
processQueryResults(data, (err, result) => {
if (sync) {
process.nextTick(next, err, result);
} else {
next(err, result);
}
});
sync = false;
}, (err, resultChunks) => {
if (err) return done(err);
const result = [].concat(...resultChunks);
done(null, result);
});
}
};

function processQueryResults(data, cb) {
Expand Down

0 comments on commit d8ca382

Please sign in to comment.