This commit is contained in:
Matias Alejo Garcia 2015-02-06 19:57:52 -03:00
parent 9cb47a680a
commit 22a88e94fa
1 changed files with 27 additions and 22 deletions

View File

@ -3,7 +3,6 @@
var _ = require('lodash');
var levelup = require('levelup');
var $ = require('preconditions').singleton();
var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
@ -34,16 +33,16 @@ var KEY = {
return 'copayer::' + id;
},
TXP: function(walletId, txProposalId) {
return 'txp!' + walletId + opKey(txProposalId);
return 'txp!' + walletId + opKey(txProposalId);
},
TXP_BY_TS: function(walletId, ts, txProposalId) {
return 'txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
},
PENDING_TXP_BY_TS: function(walletId, ts, txProposalId) {
return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
},
ADDRESS: function(walletId, address) {
return 'address!' + walletId + opKey(address);
return 'address!' + walletId + opKey(address);
},
};
@ -145,23 +144,29 @@ Storage.prototype.fetchTxs = function(walletId, cb) {
// 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;
var ops = [{
type: 'put',
key: KEY.TXP(walletId, txp.id),
value: txp,
}, {
type: 'put',
key: KEY.TXP_BY_TS(walletId, txp.createdOn, txp.id),
value: txp,
}];
async.series([
function(next) {
self.db.put(KEY.TXP(walletId, txp.id), txp, next);
},
function(next) {
self.db.put(KEY.TXP_BY_TS(walletId, txp.createdOn, txp.id), txp, next);
},
function(next) {
if (txp.isPending())
self.db.put(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), txp, next);
else
self.db.del(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), next);
}
], cb);
if (txp.isPending()) {
ops.push({
type: 'put',
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id),
value: txp,
});
} else {
ops.push({
type: 'del',
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id),
});
}
this.db.batch(ops, cb);
};
Storage.prototype.fetchAddresses = function(walletId, cb) {
@ -184,11 +189,11 @@ Storage.prototype.fetchAddresses = function(walletId, cb) {
};
Storage.prototype.storeAddress = function(walletId, address, cb) {
this.db.put(KEY.ADDRESS(walletId,address.address), address, cb);
this.db.put(KEY.ADDRESS(walletId, address.address), address, cb);
};
Storage.prototype.removeAddress = function(walletId, address, cb) {
this.db.del(KEY.ADDRESS(walletId,address.address), cb);
this.db.del(KEY.ADDRESS(walletId, address.address), cb);
};