From c3943c0111bfb5c744095ae47b3341e62bb9790e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 21 Jan 2016 01:50:22 -0800 Subject: [PATCH] chaindb. avoid buffered writes. --- lib/bcoin/chain.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 5ea973c8..63164e89 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -25,15 +25,18 @@ function Chain(options) { EventEmitter.call(this); - this.options = options || {}; - this.prefix = 'bt/chain/'; - this.storage = this.options.storage; - this.strict = this.options.strict || false; - this.db = new ChainDB(this); + if (!options) + options = {}; + + this.options = options; if (this.options.debug) bcoin.debug = this.options.debug; + this.db = new ChainDB(this); + this.heightLookup = {}; + this.request = new utils.RequestCache(); + this.loading = false; this.tip = null; this.orphan = { @@ -43,10 +46,6 @@ function Chain(options) { size: 0 }; - this.heightLookup = {}; - - this.request = new utils.RequestCache(); - this._addIndex(ChainBlock.fromJSON(this, { hash: network.genesis.hash, version: network.genesis.version, @@ -60,7 +59,6 @@ function Chain(options) { Chain.global = this; - this.loading = false; this._init(); } @@ -711,6 +709,7 @@ function ChainDB(chain, options) { this.tip = -1; this.size = 0; this.fd = null; + this._writing = 0; // Need to cache up to the retarget interval // if we're going to be checking the damn @@ -768,6 +767,7 @@ ChainDB.prototype.count = function count() { }; ChainDB.prototype.cache = function cache(entry) { + assert.equal(this.tip, this.count() - 1); if (entry.height > this.tip) { this.tip = entry.height; delete this._cache[entry.height - this._cacheWindow]; @@ -828,11 +828,16 @@ ChainDB.prototype.save = function save(entry, callback) { // Speed up writes by doing them asynchronously // and keeping the data to be written in memory. this._queue[entry.height] = entry; + this._writing++; // Write asynchronously to the db. raw = entry.toRaw(); offset = entry.height * BLOCK_SIZE; + // Avoid using buffered writes for now. + delete this._queue[entry.height]; + return this._writeSync(raw, offset); + return this._write(raw, offset, function(err, success) { var item = self._queue[entry.height]; @@ -848,6 +853,7 @@ ChainDB.prototype.save = function save(entry, callback) { } delete self._queue[entry.height]; + self._writing--; if (err) { if (callback) @@ -948,7 +954,7 @@ ChainDB.prototype._writeSync = function _writeSync(data, offset) { size -= bytes; offset += bytes; if (index === data.length) { - self.size += data.length; + this.size += data.length; return true; } }