walletdb: account path indexing.

This commit is contained in:
Christopher Jeffrey 2016-11-02 06:32:08 -07:00
parent d33557e2d7
commit f3055e57bf
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 70 additions and 23 deletions

View File

@ -770,13 +770,31 @@ Wallet.prototype.getAccounts = function getAccounts() {
/**
* Get all wallet address hashes.
* @param {(String|Number)?} acct
* @returns {Promise} - Returns Array.
*/
Wallet.prototype.getAddressHashes = function getAddressHashes() {
Wallet.prototype.getAddressHashes = function getAddressHashes(acct) {
if (acct != null)
return this.getAccountHashes(acct);
return this.db.getWalletHashes(this.wid);
};
/**
* Get all account address hashes.
* @param {String|Number} acct
* @returns {Promise} - Returns Array.
*/
Wallet.prototype.getAccountHashes = co(function* getAccountHashes(acct) {
var index = yield this.ensureIndex(acct);
if (index == null)
throw new Error('Account not provided.');
return yield this.db.getAccountHashes(this.wid, index);
});
/**
* Retrieve an account from the database.
* @param {Number|String} acct
@ -1075,12 +1093,6 @@ Wallet.prototype.getPath = co(function* getPath(address) {
assert(path.name);
// account = yield this.getAccount(path.account);
// assert(account);
// path.name = account.name;
// path.version = account.getWitnessVersion(path);
// path.type = account.getAddressType(path);
this.pathCache.set(hash, path);
return path;
@ -1111,29 +1123,47 @@ Wallet.prototype.hasPath = co(function* hasPath(address) {
*/
Wallet.prototype.getPaths = co(function* getPaths(acct) {
var index = yield this.ensureIndex(acct);
var paths = yield this.db.getWalletPaths(this.wid);
var result = [];
var i, path;
var i, paths, path, result;
if (acct != null)
return yield this.getAccountPaths(acct);
paths = yield this.db.getWalletPaths(this.wid);
result = [];
for (i = 0; i < paths.length; i++) {
path = paths[i];
if (index == null || path.account === index) {
path.id = this.id;
path.name = yield this.getAccountName(path.account);
path.id = this.id;
path.name = yield this.getAccountName(path.account);
assert(path.name);
assert(path.name);
// account = yield this.getAccount(path.account);
// assert(account);
// path.name = account.name;
// path.version = account.getWitnessVersion(path);
// path.type = account.getAddressType(path);
this.pathCache.set(path.hash, path);
this.pathCache.set(path.hash, path);
result.push(path);
}
result.push(path);
}
return result;
});
/**
* Get all account paths.
* @param {String|Number} acct
* @returns {Promise} - Returns {@link Path}.
*/
Wallet.prototype.getAccountPaths = co(function* getAccountPaths(acct) {
var index = yield this.ensureIndex(acct);
var hashes = yield this.getAccountHashes(index);
var result = [];
var i, hash, path;
for (i = 0; i < hashes.length; i++) {
hash = hashes[i];
path = yield this.getPath(hash);
assert(path);
assert(path.account === index);
result.push(path);
}
return result;

View File

@ -32,6 +32,7 @@ var PathMapRecord = records.PathMapRecord;
var OutpointMapRecord = records.OutpointMapRecord;
var TXDB = require('./txdb');
var U32 = utils.U32;
var DUMMY = new Buffer([0]);
/*
* Database Layout:
@ -1174,6 +1175,7 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) {
batch.put(layout.p(hash), map.toRaw());
batch.put(layout.P(wid, hash), path.toRaw());
batch.put(layout.r(wid, path.account, hash), DUMMY);
});
/**
@ -1265,6 +1267,21 @@ WalletDB.prototype.getWalletHashes = function getWalletHashes(wid) {
});
};
/**
* Get all account address hashes.
* @param {WalletID} wid
* @param {Number} account
* @returns {Promise}
*/
WalletDB.prototype.getAccountHashes = function getAccountHashes(wid, account) {
return this.db.keys({
gte: layout.r(wid, account, constants.NULL_HASH),
lte: layout.r(wid, account, constants.HIGH_HASH),
parse: layout.rr
});
};
/**
* Get all paths for a wallet.
* @param {WalletID} wid