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

View File

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