Skip to content

michaelnisi/level-random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Read random levelup values

Resources

License

Stars

Watchers

Forks

Packages

No packages published