diff --git a/lib/storage.js b/lib/storage.js index 87411a2..6b197ac 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -20,8 +20,29 @@ var Storage = function(opts) { }; +var KEY = { + WALLET: function(id) { + return 'wallet::' + id; + }, + COPAYER: function(id) { + return 'copayer::' + id; + }, + TXP_BY_ID: function(walletId, txProposalId) { + return 'txp::' + walletId + '::' + txProposalId; + }, + TXP_BY_TS: function(walletId, ts) { + return 'txp-ts::' + walletId + '::' + ts.toFixed(12); + }, + PENDING_TXP_BY_TS: function(walletId, ts) { + return 'pending-txp-ts::' + walletId + '::' + ts.toFixed(12); + }, + ADDRESS: function(walletId, address) { + return 'address::' + walletId + '::' + address; + }, +}; + Storage.prototype.fetchWallet = function(id, cb) { - this.db.get('wallet-' + id, function(err, data) { + this.db.get(KEY.WALLET(id), function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -31,15 +52,14 @@ Storage.prototype.fetchWallet = function(id, cb) { }; Storage.prototype.storeWallet = function(wallet, cb) { - this.db.put('wallet-' + wallet.id, wallet, cb); + this.db.put(KEY.WALLET(wallet.id), wallet, cb); }; -<< << << < HEAD Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { var ops = []; ops.push({ type: 'put', - key: 'wallet-' + wallet.id, + key: KEY.WALLET(wallet.id), value: wallet }); _.each(wallet.copayers, function(copayer) { @@ -49,7 +69,7 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { }; ops.push({ type: 'put', - key: 'copayer-' + copayer.id, + key: KEY.COPAYER(copayer.id), value: value }); }); @@ -57,7 +77,7 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { }; Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { - this.db.get('copayer-' + copayerId, function(err, data) { + this.db.get(KEY.COPAYER(copayerId), function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -67,7 +87,7 @@ Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { }; Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { - this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function(err, data) { + this.db.get(KEY.TXP_BY_ID(walletId, txProposalId), function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -76,9 +96,10 @@ Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { }); }; -Storage.prototype.fetchTxs = function(walletId, cb) { + +Storage.prototype.fetchPendingTxs = function(walletId, cb) { var txs = []; - var key = 'wallet-' + walletId + '-txp-'; + var key = KEY.PENDING_TXP_BY_TS(walletId,''); this.db.createReadStream({ gte: key, lt: key + '~' @@ -95,29 +116,51 @@ Storage.prototype.fetchTxs = function(walletId, cb) { }); }; +Storage.prototype.fetchTxs = function(walletId, cb) { + var txs = []; + var key = KEY.TXP_BY_ID(walletId,''); + this.db.createReadStream({ + gte: key, + lt: key + '~' + }) + .on('data', function(data) { + txs.push(TxProposal.fromObj(data.value)); + }) + .on('error', function(err) { + if (err.notFound) return cb(); + return cb(err); + }) + .on('end', function() { + return cb(null, txs); + }); +}; + +// TODO should we store only txp.id on keys for indexing +// or the whole txp? For now, the entire record makes sense +// (faster + easier to access) Storage.prototype.storeTx = function(walletId, txp, cb) { var self = this; async.series([ function(next) { - self.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, next); + self.db.put(KEY.TXP_BY_ID(walletId, txp.id), txp, next); }, function(next) { - self.db.put('wallet-' + walletId + '-txp-ts-' + txp.createdOn, txp.id, next); + self.db.put(KEY.TXP_BY_TS(walletId, txp.createdOn), txp, next); }, function(next) { if (txp.isPending()) - self.db.put('wallet-' + walletId + '-txp-p-' + txp.createdOn, txp.id, next); + self.db.put(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn), txp, next); else - self.db.del('wallet-' + walletId + '-txp-p-' + txp.createdOn, next); + self.db.del(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn), next); } ], cb); }; Storage.prototype.fetchAddresses = function(walletId, cb) { var addresses = []; - var key = 'wallet-' + walletId + '-address-'; + var key = KEY.ADDRESS(walletId,''); this.db.createReadStream({ gte: key, lt: key + '~' @@ -135,11 +178,11 @@ Storage.prototype.fetchAddresses = function(walletId, cb) { }; Storage.prototype.storeAddress = function(walletId, address, cb) { - this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb); + this.db.put(KEY.ADDRESS(walletId,address.address), address, cb); }; Storage.prototype.removeAddress = function(walletId, address, cb) { - this.db.del('wallet-' + walletId + '-address-' + address.address, cb); + this.db.del(KEY.ADDRESS(walletId,address.address), cb); };