Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphtheninja/69dde0ca7e1fe86a6fd8 to your computer and use it in GitHub Desktop.
Save ralphtheninja/69dde0ca7e1fe86a6fd8 to your computer and use it in GitHub Desktop.
// This uses level-sublevel to separate data from ttl meta data
// into different sublevels
var crypto = require('crypto')
var level = require('level')
var sublevel = require('level-sublevel')
var ttl = require('level-ttl')
var rmrf = require('rimraf')
var path = './duuuude_db'
rmrf(path, function (err) {
if (err) throw err
level(path, function (err, db) {
if (err) throw err
var subdb = sublevel(db)
var ttldb = ttl(subdb.sublevel('data'), { sub: subdb.sublevel('meta') })
ttldb.batch(randomBatch(10), { ttl: 10000 }, function (err) {
if (err) throw err
console.log('here comes data..')
ttldb.createReadStream() // or subdb.sublevel('data').createReadStream()
.on('data', function (data) {
console.log('- key', data.key)
console.log('- value', data.value)
})
.on('end', function () {
console.log('\nhere comes ttl meta..')
subdb.sublevel('meta').createReadStream()
.on('data', function (data) {
console.log('- key', data.key)
console.log('- value', data.value)
})
.on('end', function () {
console.log('\nhere comes everything..')
db.createReadStream()
.on('data', function (data) {
console.log('- key', data.key)
console.log('- value', data.value)
})
})
})
})
})
})
function randomBatch(count) {
var batch = []
for (var i = 0; i < count; ++i) {
var key = crypto.randomBytes(32).toString('base64')
var value = crypto.randomBytes(32).toString('base64')
batch.push({ type: 'put', key: key, value: value })
}
return batch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment