perf(db): little faster cycles over small requests

This commit is contained in:
Alexey Kasyanchuk 2018-06-15 15:46:53 +03:00
parent 88a0771579
commit 859770bdcf

View File

@ -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) => { const checker = (index = 0) => {
sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} LIMIT ${max}`, (err, torrents) => { sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} ${where} LIMIT ${max}`, (err, torrents) => {
if(err || torrents.length == 0) const finish = () => {
{
if(err) if(err)
console.log('big table parse error', err) console.log('big table parse error', err)
if(doneCallback) if(doneCallback)
doneCallback(true) doneCallback(true)
done(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() checker()