diff --git a/src/background/api.js b/src/background/api.js index 93d8e39..b7a43dc 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -1,4 +1,5 @@ const ipaddr = require('ipaddr.js'); +import forBigTable from './forBigTable' module.exports = ({ sphinx, @@ -622,23 +623,10 @@ module.exports = ({ console.log('removed torrents by filter:', toRemove.length) } - const checker = (index = 0) => { - sphinx.query(`SELECT * FROM torrents LIMIT ${index},50000`, (err, torrents) => { - if(err || torrents.length == 0) - { - done() - return - } - - torrents.forEach((torrent) => { - if(!checkTorrent(torrent)) - toRemove.push(torrent) - }) - - checker(index + torrents.length) - }); - } - checker() + forBigTable(sphinx, 'torrents', (torrent) => { + if(!checkTorrent(torrent)) + toRemove.push(torrent) + }, done) }) let socketIPV4 = () => { diff --git a/src/background/forBigTable.js b/src/background/forBigTable.js new file mode 100644 index 0000000..754cb86 --- /dev/null +++ b/src/background/forBigTable.js @@ -0,0 +1,18 @@ +export default (sphinx, table, callback, doneCallback, max = 1000) => new Promise((done) => { + const checker = (index = 0) => { + sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} LIMIT ${max}`, (err, torrents) => { + if(err || torrents.length == 0) + { + if(err) + console.log('big table parse error', err) + if(doneCallback) + doneCallback(true) + done(true) + return + } + torrents.forEach(callback) + checker(torrents[torrents.length - 1].id) + }); + } + checker() +}) \ No newline at end of file