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
Batch
: Put
and/or Del
, are atomic.
Iterator
: navigate forward and backward, starting and ending at any key.
Snapshot
: consistent view of the whole data store. Iterators create an implicit snapshot.
C++ interface between Node.js and LevelDB:
Wrap LevelDOWN to provide a Node.js-style interface:
var levelup = require('levelup')
levelup('/tmp/dprk.db', function (err, 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('close', function () {
db.close()
})
})
})
})
levelup('/path/to/database', function (err, db) {
/* use `db` */
})
// or
var db = levelup('/path/to/database')
// use `db`, operations are queued till `open` is complete
// close to clean up
db.close(function (err) { /* ... */ })
db.put('key', 'value', function (err) { /* ... */ })
db.del('key', function (err) { /* ... */ })
db.get('key', function (err, value) { /* ... */ })
var operations = [
{ type: 'put', key: 'Franciscus', value: 'Jorge Mario Bergoglio' }
, { type: 'del', key: 'Benedictus XVI' }
]
db.batch(operations, function (err) { /* ... */ })
var rs = db.createReadStream()
rs.on('error', function (err) { /* handle err */ })
rs.on('data' , function (data) { /* data.key & data.value */ })
rs.on('close', function () { /* stream finished */ })
db.createReadStream({
start : 'somewheretostart'
, end : 'endkey'
, limit : 100
, reverse : true
, keys : true // see db.createKeyStream()
, values : true // see db.createValueStream()
, fillCache : false
})
Standard readable stream operations are also supported:
function copy (srcdb, destdb, callback) {
srcdb.createReadStream()
.pipe(destdb.createWriteStream())
.on('error', callback)
.on('close', callback)
}
var db = levelup('/path/to/db', { valueEncoding: 'json' })
db.put(
'dprk'
, {
name : 'Kim Jong-un'
, spouse : 'Ri Sol-ju'
, dob : '8 January 1983'
, occupation : 'Clown'
}
, function (err) {
db.get('dprk', function (err, value) {
console.log('dprk:', value)
db.close()
})
}
)
keyEncoding
and valueEncoding
: utf8 (default), JSON, Buffer
encoding types.
function variance (db, prefix, callback) {
var n = 0, m2 = 0
db.createReadStream({
start : prefix + ':'
, end : prefix + ':\xFF'
})
.on('data', function (data) {
var delta = data.value - mean
mean += delta / ++n
m2 = m2 + delta * delta
})
.on('error', callback)
.on('close', function () {
callback(null, m2 / (n - 1))
})
}
Rod Vagg / tw:@rvagg / gh:rvagg
LevelUP ↠ github.com/rvagg/node-levelup
LevelDOWN ↠ github.com/rvagg/node-leveldown
Modules ↠ github.com/rvagg/node-levelup/wiki/Modules
Applications ↠ github.com/rvagg/node-levelup/wiki/Applications