memorydb: more validation.
This commit is contained in:
parent
d4f5f7cd66
commit
df06c91557
@ -9,7 +9,7 @@
|
||||
var assert = require('assert');
|
||||
var util = require('../utils/util');
|
||||
var RBT = require('../utils/rbt');
|
||||
var DUMMY = new Buffer([0]);
|
||||
var DUMMY = new Buffer(0);
|
||||
|
||||
/**
|
||||
* In memory database for bcoin
|
||||
@ -21,20 +21,12 @@ var DUMMY = new Buffer([0]);
|
||||
* @param {Function} options.compare - Comparator.
|
||||
*/
|
||||
|
||||
function MemoryDB(location, options) {
|
||||
function MemoryDB(location) {
|
||||
if (!(this instanceof MemoryDB))
|
||||
return new MemoryDB(location, options);
|
||||
return new MemoryDB(location);
|
||||
|
||||
if (typeof location !== 'string') {
|
||||
options = location;
|
||||
location = null;
|
||||
}
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
this.location = location;
|
||||
this.options = options;
|
||||
this.location = location || 'memory';
|
||||
this.options = {};
|
||||
this.tree = new RBT(util.cmp, true);
|
||||
}
|
||||
|
||||
@ -51,6 +43,8 @@ MemoryDB.prototype.search = function search(key) {
|
||||
if (typeof key === 'string')
|
||||
key = new Buffer(key, 'utf8');
|
||||
|
||||
assert(Buffer.isBuffer(key), 'Key must be a Buffer.');
|
||||
|
||||
node = this.tree.search(key);
|
||||
|
||||
if (!node)
|
||||
@ -73,6 +67,12 @@ MemoryDB.prototype.insert = function insert(key, value) {
|
||||
if (typeof value === 'string')
|
||||
value = new Buffer(value, 'utf8');
|
||||
|
||||
if (value == null)
|
||||
value = DUMMY;
|
||||
|
||||
assert(Buffer.isBuffer(key), 'Key must be a Buffer.');
|
||||
assert(Buffer.isBuffer(value), 'Value must be a Buffer.');
|
||||
|
||||
return this.tree.insert(key, value) != null;
|
||||
};
|
||||
|
||||
@ -87,25 +87,30 @@ MemoryDB.prototype.remove = function remove(key) {
|
||||
if (typeof key === 'string')
|
||||
key = new Buffer(key, 'utf8');
|
||||
|
||||
assert(Buffer.isBuffer(key), 'Key must be a Buffer.');
|
||||
|
||||
return this.tree.remove(key) != null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Traverse between a range of keys and collect records.
|
||||
* @private
|
||||
* @param {Buffer} gte
|
||||
* @param {Buffer} lte
|
||||
* @returns {RBTNode[]} Records.
|
||||
* @param {Buffer} min
|
||||
* @param {Buffer} max
|
||||
* @returns {RBTData[]} Records.
|
||||
*/
|
||||
|
||||
MemoryDB.prototype.range = function range(gte, lte) {
|
||||
if (typeof gte === 'string')
|
||||
gte = new Buffer(gte, 'utf8');
|
||||
MemoryDB.prototype.range = function range(min, max) {
|
||||
if (typeof min === 'string')
|
||||
min = new Buffer(min, 'utf8');
|
||||
|
||||
if (typeof lte === 'string')
|
||||
lte = new Buffer(lte, 'utf8');
|
||||
if (typeof max === 'string')
|
||||
max = new Buffer(max, 'utf8');
|
||||
|
||||
return this.tree.range(gte, lte);
|
||||
assert(!min || Buffer.isBuffer(min), 'Key must be a Buffer.');
|
||||
assert(!max || Buffer.isBuffer(max), 'Key must be a Buffer.');
|
||||
|
||||
return this.tree.range(min, max);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -158,7 +163,7 @@ MemoryDB.prototype.get = function get(key, options, callback) {
|
||||
value = this.search(key);
|
||||
|
||||
if (!value) {
|
||||
err = new Error('MemoryDB_NOTFOUND: Key not found.');
|
||||
err = new Error('MEMDB_NOTFOUND: Key not found.');
|
||||
err.notFound = true;
|
||||
err.type = 'NotFoundError';
|
||||
util.nextTick(function() {
|
||||
@ -231,7 +236,7 @@ MemoryDB.prototype.batch = function batch(ops, options, callback) {
|
||||
batch = new Batch(this, options);
|
||||
|
||||
if (ops) {
|
||||
batch.ops = ops.slice();
|
||||
batch.ops = ops;
|
||||
batch.write(callback);
|
||||
return;
|
||||
}
|
||||
@ -307,14 +312,14 @@ MemoryDB.repair = function repair(location, callback) {
|
||||
* @constructor
|
||||
* @ignore
|
||||
* @private
|
||||
* @param {RBT} tree
|
||||
* @param {MemoryDB} db
|
||||
* @param {Object?} options
|
||||
*/
|
||||
|
||||
function Batch(tree, options) {
|
||||
function Batch(db, options) {
|
||||
this.options = options || {};
|
||||
this.ops = [];
|
||||
this.tree = tree;
|
||||
this.db = db;
|
||||
this.written = false;
|
||||
}
|
||||
|
||||
@ -360,10 +365,10 @@ Batch.prototype.write = function write(callback) {
|
||||
op = this.ops[i];
|
||||
switch (op.type) {
|
||||
case 'put':
|
||||
this.tree.insert(op.key, op.value);
|
||||
this.db.insert(op.key, op.value);
|
||||
break;
|
||||
case 'del':
|
||||
this.tree.remove(op.key);
|
||||
this.db.remove(op.key);
|
||||
break;
|
||||
default:
|
||||
util.nextTick(function() {
|
||||
@ -373,7 +378,7 @@ Batch.prototype.write = function write(callback) {
|
||||
}
|
||||
}
|
||||
|
||||
this.ops.length = 0;
|
||||
this.ops = [];
|
||||
this.written = true;
|
||||
|
||||
util.nextTick(callback);
|
||||
@ -387,7 +392,7 @@ Batch.prototype.write = function write(callback) {
|
||||
|
||||
Batch.prototype.clear = function clear() {
|
||||
assert(!this.written, 'Already written.');
|
||||
this.ops.length = 0;
|
||||
this.ops = [];
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -549,6 +554,8 @@ Iterator.prototype.seek = function seek(key) {
|
||||
if (typeof key === 'string')
|
||||
key = new Buffer(key, 'utf8');
|
||||
|
||||
assert(Buffer.isBuffer(key), 'Key must be a Buffer.');
|
||||
|
||||
if (this.options.reverse)
|
||||
this.iter.seekMax(key);
|
||||
else
|
||||
@ -637,6 +644,18 @@ IteratorOptions.prototype.fromOptions = function fromOptions(options) {
|
||||
this.end = options.lt;
|
||||
}
|
||||
|
||||
if (this.start != null) {
|
||||
if (typeof this.start === 'string')
|
||||
this.start = new Buffer(this.start, 'utf8');
|
||||
assert(Buffer.isBuffer(this.start), '`start` must be a Buffer.');
|
||||
}
|
||||
|
||||
if (this.end != null) {
|
||||
if (typeof this.end === 'string')
|
||||
this.end = new Buffer(this.end, 'utf8');
|
||||
assert(Buffer.isBuffer(this.end), '`end` must be a Buffer.');
|
||||
}
|
||||
|
||||
if (options.keyAsBuffer != null) {
|
||||
assert(typeof options.keyAsBuffer === 'boolean');
|
||||
this.keyAsBuffer = options.keyAsBuffer;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user