hyperdb-index-level

2.0.2 • Public • Published

hyperdb-index-level

A hyperdb-index backed by LevelDB.

Convenience module for hyperdb that wraps up hyperdb-index with a LevelDB backend.

Usage

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

API

var hindex = require('hyperdb-index-level')

var idx = hindex(db, lvl, processorFn)

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.

idx.ready(cb)

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.

Tips

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.

Install

With npm installed, run

$ npm install hyperdb-index-level

Acknowledgments

Development was sponsored by Digital Democracy.

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i hyperdb-index-level

Weekly Downloads

1

Version

2.0.2

License

ISC

Last publish

Collaborators

  • noffle