leveldb
Version:
Bindings for using LevelDB through node.
300 lines (199 loc) • 7.06 kB
text/coffeescript
binding = require '../../build/Release/leveldb.node'
###
An iterator allows sequential and random access to the database.
Usage:
var leveldb = require('leveldb');
leveldb.open('/tmp/test.db', function(err, db) {
db.iterator(function(err, it) {
// iterator is initially invalid
it.first(function(err) {
// get a key
it.get('foobar', function(err, val) {
console.log(val);
});
});
});
});
###
exports.Iterator = class Iterator
toBuffer = (val) ->
if Buffer.isBuffer val then val else new Buffer val
toValue = (val, options) ->
unless val
null
else unless options?.as_buffer
val.toString 'utf8'
else
val
_wrapSeek: (callback, validate) =>
throw new Error 'Illegal state' if validate and not
throw new Error 'Missing callback' unless callback
(err, valid, key, val) =>
= valid
= key
= val
callback err if callback
_lock: ->
throw new Error 'Concurrent operations not supported' if
= true
_unlock: ->
throw new Error 'Not locked' unless
= false
_getKey: (options) ->
toValue , options
_getVal: (options) ->
toValue , options
###
Constructor.
{Native} self The underlying iterator object.
###
constructor: ( ) ->
= = false
= = null
###
Apply a callback over a range.
The iterator will be positioned at the given key or the first key if
not given, then the callback will be applied on each record moving
forward until the iterator is positioned at the limit key or at an
invalid key. Stops on first error.
{String|Buffer} [startKey] Optional start key (inclusive) from
which to begin applying the callback. If not given, defaults to the
first key.
{String|Buffer} [limitKey] Optional limit key (inclusive) at
which to end applying the callback.
{Object} [options] Optional options.
{Boolean} [options.as_buffer=false] If true, data will be
returned as a `Buffer`.
{Function} callback The callback to apply to the range.
{Error} error The error value on error, null otherwise.
{String|Buffer} key The key.
{String|Buffer} value The value.
{Function} finishedCallback An optional callback being called when the limit key has been reached
###
forRange: ->
args = Array.prototype.slice.call arguments
#Optional finished callback
if typeof(args[args.length - 1]) is 'function' and typeof(args[args.length - 2]) is 'function'
finishedCallback = args.pop()
# required callback
callback = args.pop()
throw new Error 'Missing callback' unless callback
# optional options
options = args[args.length - 1]
if typeof options is 'object' and not Buffer.isBuffer options
args.pop()
else
options = {}
# optional keys
[ startKey, limitKey ] = args
# for comparing end key
limit = limitKey.toString 'binary' if limitKey
# loop function
next = (err) =>
return callback err if err
if
callback null, ,
if not limit or limit isnt .toString 'binary'
next
else if finishedCallback
finishedCallback()
# start loop
if startKey
startKey, next
else
next
###
True if the iterator is positioned at a valid key.
###
valid: ->
###
Position the iterator at a key.
{String} key The key at which to position the iterator.
{Function} [callback] Optional callback.
{Error} error The error value on error, null otherwise.
###
seek: (key, callback) ->
.seek toBuffer(key), callback
###
Position the iterator at the first key.
{Function} [callback] Optional callback.
{Error} error The error value on error, null otherwise.
###
first: (callback) ->
.first callback
###
Position the iterator at the last key.
{Function} [callback] Optional callback.
{Error} error The error value on error, null otherwise.
###
last: (callback) ->
.last callback
###
Advance the iterator to the next key.
{Function} [callback] Optional callback.
{Error} error The error value on error, null otherwise.
###
next: (callback) ->
.next callback, true
###
Advance the iterator to the previous key.
{Function} [callback] Optional callback.
{Error} error The error value on error, null otherwise.
###
prev: (callback) ->
.prev callback, true
###
Get the key at the current iterator position.
{Object} [options] Optional options.
{Boolean} [options.as_buffer=false] If true, data will be
returned as a `Buffer`.
{Function} callback The callback function.
{Error} error The error value on error, null otherwise.
{String|Buffer} key The key if successful.
###
key: (options, callback) ->
# optional options
if typeof options is 'function'
callback = options
options = null
key = options
callback? null, key
key
###
Get the value at the current iterator position.
{Object} [options] Optional options.
{Boolean} [options.as_buffer=false] If true, data will be
returned as a `Buffer`.
{Function} callback The callback function.
{Error} error The error value on error, null otherwise.
{String|Buffer} value The value if successful.
###
value: (options, callback) ->
# optional options
if typeof options is 'function'
callback = options
options = null
val = options
callback? null, val
val
###
Get the key and value at the current iterator position.
{Object} [options] Optional options.
{Boolean} [options.as_buffer=false] If true, data will be
returned as a `Buffer`.
{Function} callback The callback function.
{Error} error The error value on error, null otherwise.
{String|Buffer} key The key if successful.
{String|Buffer} value The value if successful.
###
current: (options, callback) ->
# optional options
if typeof options is 'function'
callback = options
options = null
key = options
val = options
callback? null, key, val
[key, val]