LevelDOWN was extracted from LevelUP and now serves as a stand-alone binding for LevelDB.
It is strongly recommended that you use LevelUP in preference to LevelDOWN 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 LevelDOWN 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 safety below for details of known unsafe operations with LevelDOWN.
- Linux (including ARM platforms such as Raspberry Pi and Kindle!)
- Mac OS
- Solaris (SmartOS & Nodejitsu)
- FreeBSD
- Windows
leveldown()
leveldown#open()
leveldown#close()
leveldown#put()
leveldown#get()
leveldown#del()
leveldown#batch()
leveldown#approximateSize()
leveldown#getProperty()
leveldown#iterator()
iterator#next()
iterator#end()
leveldown.destroy()
leveldown.repair()
leveldown()
returns a new LevelDOWN instance. location
is a String pointing to the LevelDB location to be opened.
open()
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.
The optional options
argument may contain:
-
'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
= 8MB): The size (in bytes) of the in-memory LRU cache with frequently used uncompressed block contents.
Advanced options
The following options are for advanced performance tuning. Modify them only if you can prove actual benefit for your particular application.
'writeBufferSize'
(number, default:4 * 1024 * 1024
= 4MB): The maximum size (in bytes) of the log (in memory and stored in the .log file on disk). Beyond this size, LevelDB will convert the log data to the first level of sorted table files. From the LevelDB documentation:
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.
-
'blockSize'
(number, default4096
= 4K): The approximate size of the blocks that make up the table files. The size related to uncompressed data (hence "approximate"). Blocks are indexed in the table file and entry-lookups involve reading an entire block and parsing to discover the required entry. -
'maxOpenFiles'
(number, default:1000
): The maximum number of files that LevelDB is allowed to have open at a time. If your data store is likely to have a large working set, you may increase this value to prevent file descriptor churn. To calculate the number of files required for your working set, divide your total data by 2MB, as each table file is a maximum of 2MB. -
'blockRestartInterval'
(number, default:16
): The number of entries before restarting the "delta encoding" of keys within blocks. Each "restart" point stores the full key for the entry, between restarts, the common prefix of the keys for those entries is omitted. Restarts are similar to the concept of keyframs in video encoding and are used to minimise the amount of space required to store keys. This is particularly helpful when using deep namespacing / prefixing in your keys.
close()
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.
put()
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 String
s or Node.js Buffer
objects and cannot be undefined
or null
. Other object types are converted to JavaScript String
s 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.
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.
get()
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 String
s 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.
The optional options
object may contain:
-
'fillCache'
(boolean, default:true
): LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by settingfillCache
tofalse
. -
'asBuffer'
(boolean, default:true
): Used to determine whether to return thevalue
of the entry as aString
or a Node.jsBuffer
object. Note that converting from aBuffer
to aString
incurs a cost so if you need aString
(and thevalue
can legitimately become a UFT8 string) then you should fetch it as one withasBuffer: true
and you'll avoid this conversion cost.
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.
del()
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 String
s 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.
The only property currently available on the options
object is 'sync'
(boolean, default: false
). See leveldown#put() 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.
batch()
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
and any 'type': 'put'
entry with a 'value'
of null
or undefined
will return an error. See LevelUP for full documentation on how this works in practice.
The only property currently available on the options
object is 'sync'
(boolean, default: false
). See leveldown#put() 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.
approximateSize()
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.
getProperty
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:
-
'leveldb.num-files-at-levelN'
: return the number of files at level N, where N is an integer representing a valid level (e.g. "0"). -
'leveldb.stats'
: returns a multi-line string describing statistics about LevelDB's internal operation. -
'leveldb.sstables'
: returns a multi-line string describing all of the sstables that make up contents of the current database.
iterator()
is an instance method on an existing database object. It returns a new Iterator instance.
The optional options
object may contain:
-
'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 callback to thenext()
method should receive a non-nullkey
. There is a small efficiency gain if you ultimately don't care what the keys are as they don't need to be converted and copied into JavaScript. -
'values'
(boolean, default:true
): whether the callback to thenext()
method should receive a non-nullvalue
. There is a small efficiency gain if you ultimately don't care what the values are as they don't need to be converted and copied into JavaScript. -
'limit'
(number, default:-1
): limit the number of results collected by this iterator. 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. -
'keyAsBuffer'
(boolean, default:true
): Used to determine whether to return thekey
of each entry as aString
or a Node.jsBuffer
object. Note that converting from aBuffer
to aString
incurs a cost so if you need aString
(and thevalue
can legitimately become a UFT8 string) then you should fetch it as one. -
'valueAsBuffer'
(boolean, default:true
): Used to determine whether to return thevalue
of each entry as aString
or a Node.jsBuffer
object.
next()
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:
- the iterator comes to the end of the store
- the
end
key has been reached; or - the
limit
has been reached
Otherwise, the callback
function will be called with the following 3 arguments:
error
- any error that occurs while incrementing the iterator.key
- either aString
or a Node.jsBuffer
object depending on thekeyAsBuffer
argument when theiterator()
was called.value
- either aString
or a Node.jsBuffer
object depending on thevalueAsBuffer
argument when theiterator()
was called.
end()
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.
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 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 callback will be called when the repair operation is complete, with a possible error
argument.
Currently LevelDOWN 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 LevelDOWN. If you use LevelDOWN directly then you must track and manage state for yourself.
There are multiple ways you can find help in using LevelDB in Node.js:
- IRC: you'll find an active group of LevelUP users in the ##leveldb channel on Freenode, including most of the contributors to this project.
- Mailing list: there is an active Node.js LevelDB Google Group.
- GitHub: you're welcome to open an issue here on this GitHub repository if you have a question.
LevelDOWN 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.
LevelDOWN 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 |
Pedro Teixeira | GitHub/pgte | Twitter/@pgte |
James Halliday | GitHub/substack | Twitter/@substack |
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 LevelDOWN contributors (listed above).
LevelDOWN 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.
LevelDOWN 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.