Home

Awesome

LevelDB LevelDB Logo npm

Build Status downloads license

A Low-level Node.js LevelDB binding

Leveldown was extracted from LevelUP and now serves as a stand-alone binding for LevelDB.

nosql-leveldb was modified from LevelDown. see changes below for differencement list.

It is strongly recommended that you use LevelUP-sync in preference to LevelDB unless you have measurable performance reasons to do so. LevelUP is optimised for usability and safety. Although we are working to improve the safety of the LevelDB interface it is still easy to crash your Node process if you don't do things in just the right way.

See the section on <a href="#safety">safety</a> below for details of known unsafe operations with LevelDB.

<a name="platforms"></a> Tested & supported platforms

Chnages

v3.x

v2.1.x

v2.x.x

v1.x.x

Performance

run bench/bench.js to see a simple performance compare:

the original Leveldown bench result:


  benchmarking with 12,000 records, 24 chars each

          put :    29,411 w/s in    408ms

        batch :   101,694 w/s in    118ms

          get :    35,398 r/s in    339ms        (asBuffer)
          get :    44,444 r/s in    270ms

     iterator :    58,823 r/s in    204ms        (asBuffer)
     iterator :   127,659 r/s in     94ms
     iterator :   148,148 r/s in     81ms        (directly)

The new nosql-leveldb bench result:


benchmarking with 12,000 records, 24 chars each

          put :    30,000 w/s in    400ms
      putSync :    74,074 w/s in    162ms
      putSync :    77,922 w/s in    154ms        (directly)

        batch :   121,212 w/s in     99ms
    batchSync :   179,104 w/s in     67ms
    batchSync :   200,000 w/s in     60ms        (directly)

          get :    36,036 r/s in    333ms        (asBuffer)
          get :    56,338 r/s in    213ms
      getSync :    79,470 r/s in    151ms        (asBuffer)
      getSync :   255,319 r/s in     47ms
      getSync :   260,869 r/s in     46ms        (directly)

     iterator :    94,488 r/s in    127ms        (asBuffer)
     iterator :   153,846 r/s in     78ms
     iterator :   342,857 r/s in     35ms        (directly)
 iteratorSync :   292,682 r/s in     41ms
 iteratorSync :   300,000 r/s in     40ms        (directly)

<a name="api"></a>

API


<a name="ctor"></a>

LevelDB(location)

<code>LevelDB()</code> returns a new LevelDB instance. location is a String pointing to the LevelDB location to be opened.


<a name="LevelDB_open"></a>

LevelDB#open([options, ]callback)

<code>open()</code> is an instance method on an existing database object.

The callback function will be called with no arguments when the database has been successfully opened, or with a single error argument if the open operation failed for any reason.

options

The optional options argument may contain:

Advanced options

The following options are for advanced performance tuning. Modify them only if you can prove actual benefit for your particular application.

Larger values increase performance, especially during bulk loads. Up to two write buffers may be held in memory at the same time, so you may wish to adjust this parameter to control memory usage. Also, a larger write buffer will result in a longer recovery time the next time the database is opened.


<a name="LevelDB_close"></a>

LevelDB#close(callback)

<code>close()</code> is an instance method on an existing database object. The underlying LevelDB database will be closed and the callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_put"></a>

LevelDB#put(key, value[, options], callback)

<code>put()</code> is an instance method on an existing database object, used to store new entries, or overwrite existing entries in the LevelDB store.

The key and value objects may either be Strings or Node.js Buffer objects. Other object types are converted to JavaScript Strings with the toString() method. Keys may not be null or undefined and objects converted with toString() should not result in an empty-string. Values of null, undefined, '', [] and new Buffer(0) (and any object resulting in a toString() of one of these) will be stored as a zero-length character array and will therefore be retrieved as either '' or new Buffer(0) depending on the type requested.

A richer set of data-types are catered for in LevelUP.

options

The only property currently available on the options object is 'sync' (boolean, default: false). 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.

The callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_get"></a>

LevelDB#get(key[, options], callback)

<code>get()</code> is an instance method on an existing database object, used to fetch individual entries from the LevelDB store.

The key object may either be a String or a Node.js Buffer object and cannot be undefined or null. Other object types are converted to JavaScript Strings with the toString() method and the resulting String may not be a zero-length. A richer set of data-types are catered for in LevelUP.

Values fetched via get() that are stored as zero-length character arrays (null, undefined, '', [], new Buffer(0)) will return as empty-String ('') or new Buffer(0) when fetched with asBuffer: true (see below).

options

The optional options object may contain:

The callback function will be called with a single error if the operation failed for any reason. If successful the first argument will be null and the second argument will be the value as a String or Buffer depending on the asBuffer option.


<a name="LevelDB_del"></a>

LevelDB#del(key[, options], callback)

<code>del()</code> is an instance method on an existing database object, used to delete entries from the LevelDB store.

The key object may either be a String or a Node.js Buffer object and cannot be undefined or null. Other object types are converted to JavaScript Strings with the toString() method and the resulting String may not be a zero-length. A richer set of data-types are catered for in LevelUP.

options

The only property currently available on the options object is 'sync' (boolean, default: false). See <a href="#LevelDB_put">LevelDB#put()</a> for details about this option.

The callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_batch"></a>

LevelDB#batch(operations[, options], callback)

<code>batch()</code> is an instance method on an existing database object. Used for very fast bulk-write operations (both put and delete). The operations argument should be an Array containing 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. Any entries where the type is 'put' that have a 'value' of undefined, null, [], '' or new Buffer(0) will be stored as a zero-length character array and therefore be fetched during reads as either '' or new Buffer(0) depending on how they are requested.

See LevelUP for full documentation on how this works in practice.

options

The only property currently available on the options object is 'sync' (boolean, default: false). See <a href="#LevelDB_put">LevelDB#put()</a> for details about this option.

The callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_approximateSize"></a>

LevelDB#approximateSize(start, end, callback)

<code>approximateSize()</code> is an instance method on an existing database object. 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.

The start and end parameters may be either String or Node.js Buffer objects representing keys in the LevelDB store.

The callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_getProperty"></a>

LevelDB#getProperty(property)

<code>getProperty</code> can be used to get internal details from LevelDB. When issued with a valid property string, a readable string will be returned (this method is synchronous).

Currently, the only valid properties are:


<a name="LevelDB_iterator"></a>

LevelDB#iterator([options])

<code>iterator()</code> is an instance method on an existing database object. It returns a new Iterator instance.

options

The optional options object may contain:

When all three simultaneously, their priority is as follows:


<a name="iterator_next"></a>

iterator#next(callback)

<code>next()</code> is an instance method on an existing iterator object, used to increment the underlying LevelDB iterator and return the entry at that location.

the callback function will be called with no arguments in any of the following situations:

Otherwise, the callback function will be called with the following 3 arguments:


<a name="iterator_end"></a>

iterator#end(callback)

<code>end()</code> is an instance method on an existing iterator object. The underlying LevelDB iterator will be deleted and the callback function will be called with no arguments if the operation is successful or with a single error argument if the operation failed for any reason.


<a name="LevelDB_destroy"></a>

LevelDB.destroy(location, callback)

<code>destroy()</code> 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 callback will be called when the destroy operation is complete, with a possible error argument.

<a name="LevelDB_repair"></a>

LevelDB.repair(location, callback)

<code>repair()</code> 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 callback will be called when the repair operation is complete, with a possible error argument.

<a name="safety"></a> Safety

Database state

Currently LevelDB does not track the state of the underlying LevelDB instance. This means that calling open() on an already open database may result in an error. Likewise, calling any other operation on a non-open database may result in an error.

LevelUP currently tracks and manages state and will prevent out-of-state operations from being send to LevelDB. If you use LevelDB directly then you must track and manage state for yourself.

<a name="support"></a> Getting support

There are multiple ways you can find help in using LevelDB in Node.js:

<a name="contributing"></a> Contributing

LevelDB 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.

Contributors

LevelDB is only possible due to the excellent work of the following contributors:

<table><tbody> <tr><th align="left">Riceball LEE</th><td><a href="https://github.com/snowyu">GitHub/snowyu</a></td><td></td></tr> <tr><th align="left">Rod Vagg</th><td><a href="https://github.com/rvagg">GitHub/rvagg</a></td><td><a href="http://twitter.com/rvagg">Twitter/@rvagg</a></td></tr> <tr><th align="left">John Chesley</th><td><a href="https://github.com/chesles/">GitHub/chesles</a></td><td><a href="http://twitter.com/chesles">Twitter/@chesles</a></td></tr> <tr><th align="left">Jake Verbaten</th><td><a href="https://github.com/raynos">GitHub/raynos</a></td><td><a href="http://twitter.com/raynos2">Twitter/@raynos2</a></td></tr> <tr><th align="left">Dominic Tarr</th><td><a href="https://github.com/dominictarr">GitHub/dominictarr</a></td><td><a href="http://twitter.com/dominictarr">Twitter/@dominictarr</a></td></tr> <tr><th align="left">Max Ogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td><td><a href="http://twitter.com/maxogden">Twitter/@maxogden</a></td></tr> <tr><th align="left">Lars-Magnus Skog</th><td><a href="https://github.com/ralphtheninja">GitHub/ralphtheninja</a></td><td><a href="http://twitter.com/ralphtheninja">Twitter/@ralphtheninja</a></td></tr> <tr><th align="left">David Björklund</th><td><a href="https://github.com/kesla">GitHub/kesla</a></td><td><a href="http://twitter.com/david_bjorklund">Twitter/@david_bjorklund</a></td></tr> <tr><th align="left">Julian Gruber</th><td><a href="https://github.com/juliangruber">GitHub/juliangruber</a></td><td><a href="http://twitter.com/juliangruber">Twitter/@juliangruber</a></td></tr> <tr><th align="left">Paolo Fragomeni</th><td><a href="https://github.com/hij1nx">GitHub/hij1nx</a></td><td><a href="http://twitter.com/hij1nx">Twitter/@hij1nx</a></td></tr> <tr><th align="left">Anton Whalley</th><td><a href="https://github.com/No9">GitHub/No9</a></td><td><a href="https://twitter.com/antonwhalley">Twitter/@antonwhalley</a></td></tr> <tr><th align="left">Matteo Collina</th><td><a href="https://github.com/mcollina">GitHub/mcollina</a></td><td><a href="https://twitter.com/matteocollina">Twitter/@matteocollina</a></td></tr> <tr><th align="left">Pedro Teixeira</th><td><a href="https://github.com/pgte">GitHub/pgte</a></td><td><a href="https://twitter.com/pgte">Twitter/@pgte</a></td></tr> <tr><th align="left">James Halliday</th><td><a href="https://github.com/substack">GitHub/substack</a></td><td><a href="https://twitter.com/substack">Twitter/@substack</a></td></tr> </tbody></table>

Windows

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!

<a name="license"></a> License & copyright

Copyright (c) 2012-2014 LevelDB contributors (listed above).

LevelDB is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.

LevelDB 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.