move saveAsync elsewhere.

This commit is contained in:
Christopher Jeffrey 2016-01-21 01:56:01 -08:00
parent c3943c0111
commit 95ff41dde9

View File

@ -763,10 +763,14 @@ ChainDB.prototype.getSize = function getSize() {
};
ChainDB.prototype.count = function count() {
return this.size / BLOCK_SIZE | 0;
var len = this.size / BLOCK_SIZE;
assert(len === (len | 0));
return len;
};
ChainDB.prototype.cache = function cache(entry) {
// Could use this.count() - 1 here as
// long as we're not using saveAsync.
assert.equal(this.tip, this.count() - 1);
if (entry.height > this.tip) {
this.tip = entry.height;
@ -809,7 +813,22 @@ ChainDB.prototype.get = function get(height) {
return entry;
};
ChainDB.prototype.save = function save(entry, callback) {
ChainDB.prototype.save = function save(entry) {
var self = this;
var raw, offset;
// Cache the past 1001 blocks in memory
// (necessary for isSuperMajority)
this.cache(entry);
raw = entry.toRaw();
offset = entry.height * BLOCK_SIZE;
return this._writeSync(raw, offset);
};
// This causes weird race conditions with size/count/tip.
ChainDB.prototype.saveAsync = function save(entry, callback) {
var self = this;
var raw, offset;
@ -834,10 +853,6 @@ ChainDB.prototype.save = function save(entry, callback) {
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];