bst options.

This commit is contained in:
Christopher Jeffrey 2016-04-03 06:37:20 -07:00
parent 3f0ac32334
commit 8124b4de44

View File

@ -25,23 +25,17 @@ function BST(location, options) {
this.compare = options.compare || utils.cmp;
}
BST.prototype.search = function search(key, options) {
BST.prototype.search = function search(key) {
var current = this.root;
var cmp;
if (typeof key === 'string')
key = new Buffer(key, 'ascii');
if (!options)
options = {};
while (current) {
cmp = this.compare(key, current.key);
if (cmp === 0) {
return options.asBuffer === false
? current.value.toString('utf8')
: current.value;
}
if (cmp === 0)
return current.value;
if (cmp < 0)
current = current.left;
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 left = false;
var parent, cmp;
@ -60,9 +54,6 @@ BST.prototype.insert = function insert(key, value, options) {
if (typeof value === 'string')
value = new Buffer(value, 'utf8');
if (!options)
options = {};
while (current) {
cmp = this.compare(key, current.key);
@ -93,7 +84,7 @@ BST.prototype.insert = function insert(key, value, options) {
parent.right = { key: key, value: value };
};
BST.prototype.remove = function remove(key, options) {
BST.prototype.remove = function remove(key) {
var current = this.root;
var left = false;
var parent, use;
@ -101,9 +92,6 @@ BST.prototype.remove = function remove(key, options) {
if (typeof key === 'string')
key = new Buffer(key, 'ascii');
if (!options)
options = {};
while (current) {
cmp = this.compare(key, current.key);
@ -215,24 +203,17 @@ BST.prototype.snapshot = function snapshot() {
return snapshot;
};
BST.prototype.traverse = function traverse(options, test) {
BST.prototype.traverse = function traverse(test) {
var current = this.root;
var stack = [];
var items = [];
if (!options)
options = {};
for (;;) {
if (current) {
if (test(current)) {
items.push({
key: options.keyAsBuffer === false
? current.key.toString('ascii')
: current.key,
value: options.valueAsBuffer === false
? current.value.toString('utf8')
: current.value
key: current.key,
value: current.value
});
}
stack.push(current);
@ -250,11 +231,11 @@ BST.prototype.traverse = function traverse(options, test) {
return items;
};
BST.prototype.dump = function dump(options) {
return this.traverse(options, function() { return true; });
BST.prototype.dump = function dump() {
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 stack = [];
var items = [];
@ -266,20 +247,13 @@ BST.prototype.range = function range(gte, lte, options) {
if (typeof lte === 'string')
lte = new Buffer(lte, 'ascii');
if (!options)
options = {};
for (;;) {
if (current) {
cmp = this.rangeCompare(current.key, gte, lte);
if (cmp === 0) {
items.push({
key: options.keyAsBuffer === false
? current.key.toString('ascii')
: current.key,
value: options.valueAsBuffer === false
? current.value.toString('utf8')
: current.value
key: current.key,
value: current.value
});
stack.push(current);
}
@ -348,7 +322,7 @@ BST.prototype.close = function close(callback) {
};
BST.prototype.get = function get(key, options, callback) {
var item, err;
var value, err;
if (!callback) {
callback = options;
@ -358,16 +332,19 @@ BST.prototype.get = function get(key, options, callback) {
if (!options)
options = {};
item = this.search(key, options);
value = this.search(key);
if (!item) {
if (!value) {
err = new Error('BST_NOTFOUND: Key not found.');
err.notFound = true;
err.type = 'NotFoundError';
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) {
@ -381,7 +358,7 @@ BST.prototype.put = function put(key, value, options, callback) {
if (!options)
options = {};
this.insert(key, value, options);
this.insert(key, value);
return utils.nextTick(callback);
};
@ -397,7 +374,7 @@ BST.prototype.del = function del(key, options, callback) {
if (!options)
options = {};
this.remove(key, options);
this.remove(key);
return utils.nextTick(callback);
};
@ -671,7 +648,7 @@ function bench(tree, name) {
}
console.log('%s remove: %d', name, Date.now() - rstart);
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 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);
}
var items = tree.range('foo 700', 'foo 800', { keyAsBuffer: false, valueAsBuffer: false });
var items = tree.range('foo 700', 'foo 800');
//utils.print(items);