A hyperdb-index backed by LevelDB.
npm install hyperdb-index-level> A [hyperdb-index][hyperdb-index] backed by LevelDB.
Convenience module for [hyperdb][hyperdb] that wraps up
[hyperdb-index][hyperdb-index] with a LevelDB backend.
``js
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
`js`
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 theprocessorFn(node, next)
function signature . node is the next hyperdb node tonext
be processed, and 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 thecb
latest known change in the hyperdb. The function fires exactly once. Youidx.ready()
may call 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
[hyperdb-index]: https://github.com/noffle/hyperdb-index
[hyperdb]: https://github.com/mafintosh/hyperdb