bst options.
This commit is contained in:
parent
3f0ac32334
commit
8124b4de44
@ -25,23 +25,17 @@ function BST(location, options) {
|
|||||||
this.compare = options.compare || utils.cmp;
|
this.compare = options.compare || utils.cmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
BST.prototype.search = function search(key, options) {
|
BST.prototype.search = function search(key) {
|
||||||
var current = this.root;
|
var current = this.root;
|
||||||
var cmp;
|
var cmp;
|
||||||
|
|
||||||
if (typeof key === 'string')
|
if (typeof key === 'string')
|
||||||
key = new Buffer(key, 'ascii');
|
key = new Buffer(key, 'ascii');
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
while (current) {
|
while (current) {
|
||||||
cmp = this.compare(key, current.key);
|
cmp = this.compare(key, current.key);
|
||||||
if (cmp === 0) {
|
if (cmp === 0)
|
||||||
return options.asBuffer === false
|
return current.value;
|
||||||
? current.value.toString('utf8')
|
|
||||||
: current.value;
|
|
||||||
}
|
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
current = current.left;
|
current = current.left;
|
||||||
else
|
else
|
||||||
@ -49,7 +43,7 @@ BST.prototype.search = function search(key, options) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.insert = function insert(key, value, options) {
|
BST.prototype.insert = function insert(key, value) {
|
||||||
var current = this.root;
|
var current = this.root;
|
||||||
var left = false;
|
var left = false;
|
||||||
var parent, cmp;
|
var parent, cmp;
|
||||||
@ -60,9 +54,6 @@ BST.prototype.insert = function insert(key, value, options) {
|
|||||||
if (typeof value === 'string')
|
if (typeof value === 'string')
|
||||||
value = new Buffer(value, 'utf8');
|
value = new Buffer(value, 'utf8');
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
while (current) {
|
while (current) {
|
||||||
cmp = this.compare(key, current.key);
|
cmp = this.compare(key, current.key);
|
||||||
|
|
||||||
@ -93,7 +84,7 @@ BST.prototype.insert = function insert(key, value, options) {
|
|||||||
parent.right = { key: key, value: value };
|
parent.right = { key: key, value: value };
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.remove = function remove(key, options) {
|
BST.prototype.remove = function remove(key) {
|
||||||
var current = this.root;
|
var current = this.root;
|
||||||
var left = false;
|
var left = false;
|
||||||
var parent, use;
|
var parent, use;
|
||||||
@ -101,9 +92,6 @@ BST.prototype.remove = function remove(key, options) {
|
|||||||
if (typeof key === 'string')
|
if (typeof key === 'string')
|
||||||
key = new Buffer(key, 'ascii');
|
key = new Buffer(key, 'ascii');
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
while (current) {
|
while (current) {
|
||||||
cmp = this.compare(key, current.key);
|
cmp = this.compare(key, current.key);
|
||||||
|
|
||||||
@ -215,24 +203,17 @@ BST.prototype.snapshot = function snapshot() {
|
|||||||
return snapshot;
|
return snapshot;
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.traverse = function traverse(options, test) {
|
BST.prototype.traverse = function traverse(test) {
|
||||||
var current = this.root;
|
var current = this.root;
|
||||||
var stack = [];
|
var stack = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (current) {
|
if (current) {
|
||||||
if (test(current)) {
|
if (test(current)) {
|
||||||
items.push({
|
items.push({
|
||||||
key: options.keyAsBuffer === false
|
key: current.key,
|
||||||
? current.key.toString('ascii')
|
value: current.value
|
||||||
: current.key,
|
|
||||||
value: options.valueAsBuffer === false
|
|
||||||
? current.value.toString('utf8')
|
|
||||||
: current.value
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
stack.push(current);
|
stack.push(current);
|
||||||
@ -250,11 +231,11 @@ BST.prototype.traverse = function traverse(options, test) {
|
|||||||
return items;
|
return items;
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.dump = function dump(options) {
|
BST.prototype.dump = function dump() {
|
||||||
return this.traverse(options, function() { return true; });
|
return this.traverse(function() { return true; });
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.range = function range(gte, lte, options) {
|
BST.prototype.range = function range(gte, lte) {
|
||||||
var current = this.root;
|
var current = this.root;
|
||||||
var stack = [];
|
var stack = [];
|
||||||
var items = [];
|
var items = [];
|
||||||
@ -266,20 +247,13 @@ BST.prototype.range = function range(gte, lte, options) {
|
|||||||
if (typeof lte === 'string')
|
if (typeof lte === 'string')
|
||||||
lte = new Buffer(lte, 'ascii');
|
lte = new Buffer(lte, 'ascii');
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (current) {
|
if (current) {
|
||||||
cmp = this.rangeCompare(current.key, gte, lte);
|
cmp = this.rangeCompare(current.key, gte, lte);
|
||||||
if (cmp === 0) {
|
if (cmp === 0) {
|
||||||
items.push({
|
items.push({
|
||||||
key: options.keyAsBuffer === false
|
key: current.key,
|
||||||
? current.key.toString('ascii')
|
value: current.value
|
||||||
: current.key,
|
|
||||||
value: options.valueAsBuffer === false
|
|
||||||
? current.value.toString('utf8')
|
|
||||||
: current.value
|
|
||||||
});
|
});
|
||||||
stack.push(current);
|
stack.push(current);
|
||||||
}
|
}
|
||||||
@ -348,7 +322,7 @@ BST.prototype.close = function close(callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.get = function get(key, options, callback) {
|
BST.prototype.get = function get(key, options, callback) {
|
||||||
var item, err;
|
var value, err;
|
||||||
|
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
callback = options;
|
callback = options;
|
||||||
@ -358,16 +332,19 @@ BST.prototype.get = function get(key, options, callback) {
|
|||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
item = this.search(key, options);
|
value = this.search(key);
|
||||||
|
|
||||||
if (!item) {
|
if (!value) {
|
||||||
err = new Error('BST_NOTFOUND: Key not found.');
|
err = new Error('BST_NOTFOUND: Key not found.');
|
||||||
err.notFound = true;
|
err.notFound = true;
|
||||||
err.type = 'NotFoundError';
|
err.type = 'NotFoundError';
|
||||||
return utils.asyncify(callback)(err);
|
return utils.asyncify(callback)(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils.asyncify(callback)(null, item);
|
if (options.asBuffer === false)
|
||||||
|
value = value.toString('utf8');
|
||||||
|
|
||||||
|
return utils.asyncify(callback)(null, value);
|
||||||
};
|
};
|
||||||
|
|
||||||
BST.prototype.put = function put(key, value, options, callback) {
|
BST.prototype.put = function put(key, value, options, callback) {
|
||||||
@ -381,7 +358,7 @@ BST.prototype.put = function put(key, value, options, callback) {
|
|||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
this.insert(key, value, options);
|
this.insert(key, value);
|
||||||
|
|
||||||
return utils.nextTick(callback);
|
return utils.nextTick(callback);
|
||||||
};
|
};
|
||||||
@ -397,7 +374,7 @@ BST.prototype.del = function del(key, options, callback) {
|
|||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
this.remove(key, options);
|
this.remove(key);
|
||||||
|
|
||||||
return utils.nextTick(callback);
|
return utils.nextTick(callback);
|
||||||
};
|
};
|
||||||
@ -671,7 +648,7 @@ function bench(tree, name) {
|
|||||||
}
|
}
|
||||||
console.log('%s remove: %d', name, Date.now() - rstart);
|
console.log('%s remove: %d', name, Date.now() - rstart);
|
||||||
var itstart = Date.now();
|
var itstart = Date.now();
|
||||||
tree.range('foo 700', 'foo 80000', { keyAsBuffer: false, valueAsBuffer: false });
|
tree.range('foo 700', 'foo 80000');
|
||||||
console.log('%s iter: %d', name, Date.now() - itstart);
|
console.log('%s iter: %d', name, Date.now() - itstart);
|
||||||
console.log('%s total: %d', name, Date.now() - start);
|
console.log('%s total: %d', name, Date.now() - start);
|
||||||
}
|
}
|
||||||
@ -694,7 +671,7 @@ for (var i = 0; i < 1000; i++) {
|
|||||||
assert(tree.search('foo ' + i).toString('utf8') === 'bar ' + i);
|
assert(tree.search('foo ' + i).toString('utf8') === 'bar ' + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = tree.range('foo 700', 'foo 800', { keyAsBuffer: false, valueAsBuffer: false });
|
var items = tree.range('foo 700', 'foo 800');
|
||||||
|
|
||||||
//utils.print(items);
|
//utils.print(items);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user