The level-random Node.js module implements a Transform stream for reading values of random keys in levelup.
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
Object
passed to Transform stream constructor.
db
The mandatory levelup instance.errorIfNotFound
Emit error if key is not foundBoolean=false
.fillCache
Fill LevelDB's LRU-cacheBoolean=false
.
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.
With npm, do:
$ npm install level-random