by Rod Vagg / tw:@rvagg / gh:rvagg / bl:http://r.va.gg
Get(), Put(), Del()
Batch()
Log: | Max size of 4MB then flushed into a set of Level 0 SST files |
---|---|
Level 0: | Max of 4 SST files then one file compacted into Level 1 |
Level 1: | Max total size of 10MB then one file compacted into Level 2 |
Level 2: | Max total size of 100MB then one file compacted into Level 3 |
Level 3+: | Max total size of 10 x previous level then one file compacted into next level |
0 ↠ 4 SST, 1 ↠ 10M, 2 ↠ 100M, 3 ↠ 1G, 4 ↠ 10G, 5 ↠ 100G, 6 ↠ 1T, 7 ↠ 10T+
Pure C++ interface between Node and LevelDB
Wrap LevelDOWN to provide a Node-style interface
var db = levelup('/path/to/database')
db.put('key', 'value', function (err) { /* ... */ })
db.get('key', function (err, value) { /* ... */ })
db.del('key', function (err) { /* ... */ })
db.close(function (err) { /* closed */ })
// multiple atomic writes with batch()
var operations = [
{ type: 'put', key: 'Franciscus', value: 'Jorge Bergoglio' }
, { type: 'del', key: 'Benedictus XVI' }
]
db.batch(operations, function (err) { /* ... */ })
var levelup = require('levelup')
var db = levelup('/tmp/dprk.db')
db.put('name', 'Kim Jong-un', function (err) {
db.batch([
{ type: 'put', key: 'spouse', value: 'Ri Sol-ju' }
, { type: 'put', key: 'dob', value: '8 January 1983' }
, { type: 'put', key: 'occupation', value: 'Clown' }
], function (err) {
db.createReadStream()
.on('data', console.log)
.on('end', function () { db.close() })
})
})
var rs = db.createReadStream()
rs.on('error', function (err) { /* handle err */ })
rs.on('data' , function (data) { /* data.key & data.value */ })
rs.on('end', function () { /* stream finished */ })
// Options! Oh my!
db.createReadStream({
start : 'somewheretostart'
, end : 'endkey'
, limit : 100
, reverse : true
})
function copy (srcdb, destdb, callback) {
srcdb.createReadStream()
.pipe(destdb.createWriteStream())
.on('error', callback)
.on('end', callback)
}
var db = levelup('/path/to/db', { valueEncoding: 'json' })
var data = {
name : 'Kim Jong-un'
, spouse : 'Ri Sol-ju'
, dob : '8 January 1983'
, occupation : 'Clown'
}
db.put('dprk', data, function (err) {
db.get('dprk', function (err, value) {
console.log('dprk:', value)
db.close()
})
})
UTF8 (default), JSON, Buffer
encoding types
Swappable back-end:
Small, extensible core with a thriving ecosystem
Level-Multiply
db.put({ boom: 'bang', whoa: 'dude' }, function (err) { })
db.get([ 'boom', 'whoa' ], function (err, data) { })
db.del([ 'boom', 'whoa' ], function (err) { })
Level TTL
db.put('foo', 'bar', { ttl: 3600000 }, function (err) { })
level-live-stream
db.liveStream().on('data', console.log)
level-delete-range
deleteRange(db, {
start: "foo:"
, end: "foo:~"
}, cb)
level-store
fs.createReadStream('foo.dat')
.pipe(store.createWriteStream('foo'))
store.createReadStream('whoa').pipe(response);
Map Reduce
var mapdb = MapReduce(
db
, 'example'
, function (key, value, emit) {
var obj = JSON.parse(value)
emit([ 'all', obj.group ], String(obj.lines.length))
}
, function (acc, value, key) {
return String(acc + value)
}
, '0'
})
mapDb.createReadStream({ range: [ 'all', group ]})
level-mapped-index
db.registerIndex('id', function (key, value, emit) {
value = JSON.parse(value)
if (value.id) emit(value.id)
})
db.put('foo1', JSON.stringify({ one: 'ONE', id : '1' }))
db.put('foo2', JSON.stringify({ two: 'TWO', id : '2' }))
db.getBy('id', '1', function (err, data) {
// [{ key: 'foo1', value: '{"one":"ONE","key":"1"}' }]
})
multilevel & level-rpc: expose the LevelUP API over the network to multiple end-points
level-master: master / slave replication—multi-slave or aggregated multi-master
level-sublevel: Uses namespacing to divide a LevelUP instance into multiple sub-instances.
var db = levelup('/tmp/store.db')
sublevel(db)
var foodb = db.sublevel('foo')
var bardb = db.sublevel('bar')
Rod Vagg / tw:@rvagg / gh:rvagg / bl:http://r.va.gg
LevelUP ↠ ghub.io/levelup
LevelDOWN ↠ ghub.io/leveldown
Extensions & tools ↠ ghub.io/levelup/wiki/Modules
Articles ↠ ghub.io/levelup/wiki/Resources
IRC ↠ ##leveldb on Freenode
Google group ↠ node-levelup
Issue tracker ↠ ghub.io/levelup/issues