chaindb: back to async writes.

This commit is contained in:
Christopher Jeffrey 2016-01-21 14:33:54 -08:00
parent 32109c5de5
commit 93e0fe95d0

View File

@ -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
*/