From 859770bdcfb627af60b46da9cf60793f4c54a7ec Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Fri, 15 Jun 2018 15:46:53 +0300 Subject: [PATCH] perf(db): little faster cycles over small requests --- src/background/forBigTable.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/background/forBigTable.js b/src/background/forBigTable.js index 878a359..0917397 100644 --- a/src/background/forBigTable.js +++ b/src/background/forBigTable.js @@ -1,18 +1,23 @@ -module.exports = (sphinx, table, callback, doneCallback, max = 1000) => new Promise((done) => { +module.exports = (sphinx, table, callback, doneCallback, max = 1000, where = '') => new Promise((done) => { const checker = (index = 0) => { - sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} LIMIT ${max}`, (err, torrents) => { - if(err || torrents.length == 0) - { + sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} ${where} LIMIT ${max}`, (err, torrents) => { + const finish = () => { if(err) console.log('big table parse error', err) if(doneCallback) doneCallback(true) done(true) - return } - Promise.all(torrents.map(callback)).then(() => { - checker(torrents[torrents.length - 1].id) - }) + + if(!err && torrents.length > 0) + Promise.all(torrents.map(callback)).then(() => { + if(torrents.length === max) + checker(torrents[torrents.length - 1].id) + else + finish() + }) + else + finish() }); } checker()