abstract-nosql
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -9,2 +9,54 @@ /* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
AbstractIterator.prototype._next = function(callback) { | ||
var self = this | ||
if (this._nextSync) setImmediate(function(){ | ||
try { | ||
var result = self._nextSync() | ||
self._nexting = false | ||
if (result) { | ||
callback(null, result[0], result[1]) | ||
} else | ||
callback() | ||
} catch(e) { | ||
self._nexting = false | ||
callback(e) | ||
} | ||
}) | ||
else | ||
setImmediate(function () { | ||
self._nexting = false | ||
callback() | ||
}) | ||
} | ||
AbstractIterator.prototype._end = function(callback) { | ||
var self = this | ||
if (this._endSync) setImmediate(function(){ | ||
try { | ||
var result = self._endSync() | ||
callback(null, result) | ||
} catch(e) { | ||
callback(e) | ||
} | ||
}) | ||
setImmediate(function () { | ||
callback() | ||
}) | ||
} | ||
AbstractIterator.prototype.nextSync = function () { | ||
if (this._nextSync) { | ||
this._nexting = true | ||
var result = this._nextSync() | ||
this._nexting = false | ||
return result | ||
} | ||
throw new Error("NotImplementation") | ||
} | ||
AbstractIterator.prototype.endSync = function () { | ||
if (this._endSync) return this._endSync() | ||
throw new Error("NotImplementation") | ||
} | ||
AbstractIterator.prototype.next = function (callback) { | ||
@@ -22,12 +74,5 @@ var self = this | ||
self._nexting = true | ||
if (typeof self._next == 'function') { | ||
return self._next(function () { | ||
self._nexting = false | ||
callback.apply(null, arguments) | ||
}) | ||
} | ||
process.nextTick(function () { | ||
return self._next(function () { | ||
self._nexting = false | ||
callback() | ||
callback.apply(null, arguments) | ||
}) | ||
@@ -45,8 +90,5 @@ } | ||
if (typeof this._end == 'function') | ||
return this._end(callback) | ||
process.nextTick(callback) | ||
return this._end(callback) | ||
} | ||
module.exports = AbstractIterator |
@@ -8,8 +8,5 @@ /* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
function AbstractLevelDOWN (location) { | ||
if (!arguments.length || location === undefined) | ||
throw new Error('constructor requires at least a location argument') | ||
if (typeof location != 'string') | ||
//not all database have the location argument. | ||
if (location && typeof location != 'string') | ||
throw new Error('constructor requires a location string argument') | ||
this.location = location | ||
@@ -410,2 +407,5 @@ } | ||
//should override this to test sync | ||
AbstractLevelDOWN.prototype.IteratorClass = AbstractIterator | ||
AbstractLevelDOWN.prototype.iterator = function (options) { | ||
@@ -420,3 +420,3 @@ if (typeof options != 'object') | ||
return new AbstractIterator(this) | ||
return new this.IteratorClass(this) | ||
} | ||
@@ -423,0 +423,0 @@ |
@@ -488,2 +488,11 @@ var db | ||
module.exports.sync = function (Iterator, test) { | ||
test('sync', function (t) { | ||
if (Iterator.prototype._nextSync) { | ||
delete Iterator.prototype._next | ||
} | ||
t.end() | ||
}) | ||
} | ||
module.exports.all = function (leveldown, test, testCommon) { | ||
@@ -496,2 +505,11 @@ module.exports.setUp(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.sequence(test) | ||
module.exports.iterator(leveldown, test, testCommon, testCommon.collectEntries) | ||
module.exports.snapshot(leveldown, test, testCommon) | ||
module.exports.tearDown(test, testCommon) | ||
} | ||
} |
module.exports.args = function (leveldown, test) { | ||
test('test database creation no-arg throws', function (t) { | ||
t.throws( | ||
leveldown | ||
, { name: 'Error', message: 'constructor requires at least a location argument' } | ||
, 'no-arg leveldown() throws' | ||
) | ||
t.end() | ||
}) | ||
test('test database creation non-string location throws', function (t) { | ||
@@ -27,2 +19,2 @@ t.throws( | ||
}) | ||
} | ||
} |
{ | ||
"name": "abstract-nosql", | ||
"description": "An abstract prototype for nosql database(LevelDOWN API)", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"contributors": [ | ||
@@ -6,0 +6,0 @@ "Riceball LEE <snowyu.lee@gmail.com> (https://github.com/snowyu)", |
@@ -34,7 +34,10 @@ # 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 synchronous methods support now. You can implement the synchronous methods only. | ||
The asynchronous methods will be simulated via these synchronous methods. If you wanna | ||
support the asynchronous methods only, just do not implement these synchronous methods. | ||
But if you wanna support the synchronous only, you should override the asynchronous methods to disable it. | ||
* DB constructor allows no location. | ||
+ Add synchronous methods supports. | ||
* Add the synchronous methods support now. You can implement the synchronous methods only. | ||
* The asynchronous methods will be simulated via these synchronous methods. If you wanna | ||
* support the asynchronous methods only, just do not implement these synchronous methods. | ||
* But if you wanna support the synchronous only, you should override the asynchronous methods to disable it. | ||
## Example | ||
@@ -180,2 +183,13 @@ | ||
### AbstractLevelDOWN(location) | ||
## Sync Methods | ||
### AbstractLevelDOWN#_openSync(options) | ||
### AbstractLevelDOWN#_getSync(key, options) | ||
### AbstractLevelDOWN#_putSync(key, value, options) | ||
### AbstractLevelDOWN#_delSync(key, options) | ||
### AbstractLevelDOWN#_batchSync(array, options) | ||
## Async Methods | ||
### AbstractLevelDOWN#_open(options, callback) | ||
@@ -190,2 +204,21 @@ ### AbstractLevelDOWN#_close(callback) | ||
the batch should be rename to transact more accurate. | ||
<code>batch()</code> can be used for very fast bulk-write operations (both *put* and *delete*). The `array` argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside LevelDB. Each operation is contained in an object having the following properties: `type`, `key`, `value`, where the *type* is either `'put'` or `'del'`. In the case of `'del'` the `'value'` property is ignored. Any entries with a `'key'` of `null` or `undefined` will cause an error to be returned on the `callback` and any `'type': 'put'` entry with a `'value'` of `null` or `undefined` will return an error. | ||
```js | ||
var ops = [ | ||
{ type: 'del', key: 'father' } | ||
, { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' } | ||
, { type: 'put', key: 'dob', value: '16 February 1941' } | ||
, { type: 'put', key: 'spouse', value: 'Kim Young-sook' } | ||
, { type: 'put', key: 'occupation', value: 'Clown' } | ||
] | ||
db.batch(ops, function (err) { | ||
if (err) return console.log('Ooops!', err) | ||
console.log('Great success dear leader!') | ||
}) | ||
``` | ||
### AbstractLevelDOWN#_chainedBatch() | ||
@@ -196,2 +229,8 @@ | ||
### AbstractLevelDOWN#_approximateSize(start, end, callback) | ||
### AbstractLevelDOWN#IteratorClass | ||
You can override the `IteratorClass` to your Iterator. | ||
After override this, it is not necessary to implement the `"_iterator()"` method. | ||
### AbstractLevelDOWN#_iterator(options) | ||
@@ -207,6 +246,23 @@ | ||
### AbstractIterator#_next(callback) | ||
### AbstractIterator#_end(callback) | ||
### Sync methods: | ||
#### AbstractIterator#_nextSync() | ||
__return__ | ||
* if any result: return a two elements of array | ||
* the first is the key | ||
* the second is the value | ||
* or return false, if no any data yet. | ||
#### AbstractIterator#_endSync() | ||
### Async methods: | ||
#### AbstractIterator#_next(callback) | ||
#### AbstractIterator#_end(callback) | ||
### AbstractChainedBatch | ||
Provided with the current instance of `AbstractLevelDOWN` by default. | ||
@@ -213,0 +269,0 @@ |
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
123127
2853
308