txdb: optimize addBlock.
This commit is contained in:
parent
39a4df7b02
commit
90c53fc33c
@ -1013,6 +1013,68 @@ TXDB.prototype.getBlock = co(function* getBlock(height) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.addBlock = co(function* addBlock(tx, entry) {
|
||||
var hash = tx.hash();
|
||||
var height = tx.height;
|
||||
var key = layout.b(height);
|
||||
var data = yield this.get(key);
|
||||
var block, size;
|
||||
|
||||
if (!data) {
|
||||
block = new BlockRecord(tx.block, tx.height, tx.ts);
|
||||
data = block.toRaw();
|
||||
}
|
||||
|
||||
block = new Buffer(data.length + 32);
|
||||
data.copy(block, 0);
|
||||
|
||||
size = block.readUInt32LE(40, true);
|
||||
block.writeUInt32LE(size + 1, 40, true);
|
||||
hash.copy(block, data.length);
|
||||
|
||||
this.put(key, block);
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove from the global block record.
|
||||
* @param {TX} tx
|
||||
* @param {Number} height
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
TXDB.prototype.removeBlock = co(function* removeBlock(tx, height) {
|
||||
var hash = tx.hash();
|
||||
var height = tx.height;
|
||||
var key = layout.b(height);
|
||||
var data = yield this.get(key);
|
||||
var block, size;
|
||||
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
size = data.readUInt32LE(40, true);
|
||||
|
||||
assert(size > 0);
|
||||
assert(utils.equal(data.slice(-32), hash));
|
||||
|
||||
if (size === 1) {
|
||||
this.del(key);
|
||||
return;
|
||||
}
|
||||
|
||||
block = data.slice(0, -32);
|
||||
block.writeUInt32LE(size - 1, 40, true);
|
||||
|
||||
this.put(key, block);
|
||||
});
|
||||
|
||||
/**
|
||||
* Append to the global block record.
|
||||
* @param {TX} tx
|
||||
* @param {BlockMeta} entry
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
TXDB.prototype.addBlockSlow = co(function* addBlock(tx, entry) {
|
||||
var hash = tx.hash('hex');
|
||||
var height = tx.height;
|
||||
var block = yield this.getBlock(height);
|
||||
@ -1033,7 +1095,7 @@ TXDB.prototype.addBlock = co(function* addBlock(tx, entry) {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
TXDB.prototype.removeBlock = co(function* removeBlock(tx, height) {
|
||||
TXDB.prototype.removeBlockSlow = co(function* removeBlock(tx, height) {
|
||||
var hash = tx.hash('hex');
|
||||
var block = yield this.getBlock(height);
|
||||
|
||||
@ -1589,7 +1651,7 @@ TXDB.prototype.erase = co(function* erase(tx) {
|
||||
|
||||
if (tx.height !== -1) {
|
||||
yield this.removeBlockMap(tx, tx.height);
|
||||
yield this.removeBlock(tx, tx.height);
|
||||
yield this.removeBlockSlow(tx, tx.height);
|
||||
}
|
||||
|
||||
// Update the transaction counter
|
||||
|
||||
Loading…
Reference in New Issue
Block a user