From 93e0fe95d0b0e2aa25a3990222423b868d8e88a5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 21 Jan 2016 14:33:54 -0800 Subject: [PATCH] chaindb: back to async writes. --- lib/bcoin/chain.js | 62 ++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 442e751e..9763cc56 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -869,7 +869,7 @@ ChainDB.prototype.getAsync = function getAsync(height, callback) { if ((height + 1) * BLOCK_SIZE > this.size) return callback(); - this._readAsync(BLOCK_SIZE, height * BLOCK_SIZE, function(err, data) { + return this._readAsync(BLOCK_SIZE, height * BLOCK_SIZE, function(err, data) { var entry; // We can't ensure the integrity of @@ -896,7 +896,7 @@ ChainDB.prototype.getAsync = function getAsync(height, callback) { }; ChainDB.prototype.save = function save(entry) { - return this.saveSync(entry); + return this.saveAsync(entry); }; ChainDB.prototype.saveSync = function saveSync(entry) { @@ -1058,34 +1058,6 @@ ChainDB.prototype._readAsync = function _readSync(size, offset, callback) { })(); }; -ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) { - var self = this; - var added = Math.max(0, (offset + data.length) - this.size); - var size = data.length; - var index = 0; - - if (offset < 0 || offset == null) - return false; - - self.size += added; - - (function next() { - fs.write(self.fd, data, index, size, offset, function(err, bytes) { - if (err) - return callback(err); - - index += bytes; - size -= bytes; - offset += bytes; - - if (index === data.length) - return callback(null, true); - - next(); - }); - })(); -}; - ChainDB.prototype._writeSync = function _writeSync(data, offset) { var size = data.length; var added = Math.max(0, (offset + data.length) - this.size); @@ -1112,6 +1084,36 @@ ChainDB.prototype._writeSync = function _writeSync(data, offset) { throw new Error('_writeSync() failed.'); }; +ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) { + var self = this; + var added = Math.max(0, (offset + data.length) - this.size); + var size = data.length; + var index = 0; + + if (offset < 0 || offset == null) + return false; + + self.size += added; + + (function next() { + fs.write(self.fd, data, index, size, offset, function(err, bytes) { + if (err) { + self.size -= (added - index); + return callback(err); + } + + index += bytes; + size -= bytes; + offset += bytes; + + if (index === data.length) + return callback(null, true); + + next(); + }); + })(); +}; + /** * ChainBlock */