walletdb: use account name index more.

This commit is contained in:
Christopher Jeffrey 2016-11-02 17:43:18 -07:00
parent 508ece491f
commit c94884a188
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 65 additions and 19 deletions

View File

@ -787,11 +787,7 @@ Wallet.prototype.getAddressHashes = function getAddressHashes(acct) {
*/ */
Wallet.prototype.getAccountHashes = co(function* getAccountHashes(acct) { Wallet.prototype.getAccountHashes = co(function* getAccountHashes(acct) {
var index = yield this.ensureIndex(acct); var index = yield this.ensureIndex(acct, true);
if (index == null)
throw new Error('Account not provided.');
return yield this.db.getAccountHashes(this.wid, index); return yield this.db.getAccountHashes(this.wid, index);
}); });
@ -891,12 +887,17 @@ Wallet.prototype.getAccountIndex = co(function* getAccountIndex(name) {
*/ */
Wallet.prototype.getAccountName = co(function* getAccountName(index) { Wallet.prototype.getAccountName = co(function* getAccountName(index) {
var account = yield this.getAccount(index); var account;
if (!account) if (typeof index === 'string')
return null; return index;
return account.name; account = this.accountCache.get(index);
if (account)
return account.name;
return yield this.db.getAccountName(this.wid, index);
}); });
/** /**
@ -1072,6 +1073,28 @@ Wallet.prototype.hasAddress = co(function* hasAddress(address) {
*/ */
Wallet.prototype.getPath = co(function* getPath(address) { Wallet.prototype.getPath = co(function* getPath(address) {
var path = yield this.readPath(address);
if (!path)
return;
path.name = yield this.getAccountName(path.account);
assert(path.name);
this.pathCache.set(path.hash, path);
return path;
});
/**
* Get path by address hash (without account name).
* @private
* @param {Address|Hash} address
* @returns {Promise} - Returns {@link Path}.
*/
Wallet.prototype.readPath = co(function* readPath(address) {
var hash = Address.getHash(address, 'hex'); var hash = Address.getHash(address, 'hex');
var path; var path;
@ -1089,11 +1112,6 @@ Wallet.prototype.getPath = co(function* getPath(address) {
return; return;
path.id = this.id; path.id = this.id;
path.name = yield this.getAccountName(path.account);
assert(path.name);
this.pathCache.set(hash, path);
return path; return path;
}); });
@ -1153,16 +1171,25 @@ Wallet.prototype.getPaths = co(function* getPaths(acct) {
*/ */
Wallet.prototype.getAccountPaths = co(function* getAccountPaths(acct) { Wallet.prototype.getAccountPaths = co(function* getAccountPaths(acct) {
var index = yield this.ensureIndex(acct); var index = yield this.ensureIndex(acct, true);
var hashes = yield this.getAccountHashes(index); var hashes = yield this.getAccountHashes(index);
var name = yield this.getAccountName(acct);
var result = []; var result = [];
var i, hash, path; var i, hash, path;
assert(name);
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
path = yield this.getPath(hash); path = yield this.readPath(hash);
assert(path); assert(path);
assert(path.account === index); assert(path.account === index);
path.name = name;
this.pathCache.set(path.hash, path);
result.push(path); result.push(path);
} }
@ -2228,11 +2255,14 @@ Wallet.prototype.getLast = co(function* getLast(acct, limit) {
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.ensureIndex = co(function* ensureIndex(acct) { Wallet.prototype.ensureIndex = co(function* ensureIndex(acct, enforce) {
var index; var index;
if (acct == null) if (acct == null) {
if (enforce)
throw new Error('No account provided.');
return null; return null;
}
index = yield this.getAccountIndex(acct); index = yield this.getAccountIndex(acct);

View File

@ -1071,7 +1071,7 @@ WalletDB.prototype.getAccounts = co(function* getAccounts(wid) {
/** /**
* Lookup the corresponding account name's index. * Lookup the corresponding account name's index.
* @param {WalletID} wid * @param {WalletID} wid
* @param {String|Number} name - Account name/index. * @param {String} name - Account name/index.
* @returns {Promise} - Returns Number. * @returns {Promise} - Returns Number.
*/ */
@ -1084,6 +1084,22 @@ WalletDB.prototype.getAccountIndex = co(function* getAccountIndex(wid, name) {
return index.readUInt32LE(0, true); return index.readUInt32LE(0, true);
}); });
/**
* Lookup the corresponding account index's name.
* @param {WalletID} wid
* @param {Number} index
* @returns {Promise} - Returns Number.
*/
WalletDB.prototype.getAccountName = co(function* getAccountName(wid, index) {
var name = yield this.db.get(layout.n(wid, index));
if (!name)
return;
return name.toString('ascii');
});
/** /**
* Save an account to the database. * Save an account to the database.
* @param {Account} account * @param {Account} account