From 9502e53fc1e2613d3aa7a067059ac9328ff9b4bc Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Fri, 15 Jun 2018 16:29:17 +0300 Subject: [PATCH] fix(store): fix potential out of records on find request --- src/background/store.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/background/store.js b/src/background/store.js index 4cb01e0..89035f4 100644 --- a/src/background/store.js +++ b/src/background/store.js @@ -1,5 +1,6 @@ const objectHash = require('object-hash'); const EventEmitter = require('events'); +const forBigTable = require('./forBigTable') module.exports = class P2PStore extends EventEmitter { constructor(p2p, sphinx) @@ -127,7 +128,6 @@ module.exports = class P2PStore extends EventEmitter { _pushToDb(value, callback) { - this.emit('store', value) const data = this.sphinx.escape(JSON.stringify(value.data)) this.sphinx.query( `insert into store(id, hash, peerId, data` + (value.index || value.data._index ? ', storeIndex' : '') + `) @@ -142,6 +142,7 @@ module.exports = class P2PStore extends EventEmitter { if(callback) callback() }) + this.emit('store', value) } store(obj) @@ -176,19 +177,10 @@ module.exports = class P2PStore extends EventEmitter { return true } - find(index) + async find(index) { - return new Promise((resolve) => { - this.sphinx.query(`select * from store where match(${this.sphinx.escape(index)}) LIMIT 50000`, (err, records) => { - if(err) - { - console.log(err) - resolve(false) - return - } - - resolve(records.map( ({data, peerid}) => Object.assign(JSON.parse(data), { _peerId: peerid }) )) - }) - }) + const records = [] + await forBigTable(this.sphinx, 'store', (record) => records.push(record), null, 1000, `and match(${this.sphinx.escape(index)})`) + return records.map( ({data, peerid}) => Object.assign(JSON.parse(data), { _peerId: peerid }) ) } } \ No newline at end of file