chaindb. avoid buffered writes.

This commit is contained in:
Christopher Jeffrey 2016-01-21 01:50:22 -08:00
parent dd4660eb15
commit c3943c0111

View File

@ -25,15 +25,18 @@ function Chain(options) {
EventEmitter.call(this); EventEmitter.call(this);
this.options = options || {}; if (!options)
this.prefix = 'bt/chain/'; options = {};
this.storage = this.options.storage;
this.strict = this.options.strict || false; this.options = options;
this.db = new ChainDB(this);
if (this.options.debug) if (this.options.debug)
bcoin.debug = 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.tip = null;
this.orphan = { this.orphan = {
@ -43,10 +46,6 @@ function Chain(options) {
size: 0 size: 0
}; };
this.heightLookup = {};
this.request = new utils.RequestCache();
this._addIndex(ChainBlock.fromJSON(this, { this._addIndex(ChainBlock.fromJSON(this, {
hash: network.genesis.hash, hash: network.genesis.hash,
version: network.genesis.version, version: network.genesis.version,
@ -60,7 +59,6 @@ function Chain(options) {
Chain.global = this; Chain.global = this;
this.loading = false;
this._init(); this._init();
} }
@ -711,6 +709,7 @@ function ChainDB(chain, options) {
this.tip = -1; this.tip = -1;
this.size = 0; this.size = 0;
this.fd = null; this.fd = null;
this._writing = 0;
// Need to cache up to the retarget interval // Need to cache up to the retarget interval
// if we're going to be checking the damn // if we're going to be checking the damn
@ -768,6 +767,7 @@ ChainDB.prototype.count = function count() {
}; };
ChainDB.prototype.cache = function cache(entry) { ChainDB.prototype.cache = function cache(entry) {
assert.equal(this.tip, this.count() - 1);
if (entry.height > this.tip) { if (entry.height > this.tip) {
this.tip = entry.height; this.tip = entry.height;
delete this._cache[entry.height - this._cacheWindow]; 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 // Speed up writes by doing them asynchronously
// and keeping the data to be written in memory. // and keeping the data to be written in memory.
this._queue[entry.height] = entry; this._queue[entry.height] = entry;
this._writing++;
// Write asynchronously to the db. // Write asynchronously to the db.
raw = entry.toRaw(); raw = entry.toRaw();
offset = entry.height * BLOCK_SIZE; 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) { return this._write(raw, offset, function(err, success) {
var item = self._queue[entry.height]; var item = self._queue[entry.height];
@ -848,6 +853,7 @@ ChainDB.prototype.save = function save(entry, callback) {
} }
delete self._queue[entry.height]; delete self._queue[entry.height];
self._writing--;
if (err) { if (err) {
if (callback) if (callback)
@ -948,7 +954,7 @@ ChainDB.prototype._writeSync = function _writeSync(data, offset) {
size -= bytes; size -= bytes;
offset += bytes; offset += bytes;
if (index === data.length) { if (index === data.length) {
self.size += data.length; this.size += data.length;
return true; return true;
} }
} }