1*9507f98cSAndroid Build Coastguard Worker**LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.** 2*9507f98cSAndroid Build Coastguard Worker 3*9507f98cSAndroid Build Coastguard Worker[](https://travis-ci.org/google/leveldb) 4*9507f98cSAndroid Build Coastguard Worker[](https://ci.appveyor.com/project/pwnall/leveldb) 5*9507f98cSAndroid Build Coastguard Worker 6*9507f98cSAndroid Build Coastguard WorkerAuthors: Sanjay Ghemawat ([email protected]) and Jeff Dean ([email protected]) 7*9507f98cSAndroid Build Coastguard Worker 8*9507f98cSAndroid Build Coastguard Worker# Features 9*9507f98cSAndroid Build Coastguard Worker 10*9507f98cSAndroid Build Coastguard Worker * Keys and values are arbitrary byte arrays. 11*9507f98cSAndroid Build Coastguard Worker * Data is stored sorted by key. 12*9507f98cSAndroid Build Coastguard Worker * Callers can provide a custom comparison function to override the sort order. 13*9507f98cSAndroid Build Coastguard Worker * The basic operations are `Put(key,value)`, `Get(key)`, `Delete(key)`. 14*9507f98cSAndroid Build Coastguard Worker * Multiple changes can be made in one atomic batch. 15*9507f98cSAndroid Build Coastguard Worker * Users can create a transient snapshot to get a consistent view of data. 16*9507f98cSAndroid Build Coastguard Worker * Forward and backward iteration is supported over the data. 17*9507f98cSAndroid Build Coastguard Worker * Data is automatically compressed using the [Snappy compression library](https://google.github.io/snappy/). 18*9507f98cSAndroid Build Coastguard Worker * External activity (file system operations etc.) is relayed through a virtual interface so users can customize the operating system interactions. 19*9507f98cSAndroid Build Coastguard Worker 20*9507f98cSAndroid Build Coastguard Worker# Documentation 21*9507f98cSAndroid Build Coastguard Worker 22*9507f98cSAndroid Build Coastguard Worker [LevelDB library documentation](https://github.com/google/leveldb/blob/master/doc/index.md) is online and bundled with the source code. 23*9507f98cSAndroid Build Coastguard Worker 24*9507f98cSAndroid Build Coastguard Worker# Limitations 25*9507f98cSAndroid Build Coastguard Worker 26*9507f98cSAndroid Build Coastguard Worker * 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*9507f98cSAndroid Build Coastguard Worker * Only a single process (possibly multi-threaded) can access a particular database at a time. 28*9507f98cSAndroid Build Coastguard Worker * 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. 29*9507f98cSAndroid Build Coastguard Worker 30*9507f98cSAndroid Build Coastguard Worker# Getting the Source 31*9507f98cSAndroid Build Coastguard Worker 32*9507f98cSAndroid Build Coastguard Worker```bash 33*9507f98cSAndroid Build Coastguard Workergit clone --recurse-submodules https://github.com/google/leveldb.git 34*9507f98cSAndroid Build Coastguard Worker``` 35*9507f98cSAndroid Build Coastguard Worker 36*9507f98cSAndroid Build Coastguard Worker# Building 37*9507f98cSAndroid Build Coastguard Worker 38*9507f98cSAndroid Build Coastguard WorkerThis project supports [CMake](https://cmake.org/) out of the box. 39*9507f98cSAndroid Build Coastguard Worker 40*9507f98cSAndroid Build Coastguard Worker### Build for POSIX 41*9507f98cSAndroid Build Coastguard Worker 42*9507f98cSAndroid Build Coastguard WorkerQuick start: 43*9507f98cSAndroid Build Coastguard Worker 44*9507f98cSAndroid Build Coastguard Worker```bash 45*9507f98cSAndroid Build Coastguard Workermkdir -p build && cd build 46*9507f98cSAndroid Build Coastguard Workercmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build . 47*9507f98cSAndroid Build Coastguard Worker``` 48*9507f98cSAndroid Build Coastguard Worker 49*9507f98cSAndroid Build Coastguard Worker### Building for Windows 50*9507f98cSAndroid Build Coastguard Worker 51*9507f98cSAndroid Build Coastguard WorkerFirst generate the Visual Studio 2017 project/solution files: 52*9507f98cSAndroid Build Coastguard Worker 53*9507f98cSAndroid Build Coastguard Worker```cmd 54*9507f98cSAndroid Build Coastguard Workermkdir build 55*9507f98cSAndroid Build Coastguard Workercd build 56*9507f98cSAndroid Build Coastguard Workercmake -G "Visual Studio 15" .. 57*9507f98cSAndroid Build Coastguard Worker``` 58*9507f98cSAndroid Build Coastguard WorkerThe default default will build for x86. For 64-bit run: 59*9507f98cSAndroid Build Coastguard Worker 60*9507f98cSAndroid Build Coastguard Worker```cmd 61*9507f98cSAndroid Build Coastguard Workercmake -G "Visual Studio 15 Win64" .. 62*9507f98cSAndroid Build Coastguard Worker``` 63*9507f98cSAndroid Build Coastguard Worker 64*9507f98cSAndroid Build Coastguard WorkerTo compile the Windows solution from the command-line: 65*9507f98cSAndroid Build Coastguard Worker 66*9507f98cSAndroid Build Coastguard Worker```cmd 67*9507f98cSAndroid Build Coastguard Workerdevenv /build Debug leveldb.sln 68*9507f98cSAndroid Build Coastguard Worker``` 69*9507f98cSAndroid Build Coastguard Worker 70*9507f98cSAndroid Build Coastguard Workeror open leveldb.sln in Visual Studio and build from within. 71*9507f98cSAndroid Build Coastguard Worker 72*9507f98cSAndroid Build Coastguard WorkerPlease see the CMake documentation and `CMakeLists.txt` for more advanced usage. 73*9507f98cSAndroid Build Coastguard Worker 74*9507f98cSAndroid Build Coastguard Worker# Contributing to the leveldb Project 75*9507f98cSAndroid Build Coastguard Worker 76*9507f98cSAndroid Build Coastguard WorkerThe leveldb project welcomes contributions. leveldb's primary goal is to be 77*9507f98cSAndroid Build Coastguard Workera reliable and fast key/value store. Changes that are in line with the 78*9507f98cSAndroid Build Coastguard Workerfeatures/limitations outlined above, and meet the requirements below, 79*9507f98cSAndroid Build Coastguard Workerwill be considered. 80*9507f98cSAndroid Build Coastguard Worker 81*9507f98cSAndroid Build Coastguard WorkerContribution requirements: 82*9507f98cSAndroid Build Coastguard Worker 83*9507f98cSAndroid Build Coastguard Worker1. **Tested platforms only**. We _generally_ will only accept changes for 84*9507f98cSAndroid Build Coastguard Worker platforms that are compiled and tested. This means POSIX (for Linux and 85*9507f98cSAndroid Build Coastguard Worker macOS) or Windows. Very small changes will sometimes be accepted, but 86*9507f98cSAndroid Build Coastguard Worker consider that more of an exception than the rule. 87*9507f98cSAndroid Build Coastguard Worker 88*9507f98cSAndroid Build Coastguard Worker2. **Stable API**. We strive very hard to maintain a stable API. Changes that 89*9507f98cSAndroid Build Coastguard Worker require changes for projects using leveldb _might_ be rejected without 90*9507f98cSAndroid Build Coastguard Worker sufficient benefit to the project. 91*9507f98cSAndroid Build Coastguard Worker 92*9507f98cSAndroid Build Coastguard Worker3. **Tests**: All changes must be accompanied by a new (or changed) test, or 93*9507f98cSAndroid Build Coastguard Worker a sufficient explanation as to why a new (or changed) test is not required. 94*9507f98cSAndroid Build Coastguard Worker 95*9507f98cSAndroid Build Coastguard Worker4. **Consistent Style**: This project conforms to the 96*9507f98cSAndroid Build Coastguard Worker [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html). 97*9507f98cSAndroid Build Coastguard Worker To ensure your changes are properly formatted please run: 98*9507f98cSAndroid Build Coastguard Worker 99*9507f98cSAndroid Build Coastguard Worker ``` 100*9507f98cSAndroid Build Coastguard Worker clang-format -i --style=file <file> 101*9507f98cSAndroid Build Coastguard Worker ``` 102*9507f98cSAndroid Build Coastguard Worker 103*9507f98cSAndroid Build Coastguard Worker## Submitting a Pull Request 104*9507f98cSAndroid Build Coastguard Worker 105*9507f98cSAndroid Build Coastguard WorkerBefore any pull request will be accepted the author must first sign a 106*9507f98cSAndroid Build Coastguard WorkerContributor License Agreement (CLA) at https://cla.developers.google.com/. 107*9507f98cSAndroid Build Coastguard Worker 108*9507f98cSAndroid Build Coastguard WorkerIn order to keep the commit timeline linear 109*9507f98cSAndroid Build Coastguard Worker[squash](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Squashing-Commits) 110*9507f98cSAndroid Build Coastguard Workeryour changes down to a single commit and [rebase](https://git-scm.com/docs/git-rebase) 111*9507f98cSAndroid Build Coastguard Workeron google/leveldb/master. This keeps the commit timeline linear and more easily sync'ed 112*9507f98cSAndroid Build Coastguard Workerwith the internal repository at Google. More information at GitHub's 113*9507f98cSAndroid Build Coastguard Worker[About Git rebase](https://help.github.com/articles/about-git-rebase/) page. 114*9507f98cSAndroid Build Coastguard Worker 115*9507f98cSAndroid Build Coastguard Worker# Performance 116*9507f98cSAndroid Build Coastguard Worker 117*9507f98cSAndroid Build Coastguard WorkerHere is a performance report (with explanations) from the run of the 118*9507f98cSAndroid Build Coastguard Workerincluded db_bench program. The results are somewhat noisy, but should 119*9507f98cSAndroid Build Coastguard Workerbe enough to get a ballpark performance estimate. 120*9507f98cSAndroid Build Coastguard Worker 121*9507f98cSAndroid Build Coastguard Worker## Setup 122*9507f98cSAndroid Build Coastguard Worker 123*9507f98cSAndroid Build Coastguard WorkerWe use a database with a million entries. Each entry has a 16 byte 124*9507f98cSAndroid Build Coastguard Workerkey, and a 100 byte value. Values used by the benchmark compress to 125*9507f98cSAndroid Build Coastguard Workerabout half their original size. 126*9507f98cSAndroid Build Coastguard Worker 127*9507f98cSAndroid Build Coastguard Worker LevelDB: version 1.1 128*9507f98cSAndroid Build Coastguard Worker Date: Sun May 1 12:11:26 2011 129*9507f98cSAndroid Build Coastguard Worker CPU: 4 x Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz 130*9507f98cSAndroid Build Coastguard Worker CPUCache: 4096 KB 131*9507f98cSAndroid Build Coastguard Worker Keys: 16 bytes each 132*9507f98cSAndroid Build Coastguard Worker Values: 100 bytes each (50 bytes after compression) 133*9507f98cSAndroid Build Coastguard Worker Entries: 1000000 134*9507f98cSAndroid Build Coastguard Worker Raw Size: 110.6 MB (estimated) 135*9507f98cSAndroid Build Coastguard Worker File Size: 62.9 MB (estimated) 136*9507f98cSAndroid Build Coastguard Worker 137*9507f98cSAndroid Build Coastguard Worker## Write performance 138*9507f98cSAndroid Build Coastguard Worker 139*9507f98cSAndroid Build Coastguard WorkerThe "fill" benchmarks create a brand new database, in either 140*9507f98cSAndroid Build Coastguard Workersequential, or random order. The "fillsync" benchmark flushes data 141*9507f98cSAndroid Build Coastguard Workerfrom the operating system to the disk after every operation; the other 142*9507f98cSAndroid Build Coastguard Workerwrite operations leave the data sitting in the operating system buffer 143*9507f98cSAndroid Build Coastguard Workercache for a while. The "overwrite" benchmark does random writes that 144*9507f98cSAndroid Build Coastguard Workerupdate existing keys in the database. 145*9507f98cSAndroid Build Coastguard Worker 146*9507f98cSAndroid Build Coastguard Worker fillseq : 1.765 micros/op; 62.7 MB/s 147*9507f98cSAndroid Build Coastguard Worker fillsync : 268.409 micros/op; 0.4 MB/s (10000 ops) 148*9507f98cSAndroid Build Coastguard Worker fillrandom : 2.460 micros/op; 45.0 MB/s 149*9507f98cSAndroid Build Coastguard Worker overwrite : 2.380 micros/op; 46.5 MB/s 150*9507f98cSAndroid Build Coastguard Worker 151*9507f98cSAndroid Build Coastguard WorkerEach "op" above corresponds to a write of a single key/value pair. 152*9507f98cSAndroid Build Coastguard WorkerI.e., a random write benchmark goes at approximately 400,000 writes per second. 153*9507f98cSAndroid Build Coastguard Worker 154*9507f98cSAndroid Build Coastguard WorkerEach "fillsync" operation costs much less (0.3 millisecond) 155*9507f98cSAndroid Build Coastguard Workerthan a disk seek (typically 10 milliseconds). We suspect that this is 156*9507f98cSAndroid Build Coastguard Workerbecause the hard disk itself is buffering the update in its memory and 157*9507f98cSAndroid Build Coastguard Workerresponding before the data has been written to the platter. This may 158*9507f98cSAndroid Build Coastguard Workeror may not be safe based on whether or not the hard disk has enough 159*9507f98cSAndroid Build Coastguard Workerpower to save its memory in the event of a power failure. 160*9507f98cSAndroid Build Coastguard Worker 161*9507f98cSAndroid Build Coastguard Worker## Read performance 162*9507f98cSAndroid Build Coastguard Worker 163*9507f98cSAndroid Build Coastguard WorkerWe list the performance of reading sequentially in both the forward 164*9507f98cSAndroid Build Coastguard Workerand reverse direction, and also the performance of a random lookup. 165*9507f98cSAndroid Build Coastguard WorkerNote that the database created by the benchmark is quite small. 166*9507f98cSAndroid Build Coastguard WorkerTherefore the report characterizes the performance of leveldb when the 167*9507f98cSAndroid Build Coastguard Workerworking set fits in memory. The cost of reading a piece of data that 168*9507f98cSAndroid Build Coastguard Workeris not present in the operating system buffer cache will be dominated 169*9507f98cSAndroid Build Coastguard Workerby the one or two disk seeks needed to fetch the data from disk. 170*9507f98cSAndroid Build Coastguard WorkerWrite performance will be mostly unaffected by whether or not the 171*9507f98cSAndroid Build Coastguard Workerworking set fits in memory. 172*9507f98cSAndroid Build Coastguard Worker 173*9507f98cSAndroid Build Coastguard Worker readrandom : 16.677 micros/op; (approximately 60,000 reads per second) 174*9507f98cSAndroid Build Coastguard Worker readseq : 0.476 micros/op; 232.3 MB/s 175*9507f98cSAndroid Build Coastguard Worker readreverse : 0.724 micros/op; 152.9 MB/s 176*9507f98cSAndroid Build Coastguard Worker 177*9507f98cSAndroid Build Coastguard WorkerLevelDB compacts its underlying storage data in the background to 178*9507f98cSAndroid Build Coastguard Workerimprove read performance. The results listed above were done 179*9507f98cSAndroid Build Coastguard Workerimmediately after a lot of random writes. The results after 180*9507f98cSAndroid Build Coastguard Workercompactions (which are usually triggered automatically) are better. 181*9507f98cSAndroid Build Coastguard Worker 182*9507f98cSAndroid Build Coastguard Worker readrandom : 11.602 micros/op; (approximately 85,000 reads per second) 183*9507f98cSAndroid Build Coastguard Worker readseq : 0.423 micros/op; 261.8 MB/s 184*9507f98cSAndroid Build Coastguard Worker readreverse : 0.663 micros/op; 166.9 MB/s 185*9507f98cSAndroid Build Coastguard Worker 186*9507f98cSAndroid Build Coastguard WorkerSome of the high cost of reads comes from repeated decompression of blocks 187*9507f98cSAndroid Build Coastguard Workerread from disk. If we supply enough cache to the leveldb so it can hold the 188*9507f98cSAndroid Build Coastguard Workeruncompressed blocks in memory, the read performance improves again: 189*9507f98cSAndroid Build Coastguard Worker 190*9507f98cSAndroid Build Coastguard Worker readrandom : 9.775 micros/op; (approximately 100,000 reads per second before compaction) 191*9507f98cSAndroid Build Coastguard Worker readrandom : 5.215 micros/op; (approximately 190,000 reads per second after compaction) 192*9507f98cSAndroid Build Coastguard Worker 193*9507f98cSAndroid Build Coastguard Worker## Repository contents 194*9507f98cSAndroid Build Coastguard Worker 195*9507f98cSAndroid Build Coastguard WorkerSee [doc/index.md](doc/index.md) for more explanation. See 196*9507f98cSAndroid Build Coastguard Worker[doc/impl.md](doc/impl.md) for a brief overview of the implementation. 197*9507f98cSAndroid Build Coastguard Worker 198*9507f98cSAndroid Build Coastguard WorkerThe public interface is in include/leveldb/*.h. Callers should not include or 199*9507f98cSAndroid Build Coastguard Workerrely on the details of any other header files in this package. Those 200*9507f98cSAndroid Build Coastguard Workerinternal APIs may be changed without warning. 201*9507f98cSAndroid Build Coastguard Worker 202*9507f98cSAndroid Build Coastguard WorkerGuide to header files: 203*9507f98cSAndroid Build Coastguard Worker 204*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/db.h**: Main interface to the DB: Start here. 205*9507f98cSAndroid Build Coastguard Worker 206*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/options.h**: Control over the behavior of an entire database, 207*9507f98cSAndroid Build Coastguard Workerand also control over the behavior of individual reads and writes. 208*9507f98cSAndroid Build Coastguard Worker 209*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/comparator.h**: Abstraction for user-specified comparison function. 210*9507f98cSAndroid Build Coastguard WorkerIf you want just bytewise comparison of keys, you can use the default 211*9507f98cSAndroid Build Coastguard Workercomparator, but clients can write their own comparator implementations if they 212*9507f98cSAndroid Build Coastguard Workerwant custom ordering (e.g. to handle different character encodings, etc.). 213*9507f98cSAndroid Build Coastguard Worker 214*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/iterator.h**: Interface for iterating over data. You can get 215*9507f98cSAndroid Build Coastguard Workeran iterator from a DB object. 216*9507f98cSAndroid Build Coastguard Worker 217*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/write_batch.h**: Interface for atomically applying multiple 218*9507f98cSAndroid Build Coastguard Workerupdates to a database. 219*9507f98cSAndroid Build Coastguard Worker 220*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/slice.h**: A simple module for maintaining a pointer and a 221*9507f98cSAndroid Build Coastguard Workerlength into some other byte array. 222*9507f98cSAndroid Build Coastguard Worker 223*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/status.h**: Status is returned from many of the public interfaces 224*9507f98cSAndroid Build Coastguard Workerand is used to report success and various kinds of errors. 225*9507f98cSAndroid Build Coastguard Worker 226*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/env.h**: 227*9507f98cSAndroid Build Coastguard WorkerAbstraction of the OS environment. A posix implementation of this interface is 228*9507f98cSAndroid Build Coastguard Workerin util/env_posix.cc. 229*9507f98cSAndroid Build Coastguard Worker 230*9507f98cSAndroid Build Coastguard Worker* **include/leveldb/table.h, include/leveldb/table_builder.h**: Lower-level modules that most 231*9507f98cSAndroid Build Coastguard Workerclients probably won't use directly. 232