chaindb: add allocator.
This commit is contained in:
parent
7c2171966c
commit
cb73ab908e
@ -720,7 +720,7 @@ function ChainDB(chain, options) {
|
|||||||
|
|
||||||
this._queue = [];
|
this._queue = [];
|
||||||
this._cache = {};
|
this._cache = {};
|
||||||
this._bufferPool = {};
|
this._bufferPool = { used: {} };
|
||||||
this._nullBlock = new Buffer(BLOCK_SIZE);
|
this._nullBlock = new Buffer(BLOCK_SIZE);
|
||||||
this._nullBlock.fill(0);
|
this._nullBlock.fill(0);
|
||||||
this.tip = -1;
|
this.tip = -1;
|
||||||
@ -764,16 +764,25 @@ ChainDB.prototype._init = function _init() {
|
|||||||
this.fd = fs.openSync(this.file, 'r+');
|
this.fd = fs.openSync(this.file, 'r+');
|
||||||
};
|
};
|
||||||
|
|
||||||
ChainDB.prototype.getBuffer = function(size) {
|
ChainDB.prototype._malloc = function(size) {
|
||||||
if (!this._bufferPool[size])
|
if (!this._bufferPool[size])
|
||||||
this._bufferPool[size] = new Buffer(size);
|
this._bufferPool[size] = new Buffer(size);
|
||||||
|
|
||||||
// if (this._bufferPool[size].__used)
|
if (this._bufferPool.used[size] === this._bufferPool[size])
|
||||||
// return new Buffer(size);
|
return new Buffer(size);
|
||||||
|
|
||||||
|
this._bufferPool.used[size] = this._bufferPool[size];
|
||||||
|
|
||||||
return this._bufferPool[size];
|
return this._bufferPool[size];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ChainDB.prototype._free = function(buf) {
|
||||||
|
if (this._bufferPool.used[buf.length] === buf) {
|
||||||
|
assert(this._bufferPool[buf.length] === buf);
|
||||||
|
delete this._bufferPool.used[buf.length];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ChainDB.prototype.exists = function exists() {
|
ChainDB.prototype.exists = function exists() {
|
||||||
try {
|
try {
|
||||||
fs.statSync(this.file);
|
fs.statSync(this.file);
|
||||||
@ -982,46 +991,56 @@ ChainDB.prototype.isNull = function isNull(height) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ChainDB.prototype._read = function _read(size, offset) {
|
ChainDB.prototype._read = function _read(size, offset) {
|
||||||
var data = this.getBuffer(size);
|
|
||||||
var index = 0;
|
var index = 0;
|
||||||
var bytes;
|
var data, bytes;
|
||||||
|
|
||||||
if (offset < 0 || offset == null)
|
if (offset < 0 || offset == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
data = this._malloc(size);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (bytes = fs.readSync(this.fd, data, index, size, offset)) {
|
while (bytes = fs.readSync(this.fd, data, index, size, offset)) {
|
||||||
index += bytes;
|
index += bytes;
|
||||||
size -= bytes;
|
size -= bytes;
|
||||||
offset += bytes;
|
offset += bytes;
|
||||||
if (index === data.length)
|
if (index === data.length) {
|
||||||
|
this._free(data);
|
||||||
return data;
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._free(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
ChainDB.prototype._readAsync = function _read(size, offset, callback) {
|
ChainDB.prototype._readAsync = function _read(size, offset, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var data = new Buffer(size);
|
|
||||||
var index = 0;
|
var index = 0;
|
||||||
var bytes;
|
var data, bytes;
|
||||||
|
|
||||||
if (offset < 0 || offset == null)
|
if (offset < 0 || offset == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
data = this._malloc(size);
|
||||||
|
|
||||||
(function callee() {
|
(function callee() {
|
||||||
fs.read(self.fd, data, index, size, offset, function(err, bytes) {
|
fs.read(self.fd, data, index, size, offset, function(err, bytes) {
|
||||||
if (err)
|
if (err) {
|
||||||
|
self._free(data);
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
index += bytes;
|
index += bytes;
|
||||||
size -= bytes;
|
size -= bytes;
|
||||||
offset += bytes;
|
offset += bytes;
|
||||||
|
|
||||||
if (index === data.length)
|
if (index === data.length) {
|
||||||
|
self._free(data);
|
||||||
return callback(null, data);
|
return callback(null, data);
|
||||||
|
}
|
||||||
|
|
||||||
callee();
|
callee();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user