abstract-nosql
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -29,2 +29,3 @@ // Generated by CoffeeScript 1.8.0 | ||
function AbstractIterator(db, options) { | ||
var isKeysIterator; | ||
this.db = db; | ||
@@ -34,6 +35,8 @@ this.options = options; | ||
this._nexting = false; | ||
if (options && isArray(options.range)) { | ||
isKeysIterator = options && isArray(options.range); | ||
if (isKeysIterator) { | ||
this._resultOfKeys = options.range; | ||
this._indexOfKeys = -1; | ||
} | ||
return !isKeysIterator; | ||
} | ||
@@ -90,2 +93,21 @@ | ||
AbstractIterator.prototype.nextKeysSync = function() { | ||
var result; | ||
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; | ||
}; | ||
AbstractIterator.prototype.nextSync = function() { | ||
@@ -100,17 +122,3 @@ var result; | ||
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; | ||
return this.nextKeysSync(); | ||
} else if (this._nextSync) { | ||
@@ -132,7 +140,11 @@ this._nexting = true; | ||
AbstractIterator.prototype._endKeys = function() { | ||
delete this._resultOfKeys; | ||
this._indexOfKeys = -2; | ||
return this._ended = true; | ||
}; | ||
AbstractIterator.prototype.endSync = function() { | ||
if (this._indexOfKeys != null) { | ||
delete this._resultOfKeys; | ||
this._indexOfKeys = -2; | ||
return this._ended = true; | ||
return this._endKeys(); | ||
} else if (this._endSync) { | ||
@@ -146,4 +158,34 @@ this._ended = true; | ||
AbstractIterator.prototype.nextKeys = function(callback) { | ||
var result, self; | ||
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]); | ||
} | ||
return this; | ||
}; | ||
AbstractIterator.prototype.next = function(callback) { | ||
var result, self; | ||
var self; | ||
if (typeof callback !== "function") { | ||
@@ -159,27 +201,3 @@ throw new InvalidArgumentError("next() requires a callback argument"); | ||
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]); | ||
} | ||
this.nextKeys(callback); | ||
} else { | ||
@@ -204,5 +222,3 @@ this._nexting = true; | ||
if (this._indexOfKeys != null) { | ||
this._ended = true; | ||
delete this._resultOfKeys; | ||
this._indexOfKeys = -2; | ||
this._endKeys(); | ||
return setImmediate(callback); | ||
@@ -209,0 +225,0 @@ } else { |
@@ -119,2 +119,3 @@ // Generated by CoffeeScript 1.8.0 | ||
} | ||
options.raiseError = options.raiseError !== false; | ||
arr = this._mGetSync(keys, options); | ||
@@ -291,9 +292,24 @@ i = 0; | ||
AbstractNoSQL.prototype._mGetSync = function(keys, options) { | ||
var key, result, value, _i, _len; | ||
var err, key, needKeyName, raiseError, result, value, _i, _len; | ||
if (this._getSync) { | ||
result = []; | ||
needKeyName = options.keys; | ||
raiseError = options.raiseError; | ||
options.asBuffer = options.asBuffer === true; | ||
for (_i = 0, _len = keys.length; _i < _len; _i++) { | ||
key = keys[_i]; | ||
value = this._getSync(key, options); | ||
result.push(key, value); | ||
try { | ||
value = this._getSync(key, options); | ||
} catch (_error) { | ||
err = _error; | ||
if (raiseError) { | ||
throw err; | ||
} | ||
value = void 0; | ||
} | ||
if (needKeyName !== false) { | ||
result.push(key, value); | ||
} else { | ||
result.push(value); | ||
} | ||
} | ||
@@ -307,5 +323,5 @@ return result; | ||
AbstractNoSQL.prototype._mGet = function(keys, options, callback) { | ||
var i, readNext, result, that; | ||
var i, needKeyName, raiseError, readNext, result, that; | ||
that = this; | ||
if (this._getSync) { | ||
if (this._getSync || this._mGetSync !== AbstractNoSQL.prototype._mGetSync) { | ||
return setImmediate(function() { | ||
@@ -323,10 +339,16 @@ var err, result; | ||
}); | ||
} else if (keys.length > 0) { | ||
} else if (keys.length > 0 && this._get) { | ||
result = []; | ||
i = 0; | ||
needKeyName = options.keys; | ||
raiseError = options.raiseError; | ||
readNext = function(err, value) { | ||
if (err) { | ||
if (err && raiseError) { | ||
return callback(err); | ||
} | ||
result.push(keys[i], value); | ||
if (needKeyName !== false) { | ||
result.push(keys[i], value); | ||
} else { | ||
result.push(value); | ||
} | ||
i++; | ||
@@ -336,3 +358,3 @@ if (i >= keys.length) { | ||
} | ||
return this._get(key[i], options, readNext); | ||
return that._get(keys[i], options, readNext); | ||
}; | ||
@@ -512,3 +534,3 @@ return this._get(keys[i], options, readNext); | ||
AbstractNoSQL.prototype.mGet = function(keys, options, callback) { | ||
var err; | ||
var err, needKeyName; | ||
err = void 0; | ||
@@ -523,2 +545,5 @@ if (typeof options === "function") { | ||
} | ||
options.asBuffer = options.asBuffer === true; | ||
options.raiseError = options.raiseError !== false; | ||
needKeyName = options.keys !== false; | ||
if (callback) { | ||
@@ -530,10 +555,14 @@ return this._mGet(keys, options, function(err, arr) { | ||
} | ||
i = 0; | ||
result = []; | ||
while (i < arr.length) { | ||
result.push({ | ||
key: arr[i], | ||
value: arr[++i] | ||
}); | ||
i++; | ||
if (needKeyName) { | ||
i = 0; | ||
result = []; | ||
while (i < arr.length) { | ||
result.push({ | ||
key: arr[i], | ||
value: arr[++i] | ||
}); | ||
i++; | ||
} | ||
} else { | ||
result = arr; | ||
} | ||
@@ -540,0 +569,0 @@ return callback(null, result); |
{ | ||
"name": "abstract-nosql", | ||
"description": "An abstract prototype for nosql database with sync and streamable (LevelDOWN API)", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"contributors": [ | ||
@@ -6,0 +6,0 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)", |
@@ -55,2 +55,6 @@ # Abstract NoSQL Database [](http://travis-ci.org/snowyu/node-abstract-nosql) | ||
* But the \_mGet/\_mGetSync return the plain array: [key1, value1, key2, value2, ...] | ||
+ keys *(bool, default true)* option to return keys or not | ||
* return the values array if keys is false | ||
+ raiseError *(bool, default true)* option to raise or ignore error | ||
* some elements will be undefined for the value error if keys is false | ||
+ Add Iterator.nextSync | ||
@@ -57,0 +61,0 @@ * note: nextSync return the object: {key:key, value:value}, return false if ending. |
41
test.js
@@ -31,2 +31,5 @@ const tap = require('tap') | ||
require('./abstract/mget-test').setUp(factory, tap.test, testCommon) | ||
require('./abstract/mget-test').args(tap.test) | ||
require('./abstract/put-test').setUp(factory, tap.test, testCommon) | ||
@@ -167,2 +170,40 @@ require('./abstract/put-test').args(tap.test) | ||
tap.test('test mGet() extensibility', function (t) { | ||
var spy = sinon.spy() | ||
, expectedCb = function () {} | ||
, expectedOptions = { asBuffer: false } | ||
, expectedKey = ['a key'] | ||
, test | ||
function Test (location) { | ||
AbstractLevelDOWN.call(this, location) | ||
} | ||
util.inherits(Test, AbstractLevelDOWN) | ||
Test.prototype._mGet = spy | ||
test = new Test('foobar') | ||
test.get(expectedKey, expectedCb) | ||
t.equal(spy.callCount, 1, 'got _mGet() call') | ||
t.equal(spy.getCall(0).thisValue, test, '`this` on _mGet() was correct') | ||
t.equal(spy.getCall(0).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(0).args[0], expectedKey, 'got expected key argument') | ||
t.deepEqual(spy.getCall(0).args[1], expectedOptions, 'got default options argument') | ||
t.equal(spy.getCall(0).args[2], expectedCb, 'got expected cb argument') | ||
test.get(expectedKey, { options: 1 }, expectedCb) | ||
expectedOptions.options = 1 | ||
t.equal(spy.callCount, 2, 'got _mGet() call') | ||
t.equal(spy.getCall(1).thisValue, test, '`this` on _mGet() was correct') | ||
t.equal(spy.getCall(1).args.length, 3, 'got three arguments') | ||
t.equal(spy.getCall(1).args[0], expectedKey, 'got expected key argument') | ||
t.deepEqual(spy.getCall(1).args[1], expectedOptions, 'got expected options argument') | ||
t.equal(spy.getCall(1).args[2], expectedCb, 'got expected cb argument') | ||
t.end() | ||
}) | ||
tap.test('test del() extensibility', function (t) { | ||
@@ -169,0 +210,0 @@ var spy = sinon.spy() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
199110
34
4046
653