blockdb drop queue and cache.
This commit is contained in:
parent
91897540d6
commit
effaa57abf
@ -674,8 +674,6 @@ function BlockData(options) {
|
||||
if (!this.file)
|
||||
this.file = process.env.HOME + '/bcoin-block-' + network.type + '.db';
|
||||
|
||||
this._queue = [];
|
||||
this._cache = {};
|
||||
this._bufferPool = { used: {} };
|
||||
this.size = 0;
|
||||
this.fd = null;
|
||||
@ -707,7 +705,9 @@ BlockData.prototype._init = function _init() {
|
||||
};
|
||||
|
||||
BlockData.prototype._malloc = function(size) {
|
||||
return new Buffer(size);
|
||||
if (size > 500)
|
||||
return new Buffer(size);
|
||||
|
||||
if (!this._bufferPool[size])
|
||||
this._bufferPool[size] = new Buffer(size);
|
||||
|
||||
@ -720,7 +720,6 @@ BlockData.prototype._malloc = function(size) {
|
||||
};
|
||||
|
||||
BlockData.prototype._free = function(buf) {
|
||||
return;
|
||||
if (this._bufferPool.used[buf.length] === buf) {
|
||||
assert(this._bufferPool[buf.length] === buf);
|
||||
delete this._bufferPool.used[buf.length];
|
||||
@ -751,36 +750,20 @@ BlockData.prototype.getSize = function getSize() {
|
||||
};
|
||||
|
||||
BlockData.prototype.getSync = function getSync(size, offset) {
|
||||
var hash = size + '/' + offset;
|
||||
|
||||
if (this._cache[hash])
|
||||
return this._cache[hash];
|
||||
|
||||
if (this._queue[hash])
|
||||
return this._queue[hash];
|
||||
|
||||
return this._readSync(size, offset);
|
||||
};
|
||||
|
||||
BlockData.prototype.getAsync = function getAsync(size, offset, callback) {
|
||||
var self = this;
|
||||
var hash = size + '/' + offset;
|
||||
|
||||
callback = utils.asyncify(callback);
|
||||
|
||||
if (this._cache[hash])
|
||||
return callback(null, this._cache[hash]);
|
||||
|
||||
if (this._queue[hash])
|
||||
return callback(null, this._queue[hash]);
|
||||
|
||||
return this._readAsync(size, offset, callback);
|
||||
};
|
||||
|
||||
BlockData.prototype.saveSync = function saveSync(data) {
|
||||
var self = this;
|
||||
var offset = this.size;
|
||||
var hash = data + '/' + offset;
|
||||
|
||||
this._writeSync(data, offset);
|
||||
|
||||
@ -790,39 +773,12 @@ BlockData.prototype.saveSync = function saveSync(data) {
|
||||
BlockData.prototype.saveAsync = function saveAsync(data, callback) {
|
||||
var self = this;
|
||||
var offset = this.size;
|
||||
var hash = data + '/' + offset;
|
||||
|
||||
callback = utils.asyncify(callback);
|
||||
|
||||
// Something is already writing. Cancel it
|
||||
// and synchronously write the data after
|
||||
// it cancels.
|
||||
if (this._queue[hash]) {
|
||||
this._queue[hash] = data;
|
||||
return callback(null, { size: data.length, offset: offset });
|
||||
}
|
||||
|
||||
// Speed up writes by doing them asynchronously
|
||||
// and keeping the data to be written in memory.
|
||||
this._queue[hash] = data;
|
||||
|
||||
return this._writeAsync(data, offset, function(err, success) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
var item = self._queue[hash];
|
||||
|
||||
// Something tried to write here but couldn't.
|
||||
// Synchronously write it and get it over with.
|
||||
try {
|
||||
if (item && item !== data)
|
||||
success = self._writeSync(item, offset);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
delete self._queue[hash];
|
||||
|
||||
return callback(null, { size: data.length, offset: offset });
|
||||
});
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user