Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
/leveldownPublic archive

Superseded by classic-level. Pure C++ Node.js LevelDB binding. An abstract-leveldown compliant store.

License

NotificationsYou must be signed in to change notification settings

Level/leveldown

Repository files navigation

Superseded byclassic-level. Please seeFrequently Asked Questions.

Table of Contents

Click to expand

Introduction

This module was originally part oflevelup but was later extracted and now serves as a stand-alone binding for LevelDB.

It isstrongly recommended that you uselevelup in preference toleveldown 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 theleveldown interface it is still easy to crash your Node process if you don't do things in just the right way.

See the section onsafety below for details of known unsafe operations withleveldown.

Supported Platforms

We aim to supportat least Active LTS and Current Node.js releases, Electron 5.0.0, as well as any future Node.js and Electron releases thanks toN-API. The minimum node version forleveldown is10.12.0. Conversely, for node >= 12, the minimumleveldown version is5.0.0.

Theleveldown npm package ships with prebuilt binaries for popular 64-bit platforms as well as ARM, M1, Android and Alpine (musl) and is known to work on:

  • Linux (including ARM platforms such as Raspberry Pi and Kindle)
  • Mac OS (10.7 and later)
  • Solaris (SmartOS & Nodejitsu)
  • FreeBSD
  • Windows

When installingleveldown,node-gyp-build will check if a compatible binary exists and fallback to a compile step if it doesn't. In that case you'll need avalidnode-gyp installation.

If you don't want to use the prebuilt binary for the platform you are installing on, specify the--build-from-source flag when you install. One of:

npm install --build-from-sourcenpm install leveldown --build-from-source

If you are working onleveldown itself and want to re-compile the C++ code, runnpm run rebuild.

Notes

  • If you get compilation errors on Node.js 12, please ensure you haveleveldown >= 5. This can be checked by runningnpm ls leveldown.
  • On Linux flavors with an old glibc (Debian 8, Ubuntu 14.04, RHEL 7, CentOS 7) you must either updateleveldown to >= 5.3.0 or use--build-from-source.
  • On Alpine 3 it was previously necessary to use--build-from-source. This is no longer the case.
  • The Android prebuilds are made for and built against Node.js core rather than thenodejs-mobile fork.

API

If you are upgrading: please seeUPGRADING.md.

db = leveldown(location)

Returns a newleveldown instance.location is a String pointing to the LevelDB location to be opened.

db.open([options, ]callback)

Open the store. Thecallback function will be called with no arguments when the database has been successfully opened, or with a singleerror argument if the open operation failed for any reason.

options

The optionaloptions 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, allcompressible 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-memoryLRU 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): Theapproximate 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'maxFileSize'.

  • 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 keyframes 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.

  • maxFileSize (number, default:2* 1024 * 1024 = 2MB): The maximum amount of bytes to write to a file before switching to a new one. From the LevelDB documentation:

... if your filesystem is more efficient with larger files, you could consider increasing the value. The downside will be longer compactions and hence longer latency/performance hiccups. Another reason to increase this parameter might be when you are initially populating a large database.

db.close(callback)

close() is an instance method on an existing database object. The underlying LevelDB database will be closed and thecallback function will be called with no arguments if the operation is successful or with a singleerror argument if the operation failed for any reason.

leveldown waits for any pending operations to finish before closing. For example:

db.put('key','value',function(err){// This happens first})db.close(function(err){// This happens second})

db.put(key, value[, options], callback)

Store a new entry or overwrite an existing entry.

Thekey andvalue objects may either be strings or Buffers. Other object types are converted to strings with thetoString() method. Keys may not benull orundefined and objects converted withtoString() should not result in an empty-string. Values may not benull orundefined. Values of'',[] andBuffer.alloc(0) (and any object resulting in atoString() of one of these) will be stored as a zero-length character array and will therefore be retrieved as either'' orBuffer.alloc(0) depending on the type requested.

A richer set of data-types is catered for inlevelup.

options

The only property currently available on theoptions object issync(boolean, default:false). If you provide async value oftrue in youroptions 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 usefsync() or equivalent so your callback won't be triggered until the data is actually on disk. Synchronous filesystem writes aresignificantly slower than asynchronous writes but if you want to be absolutely sure that the data is flushed then you can use{ sync: true }.

Thecallback function will be called with no arguments if the operation is successful or with a singleerror argument if the operation failed for any reason.

db.get(key[, options], callback)

Get a value from the LevelDB store bykey.

Thekey object may either be a string or a Buffer and cannot beundefined ornull. Other object types are converted to strings with thetoString() method and the resulting stringmay not be a zero-length. A richer set of data-types is catered for inlevelup.

Values fetched viaget() that are stored as zero-length character arrays (null,undefined,'',[],Buffer.alloc(0)) will return as empty-String ('') orBuffer.alloc(0) when fetched withasBuffer: true (see below).

options

The optionaloptions object may contain:

  • asBuffer (boolean, default:true): Used to determine whether to return thevalue of the entry as a string or a Buffer. Note that converting from a Buffer to a string incurs a cost so if you need a string (and thevalue can legitimately become a UTF8 string) then you should fetch it as one with{ asBuffer: false } and you'll avoid this conversion cost.
  • 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.

Thecallback function will be called with a singleerror if the operation failed for any reason, including if the key was not found. If successful the first argument will benull and the second argument will be thevalue as a string or Buffer depending on theasBuffer option.

db.getMany(keys[, options][, callback])

Get multiple values from the store by an array ofkeys. The optionaloptions object may containasBuffer andfillCache, as described inget().

Thecallback function will be called with anError if the operation failed for any reason. If successful the first argument will benull and the second argument will be an array of values with the same order askeys. If a key was not found, the relevant value will beundefined.

If no callback is provided, a promise is returned.

db.del(key[, options], callback)

Delete an entry. Thekey object may either be a string or a Buffer and cannot beundefined ornull. Other object types are converted to strings with thetoString() method and the resulting stringmay not be a zero-length. A richer set of data-types is catered for inlevelup.

options

The only property currently available on theoptions object issync(boolean, default:false). Seedb.put() for details about this option.

Thecallback function will be called with no arguments if the operation is successful or with a singleerror argument if the operation failed for any reason.

db.batch(operations[, options], callback)(array form)

Perform multipleput and/ordel operations in bulk. Theoperations argument must be anArray containing a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation.

Each operation is contained in an object having the following properties:type,key,value, where thetype is either'put' or'del'. In the case of'del' thevalue property is ignored.

Any entries where thekey orvalue (in the case of'put') isnull orundefined will cause an error to be returned on thecallback. Any entries where thetype is'put' that have avalue of[],'' orBuffer.alloc(0) will be stored as a zero-length character array and therefore be fetched during reads as either'' orBuffer.alloc(0) depending on how they are requested. Seelevelup for full documentation on how this works in practice.

The optionaloptions argument may contain:

  • sync (boolean, default:false). Seedb.put() for details about this option.

Thecallback function will be called with no arguments if the batch is successful or with anError if the batch failed for any reason.

db.batch()(chained form)

Returns a newchainedBatch instance.

db.approximateSize(start, end, callback)

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.

Thestart andend parameters may be strings or Buffers representing keys in the LevelDB store.

Thecallback function will be called with a singleerror if the operation failed for any reason. If successful the first argument will benull and the second argument will be the approximate size as a Number.

db.compactRange(start, end, callback)

compactRange() is an instance method on an existing database object. Used to manually trigger a database compaction in the range[start..end).

Thestart andend parameters may be strings or Buffers representing keys in the LevelDB store.

Thecallback function will be called with no arguments if the operation is successful or with a singleerror argument if the operation failed for any reason.

db.getProperty(property)

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 levelN, 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 thesstables that make up contents of the current database.

db.iterator([options])

Returns a newiterator instance. Accepts the following range options:

  • gt (greater than),gte (greater than or equal) define the lower bound of the range to be iterated. Only entries where the key is greater than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries iterated will be the same.
  • lt (less than),lte (less than or equal) define the higher bound of the range to be iterated. Only entries where the key is less than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries iterated will be the same.
  • reverse(boolean, default:false): iterate entries in reverse order. Beware that a reverse seek can be slower than a forward seek.
  • limit(number, default:-1): limit the number of entries collected by this iterator. This number represents amaximum number of entries and may not be reached if you get to the end of the range first. A value of-1 means there is no limit. Whenreverse=true the entries with the highest keys will be returned instead of the lowest keys.

In addition to range options,iterator() takes the following options:

  • keys(boolean, default:true): whether to return the key of each entry. If set tofalse, calls toiterator.next(callback) will yield keys with a value ofundefined1. 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 to return the value of each entry. If set tofalse, calls toiterator.next(callback) will yield values with a value ofundefined1.
  • keyAsBuffer(boolean, default:true): Whether to return the key of each entry as a Buffer or string. Converting from a Buffer to a string incurs a cost so if you need a string (and thekey can legitimately become a UTF8 string) then you should fetch it as one.
  • valueAsBuffer(boolean, default:true): Whether to return the value of each entry as a Buffer or string.
  • fillCache (boolean, default:false): whether LevelDB's LRU-cache should be filled with data read.

1leveldown returns an empty string rather thanundefined at the moment.

db.clear([options, ]callback)

Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators):

  • gt (greater than),gte (greater than or equal) define the lower bound of the range to be deleted. Only entries where the key is greater than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries deleted will be the same.
  • lt (less than),lte (less than or equal) define the higher bound of the range to be deleted. Only entries where the key is less than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries deleted will be the same.
  • reverse(boolean, default:false): delete entries in reverse order. Only effective in combination withlimit, to remove the last N entries.
  • limit(number, default:-1): limit the number of entries to be deleted. This number represents amaximum number of entries and may not be reached if you get to the end of the range first. A value of-1 means there is no limit. Whenreverse=true the entries with the highest keys will be deleted instead of the lowest keys.

If no options are provided, all entries will be deleted. Thecallback function will be called with no arguments if the operation was successful or with anError if it failed for any reason.

chainedBatch

chainedBatch.put(key, value)

Queue aput operation on this batch. This may throw ifkey orvalue is invalid, following the same rules as thearray form ofdb.batch().

chainedBatch.del(key)

Queue adel operation on this batch. This may throw ifkey is invalid.

chainedBatch.clear()

Clear all queued operations on this batch.

chainedBatch.write([options, ]callback)

Commit the queued operations for this batch. All operations will be written atomically, that is, they will either all succeed or fail with no partial commits.

The optionaloptions argument may contain:

  • sync (boolean, default:false). Seedb.put() for details about this option.

Thecallback function will be called with no arguments if the batch is successful or with anError if the batch failed for any reason. Afterwrite has been called, no further operations are allowed.

chainedBatch.db

A reference to thedb that created this chained batch.

iterator

An iterator allows you toiterate the entire store or a range. It operates on a snapshot of the store, created at the timedb.iterator() was called. This means reads on the iterator are unaffected by simultaneous writes.

Iterators can be consumed withfor await...of or by manually callingiterator.next() in succession. In the latter mode,iterator.end() must always be called. In contrast, finishing, throwing or breaking from afor await...of loop automatically callsiterator.end().

An iterator reaches its natural end in the following situations:

  • The end of the store has been reached
  • The end of the range has been reached
  • The lastiterator.seek() was out of range.

An iterator keeps track of when anext() is in progress and when anend() has been called so it doesn't allow concurrentnext() calls, it does allowend() while anext() is in progress and it doesn't allow eithernext() orend() afterend() has been called.

for await...of iterator

Yields arrays containing akey andvalue. The type ofkey andvalue depends on the options passed todb.iterator().

try{forawait(const[key,value]ofdb.iterator()){console.log(key)}}catch(err){console.error(err)}

iterator.next([callback])

Advance the iterator and yield the entry at that key. If an error occurs, thecallback function will be called with anError. Otherwise, thecallback receivesnull, akey and avalue. The type ofkey andvalue depends on the options passed todb.iterator(). If the iterator has reached its natural end, bothkey andvalue will beundefined.

If no callback is provided, a promise is returned for either an array (containing akey andvalue) orundefined if the iterator reached its natural end.

Note: Always calliterator.end(), even if you received an error and even if the iterator reached its natural end.

iterator.seek(key)

Seek the iterator to a given key or the closest key. Subsequent calls toiterator.next() will yield entries with keys equal to or larger thantarget, or equal to or smaller thantarget if thereverse option passed todb.iterator() was true. The same applies to implicititerator.next() calls in afor await...of loop.

If range options likegt were passed todb.iterator() andtarget does not fall within that range, the iterator will reach its natural end.

iterator.end([callback])

End iteration and free up underlying resources. Thecallback function will be called with no arguments on success or with anError if ending failed for any reason.

If no callback is provided, a promise is returned.

iterator.db

A reference to thedb that created this iterator.

leveldown.destroy(location, callback)

Completely remove an existing LevelDB database directory. You can use this function in place of a full directoryrm 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 possibleerror argument.

leveldown.repair(location, callback)

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 therepair operation in theLOG file inside the store directory.

Arepair() 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 possibleerror argument.

Safety

Database State

Currentlyleveldown does not track the state of the underlying LevelDB instance. This means that callingopen() 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 toleveldown. If you useleveldown directly then you must track and manage state for yourself.

Snapshots

leveldown exposes a feature of LevelDB calledsnapshots. This means that when you do e.g.createReadStream andcreateWriteStream at the same time, any data modified by the write stream will not affect data emitted from the read stream. In other words, a LevelDB Snapshot captures the latest state at the time the snapshot was created, enabling the snapshot to iterate or read the data without seeing any subsequent writes. Any read not performed on a snapshot will implicitly use the latest state.

Getting Support

You're welcome to open an issue on theGitHub repository if you have a question.

Past and no longer active support channels include the##leveldb IRC channel on Freenode and theNode.js LevelDB Google Group.

Contributing

Level/leveldown is anOPEN 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 theContribution Guide for more details.

Git Submodules

This project uses Git Submodules. This means that you should clone it recursively if you're planning on working on it:

$ git clone --recurse-submodules https://github.com/Level/leveldown.git

Alternatively, you can initialize submodules inside the cloned folder:

$ git submodule update --init --recursive

Publishing

  1. Increment the version:npm version ..
  2. Push to GitHub:git push --follow-tags
  3. Wait for CI to complete
  4. Download prebuilds into./prebuilds:npm run download-prebuilds
  5. Optionally verify loading a prebuild:npm run test-prebuild
  6. Optionally verify which files npm will include:canadian-pub
  7. Finally:npm publish

Donate

Support us with a monthly donation onOpen Collective and help us continue our work.

License

MIT

leveldown builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under theNew BSD License. A large portion ofleveldown Windows support comes from theWindows LevelDB port (archived) byKrzysztof Kowalczyk (@kjk). If you're usingleveldown on Windows, you should give him your thanks!


[8]ページ先頭

©2009-2025 Movatter.jp