lru. blockdb.

This commit is contained in:
Christopher Jeffrey 2016-03-10 16:46:37 -08:00
parent f09a4dc8f7
commit ef3519761c
2 changed files with 68 additions and 49 deletions

View File

@ -198,27 +198,27 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) {
var uniq = {};
tx.inputs.forEach(function(input) {
var type = input.getType();
var address = input.getAddress();
var address;
if (input.isCoinbase())
return;
assert(input.output);
if (type === 'pubkey' || type === 'multisig')
address = null;
if (self.options.indexAddress) {
address = input.getAddress();
if (address && !uniq[address]) {
uniq[address] = true;
batch.put('t/a/' + address + '/' + hash, DUMMY);
}
if (address && !uniq[address]) {
uniq[address] = true;
batch.put('t/a/' + address + '/' + hash, DUMMY);
}
if (address) {
batch.del(
'u/a/' + address
+ '/' + input.prevout.hash
+ '/' + input.prevout.index);
if (address) {
batch.del(
'u/a/' + address
+ '/' + input.prevout.hash
+ '/' + input.prevout.index);
}
}
batch.del('u/t/' + input.prevout.hash + '/' + input.prevout.index);
@ -228,20 +228,20 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) {
});
tx.outputs.forEach(function(output, i) {
var type = output.getType();
var address = output.getAddress();
var address;
if (type === 'pubkey' || type === 'multisig')
address = null;
if (self.options.indexAddress) {
address = output.getAddress();
if (address && !uniq[address]) {
uniq[address] = true;
batch.put('t/a/' + address + '/' + hash, DUMMY);
if (address && !uniq[address]) {
uniq[address] = true;
batch.put('t/a/' + address + '/' + hash, DUMMY);
}
if (address)
batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY);
}
if (address)
batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY);
batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toExtended());
});
});
@ -284,28 +284,27 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat
self.cache.tx.remove(hash);
tx.inputs.forEach(function(input) {
var type = input.getType();
var address = input.getAddress();
var coin;
var coin, address;
if (input.isCoinbase())
return;
assert(input.output);
if (type === 'pubkey' || type === 'multisig')
address = null;
if (self.options.indexAddress) {
address = input.getAddress();
if (address && !uniq[address]) {
uniq[address] = true;
batch.del('t/a/' + address + '/' + hash);
}
if (address && !uniq[address]) {
uniq[address] = true;
batch.del('t/a/' + address + '/' + hash);
}
if (address) {
batch.put('u/a/' + address
+ '/' + input.prevout.hash
+ '/' + input.prevout.index,
DUMMY);
if (address) {
batch.put('u/a/' + address
+ '/' + input.prevout.hash
+ '/' + input.prevout.index,
DUMMY);
}
}
batch.put('u/t/'
@ -315,20 +314,20 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat
});
tx.outputs.forEach(function(output, i) {
var type = output.getType();
var address = output.getAddress();
var address;
if (type === 'pubkey' || type === 'multisig')
address = null;
if (self.options.indexAddress) {
address = output.getAddress();
if (address && !uniq[address]) {
uniq[address] = true;
batch.del('t/a/' + address + '/' + hash);
if (address && !uniq[address]) {
uniq[address] = true;
batch.del('t/a/' + address + '/' + hash);
}
if (address)
batch.del('u/a/' + address + '/' + hash + '/' + i);
}
if (address)
batch.del('u/a/' + address + '/' + hash + '/' + i);
batch.del('u/t/' + hash + '/' + i);
if (self.options.cache)

View File

@ -12,19 +12,33 @@ var assert = utils.assert;
* LRU
*/
function LRU(maxSize, getSize) {
function LRU(maxSize, getSize, onRemove) {
if (!(this instanceof LRU))
return new LRU(maxSize, getSize);
this.data = {};
this.size = 0;
this.maxSize = maxSize;
this.getSize = getSize;
this.getSize = this._createGetSize(getSize);
this.onRemove = onRemove;
this.head = null;
this.tail = null;
}
LRU.prototype._createGetSize = function _createGetSize(size) {
if (!size)
return;
if (typeof size === 'number')
return function() { return size; };
if (typeof size === 'function')
return size;
assert(false, 'Bad getSize callback.');
};
LRU.prototype._getSize = function _getSize(item) {
var keySize = item.key.length * 2;
@ -57,6 +71,8 @@ LRU.prototype._compact = function _compact() {
break;
this.size -= this._getSize(item);
delete this.data[item.key];
if (this.onRemove)
this.onRemove(item.key, item.value);
next = item.next;
item.prev = null;
item.next = null;
@ -77,6 +93,8 @@ LRU.prototype.reset = function reset() {
for (item = this.head; item; item = next) {
delete this.data[item.key];
if (this.onRemove)
this.onRemove(item.key, item.value);
next = item.next;
item.prev = null;
item.next = null;
@ -150,6 +168,8 @@ LRU.prototype.remove = function remove(key) {
this.size -= this._getSize(item);
delete this.data[key];
if (this.onRemove)
this.onRemove(item.key, item.value);
this._removeList(item);