add KEY to storage
This commit is contained in:
parent
196610f2eb
commit
0a7edd9523
|
@ -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) {
|
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) {
|
||||||
if (err.notFound) return cb();
|
if (err.notFound) return cb();
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
@ -31,15 +52,14 @@ Storage.prototype.fetchWallet = function(id, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.storeWallet = function(wallet, 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) {
|
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
|
||||||
var ops = [];
|
var ops = [];
|
||||||
ops.push({
|
ops.push({
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: 'wallet-' + wallet.id,
|
key: KEY.WALLET(wallet.id),
|
||||||
value: wallet
|
value: wallet
|
||||||
});
|
});
|
||||||
_.each(wallet.copayers, function(copayer) {
|
_.each(wallet.copayers, function(copayer) {
|
||||||
|
@ -49,7 +69,7 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
|
||||||
};
|
};
|
||||||
ops.push({
|
ops.push({
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: 'copayer-' + copayer.id,
|
key: KEY.COPAYER(copayer.id),
|
||||||
value: value
|
value: value
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -57,7 +77,7 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.fetchCopayerLookup = function(copayerId, 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) {
|
||||||
if (err.notFound) return cb();
|
if (err.notFound) return cb();
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
@ -67,7 +87,7 @@ Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.fetchTx = function(walletId, txProposalId, 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) {
|
||||||
if (err.notFound) return cb();
|
if (err.notFound) return cb();
|
||||||
return cb(err);
|
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 txs = [];
|
||||||
var key = 'wallet-' + walletId + '-txp-';
|
var key = KEY.PENDING_TXP_BY_TS(walletId,'');
|
||||||
this.db.createReadStream({
|
this.db.createReadStream({
|
||||||
gte: key,
|
gte: key,
|
||||||
lt: 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) {
|
Storage.prototype.storeTx = function(walletId, txp, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
|
|
||||||
function(next) {
|
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) {
|
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) {
|
function(next) {
|
||||||
if (txp.isPending())
|
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
|
else
|
||||||
self.db.del('wallet-' + walletId + '-txp-p-' + txp.createdOn, next);
|
self.db.del(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn), next);
|
||||||
}
|
}
|
||||||
], cb);
|
], cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.fetchAddresses = function(walletId, cb) {
|
Storage.prototype.fetchAddresses = function(walletId, cb) {
|
||||||
var addresses = [];
|
var addresses = [];
|
||||||
var key = 'wallet-' + walletId + '-address-';
|
var key = KEY.ADDRESS(walletId,'');
|
||||||
this.db.createReadStream({
|
this.db.createReadStream({
|
||||||
gte: key,
|
gte: key,
|
||||||
lt: key + '~'
|
lt: key + '~'
|
||||||
|
@ -135,11 +178,11 @@ Storage.prototype.fetchAddresses = function(walletId, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.storeAddress = function(walletId, address, 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) {
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue