Skip to content

Commit

Permalink
perf(db): little faster cycles over small requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 15, 2018
1 parent a9ebef7 commit 3f76d11
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/background/forBigTable.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 3f76d11

Please sign in to comment.