use iterators for getHeight.

This commit is contained in:
Christopher Jeffrey 2016-02-24 01:46:12 -08:00
parent f9577cdc04
commit 96bb12568e

View File

@ -955,29 +955,39 @@ BlockDB.prototype.isSpent = function isSpent(hash, index, callback) {
BlockDB.prototype.getHeight = function getHeight(callback) { BlockDB.prototype.getHeight = function getHeight(callback) {
var self = this; var self = this;
var maxHeight = -1; var maxHeight = -1;
var stream; var iter;
callback = utils.asyncify(callback); iter = this.index.db.iterator({
gte: 'b/h',
stream = this.index.createKeyStream({ lte: 'b/h~',
start: 'b/h', keys: true,
end: 'b/h~' values: false,
fillCache: false,
keyAsBuffer: false
}); });
stream.on('data', function(key) { (function next() {
var parts = key.split('/').slice(2); iter.next(function(err, key, value) {
var height = +parts[0]; var parts, height;
if (height > maxHeight)
maxHeight = height;
});
stream.on('error', function(err) { if (err) {
return callback(err); return iter.end(function() {
}); return callback(err);
});
}
stream.on('end', function() { // iter.end() here?
return callback(null, maxHeight); if (!key)
}); return callback(null, maxHeight);
parts = key.split('/').slice(2);
height = +parts[0];
if (height > maxHeight)
maxHeight = height;
next();
});
})();
}; };
BlockDB.prototype.resetHeight = function resetHeight(height, callback, emit) { BlockDB.prototype.resetHeight = function resetHeight(height, callback, emit) {