level-random

4.0.3 • Public • Published

Build Status Coverage Status

level-random - read random levelup values

The level-random Node.js module implements a Transform stream for reading values of random keys in levelup.

Usage

Reading values for random keys from levelup.

const encode = require('encoding-down')
const leveldown = require('leveldown')
const levelup = require('levelup')
const lr = require('./')
const { pipeline, Readable, Writable } = require('readable-stream')
 
const db = levelup(encode(leveldown('/tmp/level-random-example.db')))
 
db.batch([
  { type: 'put', key: 'a', value: 'you' },
  { type: 'put', key: 'c', value: 'are' }
], er => {
  if (er) throw er
 
  const keys = ['a', 'b', 'c']
 
  pipeline(
    new Readable({
      read (length) {
        this.push(keys.shift() || null)
      }
    }),
    lr({ db: db }),
    new Writable({
      write (chunk, enc, cb) {
        console.log('%s', chunk)
        cb()
      }
    }),
    er => {
      if (er) throw er
      console.log('ok')
    }
  )
})

Run it:

$ node example.js

Types

opts()

Object passed to Transform stream constructor.

  • db The mandatory levelup instance.
  • errorIfNotFound Emit error if key is not found Boolean=false.
  • fillCache Fill LevelDB's LRU-cache Boolean=false.

Exports

level-random exports a sole function that returns a Transform stream which transforms keys to values.

var lr = require('level-random')
lr(opts())

At large, of course, we leverage the lexicographical sort order of keys in log structured databases to very efficiently stream ranges. Occasionally though, we have to read randomly from the store. This module provides a value stream for arbitrary keys, ignoring non-existing keys by default.

Installation

With npm, do:

$ npm install level-random

License

MIT License

Package Sidebar

Install

npm i level-random

Weekly Downloads

5

Version

4.0.3

License

MIT

Unpacked Size

9 kB

Total Files

7

Last publish

Collaborators

  • michaelnisi