chaindb: fix async.
This commit is contained in:
parent
b273bec120
commit
b65ed25b29
@ -714,7 +714,6 @@ 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
|
||||||
@ -863,7 +862,6 @@ ChainDB.prototype.saveAsync = 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();
|
||||||
@ -877,14 +875,13 @@ ChainDB.prototype.saveAsync = function save(entry, callback) {
|
|||||||
// Something tried to write here but couldn't.
|
// Something tried to write here but couldn't.
|
||||||
// Synchronously write it and get it over with.
|
// Synchronously write it and get it over with.
|
||||||
try {
|
try {
|
||||||
if (item !== entry)
|
if (item && item !== entry)
|
||||||
success = self._write(item.toRaw(), offset);
|
success = self._write(item.toRaw(), offset);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e;
|
err = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete self._queue[entry.height];
|
delete self._queue[entry.height];
|
||||||
self._writing--;
|
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
if (callback)
|
if (callback)
|
||||||
@ -901,7 +898,13 @@ ChainDB.prototype.saveAsync = function save(entry, callback) {
|
|||||||
ChainDB.prototype.remove = function remove(height) {
|
ChainDB.prototype.remove = function remove(height) {
|
||||||
assert(height >= 0);
|
assert(height >= 0);
|
||||||
|
|
||||||
|
if (this._queue[height]) {
|
||||||
|
utils.debug('Warning: write job in progress.');
|
||||||
|
delete this._queue[height];
|
||||||
|
}
|
||||||
|
|
||||||
this._write(this._nullBlock, height * BLOCK_SIZE);
|
this._write(this._nullBlock, height * BLOCK_SIZE);
|
||||||
|
delete this._cache[height];
|
||||||
|
|
||||||
// If we deleted several blocks at the end, go back
|
// If we deleted several blocks at the end, go back
|
||||||
// to the last non-null block and truncate the file
|
// to the last non-null block and truncate the file
|
||||||
@ -959,6 +962,8 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
|
|||||||
if (offset < 0 || offset == null)
|
if (offset < 0 || offset == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
self.size += added;
|
||||||
|
|
||||||
(function callee() {
|
(function callee() {
|
||||||
fs.write(self.fd, data, index, size, offset, function(err, bytes) {
|
fs.write(self.fd, data, index, size, offset, function(err, bytes) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -968,10 +973,8 @@ ChainDB.prototype._writeAsync = function _writeAsync(data, offset, callback) {
|
|||||||
size -= bytes;
|
size -= bytes;
|
||||||
offset += bytes;
|
offset += bytes;
|
||||||
|
|
||||||
if (index === data.length) {
|
if (index === data.length)
|
||||||
self.size += added;
|
|
||||||
return callback(null, true);
|
return callback(null, true);
|
||||||
}
|
|
||||||
|
|
||||||
callee();
|
callee();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user