npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hyperdb-index

v2.0.8

Published

Build a realtime index over a hyperdb key prefix.

Downloads

10

Readme

hyperdb-index

Build an incremental index over a hyperdb.

If you're planning on using LevelDB for storage, consider the wrapper module hyperdb-index-level.

Depends on [email protected] or later for the createHistoryStream API.

Usage

Let's build an index that tracks all nodes in a spatial point store, for fast bounding box queries:

var index = require('hyperdb-index')
var hyperdb = require('hyperdb')
var ram = require('random-access-memory')
var GeoStore = require('grid-point-store')
var memdb = require('memdb')

//------------------------------------------------------------------------------

var db = hyperdb(ram, { valueEncoding: 'json' })
var geo = GeoStore(memdb())

var idx = index(db, {
  processFn: processFn,
  getVersion: getVersion,
  setVersion: setVersion
})

var pending = 0
for (var i = 0; i < 5; i++) {
  pending++
  db.put('/nodes/' + i, { type: 'node', lat: i, lon: -i*2 }, function () {
    if (!--pending) query()
  })
}

function query () {
  idx.ready(function () {
    geo.query([[-10, -10], [10, 10]], function (err, nodes) {
      console.log('query', nodes)
    })
  })
}

//------------------------------------------------------------------------------

var now = null
function getVersion (cb) {
  cb(null, now)
}
function setVersion (version, cb) {
  now = version
  cb(null)
}
function processFn (cur, prev, next) {
  if (cur.value.type === 'node') {
    var v = parseInt(cur.name.split('/')[cur.name.split('/').length - 1])
    console.log('process', cur.value)
    geo.insert([cur.value.lat, cur.value.lon], v, next)
  } else {
    next(null)
  }
}

outputs

process { type: 'node', lat: 0, lon: 0 }
process { type: 'node', lat: 4, lon: -8 }
process { type: 'node', lat: 3, lon: -6 }
process { type: 'node', lat: 1, lon: -2 }
process { type: 'node', lat: 2, lon: -4 }
query [ { lat: 4, lon: -8, value: 4 },
  { lat: 3, lon: -6, value: 3 },
  { lat: 2, lon: -4, value: 2 },
  { lat: 1, lon: -2, value: 1 },
  { lat: 0, lon: 0, value: 0 } ]

So here hyperdb-index is acting like a bridge between the raw point data in hyperdb and the much more efficient point storage module grid-point-store.

API

var index = require('hyperdb-index')

var idx = index(db, opts)

Create a new index. db is a hyperdb instance.

It is the module consumer's responsibility to store the indexer's version of what entry it's indexed db up to. The module consumer controls this by implementing the functions opts.getVersion and opts.setVersion (see below).

Valid opts include:

  • opts.processFn (required): a function to be called to process a new entry in db. The expected function signature is function (kv, oldKv, next), where kv is of the form { key: '...', value: {} }, oldKv is its previous value (null if none), and next is a callback to call when processing of that key-value pair is complete.
  • opts.getVersion (required): a function that will be called to retrieve the current version of the hyperdb. It has the signature function (cb) and expects a hyperdb version buffer.
  • opts.setVersion (required): a function that will be called to store the current version of the hyperdb. It has the signature function (version, cb). Call cb once you've stored the version object.
  • opts.prefix (optional): a key prefix to index. If not given, the root key '/' is assumed.

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.

Install

With npm installed, run

$ npm install hyperdb-index

See Also

License

ISC