avoid duplicate hash table lookups.
This commit is contained in:
parent
a3bb955d95
commit
c27466a7c5
@ -317,6 +317,8 @@ ChainDB.prototype.getCache = function getCache(hash) {
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getHeight = function getHeight(hash, callback) {
|
||||
var entry;
|
||||
|
||||
if (hash == null || hash < 0) {
|
||||
callback = utils.asyncify(callback);
|
||||
return callback(null, -1);
|
||||
@ -332,9 +334,11 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) {
|
||||
return callback(null, -1);
|
||||
}
|
||||
|
||||
if (this.cacheHash.has(hash)) {
|
||||
entry = this.cacheHash.get(hash);
|
||||
|
||||
if (entry) {
|
||||
callback = utils.asyncify(callback);
|
||||
return callback(null, this.cacheHash.get(hash).height);
|
||||
return callback(null, entry.height);
|
||||
}
|
||||
|
||||
this.db.fetch(layout.h(hash), function(data) {
|
||||
@ -359,6 +363,8 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) {
|
||||
*/
|
||||
|
||||
ChainDB.prototype.getHash = function getHash(height, callback) {
|
||||
var entry;
|
||||
|
||||
if (height == null || height < 0) {
|
||||
callback = utils.asyncify(callback);
|
||||
return callback(null, null);
|
||||
@ -369,9 +375,11 @@ ChainDB.prototype.getHash = function getHash(height, callback) {
|
||||
return callback(null, height);
|
||||
}
|
||||
|
||||
if (this.cacheHeight.has(height)) {
|
||||
entry = this.cacheHeight.get(height);
|
||||
|
||||
if (entry) {
|
||||
callback = utils.asyncify(callback);
|
||||
return callback(null, this.cacheHeight.get(height).hash);
|
||||
return callback(null, entry.hash);
|
||||
}
|
||||
|
||||
this.db.fetch(layout.H(height), function(data) {
|
||||
@ -445,6 +453,7 @@ ChainDB.prototype.getBoth = function getBoth(block, callback) {
|
||||
|
||||
ChainDB.prototype.getEntry = function getEntry(hash, callback) {
|
||||
var self = this;
|
||||
var entry;
|
||||
|
||||
if (hash == null || hash < 0)
|
||||
return utils.nextTick(callback);
|
||||
@ -456,8 +465,10 @@ ChainDB.prototype.getEntry = function getEntry(hash, callback) {
|
||||
if (!hash)
|
||||
return callback();
|
||||
|
||||
if (self.cacheHash.has(hash))
|
||||
return callback(null, self.cacheHash.get(hash));
|
||||
entry = self.cacheHash.get(hash);
|
||||
|
||||
if (entry)
|
||||
return callback(null, entry);
|
||||
|
||||
return self.db.fetch(layout.e(hash), function(data) {
|
||||
return bcoin.chainentry.fromRaw(self.chain, data);
|
||||
|
||||
@ -179,6 +179,7 @@ ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors(callba
|
||||
ChainEntry.prototype.getAncestors = function getAncestors(max, callback) {
|
||||
var entry = this;
|
||||
var ancestors = [];
|
||||
var cached;
|
||||
|
||||
if (max === 0)
|
||||
return callback(null, []);
|
||||
@ -193,12 +194,14 @@ ChainEntry.prototype.getAncestors = function getAncestors(max, callback) {
|
||||
if (ancestors.length >= max)
|
||||
return callback(null, ancestors);
|
||||
|
||||
if (!this.chain.db.hasCache(entry.prevBlock)) {
|
||||
cached = this.chain.db.getCache(entry.prevBlock);
|
||||
|
||||
if (!cached) {
|
||||
ancestors.pop();
|
||||
break;
|
||||
}
|
||||
|
||||
entry = this.chain.db.getCache(entry.prevBlock);
|
||||
entry = cached;
|
||||
}
|
||||
|
||||
(function next(err, entry) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user