chaindb: better loading.

This commit is contained in:
Christopher Jeffrey 2016-01-21 02:21:04 -08:00
parent 495dda79ef
commit c950a30330

View File

@ -46,7 +46,7 @@ function Chain(options) {
size: 0 size: 0
}; };
this._addIndex(ChainBlock.fromJSON(this, { this._saveEntry(ChainBlock.fromJSON(this, {
hash: network.genesis.hash, hash: network.genesis.hash,
version: network.genesis.version, version: network.genesis.version,
prevBlock: network.genesis.prevBlock, prevBlock: network.genesis.prevBlock,
@ -100,7 +100,7 @@ Chain.prototype._init = function _init() {
var i, entry; var i, entry;
for (i = 1; i < count; i++) for (i = 1; i < count; i++)
self._addIndex(self.db.get(i)); self._saveEntry(self.db.get(i));
self.loading = false; self.loading = false;
self.emit('load'); self.emit('load');
@ -109,7 +109,7 @@ Chain.prototype._init = function _init() {
}); });
}; };
Chain.prototype._addIndex = function _addIndex(entry, save) { Chain.prototype._addEntry = function _addEntry(entry) {
var self = this; var self = this;
var existing; var existing;
@ -138,6 +138,12 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
} }
} }
this._saveEntry(entry, true);
return Chain.codes.okay;
};
Chain.prototype._saveEntry = function _saveEntry(entry, save) {
if (save) if (save)
this.db.save(entry); this.db.save(entry);
@ -147,8 +153,6 @@ Chain.prototype._addIndex = function _addIndex(entry, save) {
this.tip = entry; this.tip = entry;
this.emit('tip', this.tip); this.emit('tip', this.tip);
} }
return Chain.codes.okay;
}; };
Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) { Chain.prototype.resetLastCheckpoint = function resetLastCheckpoint(height) {
@ -323,7 +327,7 @@ Chain.prototype.add = function add(block, peer) {
} }
// Attempt to add block to the chain index. // Attempt to add block to the chain index.
code = this._addIndex(entry, true); code = this._addEntry(entry);
// Result should never be `unchanged` since // Result should never be `unchanged` since
// we already verified there were no // we already verified there were no
@ -677,7 +681,7 @@ Chain.prototype.fromJSON = function fromJSON(json) {
assert.equal(json.network, network.type); assert.equal(json.network, network.type);
json.entries.forEach(function(entry) { json.entries.forEach(function(entry) {
this._addIndex(ChainBlock.fromJSON(this, entry)); this._saveEntry(ChainBlock.fromJSON(this, entry));
}, this); }, this);
}; };
@ -723,10 +727,12 @@ function ChainDB(chain, options) {
} }
ChainDB.prototype._init = function _init() { ChainDB.prototype._init = function _init() {
try { if (+process.env.BCOIN_FRESH === 1) {
fs.unlinkSync(this.file); try {
} catch (e) { fs.unlinkSync(this.file);
; } catch (e) {
;
}
} }
if (!this.exists()) { if (!this.exists()) {
@ -750,7 +756,7 @@ ChainDB.prototype.getBuffer = function(size) {
ChainDB.prototype.exists = function exists() { ChainDB.prototype.exists = function exists() {
try { try {
fs.statSync(file); fs.statSync(this.file);
return true; return true;
} catch (e) { } catch (e) {
return false; return false;
@ -774,7 +780,8 @@ ChainDB.prototype.count = function count() {
ChainDB.prototype.cache = function cache(entry) { ChainDB.prototype.cache = function cache(entry) {
// Could use this.count() - 1 here as // Could use this.count() - 1 here as
// long as we're not using saveAsync. // long as we're not using saveAsync.
assert.equal(this.tip, this.count() - 1); // if (this.tip !== -1)
// 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];
@ -934,6 +941,7 @@ ChainDB.prototype._read = function _read(size, offset) {
ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) { ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
var self = this; var self = this;
var added = Math.max(0, (offset + data.length) - this.size);
var size = data.length; var size = data.length;
var index = 0; var index = 0;
@ -950,7 +958,7 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
offset += bytes; offset += bytes;
if (index === data.length) { if (index === data.length) {
self.size += data.length; self.size += added;
return callback(null, true); return callback(null, true);
} }
@ -961,6 +969,7 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
ChainDB.prototype._write = function _write(data, offset) { ChainDB.prototype._write = function _write(data, offset) {
var size = data.length; var size = data.length;
var added = Math.max(0, (offset + data.length) - this.size);
var index = 0; var index = 0;
var bytes; var bytes;
@ -972,7 +981,7 @@ ChainDB.prototype._write = function _write(data, offset) {
size -= bytes; size -= bytes;
offset += bytes; offset += bytes;
if (index === data.length) { if (index === data.length) {
this.size += data.length; this.size += added;
return true; return true;
} }
} }