A hyperdb-index backed by LevelDB.
Convenience module for hyperdb that wraps up hyperdb-index with a LevelDB backend.
var hindex = require('hyperdb-index-level')
var hyper = require('hyperdb')
var level = require('level')
var ram = require('random-access-memory')
var db = hyper(ram, { valueEncoding: 'json' })
var lvl = level('./index')
var idx = hindex(db, lvl, processor)
function processor (node, next) {
lvl.get('sum', function (err, sum) {
if (err && !err.notFound) return next(err)
else if (err) sum = 0
else sum = Number(sum)
console.log('sum so far', sum, node.value)
sum += node.value
lvl.put('sum', Number(sum), next)
})
}
var getSum = function (cb) {
idx.ready(function () {
lvl.get('sum', function (err, sum) {
cb(err, sum ? Number(sum) : undefined)
})
})
}
db.put('/numbers/0', 15, function (err) {
db.put('/numbers/1', 2, function (err) {
db.put('/numbers/2', 8, function (err) {
getSum(function (err, sum) {
console.log('the sum is', sum)
})
})
})
})
outputs
sum so far 0 [ 8 ]
sum so far 8 [ 2 ]
sum so far 10 [ 15 ]
the sum is 25
var hindex = require('hyperdb-index-level')
Creates a new indexer on the hyperdb instance db
.
The Level instance lvl
is used for storage.
processorFn
is called on the latest value of key that gets set, with the
function signature processorFn(node, next)
. node
is the next hyperdb node to
be processed, and next
is called with an optional error to tell the indexer to
proceed.
Registers the callback cb
to fire when the indexes have "caught up" to the
latest known change in the hyperdb. The cb
function fires exactly once. You
may call idx.ready()
multiple times with different functions.
If you want to store multiple indexes in one LevelDB, you can partition it with subleveldown so that the indexes can't affect each other.
With npm installed, run
$ npm install hyperdb-index-level
Development was sponsored by Digital Democracy.
ISC