From 96bb12568e62fffb543976bff9f42a7f71aceff6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 24 Feb 2016 01:46:12 -0800 Subject: [PATCH] use iterators for getHeight. --- lib/bcoin/blockdb.js | 46 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 0b3500cc..9e0e7f3e 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -955,29 +955,39 @@ BlockDB.prototype.isSpent = function isSpent(hash, index, callback) { BlockDB.prototype.getHeight = function getHeight(callback) { var self = this; var maxHeight = -1; - var stream; + var iter; - callback = utils.asyncify(callback); - - stream = this.index.createKeyStream({ - start: 'b/h', - end: 'b/h~' + iter = this.index.db.iterator({ + gte: 'b/h', + lte: 'b/h~', + keys: true, + values: false, + fillCache: false, + keyAsBuffer: false }); - stream.on('data', function(key) { - var parts = key.split('/').slice(2); - var height = +parts[0]; - if (height > maxHeight) - maxHeight = height; - }); + (function next() { + iter.next(function(err, key, value) { + var parts, height; - stream.on('error', function(err) { - return callback(err); - }); + if (err) { + return iter.end(function() { + return callback(err); + }); + } - stream.on('end', function() { - return callback(null, maxHeight); - }); + // iter.end() here? + 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) {