fix binary index.

This commit is contained in:
Christopher Jeffrey 2016-04-01 01:12:22 -07:00
parent 3dddfcf7ea
commit 4d30c9428a

View File

@ -1429,7 +1429,12 @@ BinaryIndex.prototype.insert = function insert(tx) {
}; };
BinaryIndex.prototype.remove = function remove(tx) { BinaryIndex.prototype.remove = function remove(tx) {
var index = binarySearch(this.data, tx.hash()); var ps = new Buffer(4);
var index;
utils.writeU32BE(ps, tx.ps, 0);
index = binarySearch(this.index, ps);
if (index !== -1) { if (index !== -1) {
this.index.splice(index, 1); this.index.splice(index, 1);
@ -1439,12 +1444,12 @@ BinaryIndex.prototype.remove = function remove(tx) {
BinaryIndex.prototype.range = function range(start, end) { BinaryIndex.prototype.range = function range(start, end) {
var hashes = []; var hashes = [];
var s = new Buffer(4); var ts = new Buffer(4);
var i, ps; var i, ps;
utils.writeU32BE(s, start, 0); utils.writeU32BE(ts, start, 0);
i = binarySearch(this.index, s, true); i = binarySearch(this.index, ts, true);
for (; i < this.index.length; i++) { for (; i < this.index.length; i++) {
ps = utils.readU32BE(this.index[i], 0); ps = utils.readU32BE(this.index[i], 0);
@ -1460,14 +1465,17 @@ BinaryIndex.prototype.range = function range(start, end) {
* Helpers * Helpers
*/ */
function binarySearch(items, key, insert) { function binarySearch(items, key, insert, compare) {
var start = 0; var start = 0;
var end = items.length - 1; var end = items.length - 1;
var pos, cmp; var pos, cmp;
if (!compare)
compare = utils.cmp;
while (start <= end) { while (start <= end) {
pos = (start + end) >>> 1; pos = (start + end) >>> 1;
cmp = utils.cmp(items[pos], key); cmp = compare(items[pos], key);
if (cmp === 0) if (cmp === 0)
return pos; return pos;