Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1**LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.**3[](https://travis-ci.org/google/leveldb)4[](https://ci.appveyor.com/project/pwnall/leveldb)6Authors: Sanjay Ghemawat ([email protected]) and Jeff Dean ([email protected])8# Features10* Keys and values are arbitrary byte arrays.11* Data is stored sorted by key.12* Callers can provide a custom comparison function to override the sort order.13* The basic operations are `Put(key,value)`, `Get(key)`, `Delete(key)`.14* Multiple changes can be made in one atomic batch.15* Users can create a transient snapshot to get a consistent view of data.16* Forward and backward iteration is supported over the data.17* Data is automatically compressed using the [Snappy compression library](http://google.github.io/snappy/).18* External activity (file system operations etc.) is relayed through a virtual interface so users can customize the operating system interactions.20# Documentation22[LevelDB library documentation](https://github.com/google/leveldb/blob/master/doc/index.md) is online and bundled with the source code.24# Limitations26* This is not a SQL database. It does not have a relational data model, it does not support SQL queries, and it has no support for indexes.27* Only a single process (possibly multi-threaded) can access a particular database at a time.28* There is no client-server support builtin to the library. An application that needs such support will have to wrap their own server around the library.30# Building32This project supports [CMake](https://cmake.org/) out of the box.34### Build for POSIX36Quick start:38```bash39mkdir -p build && cd build40cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build .41```43### Building for Windows45First generate the Visual Studio 2017 project/solution files:47```cmd48mkdir build49cd build50cmake -G "Visual Studio 15" ..51```52The default will build for x86. For 64-bit run:54```cmd55cmake -G "Visual Studio 15 Win64" ..56```58To compile the Windows solution from the command-line:60```cmd61devenv /build Debug leveldb.sln62```64or open leveldb.sln in Visual Studio and build from within.66Please see the CMake documentation and `CMakeLists.txt` for more advanced usage.68# Contributing to the leveldb Project70The leveldb project welcomes contributions. leveldb's primary goal is to be71a reliable and fast key/value store. Changes that are in line with the72features/limitations outlined above, and meet the requirements below,73will be considered.75Contribution requirements:771. **Tested platforms only**. We _generally_ will only accept changes for78platforms that are compiled and tested. This means POSIX (for Linux and79macOS) or Windows. Very small changes will sometimes be accepted, but80consider that more of an exception than the rule.822. **Stable API**. We strive very hard to maintain a stable API. Changes that83require changes for projects using leveldb _might_ be rejected without84sufficient benefit to the project.863. **Tests**: All changes must be accompanied by a new (or changed) test, or87a sufficient explanation as to why a new (or changed) test is not required.894. **Consistent Style**: This project conforms to the90[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html).91To ensure your changes are properly formatted please run:93```94clang-format -i --style=file <file>95```97## Submitting a Pull Request99Before any pull request will be accepted the author must first sign a100Contributor License Agreement (CLA) at https://cla.developers.google.com/.102In order to keep the commit timeline linear103[squash](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Squashing-Commits)104your changes down to a single commit and [rebase](https://git-scm.com/docs/git-rebase)105on google/leveldb/master. This keeps the commit timeline linear and more easily sync'ed106with the internal repository at Google. More information at GitHub's107[About Git rebase](https://help.github.com/articles/about-git-rebase/) page.109# Performance111Here is a performance report (with explanations) from the run of the112included db_bench program. The results are somewhat noisy, but should113be enough to get a ballpark performance estimate.115## Setup117We use a database with a million entries. Each entry has a 16 byte118key, and a 100 byte value. Values used by the benchmark compress to119about half their original size.121LevelDB: version 1.1122Date: Sun May 1 12:11:26 2011123CPU: 4 x Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz124CPUCache: 4096 KB125Keys: 16 bytes each126Values: 100 bytes each (50 bytes after compression)127Entries: 1000000128Raw Size: 110.6 MB (estimated)129File Size: 62.9 MB (estimated)131## Write performance133The "fill" benchmarks create a brand new database, in either134sequential, or random order. The "fillsync" benchmark flushes data135from the operating system to the disk after every operation; the other136write operations leave the data sitting in the operating system buffer137cache for a while. The "overwrite" benchmark does random writes that138update existing keys in the database.140fillseq : 1.765 micros/op; 62.7 MB/s141fillsync : 268.409 micros/op; 0.4 MB/s (10000 ops)142fillrandom : 2.460 micros/op; 45.0 MB/s143overwrite : 2.380 micros/op; 46.5 MB/s145Each "op" above corresponds to a write of a single key/value pair.146I.e., a random write benchmark goes at approximately 400,000 writes per second.148Each "fillsync" operation costs much less (0.3 millisecond)149than a disk seek (typically 10 milliseconds). We suspect that this is150because the hard disk itself is buffering the update in its memory and151responding before the data has been written to the platter. This may152or may not be safe based on whether or not the hard disk has enough153power to save its memory in the event of a power failure.155## Read performance157We list the performance of reading sequentially in both the forward158and reverse direction, and also the performance of a random lookup.159Note that the database created by the benchmark is quite small.160Therefore the report characterizes the performance of leveldb when the161working set fits in memory. The cost of reading a piece of data that162is not present in the operating system buffer cache will be dominated163by the one or two disk seeks needed to fetch the data from disk.164Write performance will be mostly unaffected by whether or not the165working set fits in memory.167readrandom : 16.677 micros/op; (approximately 60,000 reads per second)168readseq : 0.476 micros/op; 232.3 MB/s169readreverse : 0.724 micros/op; 152.9 MB/s171LevelDB compacts its underlying storage data in the background to172improve read performance. The results listed above were done173immediately after a lot of random writes. The results after174compactions (which are usually triggered automatically) are better.176readrandom : 11.602 micros/op; (approximately 85,000 reads per second)177readseq : 0.423 micros/op; 261.8 MB/s178readreverse : 0.663 micros/op; 166.9 MB/s180Some of the high cost of reads comes from repeated decompression of blocks181read from disk. If we supply enough cache to the leveldb so it can hold the182uncompressed blocks in memory, the read performance improves again:184readrandom : 9.775 micros/op; (approximately 100,000 reads per second before compaction)185readrandom : 5.215 micros/op; (approximately 190,000 reads per second after compaction)187## Repository contents189See [doc/index.md](doc/index.md) for more explanation. See190[doc/impl.md](doc/impl.md) for a brief overview of the implementation.192The public interface is in include/leveldb/*.h. Callers should not include or193rely on the details of any other header files in this package. Those194internal APIs may be changed without warning.196Guide to header files:198* **include/leveldb/db.h**: Main interface to the DB: Start here.200* **include/leveldb/options.h**: Control over the behavior of an entire database,201and also control over the behavior of individual reads and writes.203* **include/leveldb/comparator.h**: Abstraction for user-specified comparison function.204If you want just bytewise comparison of keys, you can use the default205comparator, but clients can write their own comparator implementations if they206want custom ordering (e.g. to handle different character encodings, etc.).208* **include/leveldb/iterator.h**: Interface for iterating over data. You can get209an iterator from a DB object.211* **include/leveldb/write_batch.h**: Interface for atomically applying multiple212updates to a database.214* **include/leveldb/slice.h**: A simple module for maintaining a pointer and a215length into some other byte array.217* **include/leveldb/status.h**: Status is returned from many of the public interfaces218and is used to report success and various kinds of errors.220* **include/leveldb/env.h**:221Abstraction of the OS environment. A posix implementation of this interface is222in util/env_posix.cc.224* **include/leveldb/table.h, include/leveldb/table_builder.h**: Lower-level modules that most225clients probably won't use directly.
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.