abstract-nosql
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -8,2 +8,3 @@ var Errors = require('abstract-object/Error') | ||
var AlreadyEndError = createError("AlreadyEnd", 0x53) | ||
var AlreadyRunError = createError("AlreadyRun", 0x54) | ||
@@ -13,3 +14,4 @@ Errors.OpenError = OpenError | ||
Errors.AlreadyEndError = AlreadyEndError | ||
Errors.AlreadyRunError = AlreadyRunError | ||
module.exports = Errors; |
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var AbstractError, AbstractIterator, AlreadyEndError, Errors, InvalidArgumentError, NotImplementedError, inherits, util; | ||
var AbstractError, AbstractIterator, AlreadyEndError, AlreadyRunError, Errors, InvalidArgumentError, NotImplementedError, inherits, isArray, util; | ||
@@ -9,2 +9,4 @@ util = require("abstract-object/lib/util"); | ||
isArray = util.isArray; | ||
Errors = require("./abstract-error"); | ||
@@ -20,9 +22,18 @@ | ||
AlreadyRunError = Errors.AlreadyRunError; | ||
module.exports = AbstractIterator = (function() { | ||
AbstractIterator.AlreadyEndError = AlreadyEndError; | ||
function AbstractIterator(db) { | ||
AbstractIterator.AlreadyRunError = AlreadyRunError; | ||
function AbstractIterator(db, options) { | ||
this.db = db; | ||
this.options = options; | ||
this._ended = false; | ||
this._nexting = false; | ||
if (options && isArray(options.range)) { | ||
this._resultOfKeys = options.range; | ||
this._indexOfKeys = -1; | ||
} | ||
} | ||
@@ -81,20 +92,55 @@ | ||
var result; | ||
if (this._nextSync) { | ||
if (this._ended) { | ||
throw new AlreadyEndError("cannot call next() after end()"); | ||
} | ||
if (this._nexting) { | ||
throw new AlreadyRunError("cannot call next() before previous next() has completed"); | ||
} | ||
if (this._indexOfKeys != null) { | ||
this._nexting = true; | ||
if (this._indexOfKeys === -1) { | ||
this._resultOfKeys = this.db._mGetSync(this._resultOfKeys, this.options); | ||
this._indexOfKeys++; | ||
} | ||
result = this._indexOfKeys >= 0 && this._indexOfKeys < this._resultOfKeys.length; | ||
if (result) { | ||
result = { | ||
key: this._resultOfKeys[this._indexOfKeys], | ||
value: this._resultOfKeys[++this._indexOfKeys] | ||
}; | ||
this._indexOfKeys++; | ||
} | ||
this._nexting = false; | ||
return result; | ||
} else if (this._nextSync) { | ||
this._nexting = true; | ||
result = this._nextSync(); | ||
if (result !== false) { | ||
result = { | ||
key: result[0], | ||
value: result[1] | ||
}; | ||
} | ||
this._nexting = false; | ||
return result; | ||
} else { | ||
throw new NotImplementedError(); | ||
} | ||
throw new NotImplementedError(); | ||
}; | ||
AbstractIterator.prototype.endSync = function() { | ||
if (this._endSync) { | ||
if (this._indexOfKeys != null) { | ||
delete this._resultOfKeys; | ||
this._indexOfKeys = -2; | ||
return this._ended = true; | ||
} else if (this._endSync) { | ||
this._ended = true; | ||
return this._endSync(); | ||
} else { | ||
throw new NotImplementedError(); | ||
} | ||
throw new NotImplementedError(); | ||
}; | ||
AbstractIterator.prototype.next = function(callback) { | ||
var self; | ||
var result, self; | ||
if (typeof callback !== "function") { | ||
@@ -107,10 +153,39 @@ throw new InvalidArgumentError("next() requires a callback argument"); | ||
if (this._nexting) { | ||
return callback(new AlreadyEndError("cannot call next() before previous next() has completed")); | ||
return callback(new AlreadyRunError("cannot call next() before previous next() has completed")); | ||
} | ||
this._nexting = true; | ||
self = this; | ||
return this._next(function() { | ||
self._nexting = false; | ||
return callback.apply(null, arguments); | ||
}); | ||
if (this._indexOfKeys != null) { | ||
this._nexting = true; | ||
if (this._indexOfKeys === -1) { | ||
self = this; | ||
this.db._mGet(this._resultOfKeys, this.options, function(err, arr) { | ||
self._nexting = false; | ||
if (err) { | ||
return callback(err); | ||
} | ||
self._resultOfKeys = arr; | ||
self._indexOfKeys++; | ||
return self.next(callback); | ||
}); | ||
return this; | ||
} else if (this._indexOfKeys >= 0 && this._indexOfKeys < this._resultOfKeys.length) { | ||
result = this._resultOfKeys.slice(this._indexOfKeys, this._indexOfKeys += 2); | ||
this._nexting = false; | ||
} else { | ||
result = false; | ||
} | ||
this._nexting = false; | ||
if (result === false) { | ||
callback(); | ||
} else { | ||
callback(void 0, result[0], result[1]); | ||
} | ||
} else { | ||
this._nexting = true; | ||
self = this; | ||
this._next(function() { | ||
self._nexting = false; | ||
return callback.apply(null, arguments); | ||
}); | ||
} | ||
return this; | ||
}; | ||
@@ -125,4 +200,11 @@ | ||
} | ||
this._ended = true; | ||
return this._end(callback); | ||
if (this._indexOfKeys != null) { | ||
this._ended = true; | ||
delete this._resultOfKeys; | ||
this._indexOfKeys = -2; | ||
return setImmediate(callback); | ||
} else { | ||
this._ended = true; | ||
return this._end(callback); | ||
} | ||
}; | ||
@@ -129,0 +211,0 @@ |
@@ -113,2 +113,23 @@ // Generated by CoffeeScript 1.8.0 | ||
AbstractNoSQL.prototype.mGetSync = function(keys, options) { | ||
var arr, i, result; | ||
if (this._mGetSync) { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
arr = this._mGetSync(keys, options); | ||
i = 0; | ||
result = []; | ||
while (i < arr.length) { | ||
result.push({ | ||
key: arr[i], | ||
value: arr[++i] | ||
}); | ||
i++; | ||
} | ||
return result; | ||
} | ||
throw new NotImplementedError(); | ||
}; | ||
AbstractNoSQL.prototype.putSync = function(key, value, options) { | ||
@@ -269,2 +290,53 @@ var result; | ||
AbstractNoSQL.prototype._mGetSync = function(keys, options) { | ||
var key, result, value, _i, _len; | ||
if (this._getSync) { | ||
result = []; | ||
for (_i = 0, _len = keys.length; _i < _len; _i++) { | ||
key = keys[_i]; | ||
value = this._getSync(key, options); | ||
result.push(key, value); | ||
} | ||
return result; | ||
} else { | ||
throw new NotImplementedError('_mGetSync: _getSync is not implemented.'); | ||
} | ||
}; | ||
AbstractNoSQL.prototype._mGet = function(keys, options, callback) { | ||
var i, readNext, result, that; | ||
that = this; | ||
if (this._getSync) { | ||
return setImmediate(function() { | ||
var err, result; | ||
result = void 0; | ||
try { | ||
result = that._mGetSync(keys, options); | ||
} catch (_error) { | ||
err = _error; | ||
callback(err); | ||
return; | ||
} | ||
return callback(null, result); | ||
}); | ||
} else if (keys.length > 0) { | ||
result = []; | ||
i = 0; | ||
readNext = function(err, value) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
result.push(keys[i], value); | ||
i++; | ||
if (i >= keys.length) { | ||
return callback(null, result); | ||
} | ||
return this._get(key[i], options, readNext); | ||
}; | ||
return this._get(keys[i], options, readNext); | ||
} else { | ||
return setImmediate(callback); | ||
} | ||
}; | ||
AbstractNoSQL.prototype._get = function(key, options, callback) { | ||
@@ -436,2 +508,35 @@ var that; | ||
AbstractNoSQL.prototype.mGet = function(keys, options, callback) { | ||
var err; | ||
err = void 0; | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = {}; | ||
} else { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
} | ||
if (callback) { | ||
return this._mGet(keys, options, function(err, arr) { | ||
var i, result; | ||
if (err) { | ||
return callback(err); | ||
} | ||
i = 0; | ||
result = []; | ||
while (i < arr.length) { | ||
result.push({ | ||
key: arr[i], | ||
value: arr[++i] | ||
}); | ||
i++; | ||
} | ||
return callback(null, result); | ||
}); | ||
} else { | ||
return this.mGetSync(keys, options); | ||
} | ||
}; | ||
AbstractNoSQL.prototype.get = function(key, options, callback) { | ||
@@ -442,2 +547,7 @@ var err; | ||
callback = options; | ||
options = {}; | ||
} else { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
} | ||
@@ -454,5 +564,2 @@ if (err = this._checkKey(key, "key", this._isBuffer)) { | ||
} | ||
if (typeof options !== "object") { | ||
options = {}; | ||
} | ||
options.asBuffer = options.asBuffer !== false; | ||
@@ -459,0 +566,0 @@ if (callback) { |
@@ -217,434 +217,2 @@ var db | ||
}) | ||
test('test full data collection but skip first one', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt:null}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length-1, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
expected.splice(0,1) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test full data collection but skip first one with reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt:null, reverse: true}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length-1, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().map(transformSource) | ||
expected.splice(0,1) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '00' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=50', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=50 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key (49.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key (49999)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49999' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end=50', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key (50.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key (50555)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50555' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
// end='0', starting key is actually '00' so it should avoid it | ||
test('test iterator with end=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '0' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: [50,]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[50, ]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: [50,] and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[50, ]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0,50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: (50,]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(50, ]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice(51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: (50,] and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(50, ]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0,49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with rang: [,50]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[,50]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range [,50.5] being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[,50.5]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0,51).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range [30,70]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[30,70]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range [30,70] and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"[30,70]", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range (30,70)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(30,70)' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-2, 'correct number of entries') | ||
var expected = sourceData.slice(31, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range (30,70) and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"(30,70)", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-2, 'correct number of entries') | ||
var expected = sourceData.slice(31,70).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range (30,70]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(30,70]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-1, 'correct number of entries') | ||
var expected = sourceData.slice(31, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range (30,70] and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"(30,70]", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-1, 'correct number of entries') | ||
var expected = sourceData.slice(31,71).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=30 and end=70', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '30', end: '70' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=30 and end=70 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '70', end: '30', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with gte=30 and lte=70', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '30', lte: '70' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and start=20', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '20', limit: 20 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(20, 40).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and start=20 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '79', limit: 20, reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(20, 40).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
// the default limit value from levelup is -1 | ||
test('test iterator with limit=-1 should iterate over whole database', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: -1}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=0 should not iterate over anything', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 0 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end after limit', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, end: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end before limit', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 50, end: '19' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start after database end', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start after database end and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start and end after database and and reverse=true', function (t) { | ||
collectEntries(db.iterator({ start: '9b', end: '9a', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start/gt/gte=new Buffer("9a") and end/lt/lte=new Buffer("9b")', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: true, valueAsBuffer: false, start: new Buffer('9b'), end: new Buffer('9a'), lt: new Buffer('9b'), gt: new Buffer('9a'), gte: new Buffer('9a'), lte: new Buffer('9b') }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
function testIteratorCollectsFullDatabase (name, iteratorOptions) { | ||
iteratorOptions.keyAsBuffer = false | ||
iteratorOptions.valueAsBuffer = false | ||
test(name, function (t) { | ||
collectEntries(db.iterator(iteratorOptions), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 100, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
} | ||
if (!process.browser) { | ||
// Can't use buffers as query keys in indexeddb (I think :P) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with start as empty buffer' | ||
, { start: new Buffer(0) } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with end as empty buffer' | ||
, { end: new Buffer(0) } | ||
) | ||
} | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with start as empty string' | ||
, { start: '' } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with start as null' | ||
, { start: null } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with end as empty string' | ||
, { end: '' } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with end as null' | ||
, { end: null } | ||
) | ||
} | ||
@@ -651,0 +219,0 @@ |
@@ -83,2 +83,285 @@ var db | ||
test('test full data collection but skip first one', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt:null}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length-1, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
expected.splice(0,1) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test full data collection but skip first one with reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt:null, reverse: true}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length-1, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().map(transformSource) | ||
expected.splice(0,1) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range keys: ["15", "03", "99"]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: ["15", "03", "99"] }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 3, 'correct number of entries') | ||
var all = sourceData.slice().map(transformSource) | ||
var expected = [] | ||
expected[0] = all[15] | ||
expected[1] = all[3] | ||
expected[2] = all[99] | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: "[50,]"', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[50, ]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: "[50,]" and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[50, ]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0,50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: "(50,]"', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(50, ]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice(51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range: "(50,]" and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(50, ]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0,49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with rang: "[,50]"', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[,50]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "[,50.5]" being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[,50.5]', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0,51).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range [30,70]', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '[30,70]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "[30,70]" and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"[30,70]", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "(30,70)"', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(30,70)' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-2, 'correct number of entries') | ||
var expected = sourceData.slice(31, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "(30,70)" and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"(30,70)", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-2, 'correct number of entries') | ||
var expected = sourceData.slice(31,70).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "(30,70]"', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: '(30,70]' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-1, 'correct number of entries') | ||
var expected = sourceData.slice(31, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with range "(30,70]" and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range:"(30,70]", reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41-1, 'correct number of entries') | ||
var expected = sourceData.slice(31,71).reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '00' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=50', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=50 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '50', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key (49.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key (49999)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49999' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '49.5', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 50, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(50).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end=50', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key (50.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key (50555)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50555' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 51, 'correct number of entries') | ||
var expected = sourceData.slice(0, 51).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '50.5', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 49, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0, 49).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
// end='0', starting key is actually '00' so it should avoid it | ||
test('test iterator with end=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, end: '0' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=30 and end=70', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '30', end: '70' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start=30 and end=70 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '70', end: '30', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with gte=0', function (t) { | ||
@@ -114,3 +397,3 @@ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '00' }), function (err, data) { | ||
test('test iterator with start being a midway key (49.5)', function (t) { | ||
test('test iterator with gte being a midway key (49.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '49.5' }), function (err, data) { | ||
@@ -125,3 +408,3 @@ t.error(err) | ||
test('test iterator with start being a midway key (49999)', function (t) { | ||
test('test iterator with gte being a midway key (49999)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '49999' }), function (err, data) { | ||
@@ -136,3 +419,3 @@ t.error(err) | ||
test('test iterator with start being a midway key and reverse=true', function (t) { | ||
test('test iterator with gte being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '49.5', reverse: true }), function (err, data) { | ||
@@ -147,3 +430,3 @@ t.error(err) | ||
test('test iterator with start being a midway key and reverse=true', function (t) { | ||
test('test iterator with lt being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '49.5', reverse: true }), function (err, data) { | ||
@@ -158,3 +441,3 @@ t.error(err) | ||
test('test iterator with start being a midway key and reverse=true', function (t) { | ||
test('test iterator with lt being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '50', reverse: true }), function (err, data) { | ||
@@ -169,3 +452,3 @@ t.error(err) | ||
test('test iterator with end=50', function (t) { | ||
test('test iterator with lte=50', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50' }), function (err, data) { | ||
@@ -180,3 +463,3 @@ t.error(err) | ||
test('test iterator with end being a midway key (50.5)', function (t) { | ||
test('test iterator with lte being a midway key (50.5)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50.5' }), function (err, data) { | ||
@@ -191,3 +474,3 @@ t.error(err) | ||
test('test iterator with end being a midway key (50555)', function (t) { | ||
test('test iterator with lte being a midway key (50555)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '50555' }), function (err, data) { | ||
@@ -202,3 +485,3 @@ t.error(err) | ||
test('test iterator with end being a midway key (50555)', function (t) { | ||
test('test iterator with lt being a midway key (50555)', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '50555' }), function (err, data) { | ||
@@ -213,3 +496,3 @@ t.error(err) | ||
test('test iterator with end being a midway key and reverse=true', function (t) { | ||
test('test iterator with gte being a midway key and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '50.5', reverse: true }), function (err, data) { | ||
@@ -255,3 +538,3 @@ t.error(err) | ||
// end='0', starting key is actually '00' so it should avoid it | ||
test('test iterator with end=0', function (t) { | ||
test('test iterator with lte=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '0' }), function (err, data) { | ||
@@ -265,3 +548,3 @@ t.error(err) | ||
// end='0', starting key is actually '00' so it should avoid it | ||
test('test iterator with end<0', function (t) { | ||
test('test iterator with lt=0', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '0' }), function (err, data) { | ||
@@ -274,3 +557,3 @@ t.error(err) | ||
test('test iterator with start=30 and end=70', function (t) { | ||
test('test iterator with gte=30 and lte=70', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '30', lte: '70' }), function (err, data) { | ||
@@ -285,3 +568,3 @@ t.error(err) | ||
test('test iterator with start=30 and end=70', function (t) { | ||
test('test iterator with gt=29 and lt=71', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '29', lt: '71' }), function (err, data) { | ||
@@ -296,3 +579,3 @@ t.error(err) | ||
test('test iterator with start=30 and end=70 and reverse=true', function (t) { | ||
test('test iterator with gte=30 and lte=70 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '70', gte: '30', reverse: true }), function (err, data) { | ||
@@ -307,3 +590,3 @@ t.error(err) | ||
test('test iterator with start=30 and end=70 and reverse=true', function (t) { | ||
test('test iterator with gt=29 and lt=71 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '71', gt: '29', reverse: true }), function (err, data) { | ||
@@ -318,3 +601,3 @@ t.error(err) | ||
test('test iterator with limit=20 and start=20', function (t) { | ||
test('test iterator with limit=20 and gte=20', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '20', limit: 20 }), function (err, data) { | ||
@@ -329,3 +612,3 @@ t.error(err) | ||
test('test iterator with limit=20 and start=79 and reverse=true', function (t) { | ||
test('test iterator with limit=20 and lte=79 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '79', limit: 20, reverse: true }), function (err, data) { | ||
@@ -340,2 +623,61 @@ t.error(err) | ||
test('test iterator with limit=20', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and start=20', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '20', limit: 20 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(20, 40).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=20 and start=20 and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '79', limit: 20, reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(20, 40).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
// the default limit value from levelup is -1 | ||
test('test iterator with limit=-1 should iterate over whole database', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: -1}), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with limit=0 should not iterate over anything', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 0 }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end after limit', function (t) { | ||
@@ -403,2 +745,56 @@ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, lte: '50' }), function (err, data) { | ||
test('test iterator with end after limit', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 20, end: '50' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with end before limit', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, limit: 50, end: '19' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(0, 20).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start after database end', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a' }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start after database end and reverse=true', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, sourceData.length, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start and end after database and and reverse=true', function (t) { | ||
collectEntries(db.iterator({ start: '9b', end: '9a', reverse: true }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start/gt/gte=new Buffer("9a") and end/lt/lte=new Buffer("9b")', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: true, valueAsBuffer: false, start: new Buffer('9b'), end: new Buffer('9a'), lt: new Buffer('9b'), gt: new Buffer('9a'), gte: new Buffer('9a'), lte: new Buffer('9b') }), function (err, data) { | ||
t.error(err) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
function testIteratorCollectsFullDatabase (name, iteratorOptions) { | ||
@@ -430,2 +826,18 @@ iteratorOptions.keyAsBuffer = false | ||
'test iterator with start as empty string' | ||
, { start: '' } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with start as null' | ||
, { start: null } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with end as empty string' | ||
, { end: '' } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with end as null' | ||
, { end: null } | ||
) | ||
testIteratorCollectsFullDatabase( | ||
'test iterator with start as empty string' | ||
, { gte: '' } | ||
@@ -447,2 +859,68 @@ ) | ||
module.exports.nextSync = function (leveldown, test, testCommon) { | ||
function collectEntries(iterator) { | ||
result = [] | ||
while (item = iterator.nextSync()) { | ||
result.push(item) | ||
} | ||
return result | ||
} | ||
test('test sync iterator with gte=30 and lte=70', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '30', lte: '70' })) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
test('test sync iterator with gt=29 and lt=71', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gt: '29', lt: '71' })) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice(30, 71).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
test('test sync iterator with gte=30 and lte=70 and reverse=true', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lte: '70', gte: '30', reverse: true })) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
test('test sync iterator with gt=29 and lt=71 and reverse=true', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, lt: '71', gt: '29', reverse: true })) | ||
t.equal(data.length, 41, 'correct number of entries') | ||
var expected = sourceData.slice().reverse().slice(29, 70).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
test('test sync iterator with limit=20 and gte=20', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '20', limit: 20 })) | ||
t.equal(data.length, 20, 'correct number of entries') | ||
var expected = sourceData.slice(20, 40).map(transformSource) | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
test('test sync iterator with start after database end', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, gte: '9a' })) | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
test('test sync iterator with range keys: ["15", "03", "99"]', function (t) { | ||
var data = collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, range: ["15", "03", "99"] })) | ||
t.equal(data.length, 3, 'correct number of entries') | ||
var all = sourceData.slice().map(transformSource) | ||
var expected = [] | ||
expected[0] = all[15] | ||
expected[1] = all[3] | ||
expected[2] = all[99] | ||
t.deepEqual(data, expected) | ||
t.end() | ||
}) | ||
} | ||
module.exports.tearDown = function (test, testCommon) { | ||
@@ -454,6 +932,28 @@ test('tearDown', function (t) { | ||
module.exports.sync = function (Iterator, test) { | ||
test('sync', function (t) { | ||
if (Iterator.prototype._nextSync) { | ||
delete Iterator.prototype._next | ||
} | ||
if (Iterator.prototype._endSync) { | ||
delete Iterator.prototype._end | ||
} | ||
t.end() | ||
}) | ||
} | ||
module.exports.all = function (leveldown, test, testCommon) { | ||
module.exports.setUp(leveldown, test, testCommon) | ||
module.exports.iterator(leveldown, test, testCommon, testCommon.collectEntries) | ||
if (leveldown.prototype._getSync) { | ||
module.exports.nextSync(leveldown, test, testCommon) | ||
} | ||
module.exports.tearDown(test, testCommon) | ||
var Iterator = leveldown.prototype.IteratorClass | ||
if (Iterator.prototype._nextSync) { | ||
module.exports.sync(Iterator, test) | ||
module.exports.setUp(leveldown, test, testCommon) | ||
module.exports.iterator(leveldown, test, testCommon, testCommon.collectEntries) | ||
module.exports.tearDown(test, testCommon) | ||
} | ||
} |
{ | ||
"name": "abstract-nosql", | ||
"description": "An abstract prototype for nosql database(LevelDOWN API)", | ||
"version": "1.6.0", | ||
"description": "An abstract prototype for nosql database with sync and streamable (LevelDOWN API)", | ||
"version": "1.7.0", | ||
"contributors": [ | ||
@@ -35,3 +35,3 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)", | ||
], | ||
"main": "./abstract-leveldown.js", | ||
"main": "./abstract-nosql.js", | ||
"dependencies": { | ||
@@ -38,0 +38,0 @@ "abstract-object": "~1.2.2", |
408
README.md
@@ -36,3 +36,3 @@ # Abstract NoSQL Database [![Build Status](https://secure.travis-ci.org/snowyu/node-abstract-nosql.png)](http://travis-ci.org/snowyu/node-abstract-nosql) | ||
+ Add the stream ability | ||
* You should install `[nosql-stream](https://snowyu/nosql-stream)` package first to use this feature. | ||
* You should install [nosql-stream](https://github.com/snowyu/nosql-stream) package first to use this feature. | ||
+ Add the AbstractError and error code supports. | ||
@@ -46,3 +46,3 @@ * DB constructor allows no location. | ||
* But if you wanna support the synchronous only, you should override the asynchronous methods to disable it. | ||
+ isExists/isExistsSync optional method to test key whether exists. | ||
+ Add isExists/isExistsSync optional method to test key whether exists. | ||
* it will use the \_get/\_getSync method if no \_isExists or \_isExistsSync implemented | ||
@@ -52,3 +52,10 @@ + the AbstractNoSQL class supports events now. | ||
* emit `'closed'` event after the database is closed. | ||
+ isOpen()/opened to test the database whether opened. | ||
+ Add isOpen()/opened to test the database whether opened. | ||
+ Add mGetSync()/mGet() multi get keys method for the range(Array) option of the Iterator | ||
* it will use the \_get/\_getSync method if no \_mGet or \_mGetSync implemented. | ||
* Note: mGet/mGetSync return the array of object: [{key:key,value:value}, ...] | ||
* But the \_mGet/\_mGetSync return the plain array: [key1, value1, key2, value2, ...] | ||
+ Add Iterator.nextSync | ||
* note: nextSync return the object: {key:key, value:value}, return false if ending. | ||
* But the \_nextSync return the array: [key, value] | ||
@@ -112,181 +119,2 @@ ## AbstractError Classes | ||
## Example | ||
A simplistic in-memory LevelDOWN replacement | ||
use sync methods: | ||
```js | ||
var util = require('util') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
// constructor, passes through the 'location' argument to the AbstractLevelDOWN constructor | ||
function FakeLevelDOWN (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
// our new prototype inherits from AbstractLevelDOWN | ||
util.inherits(FakeLevelDOWN, AbstractLevelDOWN) | ||
// implement some methods | ||
FakeLevelDOWN.prototype._openSync = function (options) { | ||
this._store = {} | ||
return true | ||
} | ||
FakeLevelDOWN.prototype._putSync = function (key, value, options) { | ||
key = '_' + key // safety, to avoid key='__proto__'-type skullduggery | ||
this._store[key] = value | ||
return true | ||
} | ||
//the isExists is an optional method: | ||
FakeLevelDOWN.prototype._isExistsSync = function (key, options) { | ||
return this._store.hasOwnProperty('_' + key) | ||
} | ||
FakeLevelDOWN.prototype._getSync = function (key, options) { | ||
var value = this._store['_' + key] | ||
if (value === undefined) { | ||
// 'NotFound' error, consistent with LevelDOWN API | ||
throw new Error('NotFound') | ||
} | ||
return value | ||
} | ||
FakeLevelDOWN.prototype._delSync = function (key, options) { | ||
delete this._store['_' + key] | ||
return true | ||
} | ||
//use it directly | ||
var db = new FakeLevelDOWN() | ||
//sync: | ||
db.put('foo', 'bar') | ||
var result = db.get('foo') | ||
//async: | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
db.isExists('foo', function(err, isExists){ | ||
if (err) throw err | ||
console.log('isExists foo =', isExists) | ||
}) | ||
}) | ||
}) | ||
//stream: | ||
db.readStream().on('data', function(data){ | ||
}) | ||
// now use it in LevelUP | ||
var levelup = require('levelup') | ||
var db = levelup('/who/cares/', { | ||
// the 'db' option replaces LevelDOWN | ||
db: function (location) { return new FakeLevelDOWN(location) } | ||
}) | ||
//async: | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
db.isExists('foo', function(err, isExists){ | ||
if (err) throw err | ||
console.log('isExists foo =', isExists) | ||
}) | ||
}) | ||
}) | ||
//sync: | ||
db.put('foo', 'bar') | ||
console.log(db.get('foo')) | ||
console.log(db.isExists('foo')) | ||
``` | ||
use async methods(no sync supports): | ||
```js | ||
var util = require('util') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
// constructor, passes through the 'location' argument to the AbstractLevelDOWN constructor | ||
function FakeLevelDOWN (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
// our new prototype inherits from AbstractLevelDOWN | ||
util.inherits(FakeLevelDOWN, AbstractLevelDOWN) | ||
// implement some methods | ||
FakeLevelDOWN.prototype._open = function (options, callback) { | ||
// initialise a memory storage object | ||
this._store = {} | ||
// optional use of nextTick to be a nice async citizen | ||
process.nextTick(function () { callback(null, this) }.bind(this)) | ||
} | ||
FakeLevelDOWN.prototype._put = function (key, value, options, callback) { | ||
key = '_' + key // safety, to avoid key='__proto__'-type skullduggery | ||
this._store[key] = value | ||
process.nextTick(callback) | ||
} | ||
//the isExists is an optional method: | ||
FakeLevelDOWN.prototype._isExists = function (key, options, callback) { | ||
var value = this._store.hasOwnProperty('_' + key) | ||
process.nextTick(function () { | ||
callback(null, value) | ||
}) | ||
} | ||
FakeLevelDOWN.prototype._get = function (key, options, callback) { | ||
var value = this._store['_' + key] | ||
if (value === undefined) { | ||
// 'NotFound' error, consistent with LevelDOWN API | ||
return process.nextTick(function () { callback(new Error('NotFound')) }) | ||
} | ||
process.nextTick(function () { | ||
callback(null, value) | ||
}) | ||
} | ||
FakeLevelDOWN.prototype._del = function (key, options, callback) { | ||
delete this._store['_' + key] | ||
process.nextTick(callback) | ||
} | ||
// now use it in LevelUP | ||
var levelup = require('levelup') | ||
var db = levelup('/who/cares/', { | ||
// the 'db' option replaces LevelDOWN | ||
db: function (location) { return new FakeLevelDOWN(location) } | ||
}) | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
}) | ||
}) | ||
``` | ||
See [MemDOWN](https://github.com/rvagg/memdown/) if you are looking for a complete in-memory replacement for LevelDOWN. | ||
## Streamable | ||
@@ -473,5 +301,21 @@ | ||
### AbstractLevelDOWN#_isExistsSync(key, options) | ||
this is an optional method for performance. | ||
### AbstractLevelDOWN#_mGetSync(keys, options) | ||
this is an optional method for performance. | ||
__arguments__ | ||
* keys *(array)*: the keys array to get. | ||
* options *(object)*: the options for get. | ||
__return__ | ||
* array: [key1, value1, key2, value2, ...] | ||
### AbstractLevelDOWN#_openSync(options) | ||
### AbstractLevelDOWN#_getSync(key, options) | ||
### AbstractLevelDOWN#_isExistsSync(key, options) | ||
### AbstractLevelDOWN#_putSync(key, value, options) | ||
@@ -481,8 +325,24 @@ ### AbstractLevelDOWN#_delSync(key, options) | ||
## Async Methods | ||
### AbstractLevelDOWN#_isExists(key, options, callback) | ||
this is an optional method for performance. | ||
### AbstractLevelDOWN#_mGet(keys, options, callback) | ||
this is an optional method for performance. | ||
__arguments__ | ||
* keys *(array)*: the keys array to get. | ||
* options *(object)*: the options for get. | ||
* callback *(function)*: the callback function | ||
* function(err, items) | ||
* items: [key1, value1, key2, value2, ...] | ||
### AbstractLevelDOWN#_open(options, callback) | ||
### AbstractLevelDOWN#_close(callback) | ||
### AbstractLevelDOWN#_get(key, options, callback) | ||
### AbstractLevelDOWN#_isExists(key, options, callback) | ||
### AbstractLevelDOWN#_put(key, value, options, callback) | ||
@@ -576,2 +436,182 @@ ### AbstractLevelDOWN#_del(key, options, callback) | ||
## Example | ||
A simplistic in-memory LevelDOWN replacement | ||
use sync methods: | ||
```js | ||
var util = require('util') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
// constructor, passes through the 'location' argument to the AbstractLevelDOWN constructor | ||
function FakeLevelDOWN (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
// our new prototype inherits from AbstractLevelDOWN | ||
util.inherits(FakeLevelDOWN, AbstractLevelDOWN) | ||
// implement some methods | ||
FakeLevelDOWN.prototype._openSync = function (options) { | ||
this._store = {} | ||
return true | ||
} | ||
FakeLevelDOWN.prototype._putSync = function (key, value, options) { | ||
key = '_' + key // safety, to avoid key='__proto__'-type skullduggery | ||
this._store[key] = value | ||
return true | ||
} | ||
//the isExists is an optional method: | ||
FakeLevelDOWN.prototype._isExistsSync = function (key, options) { | ||
return this._store.hasOwnProperty('_' + key) | ||
} | ||
FakeLevelDOWN.prototype._getSync = function (key, options) { | ||
var value = this._store['_' + key] | ||
if (value === undefined) { | ||
// 'NotFound' error, consistent with LevelDOWN API | ||
throw new Error('NotFound') | ||
} | ||
return value | ||
} | ||
FakeLevelDOWN.prototype._delSync = function (key, options) { | ||
delete this._store['_' + key] | ||
return true | ||
} | ||
//use it directly | ||
var db = new FakeLevelDOWN() | ||
//sync: | ||
db.put('foo', 'bar') | ||
var result = db.get('foo') | ||
//async: | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
db.isExists('foo', function(err, isExists){ | ||
if (err) throw err | ||
console.log('isExists foo =', isExists) | ||
}) | ||
}) | ||
}) | ||
//stream: | ||
db.readStream().on('data', function(data){ | ||
}) | ||
// now use it in LevelUP | ||
var levelup = require('levelup') | ||
var db = levelup('/who/cares/', { | ||
// the 'db' option replaces LevelDOWN | ||
db: function (location) { return new FakeLevelDOWN(location) } | ||
}) | ||
//async: | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
db.isExists('foo', function(err, isExists){ | ||
if (err) throw err | ||
console.log('isExists foo =', isExists) | ||
}) | ||
}) | ||
}) | ||
//sync: | ||
db.put('foo', 'bar') | ||
console.log(db.get('foo')) | ||
console.log(db.isExists('foo')) | ||
``` | ||
use async methods(no sync supports): | ||
```js | ||
var util = require('util') | ||
, AbstractLevelDOWN = require('./').AbstractLevelDOWN | ||
// constructor, passes through the 'location' argument to the AbstractLevelDOWN constructor | ||
function FakeLevelDOWN (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
// our new prototype inherits from AbstractLevelDOWN | ||
util.inherits(FakeLevelDOWN, AbstractLevelDOWN) | ||
// implement some methods | ||
FakeLevelDOWN.prototype._open = function (options, callback) { | ||
// initialise a memory storage object | ||
this._store = {} | ||
// optional use of nextTick to be a nice async citizen | ||
process.nextTick(function () { callback(null, this) }.bind(this)) | ||
} | ||
FakeLevelDOWN.prototype._put = function (key, value, options, callback) { | ||
key = '_' + key // safety, to avoid key='__proto__'-type skullduggery | ||
this._store[key] = value | ||
process.nextTick(callback) | ||
} | ||
//the isExists is an optional method: | ||
FakeLevelDOWN.prototype._isExists = function (key, options, callback) { | ||
var value = this._store.hasOwnProperty('_' + key) | ||
process.nextTick(function () { | ||
callback(null, value) | ||
}) | ||
} | ||
FakeLevelDOWN.prototype._get = function (key, options, callback) { | ||
var value = this._store['_' + key] | ||
if (value === undefined) { | ||
// 'NotFound' error, consistent with LevelDOWN API | ||
return process.nextTick(function () { callback(new Error('NotFound')) }) | ||
} | ||
process.nextTick(function () { | ||
callback(null, value) | ||
}) | ||
} | ||
FakeLevelDOWN.prototype._del = function (key, options, callback) { | ||
delete this._store['_' + key] | ||
process.nextTick(callback) | ||
} | ||
// now use it in LevelUP | ||
var levelup = require('levelup') | ||
var db = levelup('/who/cares/', { | ||
// the 'db' option replaces LevelDOWN | ||
db: function (location) { return new FakeLevelDOWN(location) } | ||
}) | ||
db.put('foo', 'bar', function (err) { | ||
if (err) throw err | ||
db.get('foo', function (err, value) { | ||
if (err) throw err | ||
console.log('Got foo =', value) | ||
}) | ||
}) | ||
``` | ||
See [MemDOWN](https://github.com/rvagg/memdown/) if you are looking for a complete in-memory replacement for LevelDOWN. | ||
<a name="contributing"></a> | ||
@@ -578,0 +618,0 @@ Contributing |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
649
1
0
190855
33
3830