LevelDB is a simple key/value data store built by Google, inspired by BigTable. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singular get, put and delete operations, batched put and delete, forward and reverse iteration and simple compression using the Snappy algorithm which is optimised for speed over compression.
LevelUP aims to expose the features of LevelDB in a Node.js-friendly way. Both keys and values are treated as Buffer
objects and are automatically converted using a specified 'encoding'
. LevelDB's iterators are exposed as a Node.js style object-ReadStream
and writing can be peformed via an object-WriteStream
.
An important feature of LevelDB is that it stores entries sorted by keys. This makes LevelUP's ReadStream
interface is a very powerful way to look up items, particularly when combined with the start
option.
The native LevelDB binding is now provided by a separate package, LevelDOWN.
LevelUP is an OPEN Open Source Project, see the Contributing section to find out what this means.
- Tested & supported platforms
- Basic usage
- API
- Events
- JSON data
- Important considerations
- Contributing
- Licence & copyright
See also a list of Node.js LevelDB modules and projects in the wiki.
- Linux (including ARM platforms such as Raspberry Pi and Kindle!)
- Mac OS
- Solaris (SmartOS & Nodejitsu)
- Windows
All operations are asynchronous although they don't necessarily require a callback if you don't need to know when the operation was performed.
var levelup = require('levelup')
// 1) Create our database, supply location and options.
// This will create or open the underlying LevelDB store.
var db = levelup('./mydb')
// 2) put a key & value
db.put('name', 'LevelUP', function (err) {
if (err) return console.log('Ooops!', err) // some kind of I/O error
// 3) fetch by key
db.get('name', function (err, value) {
if (err) return console.log('Ooops!', err) // likely the key was not found
// ta da!
console.log('name=' + value)
})
})
levelup()
db.open()
db.close()
db.put()
db.get()
db.del()
db.batch()
db.approximateSize()
db.isOpen()
db.isClosed()
db.createReadStream()
db.createKeyStream()
db.createValueStream()
db.createWriteStream()
levelup.destroy()
levelup.repair()
levelup()
is the main entry point for creating a new LevelUP instance and opening the underlying store with LevelDB.
This function returns a new instance of LevelUP and will also initiate an open()
operation. Opening the database is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form: function (err, db) {}
where the db
is the LevelUP instance. If you don't provide a callback, any read & write operations are simply queued internally until the database is fully opened.
This leads to two alternative ways of managing a new LevelUP instance:
levelup(location, options, function (err, db) {
if (err) throw err
db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
})
// vs the equivalent:
var db = levelup(location, options) // will throw if an error occurs
db.get('foo', function (err, value) {
if (err) return console.log('foo does not exist')
console.log('got foo =', value)
})
The location
argument is available as a read-only property on the returned LevelUP instance.
levelup()
takes an optional options object as its second argument; the following properties are accepted:
-
'createIfMissing'
(boolean, default:true
): Iftrue
, will initialise an empty database at the specified location if one doesn't already exist. Iffalse
and a database doesn't exist you will receive an error in youropen()
callback and your database won't open. -
'errorIfExists'
(boolean, default:false
): Iftrue
, you will receive an error in youropen()
callback if the database exists at the specified location. -
'compression'
(boolean, default:true
): Iftrue
, all compressible data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off. -
'cacheSize'
(number, default:8 * 1024 * 1024
): The size (in bytes) of the in-memory LRU cache with frequently used uncompressed block contents. -
'keyEncoding'
and'valueEncoding'
(string, default:'utf8'
): The encoding of the keys and values passed through Node.js'Buffer
implementation (see Buffer#toString()). You may also use'encoding'
as an alias for'valueEncoding'
.'utf8'
is the default encoding for both keys and values so you can simply pass in strings and expect strings from yourget()
operations. You can also passBuffer
objects as keys and/or values and conversion will be performed.Supported encodings are: hex, utf8, ascii, binary, base64, ucs2, utf16le.
'json'
encoding is also supported, see below. -
'db'
(object, default: LevelDOWN): LevelUP is backed by LevelDOWN to provide an interface to LevelDB. You can completely replace the use of LevelDOWN by providing a "factory" function that will return a LevelDOWN API compatible object given alocation
argument. For further information, see MemDOWN, a fully LevelDOWN API compatible replacement that uses a memory store rather than LevelDB. Also see Abstract LevelDOWN, a partial implementation of the LevelDOWN API that can be used as a base prototype for a LevelDOWN substitute.
Additionally, each of the main interface methods accept an optional options object that can be used to override 'keyEncoding'
and 'valueEncoding'
.
open()
opens the underlying LevelDB store. In general you should never need to call this method directly as it's automatically called by levelup()
.
However, it is possible to reopen a database after it has been closed with close()
, although this is not generally advised.
close()
closes the underlying LevelDB store. The callback will receive any error encountered during closing as the first argument.
You should always clean up your LevelUP instance by calling close()
when you no longer need it to free up resources. A LevelDB store cannot be opened by multiple instances of LevelDB/LevelUP simultaneously.
put()
is the primary method for inserting data into the store. Both the key
and value
can be arbitrary data objects.
The callback argument is optional but if you don't provide one and an error occurs then expect the error to be thrown.
Encoding of the key
and value
objects will adhere to 'keyEncoding'
and 'valueEncoding'
options provided to levelup()
, although you can provide alternative encoding settings in the options for put()
(it's recommended that you stay consistent in your encoding of keys and values in a single store).
If you provide a 'sync'
value of true
in your options
object, LevelDB will perform a synchronous write of the data; although the operation will be asynchronous as far as Node is concerned. Normally, LevelDB passes the data to the operating system for writing and returns immediately, however a synchronous write will use fsync()
or equivalent so your callback won't be triggered until the data is actually on disk. Synchronous filesystem writes are significantly slower than asynchronous writes but if you want to be absolutely sure that the data is flushed then you can use 'sync': true
.
get()
is the primary method for fetching data from the store. The key
can be an arbitrary data object but if it doesn't exist in the store then the callback will receive an error as its first argument.
Encoding of the key
object will adhere to the 'keyEncoding'
option provided to levelup()
, although you can provide alternative encoding settings in the options for get()
(it's recommended that you stay consistent in your encoding of keys and values in a single store).
LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting fillCache
to false
.
del()
is the primary method for removing data from the store.
Encoding of the key
object will adhere to the 'keyEncoding'
option provided to levelup()
, although you can provide alternative encoding settings in the options for del()
(it's recommended that you stay consistent in your encoding of keys and values in a single store).
A 'sync'
option can also be passed, see put()
for details on how this works.
batch()
can be used for very fast bulk-write operations (both put and delete). The array
argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside LevelDB. Each operation is contained in an object having the following properties: type
, key
, value
, where the type is either 'put'
or 'del'
. In the case of 'del'
the 'value'
property is ignored. Any entries with a 'key'
of null
or undefined
will cause an error to be returned on the callback
and any 'type': 'put'
entry with a 'value'
of null
or undefined
will return an error.
var ops = [
{ type: 'del', key: 'father' }
, { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' }
, { type: 'put', key: 'dob', value: '16 February 1941' }
, { type: 'put', key: 'spouse', value: 'Kim Young-sook' }
, { type: 'put', key: 'occupation', value: 'Clown' }
]
db.batch(ops, function (err) {
if (err) return console.log('Ooops!', err)
console.log('Great success dear leader!')
})
See put()
for a discussion on the options
object. You can overwrite default 'keyEncoding'
and 'valueEncoding'
and also specify the use of sync
filesystem operations.
approximateSize()
can used to get the approximate number of bytes of file system space used by the range [start..end)
. The result may not include recently written data.
db.approximateSize('a', 'c', function (err, size) {
if (err) return console.error('Ooops!', err)
console.log('Approximate size of range is %d', size)
})
A LevelUP object can be in one of the following states:
- "new" - newly created, not opened or closed
- "opening" - waiting for the database to be opened
- "open" - successfully opened the database, available for use
- "closing" - waiting for the database to be closed
- "closed" - database has been successfully closed, should not be used
isOpen()
will return true
only when the state is "open".
See isOpen()
isClosed()
will return true
only when the state is "closing" or "closed", it can be useful for determining if read and write operations are permissible.
You can obtain a ReadStream of the full database by calling the createReadStream()
method. The resulting stream is a complete Node.js-style Readable Stream where 'data'
events emit objects with 'key'
and 'value'
pairs.
db.createReadStream()
.on('data', function (data) {
console.log(data.key, '=', data.value)
})
.on('error', function (err) {
console.log('Oh my!', err)
})
.on('close', function () {
console.log('Stream closed')
})
.on('end', function () {
console.log('Stream closed')
})
The standard pause()
, resume()
and destroy()
methods are implemented on the ReadStream, as is pipe()
(see below). 'data'
, 'error'
, 'end'
and 'close'
events are emitted.
Additionally, you can supply an options object as the first parameter to createReadStream()
with the following options:
-
'start'
: the key you wish to start the read at. By default it will start at the beginning of the store. Note that the start doesn't have to be an actual key that exists, LevelDB will simply find the next key, greater than the key you provide. -
'end'
: the key you wish to end the read on. By default it will continue until the end of the store. Again, the end doesn't have to be an actual key as an (inclusive)<=
-type operation is performed to detect the end. You can also use thedestroy()
method instead of supplying an'end'
parameter to achieve the same effect. -
'reverse'
(boolean, default:false
): a boolean, set to true if you want the stream to go in reverse order. Beware that due to the way LevelDB works, a reverse seek will be slower than a forward seek. -
'keys'
(boolean, default:true
): whether the'data'
event should contain keys. If set totrue
and'values'
set tofalse
then'data'
events will simply be keys, rather than objects with a'key'
property. Used internally by thecreateKeyStream()
method. -
'values'
(boolean, default:true
): whether the'data'
event should contain values. If set totrue
and'keys'
set tofalse
then'data'
events will simply be values, rather than objects with a'value'
property. Used internally by thecreateValueStream()
method. -
'limit'
(number, default:-1
): limit the number of results collected by this stream. This number represents a maximum number of results and may not be reached if you get to the end of the store or your'end'
value first. A value of-1
means there is no limit. -
'fillCache'
(boolean, default:false
): wheather LevelDB's LRU-cache should be filled with data read.
A KeyStream is a ReadStream where the 'data'
events are simply the keys from the database so it can be used like a traditional stream rather than an object stream.
You can obtain a KeyStream either by calling the createKeyStream()
method on a LevelUP object or by passing passing an options object to createReadStream()
with keys
set to true
and values
set to false
.
db.createKeyStream()
.on('data', function (data) {
console.log('key=', data)
})
// same as:
db.createReadStream({ keys: true, values: false })
.on('data', function (data) {
console.log('key=', data)
})
A ValueStream is a ReadStream where the 'data'
events are simply the values from the database so it can be used like a traditional stream rather than an object stream.
You can obtain a ValueStream either by calling the createValueStream()
method on a LevelUP object or by passing passing an options object to createReadStream()
with values
set to true
and keys
set to false
.
db.createValueStream()
.on('data', function (data) {
console.log('value=', data)
})
// same as:
db.createReadStream({ keys: false, values: true })
.on('data', function (data) {
console.log('value=', data)
})
A WriteStream can be obtained by calling the createWriteStream()
method. The resulting stream is a complete Node.js-style Writable Stream which accepts objects with 'key'
and 'value'
pairs on its write()
method. The WriteStream will buffer writes and submit them as a batch()
operation where the writes occur on the same event loop tick, otherwise they are treated as simple put()
operations.
var ws = db.createWriteStream()
ws.on('error', function (err) {
console.log('Oh my!', err)
})
ws.on('close', function () {
console.log('Stream closed')
})
ws.write({ key: 'name', value: 'Yuri Irsenovich Kim' })
ws.write({ key: 'dob', value: '16 February 1941' })
ws.write({ key: 'spouse', value: 'Kim Young-sook' })
ws.write({ key: 'occupation', value: 'Clown' })
ws.end()
The standard write()
, end()
, destroy()
and destroySoon()
methods are implemented on the WriteStream. 'drain'
, 'error'
, 'close'
and 'pipe'
events are emitted.
A ReadStream can be piped directly to a WriteStream, allowing for easy copying of an entire database. A simple copy()
operation is included in LevelUP that performs exactly this on two open databases:
function copy (srcdb, dstdb, callback) {
srcdb.createReadStream().pipe(dstdb.createWriteStream()).on('close', callback)
}
The ReadStream is also fstream-compatible which means you should be able to pipe to and from fstreams. So you can serialize and deserialize an entire database to a directory where keys are filenames and values are their contents, or even into a tar file using node-tar. See the fstream functional test for an example. (Note: I'm not really sure there's a great use-case for this but it's a fun example and it helps to harden the stream implementations.)
KeyStreams and ValueStreams can be treated like standard streams of raw data. If 'keyEncoding'
or 'valueEncoding'
is set to 'binary'
the 'data'
events will simply be standard Node Buffer
objects straight out of the data store.
destroy()
is used to completely remove an existing LevelDB database directory. You can use this function in place of a full directory rm if you want to be sure to only remove LevelDB-related files. If the directory only contains LevelDB files, the directory itself will be removed as well. If there are additional, non-LevelDB files in the directory, those files, and the directory, will be left alone.
The optional callback will be called when the destroy operation is complete, with a possible error
argument.
repair()
can be used to attempt a restoration of a damaged LevelDB store. From the LevelDB documentation:
If a DB cannot be opened, you may attempt to call this method to resurrect as much of the contents of the database as possible. Some data may be lost, so be careful when calling this function on a database that contains important information.
You will find information on the repair operation in the LOG file inside the store directory.
A repair()
can also be used to perform a compaction of the LevelDB log into table files.
The optional callback will be called when the repair operation is complete, with a possible error
argument.
LevelUP emits events when the callbacks to the corresponding methods are called.
db.emit('put', key, value)
emitted when a new value is'put'
db.emit('del', key)
emitted when a value is deleteddb.emit('batch', ary)
emitted when a batch operation has executeddb.emit('ready')
emitted when the database has opened ('open'
is synonym)db.emit('closed')
emitted when the database has closeddb.emit('opening')
emitted when the database is openingdb.emit('closing')
emitted when the database is closing
If you do not pass a callback to an async function, and there is an error, LevelUP will emit('error', err)
instead.
You specify 'json'
encoding for both keys and/or values, you can then supply JavaScript objects to LevelUP and receive them from all fetch operations, including ReadStreams. LevelUP will automatically stringify your objects and store them as utf8 and parse the strings back into objects before passing them back to you.
- LevelDB is thread-safe but is not suitable for accessing with multiple processes. You should only ever have a LevelDB database open from a single Node.js process.
LevelUP is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the CONTRIBUTING.md file for more details.
LevelUP is only possible due to the excellent work of the following contributors:
Rod Vagg | GitHub/rvagg | Twitter/@rvagg |
---|---|---|
John Chesley | GitHub/chesles | Twitter/@chesles |
Jake Verbaten | GitHub/raynos | Twitter/@raynos2 |
Dominic Tarr | GitHub/dominictarr | Twitter/@dominictarr |
Max Ogden | GitHub/maxogden | Twitter/@maxogden |
Lars-Magnus Skog | GitHub/ralphtheninja | Twitter/@ralphtheninja |
David Björklund | GitHub/kesla | Twitter/@david_bjorklund |
Julian Gruber | GitHub/juliangruber | Twitter/@juliangruber |
Paolo Fragomeni | GitHub/hij1nx | Twitter/@hij1nx |
Anton Whalley | GitHub/No9 | Twitter/@antonwhalley |
Matteo Collina | GitHub/mcollina | Twitter/@matteocollina |
A large portion of the Windows support comes from code by Krzysztof Kowalczyk @kjk, see his Windows LevelDB port here. If you're using LevelUP on Windows, you should give him your thanks!
Copyright (c) 2012-2013 LevelUP contributors (listed above).
LevelUP is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
LevelUP builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the New BSD Licence.