├── .appveyor.yml ├── .clang-format ├── .gitignore ├── .gitmodules ├── .travis.yml ├── AUTHORS ├── CMakeLists.txt ├── CONTRIBUTING.md ├── LICENSE ├── LevelDB_README.md ├── NEWS ├── README.md ├── TODO ├── benchmarks ├── create_test_db.cpp ├── db_bench.cc ├── db_bench_sqlite3.cc ├── db_bench_tree_db.cc └── iterate_test_db.cpp ├── cmake └── leveldbConfig.cmake.in ├── db ├── autocompact_test.cc ├── builder.cc ├── builder.h ├── c.cc ├── c_test.c ├── corruption_test.cc ├── db_impl.cc ├── db_impl.h ├── db_iter.cc ├── db_iter.h ├── db_test.cc ├── dbformat.cc ├── dbformat.h ├── dbformat_test.cc ├── dumpfile.cc ├── fault_injection_test.cc ├── filename.cc ├── filename.h ├── filename_test.cc ├── leveldbutil.cc ├── log_format.h ├── log_reader.cc ├── log_reader.h ├── log_test.cc ├── log_writer.cc ├── log_writer.h ├── memtable.cc ├── memtable.h ├── recovery_test.cc ├── repair.cc ├── skiplist.h ├── skiplist_test.cc ├── snapshot.h ├── table_cache.cc ├── table_cache.h ├── version_edit.cc ├── version_edit.h ├── version_edit_test.cc ├── version_set.cc ├── version_set.h ├── version_set_test.cc ├── vlog_fetcher.cc ├── vlog_fetcher.h ├── vlog_manager.cc ├── vlog_manager.h ├── vlog_reader.cc ├── vlog_reader.h ├── vlog_writer.cc ├── vlog_writer.h ├── write_batch.cc ├── write_batch_internal.h └── write_batch_test.cc ├── doc ├── benchmark.html ├── impl.md ├── index.md ├── log_format.md └── table_format.md ├── helpers └── memenv │ ├── memenv.cc │ ├── memenv.h │ └── memenv_test.cc ├── include └── leveldb │ ├── c.h │ ├── cache.h │ ├── comparator.h │ ├── db.h │ ├── dumpfile.h │ ├── env.h │ ├── export.h │ ├── filter_policy.h │ ├── iterator.h │ ├── options.h │ ├── slice.h │ ├── status.h │ ├── table.h │ ├── table_builder.h │ └── write_batch.h ├── issues ├── issue178_test.cc ├── issue200_test.cc └── issue320_test.cc ├── pics └── wisckey_figure_1.png ├── port ├── README.md ├── port.h ├── port_config.h.in ├── port_example.h ├── port_stdcxx.h └── thread_annotations.h ├── table ├── block.cc ├── block.h ├── block_builder.cc ├── block_builder.h ├── filter_block.cc ├── filter_block.h ├── filter_block_test.cc ├── format.cc ├── format.h ├── iterator.cc ├── iterator_wrapper.h ├── merger.cc ├── merger.h ├── table.cc ├── table_builder.cc ├── table_test.cc ├── two_level_iterator.cc └── two_level_iterator.h └── util ├── arena.cc ├── arena.h ├── arena_test.cc ├── bloom.cc ├── bloom_test.cc ├── cache.cc ├── cache_test.cc ├── coding.cc ├── coding.h ├── coding_test.cc ├── comparator.cc ├── crc32c.cc ├── crc32c.h ├── crc32c_test.cc ├── env.cc ├── env_posix.cc ├── env_posix_test.cc ├── env_posix_test_helper.h ├── env_test.cc ├── env_windows.cc ├── env_windows_test.cc ├── env_windows_test_helper.h ├── filter_policy.cc ├── hash.cc ├── hash.h ├── hash_test.cc ├── histogram.cc ├── histogram.h ├── logging.cc ├── logging.h ├── logging_test.cc ├── mutexlock.h ├── no_destructor.h ├── no_destructor_test.cc ├── options.cc ├── posix_logger.h ├── random.h ├── status.cc ├── status_test.cc ├── testutil.cc ├── testutil.h └── windows_logger.h /.appveyor.yml: -------------------------------------------------------------------------------- 1 | # Build matrix / environment variables are explained on: 2 | # https://www.appveyor.com/docs/appveyor-yml/ 3 | # This file can be validated on: https://ci.appveyor.com/tools/validate-yaml 4 | 5 | version: "{build}" 6 | 7 | environment: 8 | matrix: 9 | # AppVeyor currently has no custom job name feature. 10 | # http://help.appveyor.com/discussions/questions/1623-can-i-provide-a-friendly-name-for-jobs 11 | - JOB: Visual Studio 2019 12 | APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 13 | CMAKE_GENERATOR: Visual Studio 16 2019 14 | 15 | platform: 16 | - x86 17 | - x64 18 | 19 | configuration: 20 | - RelWithDebInfo 21 | - Debug 22 | 23 | build_script: 24 | - git submodule update --init --recursive 25 | - mkdir build 26 | - cd build 27 | - if "%platform%"=="x86" (set CMAKE_GENERATOR_PLATFORM="Win32") 28 | else (set CMAKE_GENERATOR_PLATFORM="%platform%") 29 | - cmake --version 30 | - cmake .. -G "%CMAKE_GENERATOR%" -A "%CMAKE_GENERATOR_PLATFORM%" 31 | -DCMAKE_CONFIGURATION_TYPES="%CONFIGURATION%" 32 | - cmake --build . --config "%CONFIGURATION%" 33 | - cd .. 34 | 35 | test_script: 36 | - cd build && ctest --verbose --build-config "%CONFIGURATION%" && cd .. 37 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | # Run manually to reformat a file: 2 | # clang-format -i --style=file 3 | # find . -iname '*.cc' -o -iname '*.h' -o -iname '*.h.in' | xargs clang-format -i --style=file 4 | BasedOnStyle: Google 5 | DerivePointerAlignment: false 6 | 7 | # Public headers are in a different location in the internal Google repository. 8 | # Order them so that when imported to the authoritative repository they will be 9 | # in correct alphabetical order. 10 | IncludeCategories: 11 | - Regex: '^(<|"(benchmarks|db|helpers)/)' 12 | Priority: 1 13 | - Regex: '^"(leveldb)/' 14 | Priority: 2 15 | - Regex: '^(<|"(issues|port|table|third_party|util)/)' 16 | Priority: 3 17 | - Regex: '.*' 18 | Priority: 4 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Editors. 2 | *.sw* 3 | .vscode 4 | .DS_Store 5 | 6 | # Build directory. 7 | build/ 8 | out/ 9 | cmake-build-debug 10 | cmake-build-release 11 | .idea -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/googletest"] 2 | path = third_party/googletest 3 | url = https://github.com/google/googletest.git 4 | [submodule "third_party/benchmark"] 5 | path = third_party/benchmark 6 | url = https://github.com/google/benchmark 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Build matrix / environment variables are explained on: 2 | # http://about.travis-ci.org/docs/user/build-configuration/ 3 | # This file can be validated on: http://lint.travis-ci.org/ 4 | 5 | language: cpp 6 | dist: bionic 7 | osx_image: xcode12.2 8 | 9 | compiler: 10 | - gcc 11 | - clang 12 | os: 13 | - linux 14 | - osx 15 | 16 | env: 17 | - BUILD_TYPE=Debug 18 | - BUILD_TYPE=RelWithDebInfo 19 | 20 | jobs: 21 | allow_failures: 22 | # Homebrew's GCC is currently broken on XCode 11. 23 | - compiler: gcc 24 | os: osx 25 | 26 | addons: 27 | apt: 28 | sources: 29 | - sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main' 30 | key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key' 31 | - sourceline: 'ppa:ubuntu-toolchain-r/test' 32 | packages: 33 | - clang-10 34 | - cmake 35 | - gcc-10 36 | - g++-10 37 | - libgoogle-perftools-dev 38 | - libkyotocabinet-dev 39 | - libsnappy-dev 40 | - libsqlite3-dev 41 | - ninja-build 42 | homebrew: 43 | packages: 44 | - cmake 45 | - crc32c 46 | - gcc@10 47 | - gperftools 48 | - kyoto-cabinet 49 | - llvm@10 50 | - ninja 51 | - snappy 52 | - sqlite3 53 | update: true 54 | 55 | install: 56 | # The following Homebrew packages aren't linked by default, and need to be 57 | # prepended to the path explicitly. 58 | - if [ "$TRAVIS_OS_NAME" = "osx" ]; then 59 | export PATH="$(brew --prefix llvm)/bin:$PATH"; 60 | fi 61 | # /usr/bin/gcc points to an older compiler on both Linux and macOS. 62 | - if [ "$CXX" = "g++" ]; then export CXX="g++-10" CC="gcc-10"; fi 63 | # /usr/bin/clang points to an older compiler on both Linux and macOS. 64 | # 65 | # Homebrew's llvm package doesn't ship a versioned clang++ binary, so the values 66 | # below don't work on macOS. Fortunately, the path change above makes the 67 | # default values (clang and clang++) resolve to the correct compiler on macOS. 68 | - if [ "$TRAVIS_OS_NAME" = "linux" ]; then 69 | if [ "$CXX" = "clang++" ]; then export CXX="clang++-10" CC="clang-10"; fi; 70 | fi 71 | - echo ${CC} 72 | - echo ${CXX} 73 | - ${CXX} --version 74 | - cmake --version 75 | 76 | before_script: 77 | - mkdir -p build && cd build 78 | - cmake .. -G Ninja -DCMAKE_BUILD_TYPE=$BUILD_TYPE 79 | -DCMAKE_INSTALL_PREFIX=$HOME/.local 80 | - cmake --build . 81 | - cd .. 82 | 83 | script: 84 | - cd build && ctest --verbose && cd .. 85 | - "if [ -f build/db_bench ] ; then build/db_bench ; fi" 86 | - "if [ -f build/db_bench_sqlite3 ] ; then build/db_bench_sqlite3 ; fi" 87 | - "if [ -f build/db_bench_tree_db ] ; then build/db_bench_tree_db ; fi" 88 | - cd build && cmake --build . --target install 89 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | # Names should be added to this file like so: 2 | # Name or Organization 3 | 4 | Google Inc. 5 | 6 | # Initial version authors: 7 | Jeffrey Dean 8 | Sanjay Ghemawat 9 | 10 | # Partial list of contributors: 11 | Kevin Regan 12 | Johan Bilien 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We'd love to accept your code patches! However, before we can take them, we 4 | have to jump a couple of legal hurdles. 5 | 6 | ## Contributor License Agreements 7 | 8 | Please fill out either the individual or corporate Contributor License 9 | Agreement as appropriate. 10 | 11 | * If you are an individual writing original source code and you're sure you 12 | own the intellectual property, then sign an [individual CLA](https://developers.google.com/open-source/cla/individual). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate). 15 | 16 | Follow either of the two links above to access the appropriate CLA and 17 | instructions for how to sign and return it. 18 | 19 | ## Submitting a Patch 20 | 21 | 1. Sign the contributors license agreement above. 22 | 2. Decide which code you want to submit. A submission should be a set of changes 23 | that addresses one issue in the [issue tracker](https://github.com/google/leveldb/issues). 24 | Please don't mix more than one logical change per submission, because it makes 25 | the history hard to follow. If you want to make a change 26 | (e.g. add a sample or feature) that doesn't have a corresponding issue in the 27 | issue tracker, please create one. 28 | 3. **Submitting**: When you are ready to submit, send us a Pull Request. Be 29 | sure to include the issue number you fixed and the name you used to sign 30 | the CLA. 31 | 32 | ## Writing Code ## 33 | 34 | If your contribution contains code, please make sure that it follows 35 | [the style guide](https://google.github.io/styleguide/cppguide.html). 36 | Otherwise we will have to ask you to make changes, and that's no fun for anyone. 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | Release 1.2 2011-05-16 2 | ---------------------- 3 | 4 | Fixes for larger databases (tested up to one billion 100-byte entries, 5 | i.e., ~100GB). 6 | 7 | (1) Place hard limit on number of level-0 files. This fixes errors 8 | of the form "too many open files". 9 | 10 | (2) Fixed memtable management. Before the fix, a heavy write burst 11 | could cause unbounded memory usage. 12 | 13 | A fix for a logging bug where the reader would incorrectly complain 14 | about corruption. 15 | 16 | Allow public access to WriteBatch contents so that users can easily 17 | wrap a DB. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **[WiscKey](https://www.usenix.org/conference/fast16/technical-sessions/presentation/lu) is a highly SSD optimized 2 | key-value storage based on LevelDB, presented in a paper published in 14th USENIX Conference on File and Storage 3 | Technologies in Feb 2016. This is my implementation derived from LevelDB v1.23.** 4 | 5 | **[WiscKey](https://www.usenix.org/conference/fast16/technical-sessions/presentation/lu) 6 | 是一个对SSD高度优化的基于LevelDB的KV存储,发表在2016年2月第14届USENIX文件和存储技术会议上。这是我从LevelDB v1.23派生出的对WiscKey的实现。** 7 | 8 | | [LevelDB Readme file](./LevelDB_README.md)| [LevelDB描述文档](./LevelDB_README.md)| 9 | | :----| ----:| 10 | 11 | --- 12 | 13 | # Feature 14 | 15 | * Separate keys from values, only keys are stored in LSM-tree, while values are stored in value-log file (vlog). 16 | 17 | * Highly write performance optimized on SSD. When benchmarked with default configuration, Wisckey is double faster than 18 | leveldb on sequential writing and treble faster on random writing. 19 | 20 | #### LevelDB vs Wisckey 21 | 22 | * Environment. 23 | 24 | ``` 25 | LevelDB: version 1.23 26 | CPU: 8 * AMD EPYC 7K62 48-Core Processor 27 | CPUCache: 512 KB 28 | Keys: 16 bytes each 29 | Values: 100 bytes each (50 bytes after compression) 30 | Entries: 1000000 31 | RawSize: 110.6 MB (estimated) 32 | FileSize: 62.9 MB (estimated) 33 | ``` 34 | 35 | * LevelDB 36 | 37 | ``` 38 | fillseq : 1.876 micros/op; 59.0 MB/s 39 | fillsync : 4339.058 micros/op; 0.0 MB/s (1000 ops) 40 | fillrandom : 4.270 micros/op; 25.9 MB/s 41 | overwrite : 6.613 micros/op; 16.7 MB/s 42 | readrandom : 4.233 micros/op; (864322 of 1000000 found) 43 | readrandom : 2.791 micros/op; (864083 of 1000000 found) 44 | readseq : 0.159 micros/op; 696.6 MB/s 45 | readreverse : 0.371 micros/op; 298.3 MB/s 46 | compact : 1360175.000 micros/op; 47 | readrandom : 2.110 micros/op; (864105 of 1000000 found) 48 | readseq : 0.136 micros/op; 816.2 MB/s 49 | readreverse : 0.333 micros/op; 332.2 MB/s 50 | fill100K : 2191.526 micros/op; 43.5 MB/s (1000 ops) 51 | crc32c : 1.543 micros/op; 2530.9 MB/s (4K per op) 52 | ``` 53 | 54 | * WiscKey 55 | 56 | ``` 57 | fillseq : 0.999 micros/op; 110.7 MB/s 58 | fillsync : 515.485 micros/op; 0.2 MB/s (1000 ops) 59 | fillrandom : 1.323 micros/op; 83.6 MB/s 60 | overwrite : 1.845 micros/op; 60.0 MB/s 61 | readrandom : 3.420 micros/op; (864322 of 1000000 found) 62 | readrandom : 3.077 micros/op; (864083 of 1000000 found) 63 | readseq : 0.671 micros/op; 164.8 MB/s 64 | readreverse : 0.853 micros/op; 129.6 MB/s 65 | compact : 447341.000 micros/op; 66 | readrandom : 2.406 micros/op; (864105 of 1000000 found) 67 | readseq : 0.638 micros/op; 173.3 MB/s 68 | readreverse : 0.809 micros/op; 136.8 MB/s 69 | fill100K : 92.249 micros/op; 1034.0 MB/s (1000 ops) 70 | crc32c : 1.527 micros/op; 2557.8 MB/s (4K per op) 71 | ``` 72 | 73 | # 简介 74 | 75 | Wisckey的主要思路是键-值分离。在LSM树中,键是按照顺序存储的,但是值却可以被分离且乱序地管理,只要访问键可以获得键到值的映射即可。 76 | Wisckey将数据的值分离地存储在日志中(称为value-log,简称vlog),LSM树中仅保存对应值在vlog文件中的地址,在查询时先获取地址,然后再到文件中读取数据。 77 | 下图示意了键-值分离存储的设计。关系型数据库引擎在实现索引时有着相同的设计:为索引建立的B+树仅存储对应行的指针或主键,当索引覆盖无效时,通过索引的查询需要在表中进行二次查找。 78 | 79 | ![键-值分离的设计](./pics/wisckey_figure_1.png) 80 | 81 | LSM树的操作开销主要来源于Compaction,这是当LSM树某一层的SSTable过大时将数据压向下一层的操作。因为通常来说键的大小要小于值, 82 | 因此把值从LSM树中分离出来能显著减小LSM树的大小,降低Compaction的开销。这能大大提高写入数据的性能,同时更少的额外写操作(称之为写放大)有利于提升存储设备寿命, 83 | 减少性能损耗,这对于SSD尤为重要。 84 | 85 | 在查询时Wisckey需要访问vlog文件,这次额外的随机读操作会降低查询的性能。但是较之传统的存储设备,SSD有着更好的随机读性能;同时Wisckey具有更小的LSM树和强度更小的Compaction, 86 | 在更小的LSM树上进行查询耗时更少,Compaction的影响也被降低。因此Wisckey的随机查询性能并不会弱于LevelDB。然而在顺序查询的场景下,因为LevelDB中值和键在一起有序存放, 87 | 通过LevelDB的迭代器进行范围查询可以顺序地读到数据,但是Wisckey需要随机读取vlog文件,这降低了Wisckey的顺序查询速度。 -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | ss 2 | - Stats 3 | 4 | db 5 | - Maybe implement DB::BulkDeleteForRange(start_key, end_key) 6 | that would blow away files whose ranges are entirely contained 7 | within [start_key..end_key]? For Chrome, deletion of obsolete 8 | object stores, etc. can be done in the background anyway, so 9 | probably not that important. 10 | - There have been requests for MultiGet. 11 | 12 | After a range is completely deleted, what gets rid of the 13 | corresponding files if we do no future changes to that range. Make 14 | the conditions for triggering compactions fire in more situations? 15 | -------------------------------------------------------------------------------- /benchmarks/create_test_db.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by YorkDow Co on 2021/12/19. 3 | // 4 | #include 5 | 6 | #include "leveldb/db.h" 7 | #include "leveldb/write_batch.h" 8 | 9 | #include "util/random.h" 10 | 11 | // Common key prefix length. 12 | static int FLAGS_key_prefix = 16; 13 | 14 | // Number of key/values to place in database 15 | static int FLAGS_num = 1000000; 16 | 17 | static double FLAGS_compression_ratio = 0.5; 18 | 19 | leveldb::WriteOptions write_options_; 20 | 21 | leveldb::Slice RandomString(leveldb::Random* rnd, int len, std::string* dst) { 22 | dst->resize(len); 23 | for (int i = 0; i < len; i++) { 24 | (*dst)[i] = static_cast(' ' + rnd->Uniform(95)); // ' ' .. '~' 25 | } 26 | return *dst; 27 | } 28 | 29 | leveldb::Slice CompressibleString(leveldb::Random* rnd, 30 | double compressed_fraction, size_t len, 31 | std::string* dst) { 32 | int raw = static_cast(len * compressed_fraction); 33 | if (raw < 1) raw = 1; 34 | std::string raw_data; 35 | RandomString(rnd, raw, &raw_data); 36 | 37 | // Duplicate the random data until we have filled "len" bytes 38 | dst->clear(); 39 | while (dst->size() < len) { 40 | dst->append(raw_data); 41 | } 42 | dst->resize(len); 43 | return *dst; 44 | } 45 | 46 | // Helper for quickly generating random data. 47 | class RandomGenerator { 48 | private: 49 | std::string data_; 50 | int pos_; 51 | 52 | public: 53 | RandomGenerator() { 54 | // We use a limited amount of data over and over again and ensure 55 | // that it is larger than the compression window (32KB), and also 56 | // large enough to serve all typical value sizes we want to write. 57 | leveldb::Random rnd(301); 58 | std::string piece; 59 | while (data_.size() < 1048576) { 60 | // Add a short fragment that is as compressible as specified 61 | // by FLAGS_compression_ratio. 62 | CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece); 63 | data_.append(piece); 64 | } 65 | pos_ = 0; 66 | } 67 | 68 | leveldb::Slice Generate(size_t len) { 69 | if (pos_ + len > data_.size()) { 70 | pos_ = 0; 71 | assert(len < data_.size()); 72 | } 73 | pos_ += len; 74 | return leveldb::Slice(data_.data() + pos_ - len, len); 75 | } 76 | }; 77 | 78 | class KeyBuffer { 79 | public: 80 | KeyBuffer() { 81 | assert(FLAGS_key_prefix < sizeof(buffer_)); 82 | memset(buffer_, 'a', FLAGS_key_prefix); 83 | } 84 | KeyBuffer& operator=(KeyBuffer& other) = delete; 85 | KeyBuffer(KeyBuffer& other) = delete; 86 | 87 | void Set(int k) { 88 | std::snprintf(buffer_ + FLAGS_key_prefix, 89 | sizeof(buffer_) - FLAGS_key_prefix, "%016d", k); 90 | } 91 | 92 | leveldb::Slice slice() const { 93 | return leveldb::Slice(buffer_, FLAGS_key_prefix + 16); 94 | } 95 | 96 | private: 97 | char buffer_[1024]{}; 98 | }; 99 | 100 | leveldb::Random r(998244353); 101 | 102 | void DoWrite(leveldb::DB* db, bool seq) { 103 | int entries_per_batch_ = 1; 104 | int num_ = FLAGS_num; 105 | int value_size_ = 100; 106 | RandomGenerator gen; 107 | leveldb::WriteBatch batch; 108 | leveldb::Status s; 109 | int64_t bytes = 0; 110 | KeyBuffer key; 111 | for (int i = 0; i < num_; i += entries_per_batch_) { 112 | batch.Clear(); 113 | for (int j = 0; j < entries_per_batch_; j++) { 114 | const int k = seq ? i + j : r.Uniform(FLAGS_num); 115 | key.Set(k); 116 | batch.Put(key.slice(), gen.Generate(value_size_)); 117 | } 118 | s = db->Write(write_options_, &batch); 119 | if (!s.ok()) { 120 | std::fprintf(stderr, "put error: %s\n", s.ToString().c_str()); 121 | std::exit(1); 122 | } 123 | } 124 | } 125 | 126 | int main() { 127 | leveldb::DB* db; 128 | leveldb::Options options; 129 | options.create_if_missing = true; 130 | leveldb::Status status = 131 | leveldb::DB::Open(options, "/tmp/testdb", &db); 132 | assert(status.ok()); 133 | DoWrite(db, true); 134 | DoWrite(db, false); 135 | DoWrite(db, false); 136 | delete db; 137 | } -------------------------------------------------------------------------------- /benchmarks/iterate_test_db.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by YorkDow Co on 2021/12/19. 3 | // 4 | #include 5 | 6 | #include "leveldb/db.h" 7 | #include "leveldb/iterator.h" 8 | #include "leveldb/options.h" 9 | 10 | #include "util/random.h" 11 | 12 | // Number of key/values to place in database 13 | static int FLAGS_num = 1000000; 14 | 15 | void ReadSequential(leveldb::DB* db) { 16 | int reads_ = FLAGS_num; 17 | leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions()); 18 | int i = 0; 19 | int64_t bytes = 0; 20 | for (iter->SeekToFirst(); i < reads_ && iter->Valid(); iter->Next()) { 21 | // bytes += iter->key().size() + iter->value().size(); 22 | ++i; 23 | } 24 | bytes += iter->datasize(); 25 | delete iter; 26 | } 27 | 28 | int main() { 29 | leveldb::DB* db; 30 | leveldb::Options options; 31 | options.create_if_missing = true; 32 | leveldb::Status status = 33 | leveldb::DB::Open(options, "/tmp/testdb", &db); 34 | assert(status.ok()); 35 | for (int i = 0; i < 5; i++) { 36 | ReadSequential(db); 37 | } 38 | delete db; 39 | } -------------------------------------------------------------------------------- /cmake/leveldbConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The LevelDB Authors. All rights reserved. 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | @PACKAGE_INIT@ 6 | 7 | include("${CMAKE_CURRENT_LIST_DIR}/leveldbTargets.cmake") 8 | 9 | check_required_components(leveldb) -------------------------------------------------------------------------------- /db/autocompact_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "gtest/gtest.h" 6 | #include "db/db_impl.h" 7 | #include "leveldb/cache.h" 8 | #include "leveldb/db.h" 9 | #include "util/testutil.h" 10 | 11 | namespace leveldb { 12 | 13 | class AutoCompactTest : public testing::Test { 14 | public: 15 | AutoCompactTest() { 16 | dbname_ = testing::TempDir() + "autocompact_test"; 17 | tiny_cache_ = NewLRUCache(100); 18 | options_.block_cache = tiny_cache_; 19 | DestroyDB(dbname_, options_); 20 | options_.create_if_missing = true; 21 | options_.compression = kNoCompression; 22 | EXPECT_LEVELDB_OK(DB::Open(options_, dbname_, &db_)); 23 | } 24 | 25 | ~AutoCompactTest() { 26 | delete db_; 27 | DestroyDB(dbname_, Options()); 28 | delete tiny_cache_; 29 | } 30 | 31 | std::string Key(int i) { 32 | char buf[100]; 33 | std::snprintf(buf, sizeof(buf), "key%06d", i); 34 | return std::string(buf); 35 | } 36 | 37 | uint64_t Size(const Slice& start, const Slice& limit) { 38 | Range r(start, limit); 39 | uint64_t size; 40 | db_->GetApproximateSizes(&r, 1, &size); 41 | return size; 42 | } 43 | 44 | void DoReads(int n); 45 | 46 | private: 47 | std::string dbname_; 48 | Cache* tiny_cache_; 49 | Options options_; 50 | DB* db_; 51 | }; 52 | 53 | static const int kValueSize = 200 * 1024; 54 | static const int kTotalSize = 100 * 1024 * 1024; 55 | static const int kCount = kTotalSize / kValueSize; 56 | 57 | // Read through the first n keys repeatedly and check that they get 58 | // compacted (verified by checking the size of the key space). 59 | void AutoCompactTest::DoReads(int n) { 60 | std::string value(kValueSize, 'x'); 61 | DBImpl* dbi = reinterpret_cast(db_); 62 | 63 | // Fill database 64 | for (int i = 0; i < kCount; i++) { 65 | ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), Key(i), value)); 66 | } 67 | ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable()); 68 | 69 | // Delete everything 70 | for (int i = 0; i < kCount; i++) { 71 | ASSERT_LEVELDB_OK(db_->Delete(WriteOptions(), Key(i))); 72 | } 73 | ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable()); 74 | 75 | // Get initial measurement of the space we will be reading. 76 | const int64_t initial_size = Size(Key(0), Key(n)); 77 | const int64_t initial_other_size = Size(Key(n), Key(kCount)); 78 | 79 | // Read until size drops significantly. 80 | std::string limit_key = Key(n); 81 | for (int read = 0; true; read++) { 82 | ASSERT_LT(read, 100) << "Taking too long to compact"; 83 | Iterator* iter = db_->NewIterator(ReadOptions()); 84 | for (iter->SeekToFirst(); 85 | iter->Valid() && iter->key().ToString() < limit_key; iter->Next()) { 86 | // Drop data 87 | } 88 | delete iter; 89 | // Wait a little bit to allow any triggered compactions to complete. 90 | Env::Default()->SleepForMicroseconds(1000000); 91 | uint64_t size = Size(Key(0), Key(n)); 92 | std::fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n", read + 1, 93 | size / 1048576.0, Size(Key(n), Key(kCount)) / 1048576.0); 94 | if (size <= initial_size / 10) { 95 | break; 96 | } 97 | } 98 | 99 | // Verify that the size of the key space not touched by the reads 100 | // is pretty much unchanged. 101 | const int64_t final_other_size = Size(Key(n), Key(kCount)); 102 | ASSERT_LE(final_other_size, initial_other_size + 1048576); 103 | ASSERT_GE(final_other_size, initial_other_size / 5 - 1048576); 104 | } 105 | 106 | TEST_F(AutoCompactTest, ReadAll) { DoReads(kCount); } 107 | 108 | TEST_F(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); } 109 | 110 | } // namespace leveldb 111 | 112 | int main(int argc, char** argv) { 113 | testing::InitGoogleTest(&argc, argv); 114 | return RUN_ALL_TESTS(); 115 | } 116 | -------------------------------------------------------------------------------- /db/builder.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "db/builder.h" 6 | 7 | #include "db/dbformat.h" 8 | #include "db/filename.h" 9 | #include "db/table_cache.h" 10 | #include "db/version_edit.h" 11 | #include "leveldb/db.h" 12 | #include "leveldb/env.h" 13 | #include "leveldb/iterator.h" 14 | 15 | namespace leveldb { 16 | 17 | Status BuildTable(const std::string& dbname, Env* env, const Options& options, 18 | TableCache* table_cache, Iterator* iter, FileMetaData* meta) { 19 | Status s; 20 | meta->file_size = 0; 21 | iter->SeekToFirst(); 22 | 23 | std::string fname = TableFileName(dbname, meta->number); 24 | if (iter->Valid()) { 25 | WritableFile* file; 26 | s = env->NewWritableFile(fname, &file); 27 | if (!s.ok()) { 28 | return s; 29 | } 30 | 31 | TableBuilder* builder = new TableBuilder(options, file); 32 | meta->smallest.DecodeFrom(iter->key()); 33 | Slice key; 34 | for (; iter->Valid(); iter->Next()) { 35 | key = iter->key(); 36 | builder->Add(key, iter->value()); 37 | } 38 | if (!key.empty()) { 39 | meta->largest.DecodeFrom(key); 40 | } 41 | 42 | // Finish and check for builder errors 43 | s = builder->Finish(); 44 | if (s.ok()) { 45 | meta->file_size = builder->FileSize(); 46 | assert(meta->file_size > 0); 47 | } 48 | delete builder; 49 | 50 | // Finish and check for file errors 51 | if (s.ok()) { 52 | s = file->Sync(); 53 | } 54 | if (s.ok()) { 55 | s = file->Close(); 56 | } 57 | delete file; 58 | file = nullptr; 59 | 60 | if (s.ok()) { 61 | // Verify that the table is usable 62 | Iterator* it = table_cache->NewIterator(ReadOptions(), meta->number, 63 | meta->file_size); 64 | s = it->status(); 65 | delete it; 66 | } 67 | } 68 | 69 | // Check for input iterator errors 70 | if (!iter->status().ok()) { 71 | s = iter->status(); 72 | } 73 | 74 | if (s.ok() && meta->file_size > 0) { 75 | // Keep it 76 | } else { 77 | env->RemoveFile(fname); 78 | } 79 | return s; 80 | } 81 | 82 | } // namespace leveldb 83 | -------------------------------------------------------------------------------- /db/builder.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_BUILDER_H_ 6 | #define STORAGE_LEVELDB_DB_BUILDER_H_ 7 | 8 | #include "leveldb/status.h" 9 | 10 | namespace leveldb { 11 | 12 | struct Options; 13 | struct FileMetaData; 14 | 15 | class Env; 16 | class Iterator; 17 | class TableCache; 18 | class VersionEdit; 19 | 20 | // Build a Table file from the contents of *iter. The generated file 21 | // will be named according to meta->number. On success, the rest of 22 | // *meta will be filled with metadata about the generated table. 23 | // If no data is present in *iter, meta->file_size will be set to 24 | // zero, and no Table file will be produced. 25 | Status BuildTable(const std::string& dbname, Env* env, const Options& options, 26 | TableCache* table_cache, Iterator* iter, FileMetaData* meta); 27 | 28 | } // namespace leveldb 29 | 30 | #endif // STORAGE_LEVELDB_DB_BUILDER_H_ 31 | -------------------------------------------------------------------------------- /db/db_iter.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_DB_ITER_H_ 6 | #define STORAGE_LEVELDB_DB_DB_ITER_H_ 7 | 8 | #include "db/dbformat.h" 9 | #include 10 | #include 11 | 12 | #include "leveldb/db.h" 13 | 14 | namespace leveldb { 15 | 16 | class DBImpl; 17 | 18 | // Return a new iterator that converts internal keys (yielded by 19 | // "*internal_iter") that were live at the specified "sequence" number 20 | // into appropriate user keys. 21 | Iterator* NewDBIterator(DBImpl* db, const Comparator* user_key_comparator, 22 | Iterator* internal_iter, SequenceNumber sequence, 23 | uint32_t seed); 24 | 25 | Iterator* NewDBAddrIterator(DBImpl* db, const Comparator* user_key_comparator, 26 | Iterator* internal_iter, SequenceNumber sequence, 27 | uint32_t seed); 28 | 29 | } // namespace leveldb 30 | 31 | #endif // STORAGE_LEVELDB_DB_DB_ITER_H_ 32 | -------------------------------------------------------------------------------- /db/filename.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // File names used by DB code 6 | 7 | #ifndef STORAGE_LEVELDB_DB_FILENAME_H_ 8 | #define STORAGE_LEVELDB_DB_FILENAME_H_ 9 | 10 | #include 11 | #include 12 | 13 | #include "leveldb/slice.h" 14 | #include "leveldb/status.h" 15 | #include "port/port.h" 16 | 17 | namespace leveldb { 18 | 19 | class Env; 20 | 21 | enum FileType { 22 | kLogFile, 23 | kDBLockFile, 24 | kTableFile, 25 | kDescriptorFile, 26 | kCurrentFile, 27 | kTempFile, 28 | kInfoLogFile // Either the current one, or an old one 29 | }; 30 | 31 | // Return the name of the log file with the specified number 32 | // in the db named by "dbname". The result will be prefixed with 33 | // "dbname". 34 | std::string LogFileName(const std::string& dbname, uint64_t number); 35 | 36 | // Return the name of the sstable with the specified number 37 | // in the db named by "dbname". The result will be prefixed with 38 | // "dbname". 39 | std::string TableFileName(const std::string& dbname, uint64_t number); 40 | 41 | // Return the legacy file name for an sstable with the specified number 42 | // in the db named by "dbname". The result will be prefixed with 43 | // "dbname". 44 | std::string SSTTableFileName(const std::string& dbname, uint64_t number); 45 | 46 | // Return the name of the descriptor file for the db named by 47 | // "dbname" and the specified incarnation number. The result will be 48 | // prefixed with "dbname". 49 | std::string DescriptorFileName(const std::string& dbname, uint64_t number); 50 | 51 | // Return the name of the current file. This file contains the name 52 | // of the current manifest file. The result will be prefixed with 53 | // "dbname". 54 | std::string CurrentFileName(const std::string& dbname); 55 | 56 | // Return the name of the lock file for the db named by 57 | // "dbname". The result will be prefixed with "dbname". 58 | std::string LockFileName(const std::string& dbname); 59 | 60 | // Return the name of a temporary file owned by the db named "dbname". 61 | // The result will be prefixed with "dbname". 62 | std::string TempFileName(const std::string& dbname, uint64_t number); 63 | 64 | // Return the name of the info log file for "dbname". 65 | std::string InfoLogFileName(const std::string& dbname); 66 | 67 | // Return the name of the old info log file for "dbname". 68 | std::string OldInfoLogFileName(const std::string& dbname); 69 | 70 | // If filename is a leveldb file, store the type of the file in *type. 71 | // The number encoded in the filename is stored in *number. If the 72 | // filename was successfully parsed, returns true. Else return false. 73 | bool ParseFileName(const std::string& filename, uint64_t* number, 74 | FileType* type); 75 | 76 | // Make the CURRENT file point to the descriptor file with the 77 | // specified number. 78 | Status SetCurrentFile(Env* env, const std::string& dbname, 79 | uint64_t descriptor_number); 80 | 81 | } // namespace leveldb 82 | 83 | #endif // STORAGE_LEVELDB_DB_FILENAME_H_ 84 | -------------------------------------------------------------------------------- /db/leveldbutil.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include 6 | 7 | #include "leveldb/dumpfile.h" 8 | #include "leveldb/env.h" 9 | #include "leveldb/status.h" 10 | 11 | namespace leveldb { 12 | namespace { 13 | 14 | class StdoutPrinter : public WritableFile { 15 | public: 16 | Status Append(const Slice& data) override { 17 | fwrite(data.data(), 1, data.size(), stdout); 18 | return Status::OK(); 19 | } 20 | Status Close() override { return Status::OK(); } 21 | Status Flush() override { return Status::OK(); } 22 | Status Sync() override { return Status::OK(); } 23 | }; 24 | 25 | bool HandleDumpCommand(Env* env, char** files, int num) { 26 | StdoutPrinter printer; 27 | bool ok = true; 28 | for (int i = 0; i < num; i++) { 29 | Status s = DumpFile(env, files[i], &printer); 30 | if (!s.ok()) { 31 | std::fprintf(stderr, "%s\n", s.ToString().c_str()); 32 | ok = false; 33 | } 34 | } 35 | return ok; 36 | } 37 | 38 | } // namespace 39 | } // namespace leveldb 40 | 41 | static void Usage() { 42 | std::fprintf( 43 | stderr, 44 | "Usage: leveldbutil command...\n" 45 | " dump files... -- dump contents of specified files\n"); 46 | } 47 | 48 | int main(int argc, char** argv) { 49 | leveldb::Env* env = leveldb::Env::Default(); 50 | bool ok = true; 51 | if (argc < 2) { 52 | Usage(); 53 | ok = false; 54 | } else { 55 | std::string command = argv[1]; 56 | if (command == "dump") { 57 | ok = leveldb::HandleDumpCommand(env, argv + 2, argc - 2); 58 | } else { 59 | Usage(); 60 | ok = false; 61 | } 62 | } 63 | return (ok ? 0 : 1); 64 | } 65 | -------------------------------------------------------------------------------- /db/log_format.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // Log format information shared by reader and writer. 6 | // See ../doc/log_format.md for more detail. 7 | 8 | #ifndef STORAGE_LEVELDB_DB_LOG_FORMAT_H_ 9 | #define STORAGE_LEVELDB_DB_LOG_FORMAT_H_ 10 | 11 | namespace leveldb { 12 | namespace log { 13 | 14 | enum RecordType { 15 | // Zero is reserved for preallocated files 16 | kZeroType = 0, 17 | 18 | kFullType = 1, 19 | 20 | // For fragments 21 | kFirstType = 2, 22 | kMiddleType = 3, 23 | kLastType = 4 24 | }; 25 | static const int kMaxRecordType = kLastType; 26 | 27 | static const int kBlockSize = 32768; 28 | 29 | // Header is checksum (4 bytes), length (2 bytes), type (1 byte). 30 | static const int kHeaderSize = 4 + 2 + 1; 31 | 32 | } // namespace log 33 | } // namespace leveldb 34 | 35 | #endif // STORAGE_LEVELDB_DB_LOG_FORMAT_H_ 36 | -------------------------------------------------------------------------------- /db/log_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_LOG_READER_H_ 6 | #define STORAGE_LEVELDB_DB_LOG_READER_H_ 7 | 8 | #include 9 | 10 | #include "db/log_format.h" 11 | #include "leveldb/slice.h" 12 | #include "leveldb/status.h" 13 | 14 | namespace leveldb { 15 | 16 | class SequentialFile; 17 | 18 | namespace log { 19 | 20 | class Reader { 21 | public: 22 | // Interface for reporting errors. 23 | class Reporter { 24 | public: 25 | virtual ~Reporter(); 26 | 27 | // Some corruption was detected. "size" is the approximate number 28 | // of bytes dropped due to the corruption. 29 | virtual void Corruption(size_t bytes, const Status& status) = 0; 30 | }; 31 | 32 | // Create a reader that will return log records from "*file". 33 | // "*file" must remain live while this Reader is in use. 34 | // 35 | // If "reporter" is non-null, it is notified whenever some data is 36 | // dropped due to a detected corruption. "*reporter" must remain 37 | // live while this Reader is in use. 38 | // 39 | // If "checksum" is true, verify checksums if available. 40 | // 41 | // The Reader will start reading at the first record located at physical 42 | // position >= initial_offset within the file. 43 | Reader(SequentialFile* file, Reporter* reporter, bool checksum, 44 | uint64_t initial_offset); 45 | 46 | Reader(const Reader&) = delete; 47 | Reader& operator=(const Reader&) = delete; 48 | 49 | ~Reader(); 50 | 51 | // Read the next record into *record. Returns true if read 52 | // successfully, false if we hit end of the input. May use 53 | // "*scratch" as temporary storage. The contents filled in *record 54 | // will only be valid until the next mutating operation on this 55 | // reader or the next mutation to *scratch. 56 | bool ReadRecord(Slice* record, std::string* scratch); 57 | 58 | // Returns the physical offset of the last record returned by ReadRecord. 59 | // 60 | // Undefined before the first call to ReadRecord. 61 | uint64_t LastRecordOffset(); 62 | 63 | private: 64 | // Extend record types with the following special values 65 | enum { 66 | kEof = kMaxRecordType + 1, 67 | // Returned whenever we find an invalid physical record. 68 | // Currently there are three situations in which this happens: 69 | // * The record has an invalid CRC (ReadPhysicalRecord reports a drop) 70 | // * The record is a 0-length record (No drop is reported) 71 | // * The record is below constructor's initial_offset (No drop is reported) 72 | kBadRecord = kMaxRecordType + 2 73 | }; 74 | 75 | // Skips all blocks that are completely before "initial_offset_". 76 | // 77 | // Returns true on success. Handles reporting. 78 | bool SkipToInitialBlock(); 79 | 80 | // Return type, or one of the preceding special values 81 | unsigned int ReadPhysicalRecord(Slice* result); 82 | 83 | // Reports dropped bytes to the reporter. 84 | // buffer_ must be updated to remove the dropped bytes prior to invocation. 85 | void ReportCorruption(uint64_t bytes, const char* reason); 86 | void ReportDrop(uint64_t bytes, const Status& reason); 87 | 88 | SequentialFile* const file_; 89 | Reporter* const reporter_; 90 | bool const checksum_; 91 | char* const backing_store_; 92 | Slice buffer_; 93 | bool eof_; // Last Read() indicated EOF by returning < kBlockSize 94 | 95 | // Offset of the last record returned by ReadRecord. 96 | uint64_t last_record_offset_; 97 | // Offset of the first location past the end of buffer_. 98 | uint64_t end_of_buffer_offset_; 99 | 100 | // Offset at which to start looking for the first record to return 101 | uint64_t const initial_offset_; 102 | 103 | // True if we are resynchronizing after a seek (initial_offset_ > 0). In 104 | // particular, a run of kMiddleType and kLastType records can be silently 105 | // skipped in this mode 106 | bool resyncing_; 107 | }; 108 | 109 | } // namespace log 110 | } // namespace leveldb 111 | 112 | #endif // STORAGE_LEVELDB_DB_LOG_READER_H_ 113 | -------------------------------------------------------------------------------- /db/log_writer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "db/log_writer.h" 6 | 7 | #include 8 | 9 | #include "leveldb/env.h" 10 | #include "util/coding.h" 11 | #include "util/crc32c.h" 12 | 13 | namespace leveldb { 14 | namespace log { 15 | 16 | static void InitTypeCrc(uint32_t* type_crc) { 17 | for (int i = 0; i <= kMaxRecordType; i++) { 18 | char t = static_cast(i); 19 | type_crc[i] = crc32c::Value(&t, 1); 20 | } 21 | } 22 | 23 | Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) { 24 | InitTypeCrc(type_crc_); 25 | } 26 | 27 | Writer::Writer(WritableFile* dest, uint64_t dest_length) 28 | : dest_(dest), block_offset_(dest_length % kBlockSize) { 29 | InitTypeCrc(type_crc_); 30 | } 31 | 32 | Writer::~Writer() = default; 33 | 34 | Status Writer::AddRecord(const Slice& slice) { 35 | const char* ptr = slice.data(); 36 | size_t left = slice.size(); 37 | 38 | // Fragment the record if necessary and emit it. Note that if slice 39 | // is empty, we still want to iterate once to emit a single 40 | // zero-length record 41 | Status s; 42 | bool begin = true; 43 | do { 44 | const int leftover = kBlockSize - block_offset_; 45 | assert(leftover >= 0); 46 | if (leftover < kHeaderSize) { 47 | // Switch to a new block 48 | if (leftover > 0) { 49 | // Fill the trailer (literal below relies on kHeaderSize being 7) 50 | static_assert(kHeaderSize == 7, ""); 51 | dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover)); 52 | } 53 | block_offset_ = 0; 54 | } 55 | 56 | // Invariant: we never leave < kHeaderSize bytes in a block. 57 | assert(kBlockSize - block_offset_ - kHeaderSize >= 0); 58 | 59 | const size_t avail = kBlockSize - block_offset_ - kHeaderSize; 60 | const size_t fragment_length = (left < avail) ? left : avail; 61 | 62 | RecordType type; 63 | const bool end = (left == fragment_length); 64 | if (begin && end) { 65 | type = kFullType; 66 | } else if (begin) { 67 | type = kFirstType; 68 | } else if (end) { 69 | type = kLastType; 70 | } else { 71 | type = kMiddleType; 72 | } 73 | 74 | s = EmitPhysicalRecord(type, ptr, fragment_length); 75 | ptr += fragment_length; 76 | left -= fragment_length; 77 | begin = false; 78 | } while (s.ok() && left > 0); 79 | return s; 80 | } 81 | 82 | Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, 83 | size_t length) { 84 | assert(length <= 0xffff); // Must fit in two bytes 85 | assert(block_offset_ + kHeaderSize + length <= kBlockSize); 86 | 87 | // Format the header 88 | char buf[kHeaderSize]; 89 | buf[4] = static_cast(length & 0xff); 90 | buf[5] = static_cast(length >> 8); 91 | buf[6] = static_cast(t); 92 | 93 | // Compute the crc of the record type and the payload. 94 | uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length); 95 | crc = crc32c::Mask(crc); // Adjust for storage 96 | EncodeFixed32(buf, crc); 97 | 98 | // Write the header and the payload 99 | Status s = dest_->Append(Slice(buf, kHeaderSize)); 100 | if (s.ok()) { 101 | s = dest_->Append(Slice(ptr, length)); 102 | if (s.ok()) { 103 | s = dest_->Flush(); 104 | } 105 | } 106 | block_offset_ += kHeaderSize + length; 107 | return s; 108 | } 109 | 110 | } // namespace log 111 | } // namespace leveldb 112 | -------------------------------------------------------------------------------- /db/log_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_LOG_WRITER_H_ 6 | #define STORAGE_LEVELDB_DB_LOG_WRITER_H_ 7 | 8 | #include 9 | 10 | #include "db/log_format.h" 11 | #include "leveldb/slice.h" 12 | #include "leveldb/status.h" 13 | 14 | namespace leveldb { 15 | 16 | class WritableFile; 17 | 18 | namespace log { 19 | 20 | class Writer { 21 | public: 22 | // Create a writer that will append data to "*dest". 23 | // "*dest" must be initially empty. 24 | // "*dest" must remain live while this Writer is in use. 25 | explicit Writer(WritableFile* dest); 26 | 27 | // Create a writer that will append data to "*dest". 28 | // "*dest" must have initial length "dest_length". 29 | // "*dest" must remain live while this Writer is in use. 30 | Writer(WritableFile* dest, uint64_t dest_length); 31 | 32 | Writer(const Writer&) = delete; 33 | Writer& operator=(const Writer&) = delete; 34 | 35 | ~Writer(); 36 | 37 | Status AddRecord(const Slice& slice); 38 | 39 | private: 40 | Status EmitPhysicalRecord(RecordType type, const char* ptr, size_t length); 41 | 42 | WritableFile* dest_; 43 | int block_offset_; // Current offset in block 44 | 45 | // crc32c values for all supported record types. These are 46 | // pre-computed to reduce the overhead of computing the crc of the 47 | // record type stored in the header. 48 | uint32_t type_crc_[kMaxRecordType + 1]; 49 | }; 50 | 51 | } // namespace log 52 | } // namespace leveldb 53 | 54 | #endif // STORAGE_LEVELDB_DB_LOG_WRITER_H_ 55 | -------------------------------------------------------------------------------- /db/memtable.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_MEMTABLE_H_ 6 | #define STORAGE_LEVELDB_DB_MEMTABLE_H_ 7 | 8 | #include 9 | 10 | #include "db/dbformat.h" 11 | #include "db/skiplist.h" 12 | #include "leveldb/db.h" 13 | #include "util/arena.h" 14 | 15 | namespace leveldb { 16 | 17 | class InternalKeyComparator; 18 | class MemTableIterator; 19 | 20 | class MemTable { 21 | public: 22 | // MemTables are reference counted. The initial reference count 23 | // is zero and the caller must call Ref() at least once. 24 | explicit MemTable(const InternalKeyComparator& comparator); 25 | 26 | MemTable(const MemTable&) = delete; 27 | MemTable& operator=(const MemTable&) = delete; 28 | 29 | // Increase reference count. 30 | void Ref() { ++refs_; } 31 | 32 | // Drop reference count. Delete if no more references exist. 33 | void Unref() { 34 | --refs_; 35 | assert(refs_ >= 0); 36 | if (refs_ <= 0) { 37 | delete this; 38 | } 39 | } 40 | 41 | // Returns an estimate of the number of bytes of data in use by this 42 | // data structure. It is safe to call when MemTable is being modified. 43 | size_t ApproximateMemoryUsage(); 44 | 45 | // Return an iterator that yields the contents of the memtable. 46 | // 47 | // The caller must ensure that the underlying MemTable remains live 48 | // while the returned iterator is live. The keys returned by this 49 | // iterator are internal keys encoded by AppendInternalKey in the 50 | // db/format.{h,cc} module. 51 | Iterator* NewIterator(); 52 | 53 | // Add an entry into memtable that maps key to value at the 54 | // specified sequence number and with the specified type. 55 | // Typically value will be empty if type==kTypeDeletion. 56 | void Add(SequenceNumber seq, ValueType type, const Slice& key, 57 | const Slice& value); 58 | 59 | // If memtable contains a value for key, store it in *value and return true. 60 | // If memtable contains a deletion for key, store a NotFound() error 61 | // in *status and return true. 62 | // Else, return false. 63 | bool Get(const LookupKey& key, std::string* value, Status* s); 64 | 65 | private: 66 | friend class MemTableIterator; 67 | friend class MemTableBackwardIterator; 68 | 69 | struct KeyComparator { 70 | const InternalKeyComparator comparator; 71 | explicit KeyComparator(const InternalKeyComparator& c) : comparator(c) {} 72 | int operator()(const char* a, const char* b) const; 73 | }; 74 | 75 | typedef SkipList Table; 76 | 77 | ~MemTable(); // Private since only Unref() should be used to delete it 78 | 79 | KeyComparator comparator_; 80 | int refs_; 81 | Arena arena_; 82 | Table table_; 83 | }; 84 | 85 | } // namespace leveldb 86 | 87 | #endif // STORAGE_LEVELDB_DB_MEMTABLE_H_ 88 | -------------------------------------------------------------------------------- /db/snapshot.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_ 6 | #define STORAGE_LEVELDB_DB_SNAPSHOT_H_ 7 | 8 | #include "db/dbformat.h" 9 | #include "leveldb/db.h" 10 | 11 | namespace leveldb { 12 | 13 | class SnapshotList; 14 | 15 | // Snapshots are kept in a doubly-linked list in the DB. 16 | // Each SnapshotImpl corresponds to a particular sequence number. 17 | class SnapshotImpl : public Snapshot { 18 | public: 19 | SnapshotImpl(SequenceNumber sequence_number) 20 | : sequence_number_(sequence_number) {} 21 | 22 | SequenceNumber sequence_number() const { return sequence_number_; } 23 | 24 | private: 25 | friend class SnapshotList; 26 | 27 | // SnapshotImpl is kept in a doubly-linked circular list. The SnapshotList 28 | // implementation operates on the next/previous fields direcly. 29 | SnapshotImpl* prev_; 30 | SnapshotImpl* next_; 31 | 32 | const SequenceNumber sequence_number_; 33 | 34 | #if !defined(NDEBUG) 35 | SnapshotList* list_ = nullptr; 36 | #endif // !defined(NDEBUG) 37 | }; 38 | 39 | class SnapshotList { 40 | public: 41 | SnapshotList() : head_(0) { 42 | head_.prev_ = &head_; 43 | head_.next_ = &head_; 44 | } 45 | 46 | bool empty() const { return head_.next_ == &head_; } 47 | SnapshotImpl* oldest() const { 48 | assert(!empty()); 49 | return head_.next_; 50 | } 51 | SnapshotImpl* newest() const { 52 | assert(!empty()); 53 | return head_.prev_; 54 | } 55 | 56 | // Creates a SnapshotImpl and appends it to the end of the list. 57 | SnapshotImpl* New(SequenceNumber sequence_number) { 58 | assert(empty() || newest()->sequence_number_ <= sequence_number); 59 | 60 | SnapshotImpl* snapshot = new SnapshotImpl(sequence_number); 61 | 62 | #if !defined(NDEBUG) 63 | snapshot->list_ = this; 64 | #endif // !defined(NDEBUG) 65 | snapshot->next_ = &head_; 66 | snapshot->prev_ = head_.prev_; 67 | snapshot->prev_->next_ = snapshot; 68 | snapshot->next_->prev_ = snapshot; 69 | return snapshot; 70 | } 71 | 72 | // Removes a SnapshotImpl from this list. 73 | // 74 | // The snapshot must have been created by calling New() on this list. 75 | // 76 | // The snapshot pointer should not be const, because its memory is 77 | // deallocated. However, that would force us to change DB::ReleaseSnapshot(), 78 | // which is in the API, and currently takes a const Snapshot. 79 | void Delete(const SnapshotImpl* snapshot) { 80 | #if !defined(NDEBUG) 81 | assert(snapshot->list_ == this); 82 | #endif // !defined(NDEBUG) 83 | snapshot->prev_->next_ = snapshot->next_; 84 | snapshot->next_->prev_ = snapshot->prev_; 85 | delete snapshot; 86 | } 87 | 88 | private: 89 | // Dummy head of doubly-linked list of snapshots 90 | SnapshotImpl head_; 91 | }; 92 | 93 | } // namespace leveldb 94 | 95 | #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_ 96 | -------------------------------------------------------------------------------- /db/table_cache.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "db/table_cache.h" 6 | 7 | #include "db/filename.h" 8 | #include "leveldb/env.h" 9 | #include "leveldb/table.h" 10 | #include "util/coding.h" 11 | 12 | namespace leveldb { 13 | 14 | struct TableAndFile { 15 | RandomAccessFile* file; 16 | Table* table; 17 | }; 18 | 19 | static void DeleteEntry(const Slice& key, void* value) { 20 | TableAndFile* tf = reinterpret_cast(value); 21 | delete tf->table; 22 | delete tf->file; 23 | delete tf; 24 | } 25 | 26 | static void UnrefEntry(void* arg1, void* arg2) { 27 | Cache* cache = reinterpret_cast(arg1); 28 | Cache::Handle* h = reinterpret_cast(arg2); 29 | cache->Release(h); 30 | } 31 | 32 | TableCache::TableCache(const std::string& dbname, const Options& options, 33 | int entries) 34 | : env_(options.env), 35 | dbname_(dbname), 36 | options_(options), 37 | cache_(NewLRUCache(entries)) {} 38 | 39 | TableCache::~TableCache() { delete cache_; } 40 | 41 | Status TableCache::FindTable(uint64_t file_number, uint64_t file_size, 42 | Cache::Handle** handle) { 43 | Status s; 44 | char buf[sizeof(file_number)]; 45 | EncodeFixed64(buf, file_number); 46 | Slice key(buf, sizeof(buf)); 47 | *handle = cache_->Lookup(key); 48 | if (*handle == nullptr) { 49 | std::string fname = TableFileName(dbname_, file_number); 50 | RandomAccessFile* file = nullptr; 51 | Table* table = nullptr; 52 | s = env_->NewRandomAccessFile(fname, &file); 53 | if (!s.ok()) { 54 | std::string old_fname = SSTTableFileName(dbname_, file_number); 55 | if (env_->NewRandomAccessFile(old_fname, &file).ok()) { 56 | s = Status::OK(); 57 | } 58 | } 59 | if (s.ok()) { 60 | s = Table::Open(options_, file, file_size, &table); 61 | } 62 | 63 | if (!s.ok()) { 64 | assert(table == nullptr); 65 | delete file; 66 | // We do not cache error results so that if the error is transient, 67 | // or somebody repairs the file, we recover automatically. 68 | } else { 69 | TableAndFile* tf = new TableAndFile; 70 | tf->file = file; 71 | tf->table = table; 72 | *handle = cache_->Insert(key, tf, 1, &DeleteEntry); 73 | } 74 | } 75 | return s; 76 | } 77 | 78 | Iterator* TableCache::NewIterator(const ReadOptions& options, 79 | uint64_t file_number, uint64_t file_size, 80 | Table** tableptr) { 81 | if (tableptr != nullptr) { 82 | *tableptr = nullptr; 83 | } 84 | 85 | Cache::Handle* handle = nullptr; 86 | Status s = FindTable(file_number, file_size, &handle); 87 | if (!s.ok()) { 88 | return NewErrorIterator(s); 89 | } 90 | 91 | Table* table = reinterpret_cast(cache_->Value(handle))->table; 92 | Iterator* result = table->NewIterator(options); 93 | result->RegisterCleanup(&UnrefEntry, cache_, handle); 94 | if (tableptr != nullptr) { 95 | *tableptr = table; 96 | } 97 | return result; 98 | } 99 | 100 | Status TableCache::Get(const ReadOptions& options, uint64_t file_number, 101 | uint64_t file_size, const Slice& k, void* arg, 102 | void (*handle_result)(void*, const Slice&, 103 | const Slice&)) { 104 | Cache::Handle* handle = nullptr; 105 | Status s = FindTable(file_number, file_size, &handle); 106 | if (s.ok()) { 107 | Table* t = reinterpret_cast(cache_->Value(handle))->table; 108 | s = t->InternalGet(options, k, arg, handle_result); 109 | cache_->Release(handle); 110 | } 111 | return s; 112 | } 113 | 114 | void TableCache::Evict(uint64_t file_number) { 115 | char buf[sizeof(file_number)]; 116 | EncodeFixed64(buf, file_number); 117 | cache_->Erase(Slice(buf, sizeof(buf))); 118 | } 119 | 120 | } // namespace leveldb 121 | -------------------------------------------------------------------------------- /db/table_cache.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // Thread-safe (provides internal synchronization) 6 | 7 | #ifndef STORAGE_LEVELDB_DB_TABLE_CACHE_H_ 8 | #define STORAGE_LEVELDB_DB_TABLE_CACHE_H_ 9 | 10 | #include 11 | #include 12 | 13 | #include "db/dbformat.h" 14 | #include "leveldb/cache.h" 15 | #include "leveldb/table.h" 16 | #include "port/port.h" 17 | 18 | namespace leveldb { 19 | 20 | class Env; 21 | 22 | class TableCache { 23 | public: 24 | TableCache(const std::string& dbname, const Options& options, int entries); 25 | ~TableCache(); 26 | 27 | // Return an iterator for the specified file number (the corresponding 28 | // file length must be exactly "file_size" bytes). If "tableptr" is 29 | // non-null, also sets "*tableptr" to point to the Table object 30 | // underlying the returned iterator, or to nullptr if no Table object 31 | // underlies the returned iterator. The returned "*tableptr" object is owned 32 | // by the cache and should not be deleted, and is valid for as long as the 33 | // returned iterator is live. 34 | Iterator* NewIterator(const ReadOptions& options, uint64_t file_number, 35 | uint64_t file_size, Table** tableptr = nullptr); 36 | 37 | // If a seek to internal key "k" in specified file finds an entry, 38 | // call (*handle_result)(arg, found_key, found_value). 39 | Status Get(const ReadOptions& options, uint64_t file_number, 40 | uint64_t file_size, const Slice& k, void* arg, 41 | void (*handle_result)(void*, const Slice&, const Slice&)); 42 | 43 | // Evict any entry for the specified file number 44 | void Evict(uint64_t file_number); 45 | 46 | private: 47 | Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle**); 48 | 49 | Env* const env_; 50 | const std::string dbname_; 51 | const Options& options_; 52 | Cache* cache_; 53 | }; 54 | 55 | } // namespace leveldb 56 | 57 | #endif // STORAGE_LEVELDB_DB_TABLE_CACHE_H_ 58 | -------------------------------------------------------------------------------- /db/version_edit.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_VERSION_EDIT_H_ 6 | #define STORAGE_LEVELDB_DB_VERSION_EDIT_H_ 7 | 8 | #include "db/dbformat.h" 9 | #include 10 | #include 11 | #include 12 | 13 | namespace leveldb { 14 | 15 | class VersionSet; 16 | 17 | struct FileMetaData { 18 | FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0) {} 19 | 20 | int refs; 21 | int allowed_seeks; // Seeks allowed until compaction 22 | uint64_t number; 23 | uint64_t file_size; // File size in bytes 24 | InternalKey smallest; // Smallest internal key served by table 25 | InternalKey largest; // Largest internal key served by table 26 | }; 27 | 28 | class VersionEdit { 29 | public: 30 | VersionEdit() { Clear(); } 31 | ~VersionEdit() = default; 32 | 33 | void Clear(); 34 | 35 | void SetComparatorName(const Slice& name) { 36 | has_comparator_ = true; 37 | comparator_ = name.ToString(); 38 | } 39 | void SetLogNumber(uint64_t num) { 40 | has_log_number_ = true; 41 | log_number_ = num; 42 | } 43 | void SetVlogHeadPos(uint64_t head) { 44 | has_head_info_ = true; 45 | head_info_ = head; 46 | } 47 | void SetVlogTailPos(uint64_t num, uint64_t tail) { 48 | has_tail_info_ = true; 49 | tail_info_ = tail; 50 | tail_vlog_number_ = num; 51 | } 52 | void SetVlogInfo(const std::string& vlog_info) { 53 | vlog_info_ = vlog_info; 54 | has_vlog_info_ = true; 55 | } 56 | void SetPrevLogNumber(uint64_t num) { 57 | has_prev_log_number_ = true; 58 | prev_log_number_ = num; 59 | } 60 | void SetNextFile(uint64_t num) { 61 | has_next_file_number_ = true; 62 | next_file_number_ = num; 63 | } 64 | void SetLastSequence(SequenceNumber seq) { 65 | has_last_sequence_ = true; 66 | last_sequence_ = seq; 67 | } 68 | void SetCompactPointer(int level, const InternalKey& key) { 69 | compact_pointers_.push_back(std::make_pair(level, key)); 70 | } 71 | 72 | // Add the specified file at the specified number. 73 | // REQUIRES: This version has not been saved (see VersionSet::SaveTo) 74 | // REQUIRES: "smallest" and "largest" are smallest and largest keys in file 75 | void AddFile(int level, uint64_t file, uint64_t file_size, 76 | const InternalKey& smallest, const InternalKey& largest) { 77 | FileMetaData f; 78 | f.number = file; 79 | f.file_size = file_size; 80 | f.smallest = smallest; 81 | f.largest = largest; 82 | new_files_.push_back(std::make_pair(level, f)); 83 | } 84 | 85 | // Delete the specified "file" from the specified "level". 86 | void RemoveFile(int level, uint64_t file) { 87 | deleted_files_.insert(std::make_pair(level, file)); 88 | } 89 | 90 | void EncodeTo(std::string* dst) const; 91 | Status DecodeFrom(const Slice& src); 92 | 93 | std::string DebugString() const; 94 | 95 | private: 96 | friend class VersionSet; 97 | 98 | typedef std::set> DeletedFileSet; 99 | 100 | std::string comparator_; 101 | uint64_t log_number_; 102 | uint64_t prev_log_number_; 103 | uint64_t next_file_number_; 104 | SequenceNumber last_sequence_; 105 | bool has_comparator_; 106 | bool has_log_number_; 107 | bool has_prev_log_number_; 108 | bool has_next_file_number_; 109 | bool has_last_sequence_; 110 | 111 | bool has_head_info_; 112 | bool has_tail_info_; 113 | 114 | // head of the vlog. 115 | uint64_t head_info_; 116 | 117 | // tail of teh vlog. During garbage collection, WiscKey first reads a chunk of 118 | // key-value pairs (e.g., several MBs) from the tail of the vLog, then finds 119 | // which of those values are valid (not yet overwritten or deleted) by 120 | // querying the LSM-tree. WiscKey then appends valid values back to the head 121 | // of the vLog. 122 | uint64_t tail_info_; 123 | 124 | // We should store the number of the vlog file where the tail position is 125 | // located. 126 | uint64_t tail_vlog_number_; 127 | 128 | bool has_vlog_info_; 129 | std::string vlog_info_; 130 | 131 | std::vector> compact_pointers_; 132 | DeletedFileSet deleted_files_; 133 | std::vector> new_files_; 134 | }; 135 | 136 | } // namespace leveldb 137 | 138 | #endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_ 139 | -------------------------------------------------------------------------------- /db/version_edit_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "db/version_edit.h" 6 | 7 | #include "gtest/gtest.h" 8 | 9 | namespace leveldb { 10 | 11 | static void TestEncodeDecode(const VersionEdit& edit) { 12 | std::string encoded, encoded2; 13 | edit.EncodeTo(&encoded); 14 | VersionEdit parsed; 15 | Status s = parsed.DecodeFrom(encoded); 16 | ASSERT_TRUE(s.ok()) << s.ToString(); 17 | parsed.EncodeTo(&encoded2); 18 | ASSERT_EQ(encoded, encoded2); 19 | } 20 | 21 | TEST(VersionEditTest, EncodeDecode) { 22 | static const uint64_t kBig = 1ull << 50; 23 | 24 | VersionEdit edit; 25 | for (int i = 0; i < 4; i++) { 26 | TestEncodeDecode(edit); 27 | edit.AddFile(3, kBig + 300 + i, kBig + 400 + i, 28 | InternalKey("foo", kBig + 500 + i, kTypeValue), 29 | InternalKey("zoo", kBig + 600 + i, kTypeDeletion)); 30 | edit.RemoveFile(4, kBig + 700 + i); 31 | edit.SetCompactPointer(i, InternalKey("x", kBig + 900 + i, kTypeValue)); 32 | } 33 | 34 | edit.SetComparatorName("foo"); 35 | edit.SetLogNumber(kBig + 100); 36 | edit.SetNextFile(kBig + 200); 37 | edit.SetLastSequence(kBig + 1000); 38 | TestEncodeDecode(edit); 39 | } 40 | 41 | } // namespace leveldb 42 | 43 | int main(int argc, char** argv) { 44 | testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /db/vlog_fetcher.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "vlog_fetcher.h" 3 | 4 | #include 5 | 6 | #include "filename.h" 7 | 8 | namespace leveldb { 9 | namespace vlog { 10 | 11 | inline Status Parse(Slice* r, std::string* value) { 12 | Slice k, v; 13 | assert((*r)[0] == kTypeValue); 14 | r->remove_prefix(1); 15 | if (GetLengthPrefixedSlice(r, &k) && GetLengthPrefixedSlice(r, &v)) { 16 | value->assign(v.data(), v.size()); 17 | return Status::OK(); 18 | } else { 19 | return Status::Corruption("failed to decode value from vlog"); 20 | } 21 | } 22 | 23 | Status VlogFetcher::Get(const uint64_t offset, const uint64_t size, 24 | std::string* value) { 25 | const char* scratch; 26 | Slice result; 27 | Status s; 28 | 29 | // It seems that additional cache is useless for the cost of insert is 30 | // remarkable. 31 | 32 | char buf[1 << 16]; 33 | bool need_deallocate = false; 34 | bool in_buffer = false; 35 | 36 | my_info_->rwlock_->SharedLock(); 37 | if (offset >= my_info_->head_) { 38 | assert(offset - my_info_->head_ < my_info_->size_); 39 | scratch = &my_info_->buffer_[offset - my_info_->head_]; 40 | result = Slice(scratch, size); 41 | s = Parse(&result, value); 42 | in_buffer = true; 43 | } 44 | my_info_->rwlock_->SharedUnlock(); 45 | 46 | if (!in_buffer) { 47 | if (size <= (1 << 16)) { 48 | scratch = buf; 49 | } else { 50 | scratch = new char[size]; 51 | need_deallocate = true; 52 | } 53 | file_->Read(offset, size, &result, const_cast(scratch)); 54 | s = Parse(&result, value); 55 | } 56 | 57 | if (need_deallocate) { 58 | delete[] scratch; 59 | } 60 | 61 | return s; 62 | } 63 | 64 | VlogFetcher::VlogFetcher(const std::string& dbname, const Options& options, 65 | const uint32_t log_number) { 66 | Status s = options.env->NewNonMmapRandomAccessFile( 67 | LogFileName(dbname, log_number), &file_); 68 | assert(s.ok()); 69 | } 70 | 71 | VlogFetcher::~VlogFetcher() { 72 | delete file_; 73 | } 74 | 75 | } // namespace vlog 76 | } // namespace leveldb -------------------------------------------------------------------------------- /db/vlog_fetcher.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef STORAGE_LEVELDB_DB_VLOG_CACHE_H_ 3 | #define STORAGE_LEVELDB_DB_VLOG_CACHE_H_ 4 | 5 | #include "db/dbformat.h" 6 | #include "db/vlog_manager.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "leveldb/cache.h" 15 | #include "leveldb/table.h" 16 | 17 | #include "port/port.h" 18 | 19 | #include "vlog_manager.h" 20 | 21 | namespace leveldb { 22 | namespace vlog { 23 | 24 | class VlogInfo; 25 | class VlogManager; 26 | 27 | class VlogFetcher { 28 | public: 29 | VlogFetcher(const std::string& dbname, const Options& options, 30 | uint32_t log_number); 31 | 32 | ~VlogFetcher(); 33 | 34 | Status Get(uint64_t offset, uint64_t size, std::string* value); 35 | 36 | friend class VlogManager; 37 | 38 | private: 39 | VlogInfo* my_info_; 40 | 41 | RandomAccessFile* file_; 42 | }; 43 | } // namespace vlog 44 | } // namespace leveldb 45 | 46 | #endif // STORAGE_LEVELDB_DB_VLOG_CACHE_H_ -------------------------------------------------------------------------------- /db/vlog_manager.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "db/vlog_manager.h" 3 | 4 | #include "db/vlog_reader.h" 5 | 6 | #include "util/coding.h" 7 | 8 | #include "filename.h" 9 | 10 | namespace leveldb { 11 | namespace vlog { 12 | 13 | VlogManager::VlogManager(uint64_t clean_threshold) 14 | : clean_threshold_(clean_threshold), cur_vlog_(0) {} 15 | 16 | VlogManager::~VlogManager() { 17 | for (auto& it : manager_) { 18 | if (it.first == cur_vlog_) { 19 | it.second->vlog_write_->dest_->SyncedAppend( 20 | Slice(it.second->buffer_, it.second->size_)); 21 | } 22 | delete it.second->vlog_fetch_; 23 | delete it.second->vlog_write_->dest_; 24 | delete it.second->vlog_write_; 25 | delete it.second; 26 | } 27 | } 28 | 29 | void VlogManager::AddVlog(const std::string& dbname, const Options& options, 30 | uint64_t vlog_numb) { 31 | VlogInfo* old = manager_[vlog_numb]; 32 | if (old != nullptr) { 33 | old->vlog_write_->dest_->SyncedAppend(Slice(old->buffer_, old->size_)); 34 | } 35 | VlogInfo* v = new VlogInfo; 36 | v->vlog_write_ = new VWriter; 37 | Status s = options.env->NewAppendableFile(LogFileName(dbname, vlog_numb), 38 | &v->vlog_write_->dest_); 39 | assert(s.ok()); 40 | // VlogFetcher must initialize after WritableFile is created; 41 | v->vlog_fetch_ = new VlogFetcher(dbname, options, vlog_numb); 42 | v->vlog_write_->my_info_ = v; 43 | v->vlog_fetch_->my_info_ = v; 44 | v->count_ = 0; 45 | manager_[vlog_numb] = v; 46 | cur_vlog_ = vlog_numb; 47 | } 48 | 49 | void VlogManager::SetCurrentVlog(uint64_t vlog_numb) { cur_vlog_ = vlog_numb; } 50 | 51 | Status VlogManager::FetchValueFromVlog(Slice addr, std::string* value) { 52 | Status s; 53 | uint64_t file_numb, offset, size; 54 | // address is 55 | if (!GetVarint64(&addr, &file_numb)) 56 | return Status::Corruption("parse size false in RealValue"); 57 | if (!GetVarint64(&addr, &offset)) 58 | return Status::Corruption("parse file_numb false in RealValue"); 59 | if (!GetVarint64(&addr, &size)) 60 | return Status::Corruption("parse pos false in RealValue"); 61 | 62 | std::map::const_iterator iter = manager_.find(file_numb); 63 | if (iter == manager_.end() || iter->second->vlog_fetch_ == nullptr) { 64 | s = Status::Corruption("can not find vlog"); 65 | } else { 66 | VlogFetcher* cache = iter->second->vlog_fetch_; 67 | s = cache->Get(offset, size, value); 68 | } 69 | 70 | return s; 71 | } 72 | Status VlogManager::AddRecord(const Slice& slice) { 73 | std::map::const_iterator iter = manager_.find(cur_vlog_); 74 | assert(iter != manager_.end()); 75 | assert(iter->second != nullptr); 76 | return iter->second->vlog_write_->AddRecord(slice); 77 | } 78 | Status VlogManager::Sync() { 79 | std::map::const_iterator iter = manager_.find(cur_vlog_); 80 | assert(iter != manager_.end()); 81 | assert(iter->second != nullptr); 82 | return iter->second->vlog_write_->dest_->Sync(); 83 | } 84 | 85 | Status VlogManager::SetHead(size_t offset) { 86 | std::map::const_iterator iter = manager_.find(cur_vlog_); 87 | if (iter == manager_.end() || iter->second->vlog_fetch_ == nullptr) { 88 | return Status::Corruption("can not find vlog"); 89 | } else { 90 | iter->second->head_ = offset; 91 | return Status::OK(); 92 | } 93 | } 94 | 95 | } // namespace vlog 96 | } // namespace leveldb 97 | -------------------------------------------------------------------------------- /db/vlog_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef STORAGE_LEVELDB_DB_VLOG_MANAGER_H_ 2 | #define STORAGE_LEVELDB_DB_VLOG_MANAGER_H_ 3 | 4 | #include "db/vlog_fetcher.h" 5 | #include "db/vlog_reader.h" 6 | #include "db/vlog_writer.h" 7 | #include 8 | #include 9 | #include 10 | 11 | #include "port/port_stdcxx.h" 12 | 13 | namespace leveldb { 14 | namespace vlog { 15 | // Header is checksum (4 bytes), length (8 bytes). 16 | static const int kVHeaderSize = 4 + 8; 17 | 18 | static const int WriteBufferSize = 1 << 12; 19 | 20 | class VlogFetcher; 21 | class VWriter; 22 | 23 | class VlogInfo { 24 | char buffer_[WriteBufferSize]; 25 | size_t size_; 26 | VlogFetcher* vlog_fetch_; 27 | VWriter* vlog_write_; 28 | size_t head_; 29 | 30 | uint64_t count_; //代表该vlog文件垃圾kv的数量 31 | 32 | port::SharedMutex* rwlock_; 33 | 34 | public: 35 | VlogInfo() : size_(0), head_(0), rwlock_(new port::SpinSharedMutex) {} 36 | ~VlogInfo() { delete rwlock_; } 37 | 38 | friend class VWriter; 39 | friend class VlogFetcher; 40 | friend class VlogManager; 41 | }; 42 | 43 | class VlogManager { 44 | public: 45 | explicit VlogManager(uint64_t clean_threshold); 46 | ~VlogManager(); 47 | 48 | void AddVlog(const std::string& dbname, const Options& options, 49 | uint64_t vlog_numb); 50 | 51 | Status AddRecord(const Slice& slice); 52 | 53 | Status SetHead(size_t offset); 54 | 55 | Status Sync(); 56 | 57 | Status FetchValueFromVlog(Slice addr, std::string* value); 58 | 59 | void SetCurrentVlog(uint64_t vlog_numb); 60 | 61 | private: 62 | std::map manager_; 63 | std::set cleaning_vlog_set_; 64 | uint64_t clean_threshold_; 65 | uint64_t cur_vlog_; 66 | }; 67 | 68 | } // namespace vlog 69 | } // namespace leveldb 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /db/vlog_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_VLOG_READER_H_ 6 | #define STORAGE_LEVELDB_DB_VLOG_READER_H_ 7 | 8 | #include "db/log_format.h" 9 | #include 10 | 11 | #include "leveldb/slice.h" 12 | #include "leveldb/status.h" 13 | 14 | #include "port/port.h" 15 | 16 | namespace leveldb { 17 | 18 | class SequentialFile; 19 | 20 | namespace vlog { 21 | class VReader { 22 | public: 23 | class Reporter { 24 | public: 25 | virtual ~Reporter(); 26 | 27 | // Some corruption was detected. "size" is the approximate number 28 | // of bytes dropped due to the corruption. 29 | virtual void Corruption(size_t bytes, const Status& status) = 0; 30 | }; 31 | 32 | VReader(SequentialFile* file, Reporter* reporter, bool checksum, 33 | uint64_t initial_offset = 0); 34 | 35 | ~VReader(); 36 | 37 | bool ReadRecord(Slice* record, std::string* scratch); 38 | bool DeallocateDiskSpace(uint64_t offset, size_t len); 39 | 40 | private: 41 | port::Mutex mutex_; 42 | SequentialFile* const file_; 43 | Reporter* const reporter_; 44 | bool const checksum_; 45 | char* const backing_store_; 46 | Slice buffer_; 47 | bool eof_; // Last Read() indicated EOF by returning < 48 | // Reports dropped bytes to the reporter. 49 | // buffer_ must be updated to remove the dropped bytes prior to invocation. 50 | void ReportCorruption(uint64_t bytes, const char* reason); 51 | void ReportDrop(uint64_t bytes, const Status& reason); 52 | // No copying allowed 53 | VReader(const VReader&); 54 | void operator=(const VReader&); 55 | }; 56 | 57 | } // namespace vlog 58 | } // namespace leveldb 59 | 60 | #endif // STORAGE_LEVELDB_DB_LOG_READER_H_ 61 | -------------------------------------------------------------------------------- /db/vlog_writer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "db/vlog_writer.h" 6 | 7 | #include "db/vlog_manager.h" 8 | #include 9 | 10 | #include "leveldb/env.h" 11 | 12 | #include "util/coding.h" 13 | #include "util/crc32c.h" 14 | 15 | #include "dbformat.h" 16 | 17 | namespace leveldb { 18 | namespace vlog { 19 | 20 | VWriter::VWriter(WritableFile* dest) : dest_(dest) {} 21 | 22 | VWriter::~VWriter() = default; 23 | 24 | Status VWriter::AddRecord(const Slice& slice) { 25 | const char* ptr = slice.data(); 26 | size_t left = slice.size(); 27 | char head[kVHeaderSize]; 28 | uint32_t crc = crc32c::Extend(0, ptr, left); 29 | crc = crc32c::Mask(crc); // Adjust for storage 30 | EncodeFixed32(head, crc); 31 | EncodeFixed64(&head[4], left); 32 | 33 | Status s; 34 | 35 | if (my_info_->size_ + kVHeaderSize + left > WriteBufferSize) { 36 | WLock l(my_info_->rwlock_); 37 | if (!(s = dest_->SyncedAppend(Slice(my_info_->buffer_, my_info_->size_))) 38 | .ok()) { 39 | return s; 40 | } 41 | my_info_->head_ += my_info_->size_; 42 | my_info_->size_ = 0; 43 | if (kVHeaderSize + left > WriteBufferSize) { 44 | s = dest_->SyncedAppend(Slice(head, kVHeaderSize)); 45 | my_info_->head_ += kVHeaderSize; 46 | s = dest_->SyncedAppend(Slice(ptr, left)); 47 | my_info_->head_ += left; 48 | } else { 49 | memcpy(my_info_->buffer_ + my_info_->size_, head, kVHeaderSize); 50 | my_info_->size_ += kVHeaderSize; 51 | memcpy(my_info_->buffer_ + my_info_->size_, ptr, left); 52 | my_info_->size_ += left; 53 | } 54 | return s; 55 | } 56 | 57 | memcpy(my_info_->buffer_ + my_info_->size_, head, kVHeaderSize); 58 | my_info_->size_ += kVHeaderSize; 59 | memcpy(my_info_->buffer_ + my_info_->size_, ptr, left); 60 | my_info_->size_ += left; 61 | 62 | return s; 63 | } 64 | 65 | } // namespace vlog 66 | } // namespace leveldb 67 | -------------------------------------------------------------------------------- /db/vlog_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_VLOG_WRITER_H_ 6 | #define STORAGE_LEVELDB_DB_VLOG_WRITER_H_ 7 | 8 | #include "db/log_format.h" 9 | #include "db/vlog_manager.h" 10 | #include 11 | 12 | #include "leveldb/slice.h" 13 | #include "leveldb/status.h" 14 | 15 | namespace leveldb { 16 | 17 | class WritableFile; 18 | class VlogInfo; 19 | class VlogManager; 20 | 21 | namespace vlog { 22 | 23 | class VWriter { 24 | public: 25 | // Create a writer that will append data to "*dest". 26 | // "*dest" must be initially empty. 27 | // "*dest" must remain live while this Writer is in use. 28 | explicit VWriter(WritableFile* dest); 29 | 30 | ~VWriter(); 31 | 32 | Status AddRecord(const Slice& slice); 33 | 34 | friend class VlogManager; 35 | 36 | private: 37 | VWriter() = default; 38 | 39 | VlogInfo* my_info_; 40 | WritableFile* dest_; 41 | // No copying allowed 42 | VWriter(const VWriter&); 43 | void operator=(const VWriter&); 44 | }; 45 | 46 | } // namespace vlog 47 | } // namespace leveldb 48 | 49 | #endif // STORAGE_LEVELDB_DB_VLOG_WRITER_H_ 50 | -------------------------------------------------------------------------------- /db/write_batch_internal.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_ 6 | #define STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_ 7 | 8 | #include "db/dbformat.h" 9 | 10 | #include "leveldb/write_batch.h" 11 | 12 | namespace leveldb { 13 | 14 | class MemTable; 15 | 16 | // WriteBatchInternal provides static methods for manipulating a 17 | // WriteBatch that we don't want in the public WriteBatch interface. 18 | class WriteBatchInternal { 19 | public: 20 | // Return the number of entries in the batch. 21 | static int Count(const WriteBatch* batch); 22 | 23 | // Set the count for the number of entries in the batch. 24 | static void SetCount(WriteBatch* batch, int n); 25 | 26 | // Return the sequence number for the start of this batch. 27 | static SequenceNumber Sequence(const WriteBatch* batch); 28 | 29 | // Store the specified number as the sequence number for the start of 30 | // this batch. 31 | static void SetSequence(WriteBatch* batch, SequenceNumber seq); 32 | 33 | static Slice Contents(const WriteBatch* batch) { return Slice(batch->rep_); } 34 | 35 | static size_t ByteSize(const WriteBatch* batch) { return batch->rep_.size(); } 36 | 37 | static void SetContents(WriteBatch* batch, const Slice& contents); 38 | 39 | static Status InsertInto(const WriteBatch* batch, MemTable* memtable); 40 | 41 | static Status InsertAddressInto(const WriteBatch* batch, uint64_t vlog_number, 42 | MemTable* memTable, size_t* vlog_head); 43 | 44 | static void Append(WriteBatch* dst, const WriteBatch* src); 45 | }; 46 | 47 | } // namespace leveldb 48 | 49 | #endif // STORAGE_LEVELDB_DB_WRITE_BATCH_INTERNAL_H_ 50 | -------------------------------------------------------------------------------- /db/write_batch_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "gtest/gtest.h" 6 | #include "db/memtable.h" 7 | #include "db/write_batch_internal.h" 8 | #include "leveldb/db.h" 9 | #include "leveldb/env.h" 10 | #include "util/logging.h" 11 | 12 | namespace leveldb { 13 | 14 | static std::string PrintContents(WriteBatch* b) { 15 | InternalKeyComparator cmp(BytewiseComparator()); 16 | MemTable* mem = new MemTable(cmp); 17 | mem->Ref(); 18 | std::string state; 19 | Status s = WriteBatchInternal::InsertInto(b, mem); 20 | int count = 0; 21 | Iterator* iter = mem->NewIterator(); 22 | for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 23 | ParsedInternalKey ikey; 24 | EXPECT_TRUE(ParseInternalKey(iter->key(), &ikey)); 25 | switch (ikey.type) { 26 | case kTypeValue: 27 | state.append("Put("); 28 | state.append(ikey.user_key.ToString()); 29 | state.append(", "); 30 | state.append(iter->value().ToString()); 31 | state.append(")"); 32 | count++; 33 | break; 34 | case kTypeDeletion: 35 | state.append("Delete("); 36 | state.append(ikey.user_key.ToString()); 37 | state.append(")"); 38 | count++; 39 | break; 40 | } 41 | state.append("@"); 42 | state.append(NumberToString(ikey.sequence)); 43 | } 44 | delete iter; 45 | if (!s.ok()) { 46 | state.append("ParseError()"); 47 | } else if (count != WriteBatchInternal::Count(b)) { 48 | state.append("CountMismatch()"); 49 | } 50 | mem->Unref(); 51 | return state; 52 | } 53 | 54 | TEST(WriteBatchTest, Empty) { 55 | WriteBatch batch; 56 | ASSERT_EQ("", PrintContents(&batch)); 57 | ASSERT_EQ(0, WriteBatchInternal::Count(&batch)); 58 | } 59 | 60 | TEST(WriteBatchTest, Multiple) { 61 | WriteBatch batch; 62 | batch.Put(Slice("foo"), Slice("bar")); 63 | batch.Delete(Slice("box")); 64 | batch.Put(Slice("baz"), Slice("boo")); 65 | WriteBatchInternal::SetSequence(&batch, 100); 66 | ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch)); 67 | ASSERT_EQ(3, WriteBatchInternal::Count(&batch)); 68 | ASSERT_EQ( 69 | "Put(baz, boo)@102" 70 | "Delete(box)@101" 71 | "Put(foo, bar)@100", 72 | PrintContents(&batch)); 73 | } 74 | 75 | TEST(WriteBatchTest, Corruption) { 76 | WriteBatch batch; 77 | batch.Put(Slice("foo"), Slice("bar")); 78 | batch.Delete(Slice("box")); 79 | WriteBatchInternal::SetSequence(&batch, 200); 80 | Slice contents = WriteBatchInternal::Contents(&batch); 81 | WriteBatchInternal::SetContents(&batch, 82 | Slice(contents.data(), contents.size() - 1)); 83 | ASSERT_EQ( 84 | "Put(foo, bar)@200" 85 | "ParseError()", 86 | PrintContents(&batch)); 87 | } 88 | 89 | TEST(WriteBatchTest, Append) { 90 | WriteBatch b1, b2; 91 | WriteBatchInternal::SetSequence(&b1, 200); 92 | WriteBatchInternal::SetSequence(&b2, 300); 93 | b1.Append(b2); 94 | ASSERT_EQ("", PrintContents(&b1)); 95 | b2.Put("a", "va"); 96 | b1.Append(b2); 97 | ASSERT_EQ("Put(a, va)@200", PrintContents(&b1)); 98 | b2.Clear(); 99 | b2.Put("b", "vb"); 100 | b1.Append(b2); 101 | ASSERT_EQ( 102 | "Put(a, va)@200" 103 | "Put(b, vb)@201", 104 | PrintContents(&b1)); 105 | b2.Delete("foo"); 106 | b1.Append(b2); 107 | ASSERT_EQ( 108 | "Put(a, va)@200" 109 | "Put(b, vb)@202" 110 | "Put(b, vb)@201" 111 | "Delete(foo)@203", 112 | PrintContents(&b1)); 113 | } 114 | 115 | TEST(WriteBatchTest, ApproximateSize) { 116 | WriteBatch batch; 117 | size_t empty_size = batch.ApproximateSize(); 118 | 119 | batch.Put(Slice("foo"), Slice("bar")); 120 | size_t one_key_size = batch.ApproximateSize(); 121 | ASSERT_LT(empty_size, one_key_size); 122 | 123 | batch.Put(Slice("baz"), Slice("boo")); 124 | size_t two_keys_size = batch.ApproximateSize(); 125 | ASSERT_LT(one_key_size, two_keys_size); 126 | 127 | batch.Delete(Slice("box")); 128 | size_t post_delete_size = batch.ApproximateSize(); 129 | ASSERT_LT(two_keys_size, post_delete_size); 130 | } 131 | 132 | } // namespace leveldb 133 | 134 | int main(int argc, char** argv) { 135 | testing::InitGoogleTest(&argc, argv); 136 | return RUN_ALL_TESTS(); 137 | } 138 | -------------------------------------------------------------------------------- /doc/log_format.md: -------------------------------------------------------------------------------- 1 | leveldb Log format 2 | ================== 3 | The log file contents are a sequence of 32KB blocks. The only exception is that 4 | the tail of the file may contain a partial block. 5 | 6 | Each block consists of a sequence of records: 7 | 8 | block := record* trailer? 9 | record := 10 | checksum: uint32 // crc32c of type and data[] ; little-endian 11 | length: uint16 // little-endian 12 | type: uint8 // One of FULL, FIRST, MIDDLE, LAST 13 | data: uint8[length] 14 | 15 | A record never starts within the last six bytes of a block (since it won't fit). 16 | Any leftover bytes here form the trailer, which must consist entirely of zero 17 | bytes and must be skipped by readers. 18 | 19 | Aside: if exactly seven bytes are left in the current block, and a new non-zero 20 | length record is added, the writer must emit a FIRST record (which contains zero 21 | bytes of user data) to fill up the trailing seven bytes of the block and then 22 | emit all of the user data in subsequent blocks. 23 | 24 | More types may be added in the future. Some Readers may skip record types they 25 | do not understand, others may report that some data was skipped. 26 | 27 | FULL == 1 28 | FIRST == 2 29 | MIDDLE == 3 30 | LAST == 4 31 | 32 | The FULL record contains the contents of an entire user record. 33 | 34 | FIRST, MIDDLE, LAST are types used for user records that have been split into 35 | multiple fragments (typically because of block boundaries). FIRST is the type 36 | of the first fragment of a user record, LAST is the type of the last fragment of 37 | a user record, and MIDDLE is the type of all interior fragments of a user 38 | record. 39 | 40 | Example: consider a sequence of user records: 41 | 42 | A: length 1000 43 | B: length 97270 44 | C: length 8000 45 | 46 | **A** will be stored as a FULL record in the first block. 47 | 48 | **B** will be split into three fragments: first fragment occupies the rest of 49 | the first block, second fragment occupies the entirety of the second block, and 50 | the third fragment occupies a prefix of the third block. This will leave six 51 | bytes free in the third block, which will be left empty as the trailer. 52 | 53 | **C** will be stored as a FULL record in the fourth block. 54 | 55 | ---- 56 | 57 | ## Some benefits over the recordio format: 58 | 59 | 1. We do not need any heuristics for resyncing - just go to next block boundary 60 | and scan. If there is a corruption, skip to the next block. As a 61 | side-benefit, we do not get confused when part of the contents of one log 62 | file are embedded as a record inside another log file. 63 | 64 | 2. Splitting at approximate boundaries (e.g., for mapreduce) is simple: find the 65 | next block boundary and skip records until we hit a FULL or FIRST record. 66 | 67 | 3. We do not need extra buffering for large records. 68 | 69 | ## Some downsides compared to recordio format: 70 | 71 | 1. No packing of tiny records. This could be fixed by adding a new record type, 72 | so it is a shortcoming of the current implementation, not necessarily the 73 | format. 74 | 75 | 2. No compression. Again, this could be fixed by adding new record types. 76 | -------------------------------------------------------------------------------- /doc/table_format.md: -------------------------------------------------------------------------------- 1 | leveldb File format 2 | =================== 3 | 4 | 5 | [data block 1] 6 | [data block 2] 7 | ... 8 | [data block N] 9 | [meta block 1] 10 | ... 11 | [meta block K] 12 | [metaindex block] 13 | [index block] 14 | [Footer] (fixed size; starts at file_size - sizeof(Footer)) 15 | 16 | 17 | The file contains internal pointers. Each such pointer is called 18 | a BlockHandle and contains the following information: 19 | 20 | offset: varint64 21 | size: varint64 22 | 23 | See [varints](https://developers.google.com/protocol-buffers/docs/encoding#varints) 24 | for an explanation of varint64 format. 25 | 26 | 1. The sequence of key/value pairs in the file are stored in sorted 27 | order and partitioned into a sequence of data blocks. These blocks 28 | come one after another at the beginning of the file. Each data block 29 | is formatted according to the code in `block_builder.cc`, and then 30 | optionally compressed. 31 | 32 | 2. After the data blocks we store a bunch of meta blocks. The 33 | supported meta block types are described below. More meta block types 34 | may be added in the future. Each meta block is again formatted using 35 | `block_builder.cc` and then optionally compressed. 36 | 37 | 3. A "metaindex" block. It contains one entry for every other meta 38 | block where the key is the name of the meta block and the value is a 39 | BlockHandle pointing to that meta block. 40 | 41 | 4. An "index" block. This block contains one entry per data block, 42 | where the key is a string >= last key in that data block and before 43 | the first key in the successive data block. The value is the 44 | BlockHandle for the data block. 45 | 46 | 5. At the very end of the file is a fixed length footer that contains 47 | the BlockHandle of the metaindex and index blocks as well as a magic number. 48 | 49 | metaindex_handle: char[p]; // Block handle for metaindex 50 | index_handle: char[q]; // Block handle for index 51 | padding: char[40-p-q];// zeroed bytes to make fixed length 52 | // (40==2*BlockHandle::kMaxEncodedLength) 53 | magic: fixed64; // == 0xdb4775248b80fb57 (little-endian) 54 | 55 | ## "filter" Meta Block 56 | 57 | If a `FilterPolicy` was specified when the database was opened, a 58 | filter block is stored in each table. The "metaindex" block contains 59 | an entry that maps from `filter.` to the BlockHandle for the filter 60 | block where `` is the string returned by the filter policy's 61 | `Name()` method. 62 | 63 | The filter block stores a sequence of filters, where filter i contains 64 | the output of `FilterPolicy::CreateFilter()` on all keys that are stored 65 | in a block whose file offset falls within the range 66 | 67 | [ i*base ... (i+1)*base-1 ] 68 | 69 | Currently, "base" is 2KB. So for example, if blocks X and Y start in 70 | the range `[ 0KB .. 2KB-1 ]`, all of the keys in X and Y will be 71 | converted to a filter by calling `FilterPolicy::CreateFilter()`, and the 72 | resulting filter will be stored as the first filter in the filter 73 | block. 74 | 75 | The filter block is formatted as follows: 76 | 77 | [filter 0] 78 | [filter 1] 79 | [filter 2] 80 | ... 81 | [filter N-1] 82 | 83 | [offset of filter 0] : 4 bytes 84 | [offset of filter 1] : 4 bytes 85 | [offset of filter 2] : 4 bytes 86 | ... 87 | [offset of filter N-1] : 4 bytes 88 | 89 | [offset of beginning of offset array] : 4 bytes 90 | lg(base) : 1 byte 91 | 92 | The offset array at the end of the filter block allows efficient 93 | mapping from a data block offset to the corresponding filter. 94 | 95 | ## "stats" Meta Block 96 | 97 | This meta block contains a bunch of stats. The key is the name 98 | of the statistic. The value contains the statistic. 99 | 100 | TODO(postrelease): record following stats. 101 | 102 | data size 103 | index size 104 | key size (uncompressed) 105 | value size (uncompressed) 106 | number of entries 107 | number of data blocks 108 | -------------------------------------------------------------------------------- /helpers/memenv/memenv.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_ 6 | #define STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_ 7 | 8 | #include "leveldb/export.h" 9 | 10 | namespace leveldb { 11 | 12 | class Env; 13 | 14 | // Returns a new environment that stores its data in memory and delegates 15 | // all non-file-storage tasks to base_env. The caller must delete the result 16 | // when it is no longer needed. 17 | // *base_env must remain live while the result is in use. 18 | LEVELDB_EXPORT Env* NewMemEnv(Env* base_env); 19 | 20 | } // namespace leveldb 21 | 22 | #endif // STORAGE_LEVELDB_HELPERS_MEMENV_MEMENV_H_ 23 | -------------------------------------------------------------------------------- /include/leveldb/comparator.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_ 6 | #define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_ 7 | 8 | #include 9 | 10 | #include "leveldb/export.h" 11 | 12 | namespace leveldb { 13 | 14 | class Slice; 15 | 16 | // A Comparator object provides a total order across slices that are 17 | // used as keys in an sstable or a database. A Comparator implementation 18 | // must be thread-safe since leveldb may invoke its methods concurrently 19 | // from multiple threads. 20 | class LEVELDB_EXPORT Comparator { 21 | public: 22 | virtual ~Comparator(); 23 | 24 | // Three-way comparison. Returns value: 25 | // < 0 iff "a" < "b", 26 | // == 0 iff "a" == "b", 27 | // > 0 iff "a" > "b" 28 | virtual int Compare(const Slice& a, const Slice& b) const = 0; 29 | 30 | // The name of the comparator. Used to check for comparator 31 | // mismatches (i.e., a DB created with one comparator is 32 | // accessed using a different comparator. 33 | // 34 | // The client of this package should switch to a new name whenever 35 | // the comparator implementation changes in a way that will cause 36 | // the relative ordering of any two keys to change. 37 | // 38 | // Names starting with "leveldb." are reserved and should not be used 39 | // by any clients of this package. 40 | virtual const char* Name() const = 0; 41 | 42 | // Advanced functions: these are used to reduce the space requirements 43 | // for internal data structures like index blocks. 44 | 45 | // If *start < limit, changes *start to a short string in [start,limit). 46 | // Simple comparator implementations may return with *start unchanged, 47 | // i.e., an implementation of this method that does nothing is correct. 48 | virtual void FindShortestSeparator(std::string* start, 49 | const Slice& limit) const = 0; 50 | 51 | // Changes *key to a short string >= *key. 52 | // Simple comparator implementations may return with *key unchanged, 53 | // i.e., an implementation of this method that does nothing is correct. 54 | virtual void FindShortSuccessor(std::string* key) const = 0; 55 | }; 56 | 57 | // Return a builtin comparator that uses lexicographic byte-wise 58 | // ordering. The result remains the property of this module and 59 | // must not be deleted. 60 | LEVELDB_EXPORT const Comparator* BytewiseComparator(); 61 | 62 | } // namespace leveldb 63 | 64 | #endif // STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_ 65 | -------------------------------------------------------------------------------- /include/leveldb/dumpfile.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_ 6 | #define STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_ 7 | 8 | #include 9 | 10 | #include "leveldb/env.h" 11 | #include "leveldb/export.h" 12 | #include "leveldb/status.h" 13 | 14 | namespace leveldb { 15 | 16 | // Dump the contents of the file named by fname in text format to 17 | // *dst. Makes a sequence of dst->Append() calls; each call is passed 18 | // the newline-terminated text corresponding to a single item found 19 | // in the file. 20 | // 21 | // Returns a non-OK result if fname does not name a leveldb storage 22 | // file, or if the file cannot be read. 23 | LEVELDB_EXPORT Status DumpFile(Env* env, const std::string& fname, 24 | WritableFile* dst); 25 | 26 | } // namespace leveldb 27 | 28 | #endif // STORAGE_LEVELDB_INCLUDE_DUMPFILE_H_ 29 | -------------------------------------------------------------------------------- /include/leveldb/export.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_INCLUDE_EXPORT_H_ 6 | #define STORAGE_LEVELDB_INCLUDE_EXPORT_H_ 7 | 8 | #if !defined(LEVELDB_EXPORT) 9 | 10 | #if defined(LEVELDB_SHARED_LIBRARY) 11 | #if defined(_WIN32) 12 | 13 | #if defined(LEVELDB_COMPILE_LIBRARY) 14 | #define LEVELDB_EXPORT __declspec(dllexport) 15 | #else 16 | #define LEVELDB_EXPORT __declspec(dllimport) 17 | #endif // defined(LEVELDB_COMPILE_LIBRARY) 18 | 19 | #else // defined(_WIN32) 20 | #if defined(LEVELDB_COMPILE_LIBRARY) 21 | #define LEVELDB_EXPORT __attribute__((visibility("default"))) 22 | #else 23 | #define LEVELDB_EXPORT 24 | #endif 25 | #endif // defined(_WIN32) 26 | 27 | #else // defined(LEVELDB_SHARED_LIBRARY) 28 | #define LEVELDB_EXPORT 29 | #endif 30 | 31 | #endif // !defined(LEVELDB_EXPORT) 32 | 33 | #endif // STORAGE_LEVELDB_INCLUDE_EXPORT_H_ 34 | -------------------------------------------------------------------------------- /include/leveldb/filter_policy.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // A database can be configured with a custom FilterPolicy object. 6 | // This object is responsible for creating a small filter from a set 7 | // of keys. These filters are stored in leveldb and are consulted 8 | // automatically by leveldb to decide whether or not to read some 9 | // information from disk. In many cases, a filter can cut down the 10 | // number of disk seeks form a handful to a single disk seek per 11 | // DB::Get() call. 12 | // 13 | // Most people will want to use the builtin bloom filter support (see 14 | // NewBloomFilterPolicy() below). 15 | 16 | #ifndef STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_ 17 | #define STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_ 18 | 19 | #include 20 | 21 | #include "leveldb/export.h" 22 | 23 | namespace leveldb { 24 | 25 | class Slice; 26 | 27 | class LEVELDB_EXPORT FilterPolicy { 28 | public: 29 | virtual ~FilterPolicy(); 30 | 31 | // Return the name of this policy. Note that if the filter encoding 32 | // changes in an incompatible way, the name returned by this method 33 | // must be changed. Otherwise, old incompatible filters may be 34 | // passed to methods of this type. 35 | virtual const char* Name() const = 0; 36 | 37 | // keys[0,n-1] contains a list of keys (potentially with duplicates) 38 | // that are ordered according to the user supplied comparator. 39 | // Append a filter that summarizes keys[0,n-1] to *dst. 40 | // 41 | // Warning: do not change the initial contents of *dst. Instead, 42 | // append the newly constructed filter to *dst. 43 | virtual void CreateFilter(const Slice* keys, int n, 44 | std::string* dst) const = 0; 45 | 46 | // "filter" contains the data appended by a preceding call to 47 | // CreateFilter() on this class. This method must return true if 48 | // the key was in the list of keys passed to CreateFilter(). 49 | // This method may return true or false if the key was not on the 50 | // list, but it should aim to return false with a high probability. 51 | virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const = 0; 52 | }; 53 | 54 | // Return a new filter policy that uses a bloom filter with approximately 55 | // the specified number of bits per key. A good value for bits_per_key 56 | // is 10, which yields a filter with ~ 1% false positive rate. 57 | // 58 | // Callers must delete the result after any database that is using the 59 | // result has been closed. 60 | // 61 | // Note: if you are using a custom comparator that ignores some parts 62 | // of the keys being compared, you must not use NewBloomFilterPolicy() 63 | // and must provide your own FilterPolicy that also ignores the 64 | // corresponding parts of the keys. For example, if the comparator 65 | // ignores trailing spaces, it would be incorrect to use a 66 | // FilterPolicy (like NewBloomFilterPolicy) that does not ignore 67 | // trailing spaces in keys. 68 | LEVELDB_EXPORT const FilterPolicy* NewBloomFilterPolicy(int bits_per_key); 69 | 70 | } // namespace leveldb 71 | 72 | #endif // STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_ 73 | -------------------------------------------------------------------------------- /include/leveldb/iterator.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // An iterator yields a sequence of key/value pairs from a source. 6 | // The following class defines the interface. Multiple implementations 7 | // are provided by this library. In particular, iterators are provided 8 | // to access the contents of a Table or a DB. 9 | // 10 | // Multiple threads can invoke const methods on an Iterator without 11 | // external synchronization, but if any of the threads may call a 12 | // non-const method, all threads accessing the same Iterator must use 13 | // external synchronization. 14 | 15 | #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_ 16 | #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_ 17 | 18 | #include "leveldb/export.h" 19 | #include "leveldb/slice.h" 20 | #include "leveldb/status.h" 21 | 22 | namespace leveldb { 23 | 24 | class LEVELDB_EXPORT Iterator { 25 | public: 26 | Iterator(); 27 | 28 | Iterator(const Iterator&) = delete; 29 | Iterator& operator=(const Iterator&) = delete; 30 | 31 | virtual ~Iterator(); 32 | 33 | virtual uint64_t datasize() const { return 0; } 34 | 35 | // An iterator is either positioned at a key/value pair, or 36 | // not valid. This method returns true iff the iterator is valid. 37 | virtual bool Valid() const = 0; 38 | 39 | // Position at the first key in the source. The iterator is Valid() 40 | // after this call iff the source is not empty. 41 | virtual void SeekToFirst() = 0; 42 | 43 | // Position at the last key in the source. The iterator is 44 | // Valid() after this call iff the source is not empty. 45 | virtual void SeekToLast() = 0; 46 | 47 | // Position at the first key in the source that is at or past target. 48 | // The iterator is Valid() after this call iff the source contains 49 | // an entry that comes at or past target. 50 | virtual void Seek(const Slice& target) = 0; 51 | 52 | // Moves to the next entry in the source. After this call, Valid() is 53 | // true iff the iterator was not positioned at the last entry in the source. 54 | // REQUIRES: Valid() 55 | virtual void Next() = 0; 56 | 57 | // Moves to the previous entry in the source. After this call, Valid() is 58 | // true iff the iterator was not positioned at the first entry in source. 59 | // REQUIRES: Valid() 60 | virtual void Prev() = 0; 61 | 62 | // Return the key for the current entry. The underlying storage for 63 | // the returned slice is valid only until the next modification of 64 | // the iterator. 65 | // REQUIRES: Valid() 66 | virtual Slice key() const = 0; 67 | 68 | // Return the value for the current entry. The underlying storage for 69 | // the returned slice is valid only until the next modification of 70 | // the iterator. 71 | // REQUIRES: Valid() 72 | virtual Slice value() const = 0; 73 | 74 | // If an error has occurred, return it. Else return an ok status. 75 | virtual Status status() const = 0; 76 | 77 | // Clients are allowed to register function/arg1/arg2 triples that 78 | // will be invoked when this iterator is destroyed. 79 | // 80 | // Note that unlike all of the preceding methods, this method is 81 | // not abstract and therefore clients should not override it. 82 | using CleanupFunction = void (*)(void* arg1, void* arg2); 83 | void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2); 84 | 85 | private: 86 | // Cleanup functions are stored in a single-linked list. 87 | // The list's head node is inlined in the iterator. 88 | struct CleanupNode { 89 | // True if the node is not used. Only head nodes might be unused. 90 | bool IsEmpty() const { return function == nullptr; } 91 | // Invokes the cleanup function. 92 | void Run() { 93 | assert(function != nullptr); 94 | (*function)(arg1, arg2); 95 | } 96 | 97 | // The head node is used if the function pointer is not null. 98 | CleanupFunction function; 99 | void* arg1; 100 | void* arg2; 101 | CleanupNode* next; 102 | }; 103 | CleanupNode cleanup_head_; 104 | }; 105 | 106 | // Return an empty iterator (yields nothing). 107 | LEVELDB_EXPORT Iterator* NewEmptyIterator(); 108 | 109 | // Return an empty iterator with the specified status. 110 | LEVELDB_EXPORT Iterator* NewErrorIterator(const Status& status); 111 | 112 | } // namespace leveldb 113 | 114 | #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_ 115 | -------------------------------------------------------------------------------- /include/leveldb/slice.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // Slice is a simple structure containing a pointer into some external 6 | // storage and a size. The user of a Slice must ensure that the slice 7 | // is not used after the corresponding external storage has been 8 | // deallocated. 9 | // 10 | // Multiple threads can invoke const methods on a Slice without 11 | // external synchronization, but if any of the threads may call a 12 | // non-const method, all threads accessing the same Slice must use 13 | // external synchronization. 14 | 15 | #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_ 16 | #define STORAGE_LEVELDB_INCLUDE_SLICE_H_ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "leveldb/export.h" 24 | 25 | namespace leveldb { 26 | 27 | class LEVELDB_EXPORT Slice { 28 | public: 29 | // Create an empty slice. 30 | Slice() : data_(""), size_(0) {} 31 | 32 | // Create a slice that refers to d[0,n-1]. 33 | Slice(const char* d, size_t n) : data_(d), size_(n) {} 34 | 35 | // Create a slice that refers to the contents of "s" 36 | Slice(const std::string& s) : data_(s.data()), size_(s.size()) {} 37 | 38 | // Create a slice that refers to s[0,strlen(s)-1] 39 | Slice(const char* s) : data_(s), size_(strlen(s)) {} 40 | 41 | // Intentionally copyable. 42 | Slice(const Slice&) = default; 43 | Slice& operator=(const Slice&) = default; 44 | 45 | // Return a pointer to the beginning of the referenced data 46 | const char* data() const { return data_; } 47 | 48 | // Return the length (in bytes) of the referenced data 49 | size_t size() const { return size_; } 50 | 51 | // Return true iff the length of the referenced data is zero 52 | bool empty() const { return size_ == 0; } 53 | 54 | // Return the ith byte in the referenced data. 55 | // REQUIRES: n < size() 56 | char operator[](size_t n) const { 57 | assert(n < size()); 58 | return data_[n]; 59 | } 60 | 61 | // Change this slice to refer to an empty array 62 | void clear() { 63 | data_ = ""; 64 | size_ = 0; 65 | } 66 | 67 | // Drop the first "n" bytes from this slice. 68 | void remove_prefix(size_t n) { 69 | assert(n <= size()); 70 | data_ += n; 71 | size_ -= n; 72 | } 73 | 74 | // Return a string that contains the copy of the referenced data. 75 | std::string ToString() const { return std::string(data_, size_); } 76 | 77 | // Three-way comparison. Returns value: 78 | // < 0 iff "*this" < "b", 79 | // == 0 iff "*this" == "b", 80 | // > 0 iff "*this" > "b" 81 | int compare(const Slice& b) const; 82 | 83 | // Return true iff "x" is a prefix of "*this" 84 | bool starts_with(const Slice& x) const { 85 | return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0)); 86 | } 87 | 88 | private: 89 | const char* data_; 90 | size_t size_; 91 | }; 92 | 93 | inline bool operator==(const Slice& x, const Slice& y) { 94 | return ((x.size() == y.size()) && 95 | (memcmp(x.data(), y.data(), x.size()) == 0)); 96 | } 97 | 98 | inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); } 99 | 100 | inline int Slice::compare(const Slice& b) const { 101 | const size_t min_len = (size_ < b.size_) ? size_ : b.size_; 102 | int r = memcmp(data_, b.data_, min_len); 103 | if (r == 0) { 104 | if (size_ < b.size_) 105 | r = -1; 106 | else if (size_ > b.size_) 107 | r = +1; 108 | } 109 | return r; 110 | } 111 | 112 | } // namespace leveldb 113 | 114 | #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_ 115 | -------------------------------------------------------------------------------- /include/leveldb/status.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // A Status encapsulates the result of an operation. It may indicate success, 6 | // or it may indicate an error with an associated error message. 7 | // 8 | // Multiple threads can invoke const methods on a Status without 9 | // external synchronization, but if any of the threads may call a 10 | // non-const method, all threads accessing the same Status must use 11 | // external synchronization. 12 | 13 | #ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_ 14 | #define STORAGE_LEVELDB_INCLUDE_STATUS_H_ 15 | 16 | #include 17 | #include 18 | 19 | #include "leveldb/export.h" 20 | #include "leveldb/slice.h" 21 | 22 | namespace leveldb { 23 | 24 | class LEVELDB_EXPORT Status { 25 | public: 26 | // Create a success status. 27 | Status() noexcept : state_(nullptr) {} 28 | ~Status() { delete[] state_; } 29 | 30 | Status(const Status& rhs); 31 | Status& operator=(const Status& rhs); 32 | 33 | Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; } 34 | Status& operator=(Status&& rhs) noexcept; 35 | 36 | // Return a success status. 37 | static Status OK() { return Status(); } 38 | 39 | // Return error status of an appropriate type. 40 | static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) { 41 | return Status(kNotFound, msg, msg2); 42 | } 43 | static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) { 44 | return Status(kCorruption, msg, msg2); 45 | } 46 | static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) { 47 | return Status(kNotSupported, msg, msg2); 48 | } 49 | static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) { 50 | return Status(kInvalidArgument, msg, msg2); 51 | } 52 | static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) { 53 | return Status(kIOError, msg, msg2); 54 | } 55 | 56 | // Returns true iff the status indicates success. 57 | bool ok() const { return (state_ == nullptr); } 58 | 59 | // Returns true iff the status indicates a NotFound error. 60 | bool IsNotFound() const { return code() == kNotFound; } 61 | 62 | // Returns true iff the status indicates a Corruption error. 63 | bool IsCorruption() const { return code() == kCorruption; } 64 | 65 | // Returns true iff the status indicates an IOError. 66 | bool IsIOError() const { return code() == kIOError; } 67 | 68 | // Returns true iff the status indicates a NotSupportedError. 69 | bool IsNotSupportedError() const { return code() == kNotSupported; } 70 | 71 | // Returns true iff the status indicates an InvalidArgument. 72 | bool IsInvalidArgument() const { return code() == kInvalidArgument; } 73 | 74 | // Return a string representation of this status suitable for printing. 75 | // Returns the string "OK" for success. 76 | std::string ToString() const; 77 | 78 | private: 79 | enum Code { 80 | kOk = 0, 81 | kNotFound = 1, 82 | kCorruption = 2, 83 | kNotSupported = 3, 84 | kInvalidArgument = 4, 85 | kIOError = 5 86 | }; 87 | 88 | Code code() const { 89 | return (state_ == nullptr) ? kOk : static_cast(state_[4]); 90 | } 91 | 92 | Status(Code code, const Slice& msg, const Slice& msg2); 93 | static const char* CopyState(const char* s); 94 | 95 | // OK status has a null state_. Otherwise, state_ is a new[] array 96 | // of the following form: 97 | // state_[0..3] == length of message 98 | // state_[4] == code 99 | // state_[5..] == message 100 | const char* state_; 101 | }; 102 | 103 | inline Status::Status(const Status& rhs) { 104 | state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_); 105 | } 106 | inline Status& Status::operator=(const Status& rhs) { 107 | // The following condition catches both aliasing (when this == &rhs), 108 | // and the common case where both rhs and *this are ok. 109 | if (state_ != rhs.state_) { 110 | delete[] state_; 111 | state_ = (rhs.state_ == nullptr) ? nullptr : CopyState(rhs.state_); 112 | } 113 | return *this; 114 | } 115 | inline Status& Status::operator=(Status&& rhs) noexcept { 116 | std::swap(state_, rhs.state_); 117 | return *this; 118 | } 119 | 120 | } // namespace leveldb 121 | 122 | #endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_ 123 | -------------------------------------------------------------------------------- /include/leveldb/table.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_ 6 | #define STORAGE_LEVELDB_INCLUDE_TABLE_H_ 7 | 8 | #include 9 | 10 | #include "leveldb/export.h" 11 | #include "leveldb/iterator.h" 12 | 13 | namespace leveldb { 14 | 15 | class Block; 16 | class BlockHandle; 17 | class Footer; 18 | struct Options; 19 | class RandomAccessFile; 20 | struct ReadOptions; 21 | class TableCache; 22 | 23 | // A Table is a sorted map from strings to strings. Tables are 24 | // immutable and persistent. A Table may be safely accessed from 25 | // multiple threads without external synchronization. 26 | class LEVELDB_EXPORT Table { 27 | public: 28 | // Attempt to open the table that is stored in bytes [0..file_size) 29 | // of "file", and read the metadata entries necessary to allow 30 | // retrieving data from the table. 31 | // 32 | // If successful, returns ok and sets "*table" to the newly opened 33 | // table. The client should delete "*table" when no longer needed. 34 | // If there was an error while initializing the table, sets "*table" 35 | // to nullptr and returns a non-ok status. Does not take ownership of 36 | // "*source", but the client must ensure that "source" remains live 37 | // for the duration of the returned table's lifetime. 38 | // 39 | // *file must remain live while this Table is in use. 40 | static Status Open(const Options& options, RandomAccessFile* file, 41 | uint64_t file_size, Table** table); 42 | 43 | Table(const Table&) = delete; 44 | Table& operator=(const Table&) = delete; 45 | 46 | ~Table(); 47 | 48 | // Returns a new iterator over the table contents. 49 | // The result of NewIterator() is initially invalid (caller must 50 | // call one of the Seek methods on the iterator before using it). 51 | Iterator* NewIterator(const ReadOptions&) const; 52 | 53 | // Given a key, return an approximate byte offset in the file where 54 | // the data for that key begins (or would begin if the key were 55 | // present in the file). The returned value is in terms of file 56 | // bytes, and so includes effects like compression of the underlying data. 57 | // E.g., the approximate offset of the last key in the table will 58 | // be close to the file length. 59 | uint64_t ApproximateOffsetOf(const Slice& key) const; 60 | 61 | private: 62 | friend class TableCache; 63 | struct Rep; 64 | 65 | static Iterator* BlockReader(void*, const ReadOptions&, const Slice&); 66 | 67 | explicit Table(Rep* rep) : rep_(rep) {} 68 | 69 | // Calls (*handle_result)(arg, ...) with the entry found after a call 70 | // to Seek(key). May not make such a call if filter policy says 71 | // that key is not present. 72 | Status InternalGet(const ReadOptions&, const Slice& key, void* arg, 73 | void (*handle_result)(void* arg, const Slice& k, 74 | const Slice& v)); 75 | 76 | void ReadMeta(const Footer& footer); 77 | void ReadFilter(const Slice& filter_handle_value); 78 | 79 | Rep* const rep_; 80 | }; 81 | 82 | } // namespace leveldb 83 | 84 | #endif // STORAGE_LEVELDB_INCLUDE_TABLE_H_ 85 | -------------------------------------------------------------------------------- /include/leveldb/table_builder.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // TableBuilder provides the interface used to build a Table 6 | // (an immutable and sorted map from keys to values). 7 | // 8 | // Multiple threads can invoke const methods on a TableBuilder without 9 | // external synchronization, but if any of the threads may call a 10 | // non-const method, all threads accessing the same TableBuilder must use 11 | // external synchronization. 12 | 13 | #ifndef STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_ 14 | #define STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_ 15 | 16 | #include 17 | 18 | #include "leveldb/export.h" 19 | #include "leveldb/options.h" 20 | #include "leveldb/status.h" 21 | 22 | namespace leveldb { 23 | 24 | class BlockBuilder; 25 | class BlockHandle; 26 | class WritableFile; 27 | 28 | class LEVELDB_EXPORT TableBuilder { 29 | public: 30 | // Create a builder that will store the contents of the table it is 31 | // building in *file. Does not close the file. It is up to the 32 | // caller to close the file after calling Finish(). 33 | TableBuilder(const Options& options, WritableFile* file); 34 | 35 | TableBuilder(const TableBuilder&) = delete; 36 | TableBuilder& operator=(const TableBuilder&) = delete; 37 | 38 | // REQUIRES: Either Finish() or Abandon() has been called. 39 | ~TableBuilder(); 40 | 41 | // Change the options used by this builder. Note: only some of the 42 | // option fields can be changed after construction. If a field is 43 | // not allowed to change dynamically and its value in the structure 44 | // passed to the constructor is different from its value in the 45 | // structure passed to this method, this method will return an error 46 | // without changing any fields. 47 | Status ChangeOptions(const Options& options); 48 | 49 | // Add key,value to the table being constructed. 50 | // REQUIRES: key is after any previously added key according to comparator. 51 | // REQUIRES: Finish(), Abandon() have not been called 52 | void Add(const Slice& key, const Slice& value); 53 | 54 | // Advanced operation: flush any buffered key/value pairs to file. 55 | // Can be used to ensure that two adjacent entries never live in 56 | // the same data block. Most clients should not need to use this method. 57 | // REQUIRES: Finish(), Abandon() have not been called 58 | void Flush(); 59 | 60 | // Return non-ok iff some error has been detected. 61 | Status status() const; 62 | 63 | // Finish building the table. Stops using the file passed to the 64 | // constructor after this function returns. 65 | // REQUIRES: Finish(), Abandon() have not been called 66 | Status Finish(); 67 | 68 | // Indicate that the contents of this builder should be abandoned. Stops 69 | // using the file passed to the constructor after this function returns. 70 | // If the caller is not going to call Finish(), it must call Abandon() 71 | // before destroying this builder. 72 | // REQUIRES: Finish(), Abandon() have not been called 73 | void Abandon(); 74 | 75 | // Number of calls to Add() so far. 76 | uint64_t NumEntries() const; 77 | 78 | // Size of the file generated so far. If invoked after a successful 79 | // Finish() call, returns the size of the final generated file. 80 | uint64_t FileSize() const; 81 | 82 | private: 83 | bool ok() const { return status().ok(); } 84 | void WriteBlock(BlockBuilder* block, BlockHandle* handle); 85 | void WriteRawBlock(const Slice& data, CompressionType, BlockHandle* handle); 86 | 87 | struct Rep; 88 | Rep* rep_; 89 | }; 90 | 91 | } // namespace leveldb 92 | 93 | #endif // STORAGE_LEVELDB_INCLUDE_TABLE_BUILDER_H_ 94 | -------------------------------------------------------------------------------- /include/leveldb/write_batch.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // WriteBatch holds a collection of updates to apply atomically to a DB. 6 | // 7 | // The updates are applied in the order in which they are added 8 | // to the WriteBatch. For example, the value of "key" will be "v3" 9 | // after the following batch is written: 10 | // 11 | // batch.Put("key", "v1"); 12 | // batch.Delete("key"); 13 | // batch.Put("key", "v2"); 14 | // batch.Put("key", "v3"); 15 | // 16 | // Multiple threads can invoke const methods on a WriteBatch without 17 | // external synchronization, but if any of the threads may call a 18 | // non-const method, all threads accessing the same WriteBatch must use 19 | // external synchronization. 20 | 21 | #ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 22 | #define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 23 | 24 | #include 25 | 26 | #include "leveldb/export.h" 27 | #include "leveldb/status.h" 28 | 29 | namespace leveldb { 30 | 31 | class Slice; 32 | 33 | class LEVELDB_EXPORT WriteBatch { 34 | public: 35 | class LEVELDB_EXPORT Handler { 36 | public: 37 | virtual ~Handler(); 38 | virtual void Put(const Slice& key, const Slice& value) = 0; 39 | virtual void Delete(const Slice& key) = 0; 40 | }; 41 | 42 | WriteBatch(); 43 | 44 | // Intentionally copyable. 45 | WriteBatch(const WriteBatch&) = default; 46 | WriteBatch& operator=(const WriteBatch&) = default; 47 | 48 | ~WriteBatch(); 49 | 50 | // Store the mapping "key->value" in the database. 51 | void Put(const Slice& key, const Slice& value); 52 | 53 | // If the database contains a mapping for "key", erase it. Else do nothing. 54 | void Delete(const Slice& key); 55 | 56 | // Clear all updates buffered in this batch. 57 | void Clear(); 58 | 59 | // The size of the database changes caused by this batch. 60 | // 61 | // This number is tied to implementation details, and may change across 62 | // releases. It is intended for LevelDB usage metrics. 63 | size_t ApproximateSize() const; 64 | 65 | // Copies the operations in "source" to this batch. 66 | // 67 | // This runs in O(source size) time. However, the constant factor is better 68 | // than calling Iterate() over the source batch with a Handler that replicates 69 | // the operations into this batch. 70 | void Append(const WriteBatch& source); 71 | 72 | // Support for iterating over the contents of a batch. 73 | Status Iterate(Handler* handler) const; 74 | 75 | // the only difference between WriteBatch::Iterate is that Iterate only 76 | // put the address of the value in the vlog file into the LSM-tree. 77 | Status Iterate(Handler* handler, const uint64_t vlog_number, 78 | size_t* vlog_head) const; 79 | 80 | private: 81 | friend class WriteBatchInternal; 82 | 83 | std::string rep_; // See comment in write_batch.cc for the format of rep_ 84 | }; 85 | 86 | } // namespace leveldb 87 | 88 | #endif // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 89 | -------------------------------------------------------------------------------- /issues/issue178_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | // Test for issue 178: a manual compaction causes deleted data to reappear. 6 | #include 7 | #include 8 | #include 9 | 10 | #include "gtest/gtest.h" 11 | #include "leveldb/db.h" 12 | #include "leveldb/write_batch.h" 13 | #include "util/testutil.h" 14 | 15 | namespace { 16 | 17 | const int kNumKeys = 1100000; 18 | 19 | std::string Key1(int i) { 20 | char buf[100]; 21 | std::snprintf(buf, sizeof(buf), "my_key_%d", i); 22 | return buf; 23 | } 24 | 25 | std::string Key2(int i) { return Key1(i) + "_xxx"; } 26 | 27 | TEST(Issue178, Test) { 28 | // Get rid of any state from an old run. 29 | std::string dbpath = testing::TempDir() + "leveldb_cbug_test"; 30 | DestroyDB(dbpath, leveldb::Options()); 31 | 32 | // Open database. Disable compression since it affects the creation 33 | // of layers and the code below is trying to test against a very 34 | // specific scenario. 35 | leveldb::DB* db; 36 | leveldb::Options db_options; 37 | db_options.create_if_missing = true; 38 | db_options.compression = leveldb::kNoCompression; 39 | ASSERT_LEVELDB_OK(leveldb::DB::Open(db_options, dbpath, &db)); 40 | 41 | // create first key range 42 | leveldb::WriteBatch batch; 43 | for (size_t i = 0; i < kNumKeys; i++) { 44 | batch.Put(Key1(i), "value for range 1 key"); 45 | } 46 | ASSERT_LEVELDB_OK(db->Write(leveldb::WriteOptions(), &batch)); 47 | 48 | // create second key range 49 | batch.Clear(); 50 | for (size_t i = 0; i < kNumKeys; i++) { 51 | batch.Put(Key2(i), "value for range 2 key"); 52 | } 53 | ASSERT_LEVELDB_OK(db->Write(leveldb::WriteOptions(), &batch)); 54 | 55 | // delete second key range 56 | batch.Clear(); 57 | for (size_t i = 0; i < kNumKeys; i++) { 58 | batch.Delete(Key2(i)); 59 | } 60 | ASSERT_LEVELDB_OK(db->Write(leveldb::WriteOptions(), &batch)); 61 | 62 | // compact database 63 | std::string start_key = Key1(0); 64 | std::string end_key = Key1(kNumKeys - 1); 65 | leveldb::Slice least(start_key.data(), start_key.size()); 66 | leveldb::Slice greatest(end_key.data(), end_key.size()); 67 | 68 | // commenting out the line below causes the example to work correctly 69 | db->CompactRange(&least, &greatest); 70 | 71 | // count the keys 72 | leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions()); 73 | size_t num_keys = 0; 74 | for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 75 | num_keys++; 76 | } 77 | delete iter; 78 | ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys"; 79 | 80 | // close database 81 | delete db; 82 | DestroyDB(dbpath, leveldb::Options()); 83 | } 84 | 85 | } // anonymous namespace 86 | 87 | int main(int argc, char** argv) { 88 | testing::InitGoogleTest(&argc, argv); 89 | return RUN_ALL_TESTS(); 90 | } 91 | -------------------------------------------------------------------------------- /issues/issue200_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | // Test for issue 200: when iterator switches direction from backward 6 | // to forward, the current key can be yielded unexpectedly if a new 7 | // mutation has been added just before the current key. 8 | 9 | #include "gtest/gtest.h" 10 | #include "leveldb/db.h" 11 | #include "util/testutil.h" 12 | 13 | namespace leveldb { 14 | 15 | TEST(Issue200, Test) { 16 | // Get rid of any state from an old run. 17 | std::string dbpath = testing::TempDir() + "leveldb_issue200_test"; 18 | DestroyDB(dbpath, Options()); 19 | 20 | DB* db; 21 | Options options; 22 | options.create_if_missing = true; 23 | ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db)); 24 | 25 | WriteOptions write_options; 26 | ASSERT_LEVELDB_OK(db->Put(write_options, "1", "b")); 27 | ASSERT_LEVELDB_OK(db->Put(write_options, "2", "c")); 28 | ASSERT_LEVELDB_OK(db->Put(write_options, "3", "d")); 29 | ASSERT_LEVELDB_OK(db->Put(write_options, "4", "e")); 30 | ASSERT_LEVELDB_OK(db->Put(write_options, "5", "f")); 31 | 32 | ReadOptions read_options; 33 | Iterator* iter = db->NewIterator(read_options); 34 | 35 | // Add an element that should not be reflected in the iterator. 36 | ASSERT_LEVELDB_OK(db->Put(write_options, "25", "cd")); 37 | 38 | iter->Seek("5"); 39 | ASSERT_EQ(iter->key().ToString(), "5"); 40 | iter->Prev(); 41 | ASSERT_EQ(iter->key().ToString(), "4"); 42 | iter->Prev(); 43 | ASSERT_EQ(iter->key().ToString(), "3"); 44 | iter->Next(); 45 | ASSERT_EQ(iter->key().ToString(), "4"); 46 | iter->Next(); 47 | ASSERT_EQ(iter->key().ToString(), "5"); 48 | 49 | delete iter; 50 | delete db; 51 | DestroyDB(dbpath, options); 52 | } 53 | 54 | } // namespace leveldb 55 | 56 | int main(int argc, char** argv) { 57 | testing::InitGoogleTest(&argc, argv); 58 | return RUN_ALL_TESTS(); 59 | } 60 | -------------------------------------------------------------------------------- /issues/issue320_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2019 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "gtest/gtest.h" 13 | #include "leveldb/db.h" 14 | #include "leveldb/write_batch.h" 15 | #include "util/testutil.h" 16 | 17 | namespace leveldb { 18 | 19 | namespace { 20 | 21 | // Creates a random number in the range of [0, max). 22 | int GenerateRandomNumber(int max) { return std::rand() % max; } 23 | 24 | std::string CreateRandomString(int32_t index) { 25 | static const size_t len = 1024; 26 | char bytes[len]; 27 | size_t i = 0; 28 | while (i < 8) { 29 | bytes[i] = 'a' + ((index >> (4 * i)) & 0xf); 30 | ++i; 31 | } 32 | while (i < sizeof(bytes)) { 33 | bytes[i] = 'a' + GenerateRandomNumber(26); 34 | ++i; 35 | } 36 | return std::string(bytes, sizeof(bytes)); 37 | } 38 | 39 | } // namespace 40 | 41 | TEST(Issue320, Test) { 42 | std::srand(0); 43 | 44 | bool delete_before_put = false; 45 | bool keep_snapshots = true; 46 | 47 | std::vector>> test_map( 48 | 10000); 49 | std::vector snapshots(100, nullptr); 50 | 51 | DB* db; 52 | Options options; 53 | options.create_if_missing = true; 54 | 55 | std::string dbpath = testing::TempDir() + "leveldb_issue320_test"; 56 | ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db)); 57 | 58 | uint32_t target_size = 10000; 59 | uint32_t num_items = 0; 60 | uint32_t count = 0; 61 | std::string key; 62 | std::string value, old_value; 63 | 64 | WriteOptions writeOptions; 65 | ReadOptions readOptions; 66 | while (count < 200000) { 67 | if ((++count % 1000) == 0) { 68 | std::cout << "count: " << count << std::endl; 69 | } 70 | 71 | int index = GenerateRandomNumber(test_map.size()); 72 | WriteBatch batch; 73 | 74 | if (test_map[index] == nullptr) { 75 | num_items++; 76 | test_map[index].reset(new std::pair( 77 | CreateRandomString(index), CreateRandomString(index))); 78 | batch.Put(test_map[index]->first, test_map[index]->second); 79 | } else { 80 | ASSERT_LEVELDB_OK( 81 | db->Get(readOptions, test_map[index]->first, &old_value)); 82 | if (old_value != test_map[index]->second) { 83 | std::cout << "ERROR incorrect value returned by Get" << std::endl; 84 | std::cout << " count=" << count << std::endl; 85 | std::cout << " old value=" << old_value << std::endl; 86 | std::cout << " test_map[index]->second=" << test_map[index]->second 87 | << std::endl; 88 | std::cout << " test_map[index]->first=" << test_map[index]->first 89 | << std::endl; 90 | std::cout << " index=" << index << std::endl; 91 | ASSERT_EQ(old_value, test_map[index]->second); 92 | } 93 | 94 | if (num_items >= target_size && GenerateRandomNumber(100) > 30) { 95 | batch.Delete(test_map[index]->first); 96 | test_map[index] = nullptr; 97 | --num_items; 98 | } else { 99 | test_map[index]->second = CreateRandomString(index); 100 | if (delete_before_put) batch.Delete(test_map[index]->first); 101 | batch.Put(test_map[index]->first, test_map[index]->second); 102 | } 103 | } 104 | 105 | ASSERT_LEVELDB_OK(db->Write(writeOptions, &batch)); 106 | 107 | if (keep_snapshots && GenerateRandomNumber(10) == 0) { 108 | int i = GenerateRandomNumber(snapshots.size()); 109 | if (snapshots[i] != nullptr) { 110 | db->ReleaseSnapshot(snapshots[i]); 111 | } 112 | snapshots[i] = db->GetSnapshot(); 113 | } 114 | } 115 | 116 | for (Snapshot const* snapshot : snapshots) { 117 | if (snapshot) { 118 | db->ReleaseSnapshot(snapshot); 119 | } 120 | } 121 | 122 | delete db; 123 | DestroyDB(dbpath, options); 124 | } 125 | 126 | } // namespace leveldb 127 | 128 | int main(int argc, char** argv) { 129 | testing::InitGoogleTest(&argc, argv); 130 | return RUN_ALL_TESTS(); 131 | } 132 | -------------------------------------------------------------------------------- /pics/wisckey_figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coyorkdow/wisckey/8eb385ca651bf46876eb609075666918b6868d35/pics/wisckey_figure_1.png -------------------------------------------------------------------------------- /port/README.md: -------------------------------------------------------------------------------- 1 | This directory contains interfaces and implementations that isolate the 2 | rest of the package from platform details. 3 | 4 | Code in the rest of the package includes "port.h" from this directory. 5 | "port.h" in turn includes a platform specific "port_.h" file 6 | that provides the platform specific implementation. 7 | 8 | See port_stdcxx.h for an example of what must be provided in a platform 9 | specific header file. 10 | 11 | -------------------------------------------------------------------------------- /port/port.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_PORT_PORT_H_ 6 | #define STORAGE_LEVELDB_PORT_PORT_H_ 7 | 8 | #include 9 | 10 | // Include the appropriate platform specific file below. If you are 11 | // porting to a new platform, see "port_example.h" for documentation 12 | // of what the new port_.h file must provide. 13 | #if defined(LEVELDB_PLATFORM_POSIX) || defined(LEVELDB_PLATFORM_WINDOWS) 14 | #include "port/port_stdcxx.h" 15 | #elif defined(LEVELDB_PLATFORM_CHROMIUM) 16 | #include "port/port_chromium.h" 17 | #endif 18 | 19 | #endif // STORAGE_LEVELDB_PORT_PORT_H_ 20 | -------------------------------------------------------------------------------- /port/port_config.h.in: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ 6 | #define STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ 7 | 8 | // Define to 1 if you have a definition for fdatasync() in . 9 | #if !defined(HAVE_FDATASYNC) 10 | #cmakedefine01 HAVE_FDATASYNC 11 | #endif // !defined(HAVE_FDATASYNC) 12 | 13 | // Define to 1 if you have a definition for F_FULLFSYNC in . 14 | #if !defined(HAVE_FULLFSYNC) 15 | #cmakedefine01 HAVE_FULLFSYNC 16 | #endif // !defined(HAVE_FULLFSYNC) 17 | 18 | // Define to 1 if you have a definition for O_CLOEXEC in . 19 | #if !defined(HAVE_O_CLOEXEC) 20 | #cmakedefine01 HAVE_O_CLOEXEC 21 | #endif // !defined(HAVE_O_CLOEXEC) 22 | 23 | // Define to 1 if you have Google CRC32C. 24 | #if !defined(HAVE_CRC32C) 25 | #cmakedefine01 HAVE_CRC32C 26 | #endif // !defined(HAVE_CRC32C) 27 | 28 | // Define to 1 if you have Google Snappy. 29 | #if !defined(HAVE_SNAPPY) 30 | #cmakedefine01 HAVE_SNAPPY 31 | #endif // !defined(HAVE_SNAPPY) 32 | 33 | #endif // STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ -------------------------------------------------------------------------------- /port/port_example.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // This file contains the specification, but not the implementations, 6 | // of the types/operations/etc. that should be defined by a platform 7 | // specific port_.h file. Use this file as a reference for 8 | // how to port this package to a new platform. 9 | 10 | #ifndef STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ 11 | #define STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ 12 | 13 | #include "port/thread_annotations.h" 14 | 15 | namespace leveldb { 16 | namespace port { 17 | 18 | // TODO(jorlow): Many of these belong more in the environment class rather than 19 | // here. We should try moving them and see if it affects perf. 20 | 21 | // ------------------ Threading ------------------- 22 | 23 | // A Mutex represents an exclusive lock. 24 | class LOCKABLE Mutex { 25 | public: 26 | Mutex(); 27 | ~Mutex(); 28 | 29 | // Lock the mutex. Waits until other lockers have exited. 30 | // Will deadlock if the mutex is already locked by this thread. 31 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); 32 | 33 | // Unlock the mutex. 34 | // REQUIRES: This mutex was locked by this thread. 35 | void Unlock() UNLOCK_FUNCTION(); 36 | 37 | // Optionally crash if this thread does not hold this mutex. 38 | // The implementation must be fast, especially if NDEBUG is 39 | // defined. The implementation is allowed to skip all checks. 40 | void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); 41 | }; 42 | 43 | class CondVar { 44 | public: 45 | explicit CondVar(Mutex* mu); 46 | ~CondVar(); 47 | 48 | // Atomically release *mu and block on this condition variable until 49 | // either a call to SignalAll(), or a call to Signal() that picks 50 | // this thread to wakeup. 51 | // REQUIRES: this thread holds *mu 52 | void Wait(); 53 | 54 | // If there are some threads waiting, wake up at least one of them. 55 | void Signal(); 56 | 57 | // Wake up all waiting threads. 58 | void SignallAll(); 59 | }; 60 | 61 | // ------------------ Compression ------------------- 62 | 63 | // Store the snappy compression of "input[0,input_length-1]" in *output. 64 | // Returns false if snappy is not supported by this port. 65 | bool Snappy_Compress(const char* input, size_t input_length, 66 | std::string* output); 67 | 68 | // If input[0,input_length-1] looks like a valid snappy compressed 69 | // buffer, store the size of the uncompressed data in *result and 70 | // return true. Else return false. 71 | bool Snappy_GetUncompressedLength(const char* input, size_t length, 72 | size_t* result); 73 | 74 | // Attempt to snappy uncompress input[0,input_length-1] into *output. 75 | // Returns true if successful, false if the input is invalid lightweight 76 | // compressed data. 77 | // 78 | // REQUIRES: at least the first "n" bytes of output[] must be writable 79 | // where "n" is the result of a successful call to 80 | // Snappy_GetUncompressedLength. 81 | bool Snappy_Uncompress(const char* input_data, size_t input_length, 82 | char* output); 83 | 84 | // ------------------ Miscellaneous ------------------- 85 | 86 | // If heap profiling is not supported, returns false. 87 | // Else repeatedly calls (*func)(arg, data, n) and then returns true. 88 | // The concatenation of all "data[0,n-1]" fragments is the heap profile. 89 | bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg); 90 | 91 | // Extend the CRC to include the first n bytes of buf. 92 | // 93 | // Returns zero if the CRC cannot be extended using acceleration, else returns 94 | // the newly extended CRC value (which may also be zero). 95 | uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size); 96 | 97 | } // namespace port 98 | } // namespace leveldb 99 | 100 | #endif // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ 101 | -------------------------------------------------------------------------------- /port/thread_annotations.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_ 6 | #define STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_ 7 | 8 | // Use Clang's thread safety analysis annotations when available. In other 9 | // environments, the macros receive empty definitions. 10 | // Usage documentation: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html 11 | 12 | #if !defined(THREAD_ANNOTATION_ATTRIBUTE__) 13 | 14 | #if defined(__clang__) 15 | 16 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) 17 | #else 18 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op 19 | #endif 20 | 21 | #endif // !defined(THREAD_ANNOTATION_ATTRIBUTE__) 22 | 23 | #ifndef GUARDED_BY 24 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) 25 | #endif 26 | 27 | #ifndef PT_GUARDED_BY 28 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) 29 | #endif 30 | 31 | #ifndef ACQUIRED_AFTER 32 | #define ACQUIRED_AFTER(...) \ 33 | THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) 34 | #endif 35 | 36 | #ifndef ACQUIRED_BEFORE 37 | #define ACQUIRED_BEFORE(...) \ 38 | THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) 39 | #endif 40 | 41 | #ifndef EXCLUSIVE_LOCKS_REQUIRED 42 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ 43 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(__VA_ARGS__)) 44 | #endif 45 | 46 | #ifndef SHARED_LOCKS_REQUIRED 47 | #define SHARED_LOCKS_REQUIRED(...) \ 48 | THREAD_ANNOTATION_ATTRIBUTE__(shared_locks_required(__VA_ARGS__)) 49 | #endif 50 | 51 | #ifndef LOCKS_EXCLUDED 52 | #define LOCKS_EXCLUDED(...) \ 53 | THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) 54 | #endif 55 | 56 | #ifndef LOCK_RETURNED 57 | #define LOCK_RETURNED(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) 58 | #endif 59 | 60 | #ifndef LOCKABLE 61 | #define LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(lockable) 62 | #endif 63 | 64 | #ifndef SCOPED_LOCKABLE 65 | #define SCOPED_LOCKABLE THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) 66 | #endif 67 | 68 | #ifndef EXCLUSIVE_LOCK_FUNCTION 69 | #define EXCLUSIVE_LOCK_FUNCTION(...) \ 70 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) 71 | #endif 72 | 73 | #ifndef SHARED_LOCK_FUNCTION 74 | #define SHARED_LOCK_FUNCTION(...) \ 75 | __attribute__((acquire_shared_capability(__VA_ARGS__))) 76 | #endif 77 | 78 | #ifndef SHARED_LOCK_FUNCTION 79 | #define SHARED_LOCK_FUNCTION(...) \ 80 | THREAD_ANNOTATION_ATTRIBUTE__(shared_lock_function(__VA_ARGS__)) 81 | #endif 82 | 83 | #ifndef SHARED_UNLOCK_FUNCTION 84 | #define SHARED_UNLOCK_FUNCTION(...) \ 85 | __attribute__((release_shared_capability(__VA_ARGS__))) 86 | #endif 87 | 88 | #ifndef EXCLUSIVE_TRYLOCK_FUNCTION 89 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ 90 | THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) 91 | #endif 92 | 93 | #ifndef SHARED_TRYLOCK_FUNCTION 94 | #define SHARED_TRYLOCK_FUNCTION(...) \ 95 | THREAD_ANNOTATION_ATTRIBUTE__(shared_trylock_function(__VA_ARGS__)) 96 | #endif 97 | 98 | #ifndef UNLOCK_FUNCTION 99 | #define UNLOCK_FUNCTION(...) \ 100 | THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) 101 | #endif 102 | 103 | #ifndef NO_THREAD_SAFETY_ANALYSIS 104 | #define NO_THREAD_SAFETY_ANALYSIS \ 105 | THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) 106 | #endif 107 | 108 | #ifndef ASSERT_EXCLUSIVE_LOCK 109 | #define ASSERT_EXCLUSIVE_LOCK(...) \ 110 | THREAD_ANNOTATION_ATTRIBUTE__(assert_exclusive_lock(__VA_ARGS__)) 111 | #endif 112 | 113 | #ifndef ASSERT_SHARED_LOCK 114 | #define ASSERT_SHARED_LOCK(...) \ 115 | THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_lock(__VA_ARGS__)) 116 | #endif 117 | 118 | #endif // STORAGE_LEVELDB_PORT_THREAD_ANNOTATIONS_H_ 119 | -------------------------------------------------------------------------------- /table/block.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_BLOCK_H_ 6 | #define STORAGE_LEVELDB_TABLE_BLOCK_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "leveldb/iterator.h" 12 | 13 | namespace leveldb { 14 | 15 | struct BlockContents; 16 | class Comparator; 17 | 18 | class Block { 19 | public: 20 | // Initialize the block with the specified contents. 21 | explicit Block(const BlockContents& contents); 22 | 23 | Block(const Block&) = delete; 24 | Block& operator=(const Block&) = delete; 25 | 26 | ~Block(); 27 | 28 | size_t size() const { return size_; } 29 | Iterator* NewIterator(const Comparator* comparator); 30 | 31 | private: 32 | class Iter; 33 | 34 | uint32_t NumRestarts() const; 35 | 36 | const char* data_; 37 | size_t size_; 38 | uint32_t restart_offset_; // Offset in data_ of restart array 39 | bool owned_; // Block owns data_[] 40 | }; 41 | 42 | } // namespace leveldb 43 | 44 | #endif // STORAGE_LEVELDB_TABLE_BLOCK_H_ 45 | -------------------------------------------------------------------------------- /table/block_builder.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // BlockBuilder generates blocks where keys are prefix-compressed: 6 | // 7 | // When we store a key, we drop the prefix shared with the previous 8 | // string. This helps reduce the space requirement significantly. 9 | // Furthermore, once every K keys, we do not apply the prefix 10 | // compression and store the entire key. We call this a "restart 11 | // point". The tail end of the block stores the offsets of all of the 12 | // restart points, and can be used to do a binary search when looking 13 | // for a particular key. Values are stored as-is (without compression) 14 | // immediately following the corresponding key. 15 | // 16 | // An entry for a particular key-value pair has the form: 17 | // shared_bytes: varint32 18 | // unshared_bytes: varint32 19 | // value_length: varint32 20 | // key_delta: char[unshared_bytes] 21 | // value: char[value_length] 22 | // shared_bytes == 0 for restart points. 23 | // 24 | // The trailer of the block has the form: 25 | // restarts: uint32[num_restarts] 26 | // num_restarts: uint32 27 | // restarts[i] contains the offset within the block of the ith restart point. 28 | 29 | #include "table/block_builder.h" 30 | 31 | #include 32 | #include 33 | 34 | #include "leveldb/comparator.h" 35 | #include "leveldb/options.h" 36 | #include "util/coding.h" 37 | 38 | namespace leveldb { 39 | 40 | BlockBuilder::BlockBuilder(const Options* options) 41 | : options_(options), restarts_(), counter_(0), finished_(false) { 42 | assert(options->block_restart_interval >= 1); 43 | restarts_.push_back(0); // First restart point is at offset 0 44 | } 45 | 46 | void BlockBuilder::Reset() { 47 | buffer_.clear(); 48 | restarts_.clear(); 49 | restarts_.push_back(0); // First restart point is at offset 0 50 | counter_ = 0; 51 | finished_ = false; 52 | last_key_.clear(); 53 | } 54 | 55 | size_t BlockBuilder::CurrentSizeEstimate() const { 56 | return (buffer_.size() + // Raw data buffer 57 | restarts_.size() * sizeof(uint32_t) + // Restart array 58 | sizeof(uint32_t)); // Restart array length 59 | } 60 | 61 | Slice BlockBuilder::Finish() { 62 | // Append restart array 63 | for (size_t i = 0; i < restarts_.size(); i++) { 64 | PutFixed32(&buffer_, restarts_[i]); 65 | } 66 | PutFixed32(&buffer_, restarts_.size()); 67 | finished_ = true; 68 | return Slice(buffer_); 69 | } 70 | 71 | void BlockBuilder::Add(const Slice& key, const Slice& value) { 72 | Slice last_key_piece(last_key_); 73 | assert(!finished_); 74 | assert(counter_ <= options_->block_restart_interval); 75 | assert(buffer_.empty() // No values yet? 76 | || options_->comparator->Compare(key, last_key_piece) > 0); 77 | size_t shared = 0; 78 | if (counter_ < options_->block_restart_interval) { 79 | // See how much sharing to do with previous string 80 | const size_t min_length = std::min(last_key_piece.size(), key.size()); 81 | while ((shared < min_length) && (last_key_piece[shared] == key[shared])) { 82 | shared++; 83 | } 84 | } else { 85 | // Restart compression 86 | restarts_.push_back(buffer_.size()); 87 | counter_ = 0; 88 | } 89 | const size_t non_shared = key.size() - shared; 90 | 91 | // Add "" to buffer_ 92 | PutVarint32(&buffer_, shared); 93 | PutVarint32(&buffer_, non_shared); 94 | PutVarint32(&buffer_, value.size()); 95 | 96 | // Add string delta to buffer_ followed by value 97 | buffer_.append(key.data() + shared, non_shared); 98 | buffer_.append(value.data(), value.size()); 99 | 100 | // Update state 101 | last_key_.resize(shared); 102 | last_key_.append(key.data() + shared, non_shared); 103 | assert(Slice(last_key_) == key); 104 | counter_++; 105 | } 106 | 107 | } // namespace leveldb 108 | -------------------------------------------------------------------------------- /table/block_builder.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 6 | #define STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "leveldb/slice.h" 12 | 13 | namespace leveldb { 14 | 15 | struct Options; 16 | 17 | class BlockBuilder { 18 | public: 19 | explicit BlockBuilder(const Options* options); 20 | 21 | BlockBuilder(const BlockBuilder&) = delete; 22 | BlockBuilder& operator=(const BlockBuilder&) = delete; 23 | 24 | // Reset the contents as if the BlockBuilder was just constructed. 25 | void Reset(); 26 | 27 | // REQUIRES: Finish() has not been called since the last call to Reset(). 28 | // REQUIRES: key is larger than any previously added key 29 | void Add(const Slice& key, const Slice& value); 30 | 31 | // Finish building the block and return a slice that refers to the 32 | // block contents. The returned slice will remain valid for the 33 | // lifetime of this builder or until Reset() is called. 34 | Slice Finish(); 35 | 36 | // Returns an estimate of the current (uncompressed) size of the block 37 | // we are building. 38 | size_t CurrentSizeEstimate() const; 39 | 40 | // Return true iff no entries have been added since the last Reset() 41 | bool empty() const { return buffer_.empty(); } 42 | 43 | private: 44 | const Options* options_; 45 | std::string buffer_; // Destination buffer 46 | std::vector restarts_; // Restart points 47 | int counter_; // Number of entries emitted since restart 48 | bool finished_; // Has Finish() been called? 49 | std::string last_key_; 50 | }; 51 | 52 | } // namespace leveldb 53 | 54 | #endif // STORAGE_LEVELDB_TABLE_BLOCK_BUILDER_H_ 55 | -------------------------------------------------------------------------------- /table/filter_block.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "table/filter_block.h" 6 | 7 | #include "leveldb/filter_policy.h" 8 | #include "util/coding.h" 9 | 10 | namespace leveldb { 11 | 12 | // See doc/table_format.md for an explanation of the filter block format. 13 | 14 | // Generate new filter every 2KB of data 15 | static const size_t kFilterBaseLg = 11; 16 | static const size_t kFilterBase = 1 << kFilterBaseLg; 17 | 18 | FilterBlockBuilder::FilterBlockBuilder(const FilterPolicy* policy) 19 | : policy_(policy) {} 20 | 21 | void FilterBlockBuilder::StartBlock(uint64_t block_offset) { 22 | uint64_t filter_index = (block_offset / kFilterBase); 23 | assert(filter_index >= filter_offsets_.size()); 24 | while (filter_index > filter_offsets_.size()) { 25 | GenerateFilter(); 26 | } 27 | } 28 | 29 | void FilterBlockBuilder::AddKey(const Slice& key) { 30 | Slice k = key; 31 | start_.push_back(keys_.size()); 32 | keys_.append(k.data(), k.size()); 33 | } 34 | 35 | Slice FilterBlockBuilder::Finish() { 36 | if (!start_.empty()) { 37 | GenerateFilter(); 38 | } 39 | 40 | // Append array of per-filter offsets 41 | const uint32_t array_offset = result_.size(); 42 | for (size_t i = 0; i < filter_offsets_.size(); i++) { 43 | PutFixed32(&result_, filter_offsets_[i]); 44 | } 45 | 46 | PutFixed32(&result_, array_offset); 47 | result_.push_back(kFilterBaseLg); // Save encoding parameter in result 48 | return Slice(result_); 49 | } 50 | 51 | void FilterBlockBuilder::GenerateFilter() { 52 | const size_t num_keys = start_.size(); 53 | if (num_keys == 0) { 54 | // Fast path if there are no keys for this filter 55 | filter_offsets_.push_back(result_.size()); 56 | return; 57 | } 58 | 59 | // Make list of keys from flattened key structure 60 | start_.push_back(keys_.size()); // Simplify length computation 61 | tmp_keys_.resize(num_keys); 62 | for (size_t i = 0; i < num_keys; i++) { 63 | const char* base = keys_.data() + start_[i]; 64 | size_t length = start_[i + 1] - start_[i]; 65 | tmp_keys_[i] = Slice(base, length); 66 | } 67 | 68 | // Generate filter for current set of keys and append to result_. 69 | filter_offsets_.push_back(result_.size()); 70 | policy_->CreateFilter(&tmp_keys_[0], static_cast(num_keys), &result_); 71 | 72 | tmp_keys_.clear(); 73 | keys_.clear(); 74 | start_.clear(); 75 | } 76 | 77 | FilterBlockReader::FilterBlockReader(const FilterPolicy* policy, 78 | const Slice& contents) 79 | : policy_(policy), data_(nullptr), offset_(nullptr), num_(0), base_lg_(0) { 80 | size_t n = contents.size(); 81 | if (n < 5) return; // 1 byte for base_lg_ and 4 for start of offset array 82 | base_lg_ = contents[n - 1]; 83 | uint32_t last_word = DecodeFixed32(contents.data() + n - 5); 84 | if (last_word > n - 5) return; 85 | data_ = contents.data(); 86 | offset_ = data_ + last_word; 87 | num_ = (n - 5 - last_word) / 4; 88 | } 89 | 90 | bool FilterBlockReader::KeyMayMatch(uint64_t block_offset, const Slice& key) { 91 | uint64_t index = block_offset >> base_lg_; 92 | if (index < num_) { 93 | uint32_t start = DecodeFixed32(offset_ + index * 4); 94 | uint32_t limit = DecodeFixed32(offset_ + index * 4 + 4); 95 | if (start <= limit && limit <= static_cast(offset_ - data_)) { 96 | Slice filter = Slice(data_ + start, limit - start); 97 | return policy_->KeyMayMatch(key, filter); 98 | } else if (start == limit) { 99 | // Empty filters do not match any keys 100 | return false; 101 | } 102 | } 103 | return true; // Errors are treated as potential matches 104 | } 105 | 106 | } // namespace leveldb 107 | -------------------------------------------------------------------------------- /table/filter_block.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // A filter block is stored near the end of a Table file. It contains 6 | // filters (e.g., bloom filters) for all data blocks in the table combined 7 | // into a single filter block. 8 | 9 | #ifndef STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_ 10 | #define STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "leveldb/slice.h" 18 | #include "util/hash.h" 19 | 20 | namespace leveldb { 21 | 22 | class FilterPolicy; 23 | 24 | // A FilterBlockBuilder is used to construct all of the filters for a 25 | // particular Table. It generates a single string which is stored as 26 | // a special block in the Table. 27 | // 28 | // The sequence of calls to FilterBlockBuilder must match the regexp: 29 | // (StartBlock AddKey*)* Finish 30 | class FilterBlockBuilder { 31 | public: 32 | explicit FilterBlockBuilder(const FilterPolicy*); 33 | 34 | FilterBlockBuilder(const FilterBlockBuilder&) = delete; 35 | FilterBlockBuilder& operator=(const FilterBlockBuilder&) = delete; 36 | 37 | void StartBlock(uint64_t block_offset); 38 | void AddKey(const Slice& key); 39 | Slice Finish(); 40 | 41 | private: 42 | void GenerateFilter(); 43 | 44 | const FilterPolicy* policy_; 45 | std::string keys_; // Flattened key contents 46 | std::vector start_; // Starting index in keys_ of each key 47 | std::string result_; // Filter data computed so far 48 | std::vector tmp_keys_; // policy_->CreateFilter() argument 49 | std::vector filter_offsets_; 50 | }; 51 | 52 | class FilterBlockReader { 53 | public: 54 | // REQUIRES: "contents" and *policy must stay live while *this is live. 55 | FilterBlockReader(const FilterPolicy* policy, const Slice& contents); 56 | bool KeyMayMatch(uint64_t block_offset, const Slice& key); 57 | 58 | private: 59 | const FilterPolicy* policy_; 60 | const char* data_; // Pointer to filter data (at block-start) 61 | const char* offset_; // Pointer to beginning of offset array (at block-end) 62 | size_t num_; // Number of entries in offset array 63 | size_t base_lg_; // Encoding parameter (see kFilterBaseLg in .cc file) 64 | }; 65 | 66 | } // namespace leveldb 67 | 68 | #endif // STORAGE_LEVELDB_TABLE_FILTER_BLOCK_H_ 69 | -------------------------------------------------------------------------------- /table/filter_block_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "table/filter_block.h" 6 | 7 | #include "gtest/gtest.h" 8 | #include "leveldb/filter_policy.h" 9 | #include "util/coding.h" 10 | #include "util/hash.h" 11 | #include "util/logging.h" 12 | #include "util/testutil.h" 13 | 14 | namespace leveldb { 15 | 16 | // For testing: emit an array with one hash value per key 17 | class TestHashFilter : public FilterPolicy { 18 | public: 19 | const char* Name() const override { return "TestHashFilter"; } 20 | 21 | void CreateFilter(const Slice* keys, int n, std::string* dst) const override { 22 | for (int i = 0; i < n; i++) { 23 | uint32_t h = Hash(keys[i].data(), keys[i].size(), 1); 24 | PutFixed32(dst, h); 25 | } 26 | } 27 | 28 | bool KeyMayMatch(const Slice& key, const Slice& filter) const override { 29 | uint32_t h = Hash(key.data(), key.size(), 1); 30 | for (size_t i = 0; i + 4 <= filter.size(); i += 4) { 31 | if (h == DecodeFixed32(filter.data() + i)) { 32 | return true; 33 | } 34 | } 35 | return false; 36 | } 37 | }; 38 | 39 | class FilterBlockTest : public testing::Test { 40 | public: 41 | TestHashFilter policy_; 42 | }; 43 | 44 | TEST_F(FilterBlockTest, EmptyBuilder) { 45 | FilterBlockBuilder builder(&policy_); 46 | Slice block = builder.Finish(); 47 | ASSERT_EQ("\\x00\\x00\\x00\\x00\\x0b", EscapeString(block)); 48 | FilterBlockReader reader(&policy_, block); 49 | ASSERT_TRUE(reader.KeyMayMatch(0, "foo")); 50 | ASSERT_TRUE(reader.KeyMayMatch(100000, "foo")); 51 | } 52 | 53 | TEST_F(FilterBlockTest, SingleChunk) { 54 | FilterBlockBuilder builder(&policy_); 55 | builder.StartBlock(100); 56 | builder.AddKey("foo"); 57 | builder.AddKey("bar"); 58 | builder.AddKey("box"); 59 | builder.StartBlock(200); 60 | builder.AddKey("box"); 61 | builder.StartBlock(300); 62 | builder.AddKey("hello"); 63 | Slice block = builder.Finish(); 64 | FilterBlockReader reader(&policy_, block); 65 | ASSERT_TRUE(reader.KeyMayMatch(100, "foo")); 66 | ASSERT_TRUE(reader.KeyMayMatch(100, "bar")); 67 | ASSERT_TRUE(reader.KeyMayMatch(100, "box")); 68 | ASSERT_TRUE(reader.KeyMayMatch(100, "hello")); 69 | ASSERT_TRUE(reader.KeyMayMatch(100, "foo")); 70 | ASSERT_TRUE(!reader.KeyMayMatch(100, "missing")); 71 | ASSERT_TRUE(!reader.KeyMayMatch(100, "other")); 72 | } 73 | 74 | TEST_F(FilterBlockTest, MultiChunk) { 75 | FilterBlockBuilder builder(&policy_); 76 | 77 | // First filter 78 | builder.StartBlock(0); 79 | builder.AddKey("foo"); 80 | builder.StartBlock(2000); 81 | builder.AddKey("bar"); 82 | 83 | // Second filter 84 | builder.StartBlock(3100); 85 | builder.AddKey("box"); 86 | 87 | // Third filter is empty 88 | 89 | // Last filter 90 | builder.StartBlock(9000); 91 | builder.AddKey("box"); 92 | builder.AddKey("hello"); 93 | 94 | Slice block = builder.Finish(); 95 | FilterBlockReader reader(&policy_, block); 96 | 97 | // Check first filter 98 | ASSERT_TRUE(reader.KeyMayMatch(0, "foo")); 99 | ASSERT_TRUE(reader.KeyMayMatch(2000, "bar")); 100 | ASSERT_TRUE(!reader.KeyMayMatch(0, "box")); 101 | ASSERT_TRUE(!reader.KeyMayMatch(0, "hello")); 102 | 103 | // Check second filter 104 | ASSERT_TRUE(reader.KeyMayMatch(3100, "box")); 105 | ASSERT_TRUE(!reader.KeyMayMatch(3100, "foo")); 106 | ASSERT_TRUE(!reader.KeyMayMatch(3100, "bar")); 107 | ASSERT_TRUE(!reader.KeyMayMatch(3100, "hello")); 108 | 109 | // Check third filter (empty) 110 | ASSERT_TRUE(!reader.KeyMayMatch(4100, "foo")); 111 | ASSERT_TRUE(!reader.KeyMayMatch(4100, "bar")); 112 | ASSERT_TRUE(!reader.KeyMayMatch(4100, "box")); 113 | ASSERT_TRUE(!reader.KeyMayMatch(4100, "hello")); 114 | 115 | // Check last filter 116 | ASSERT_TRUE(reader.KeyMayMatch(9000, "box")); 117 | ASSERT_TRUE(reader.KeyMayMatch(9000, "hello")); 118 | ASSERT_TRUE(!reader.KeyMayMatch(9000, "foo")); 119 | ASSERT_TRUE(!reader.KeyMayMatch(9000, "bar")); 120 | } 121 | 122 | } // namespace leveldb 123 | 124 | int main(int argc, char** argv) { 125 | testing::InitGoogleTest(&argc, argv); 126 | return RUN_ALL_TESTS(); 127 | } 128 | -------------------------------------------------------------------------------- /table/format.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_FORMAT_H_ 6 | #define STORAGE_LEVELDB_TABLE_FORMAT_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "leveldb/slice.h" 12 | #include "leveldb/status.h" 13 | #include "leveldb/table_builder.h" 14 | 15 | namespace leveldb { 16 | 17 | class Block; 18 | class RandomAccessFile; 19 | struct ReadOptions; 20 | 21 | // BlockHandle is a pointer to the extent of a file that stores a data 22 | // block or a meta block. 23 | class BlockHandle { 24 | public: 25 | // Maximum encoding length of a BlockHandle 26 | enum { kMaxEncodedLength = 10 + 10 }; 27 | 28 | BlockHandle(); 29 | 30 | // The offset of the block in the file. 31 | uint64_t offset() const { return offset_; } 32 | void set_offset(uint64_t offset) { offset_ = offset; } 33 | 34 | // The size of the stored block 35 | uint64_t size() const { return size_; } 36 | void set_size(uint64_t size) { size_ = size; } 37 | 38 | void EncodeTo(std::string* dst) const; 39 | Status DecodeFrom(Slice* input); 40 | 41 | private: 42 | uint64_t offset_; 43 | uint64_t size_; 44 | }; 45 | 46 | // Footer encapsulates the fixed information stored at the tail 47 | // end of every table file. 48 | class Footer { 49 | public: 50 | // Encoded length of a Footer. Note that the serialization of a 51 | // Footer will always occupy exactly this many bytes. It consists 52 | // of two block handles and a magic number. 53 | enum { kEncodedLength = 2 * BlockHandle::kMaxEncodedLength + 8 }; 54 | 55 | Footer() = default; 56 | 57 | // The block handle for the metaindex block of the table 58 | const BlockHandle& metaindex_handle() const { return metaindex_handle_; } 59 | void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; } 60 | 61 | // The block handle for the index block of the table 62 | const BlockHandle& index_handle() const { return index_handle_; } 63 | void set_index_handle(const BlockHandle& h) { index_handle_ = h; } 64 | 65 | void EncodeTo(std::string* dst) const; 66 | Status DecodeFrom(Slice* input); 67 | 68 | private: 69 | BlockHandle metaindex_handle_; 70 | BlockHandle index_handle_; 71 | }; 72 | 73 | // kTableMagicNumber was picked by running 74 | // echo http://code.google.com/p/leveldb/ | sha1sum 75 | // and taking the leading 64 bits. 76 | static const uint64_t kTableMagicNumber = 0xdb4775248b80fb57ull; 77 | 78 | // 1-byte type + 32-bit crc 79 | static const size_t kBlockTrailerSize = 5; 80 | 81 | struct BlockContents { 82 | Slice data; // Actual contents of data 83 | bool cachable; // True iff data can be cached 84 | bool heap_allocated; // True iff caller should delete[] data.data() 85 | }; 86 | 87 | // Read the block identified by "handle" from "file". On failure 88 | // return non-OK. On success fill *result and return OK. 89 | Status ReadBlock(RandomAccessFile* file, const ReadOptions& options, 90 | const BlockHandle& handle, BlockContents* result); 91 | 92 | // Implementation details follow. Clients should ignore, 93 | 94 | inline BlockHandle::BlockHandle() 95 | : offset_(~static_cast(0)), size_(~static_cast(0)) {} 96 | 97 | } // namespace leveldb 98 | 99 | #endif // STORAGE_LEVELDB_TABLE_FORMAT_H_ 100 | -------------------------------------------------------------------------------- /table/iterator.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/iterator.h" 6 | 7 | namespace leveldb { 8 | 9 | Iterator::Iterator() { 10 | cleanup_head_.function = nullptr; 11 | cleanup_head_.next = nullptr; 12 | } 13 | 14 | Iterator::~Iterator() { 15 | if (!cleanup_head_.IsEmpty()) { 16 | cleanup_head_.Run(); 17 | for (CleanupNode* node = cleanup_head_.next; node != nullptr;) { 18 | node->Run(); 19 | CleanupNode* next_node = node->next; 20 | delete node; 21 | node = next_node; 22 | } 23 | } 24 | } 25 | 26 | void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) { 27 | assert(func != nullptr); 28 | CleanupNode* node; 29 | if (cleanup_head_.IsEmpty()) { 30 | node = &cleanup_head_; 31 | } else { 32 | node = new CleanupNode(); 33 | node->next = cleanup_head_.next; 34 | cleanup_head_.next = node; 35 | } 36 | node->function = func; 37 | node->arg1 = arg1; 38 | node->arg2 = arg2; 39 | } 40 | 41 | namespace { 42 | 43 | class EmptyIterator : public Iterator { 44 | public: 45 | EmptyIterator(const Status& s) : status_(s) {} 46 | ~EmptyIterator() override = default; 47 | 48 | bool Valid() const override { return false; } 49 | void Seek(const Slice& target) override {} 50 | void SeekToFirst() override {} 51 | void SeekToLast() override {} 52 | void Next() override { assert(false); } 53 | void Prev() override { assert(false); } 54 | Slice key() const override { 55 | assert(false); 56 | return Slice(); 57 | } 58 | Slice value() const override { 59 | assert(false); 60 | return Slice(); 61 | } 62 | Status status() const override { return status_; } 63 | 64 | private: 65 | Status status_; 66 | }; 67 | 68 | } // anonymous namespace 69 | 70 | Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); } 71 | 72 | Iterator* NewErrorIterator(const Status& status) { 73 | return new EmptyIterator(status); 74 | } 75 | 76 | } // namespace leveldb 77 | -------------------------------------------------------------------------------- /table/iterator_wrapper.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_ 6 | #define STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_ 7 | 8 | #include "leveldb/iterator.h" 9 | #include "leveldb/slice.h" 10 | 11 | namespace leveldb { 12 | 13 | // A internal wrapper class with an interface similar to Iterator that 14 | // caches the valid() and key() results for an underlying iterator. 15 | // This can help avoid virtual function calls and also gives better 16 | // cache locality. 17 | class IteratorWrapper { 18 | public: 19 | IteratorWrapper() : iter_(nullptr), valid_(false) {} 20 | explicit IteratorWrapper(Iterator* iter) : iter_(nullptr) { Set(iter); } 21 | ~IteratorWrapper() { delete iter_; } 22 | Iterator* iter() const { return iter_; } 23 | 24 | // Takes ownership of "iter" and will delete it when destroyed, or 25 | // when Set() is invoked again. 26 | void Set(Iterator* iter) { 27 | delete iter_; 28 | iter_ = iter; 29 | if (iter_ == nullptr) { 30 | valid_ = false; 31 | } else { 32 | Update(); 33 | } 34 | } 35 | 36 | // Iterator interface methods 37 | bool Valid() const { return valid_; } 38 | Slice key() const { 39 | assert(Valid()); 40 | return key_; 41 | } 42 | Slice value() const { 43 | assert(Valid()); 44 | return iter_->value(); 45 | } 46 | // Methods below require iter() != nullptr 47 | Status status() const { 48 | assert(iter_); 49 | return iter_->status(); 50 | } 51 | void Next() { 52 | assert(iter_); 53 | iter_->Next(); 54 | Update(); 55 | } 56 | void Prev() { 57 | assert(iter_); 58 | iter_->Prev(); 59 | Update(); 60 | } 61 | void Seek(const Slice& k) { 62 | assert(iter_); 63 | iter_->Seek(k); 64 | Update(); 65 | } 66 | void SeekToFirst() { 67 | assert(iter_); 68 | iter_->SeekToFirst(); 69 | Update(); 70 | } 71 | void SeekToLast() { 72 | assert(iter_); 73 | iter_->SeekToLast(); 74 | Update(); 75 | } 76 | 77 | private: 78 | void Update() { 79 | valid_ = iter_->Valid(); 80 | if (valid_) { 81 | key_ = iter_->key(); 82 | } 83 | } 84 | 85 | Iterator* iter_; 86 | bool valid_; 87 | Slice key_; 88 | }; 89 | 90 | } // namespace leveldb 91 | 92 | #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_ 93 | -------------------------------------------------------------------------------- /table/merger.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_MERGER_H_ 6 | #define STORAGE_LEVELDB_TABLE_MERGER_H_ 7 | 8 | namespace leveldb { 9 | 10 | class Comparator; 11 | class Iterator; 12 | 13 | // Return an iterator that provided the union of the data in 14 | // children[0,n-1]. Takes ownership of the child iterators and 15 | // will delete them when the result iterator is deleted. 16 | // 17 | // The result does no duplicate suppression. I.e., if a particular 18 | // key is present in K child iterators, it will be yielded K times. 19 | // 20 | // REQUIRES: n >= 0 21 | Iterator* NewMergingIterator(const Comparator* comparator, Iterator** children, 22 | int n); 23 | 24 | } // namespace leveldb 25 | 26 | #endif // STORAGE_LEVELDB_TABLE_MERGER_H_ 27 | -------------------------------------------------------------------------------- /table/two_level_iterator.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_ 6 | #define STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_ 7 | 8 | #include "leveldb/iterator.h" 9 | 10 | namespace leveldb { 11 | 12 | struct ReadOptions; 13 | 14 | // Return a new two level iterator. A two-level iterator contains an 15 | // index iterator whose values point to a sequence of blocks where 16 | // each block is itself a sequence of key,value pairs. The returned 17 | // two-level iterator yields the concatenation of all key/value pairs 18 | // in the sequence of blocks. Takes ownership of "index_iter" and 19 | // will delete it when no longer needed. 20 | // 21 | // Uses a supplied function to convert an index_iter value into 22 | // an iterator over the contents of the corresponding block. 23 | Iterator* NewTwoLevelIterator( 24 | Iterator* index_iter, 25 | Iterator* (*block_function)(void* arg, const ReadOptions& options, 26 | const Slice& index_value), 27 | void* arg, const ReadOptions& options); 28 | 29 | } // namespace leveldb 30 | 31 | #endif // STORAGE_LEVELDB_TABLE_TWO_LEVEL_ITERATOR_H_ 32 | -------------------------------------------------------------------------------- /util/arena.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/arena.h" 6 | 7 | namespace leveldb { 8 | 9 | static const int kBlockSize = 4096; 10 | 11 | Arena::Arena() 12 | : alloc_ptr_(nullptr), alloc_bytes_remaining_(0), memory_usage_(0) {} 13 | 14 | Arena::~Arena() { 15 | for (size_t i = 0; i < blocks_.size(); i++) { 16 | delete[] blocks_[i]; 17 | } 18 | } 19 | 20 | char* Arena::AllocateFallback(size_t bytes) { 21 | if (bytes > kBlockSize / 4) { 22 | // Object is more than a quarter of our block size. Allocate it separately 23 | // to avoid wasting too much space in leftover bytes. 24 | char* result = AllocateNewBlock(bytes); 25 | return result; 26 | } 27 | 28 | // We waste the remaining space in the current block. 29 | alloc_ptr_ = AllocateNewBlock(kBlockSize); 30 | alloc_bytes_remaining_ = kBlockSize; 31 | 32 | char* result = alloc_ptr_; 33 | alloc_ptr_ += bytes; 34 | alloc_bytes_remaining_ -= bytes; 35 | return result; 36 | } 37 | 38 | char* Arena::AllocateAligned(size_t bytes) { 39 | const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8; 40 | static_assert((align & (align - 1)) == 0, 41 | "Pointer size should be a power of 2"); 42 | size_t current_mod = reinterpret_cast(alloc_ptr_) & (align - 1); 43 | size_t slop = (current_mod == 0 ? 0 : align - current_mod); 44 | size_t needed = bytes + slop; 45 | char* result; 46 | if (needed <= alloc_bytes_remaining_) { 47 | result = alloc_ptr_ + slop; 48 | alloc_ptr_ += needed; 49 | alloc_bytes_remaining_ -= needed; 50 | } else { 51 | // AllocateFallback always returned aligned memory 52 | result = AllocateFallback(bytes); 53 | } 54 | assert((reinterpret_cast(result) & (align - 1)) == 0); 55 | return result; 56 | } 57 | 58 | char* Arena::AllocateNewBlock(size_t block_bytes) { 59 | char* result = new char[block_bytes]; 60 | blocks_.push_back(result); 61 | memory_usage_.fetch_add(block_bytes + sizeof(char*), 62 | std::memory_order_relaxed); 63 | return result; 64 | } 65 | 66 | } // namespace leveldb 67 | -------------------------------------------------------------------------------- /util/arena.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_ARENA_H_ 6 | #define STORAGE_LEVELDB_UTIL_ARENA_H_ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace leveldb { 15 | 16 | class Arena { 17 | public: 18 | Arena(); 19 | 20 | Arena(const Arena&) = delete; 21 | Arena& operator=(const Arena&) = delete; 22 | 23 | ~Arena(); 24 | 25 | // Return a pointer to a newly allocated memory block of "bytes" bytes. 26 | char* Allocate(size_t bytes); 27 | 28 | // Allocate memory with the normal alignment guarantees provided by malloc. 29 | char* AllocateAligned(size_t bytes); 30 | 31 | // Returns an estimate of the total memory usage of data allocated 32 | // by the arena. 33 | size_t MemoryUsage() const { 34 | return memory_usage_.load(std::memory_order_relaxed); 35 | } 36 | 37 | private: 38 | char* AllocateFallback(size_t bytes); 39 | char* AllocateNewBlock(size_t block_bytes); 40 | 41 | // Allocation state 42 | char* alloc_ptr_; 43 | size_t alloc_bytes_remaining_; 44 | 45 | // Array of new[] allocated memory blocks 46 | std::vector blocks_; 47 | 48 | // Total memory usage of the arena. 49 | // 50 | // TODO(costan): This member is accessed via atomics, but the others are 51 | // accessed without any locking. Is this OK? 52 | std::atomic memory_usage_; 53 | }; 54 | 55 | inline char* Arena::Allocate(size_t bytes) { 56 | // The semantics of what to return are a bit messy if we allow 57 | // 0-byte allocations, so we disallow them here (we don't need 58 | // them for our internal use). 59 | assert(bytes > 0); 60 | if (bytes <= alloc_bytes_remaining_) { 61 | char* result = alloc_ptr_; 62 | alloc_ptr_ += bytes; 63 | alloc_bytes_remaining_ -= bytes; 64 | return result; 65 | } 66 | return AllocateFallback(bytes); 67 | } 68 | 69 | } // namespace leveldb 70 | 71 | #endif // STORAGE_LEVELDB_UTIL_ARENA_H_ 72 | -------------------------------------------------------------------------------- /util/arena_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/arena.h" 6 | 7 | #include "gtest/gtest.h" 8 | #include "util/random.h" 9 | 10 | namespace leveldb { 11 | 12 | TEST(ArenaTest, Empty) { Arena arena; } 13 | 14 | TEST(ArenaTest, Simple) { 15 | std::vector> allocated; 16 | Arena arena; 17 | const int N = 100000; 18 | size_t bytes = 0; 19 | Random rnd(301); 20 | for (int i = 0; i < N; i++) { 21 | size_t s; 22 | if (i % (N / 10) == 0) { 23 | s = i; 24 | } else { 25 | s = rnd.OneIn(4000) 26 | ? rnd.Uniform(6000) 27 | : (rnd.OneIn(10) ? rnd.Uniform(100) : rnd.Uniform(20)); 28 | } 29 | if (s == 0) { 30 | // Our arena disallows size 0 allocations. 31 | s = 1; 32 | } 33 | char* r; 34 | if (rnd.OneIn(10)) { 35 | r = arena.AllocateAligned(s); 36 | } else { 37 | r = arena.Allocate(s); 38 | } 39 | 40 | for (size_t b = 0; b < s; b++) { 41 | // Fill the "i"th allocation with a known bit pattern 42 | r[b] = i % 256; 43 | } 44 | bytes += s; 45 | allocated.push_back(std::make_pair(s, r)); 46 | ASSERT_GE(arena.MemoryUsage(), bytes); 47 | if (i > N / 10) { 48 | ASSERT_LE(arena.MemoryUsage(), bytes * 1.10); 49 | } 50 | } 51 | for (size_t i = 0; i < allocated.size(); i++) { 52 | size_t num_bytes = allocated[i].first; 53 | const char* p = allocated[i].second; 54 | for (size_t b = 0; b < num_bytes; b++) { 55 | // Check the "i"th allocation for the known bit pattern 56 | ASSERT_EQ(int(p[b]) & 0xff, i % 256); 57 | } 58 | } 59 | } 60 | 61 | } // namespace leveldb 62 | 63 | int main(int argc, char** argv) { 64 | testing::InitGoogleTest(&argc, argv); 65 | return RUN_ALL_TESTS(); 66 | } 67 | -------------------------------------------------------------------------------- /util/bloom.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/filter_policy.h" 6 | 7 | #include "leveldb/slice.h" 8 | #include "util/hash.h" 9 | 10 | namespace leveldb { 11 | 12 | namespace { 13 | static uint32_t BloomHash(const Slice& key) { 14 | return Hash(key.data(), key.size(), 0xbc9f1d34); 15 | } 16 | 17 | class BloomFilterPolicy : public FilterPolicy { 18 | public: 19 | explicit BloomFilterPolicy(int bits_per_key) : bits_per_key_(bits_per_key) { 20 | // We intentionally round down to reduce probing cost a little bit 21 | k_ = static_cast(bits_per_key * 0.69); // 0.69 =~ ln(2) 22 | if (k_ < 1) k_ = 1; 23 | if (k_ > 30) k_ = 30; 24 | } 25 | 26 | const char* Name() const override { return "leveldb.BuiltinBloomFilter2"; } 27 | 28 | void CreateFilter(const Slice* keys, int n, std::string* dst) const override { 29 | // Compute bloom filter size (in both bits and bytes) 30 | size_t bits = n * bits_per_key_; 31 | 32 | // For small n, we can see a very high false positive rate. Fix it 33 | // by enforcing a minimum bloom filter length. 34 | if (bits < 64) bits = 64; 35 | 36 | size_t bytes = (bits + 7) / 8; 37 | bits = bytes * 8; 38 | 39 | const size_t init_size = dst->size(); 40 | dst->resize(init_size + bytes, 0); 41 | dst->push_back(static_cast(k_)); // Remember # of probes in filter 42 | char* array = &(*dst)[init_size]; 43 | for (int i = 0; i < n; i++) { 44 | // Use double-hashing to generate a sequence of hash values. 45 | // See analysis in [Kirsch,Mitzenmacher 2006]. 46 | uint32_t h = BloomHash(keys[i]); 47 | const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits 48 | for (size_t j = 0; j < k_; j++) { 49 | const uint32_t bitpos = h % bits; 50 | array[bitpos / 8] |= (1 << (bitpos % 8)); 51 | h += delta; 52 | } 53 | } 54 | } 55 | 56 | bool KeyMayMatch(const Slice& key, const Slice& bloom_filter) const override { 57 | const size_t len = bloom_filter.size(); 58 | if (len < 2) return false; 59 | 60 | const char* array = bloom_filter.data(); 61 | const size_t bits = (len - 1) * 8; 62 | 63 | // Use the encoded k so that we can read filters generated by 64 | // bloom filters created using different parameters. 65 | const size_t k = array[len - 1]; 66 | if (k > 30) { 67 | // Reserved for potentially new encodings for short bloom filters. 68 | // Consider it a match. 69 | return true; 70 | } 71 | 72 | uint32_t h = BloomHash(key); 73 | const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits 74 | for (size_t j = 0; j < k; j++) { 75 | const uint32_t bitpos = h % bits; 76 | if ((array[bitpos / 8] & (1 << (bitpos % 8))) == 0) return false; 77 | h += delta; 78 | } 79 | return true; 80 | } 81 | 82 | private: 83 | size_t bits_per_key_; 84 | size_t k_; 85 | }; 86 | } // namespace 87 | 88 | const FilterPolicy* NewBloomFilterPolicy(int bits_per_key) { 89 | return new BloomFilterPolicy(bits_per_key); 90 | } 91 | 92 | } // namespace leveldb 93 | -------------------------------------------------------------------------------- /util/comparator.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/comparator.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "leveldb/slice.h" 13 | #include "util/logging.h" 14 | #include "util/no_destructor.h" 15 | 16 | namespace leveldb { 17 | 18 | Comparator::~Comparator() = default; 19 | 20 | namespace { 21 | class BytewiseComparatorImpl : public Comparator { 22 | public: 23 | BytewiseComparatorImpl() = default; 24 | 25 | const char* Name() const override { return "leveldb.BytewiseComparator"; } 26 | 27 | int Compare(const Slice& a, const Slice& b) const override { 28 | return a.compare(b); 29 | } 30 | 31 | void FindShortestSeparator(std::string* start, 32 | const Slice& limit) const override { 33 | // Find length of common prefix 34 | size_t min_length = std::min(start->size(), limit.size()); 35 | size_t diff_index = 0; 36 | while ((diff_index < min_length) && 37 | ((*start)[diff_index] == limit[diff_index])) { 38 | diff_index++; 39 | } 40 | 41 | if (diff_index >= min_length) { 42 | // Do not shorten if one string is a prefix of the other 43 | } else { 44 | uint8_t diff_byte = static_cast((*start)[diff_index]); 45 | if (diff_byte < static_cast(0xff) && 46 | diff_byte + 1 < static_cast(limit[diff_index])) { 47 | (*start)[diff_index]++; 48 | start->resize(diff_index + 1); 49 | assert(Compare(*start, limit) < 0); 50 | } 51 | } 52 | } 53 | 54 | void FindShortSuccessor(std::string* key) const override { 55 | // Find first character that can be incremented 56 | size_t n = key->size(); 57 | for (size_t i = 0; i < n; i++) { 58 | const uint8_t byte = (*key)[i]; 59 | if (byte != static_cast(0xff)) { 60 | (*key)[i] = byte + 1; 61 | key->resize(i + 1); 62 | return; 63 | } 64 | } 65 | // *key is a run of 0xffs. Leave it alone. 66 | } 67 | }; 68 | } // namespace 69 | 70 | const Comparator* BytewiseComparator() { 71 | static NoDestructor singleton; 72 | return singleton.get(); 73 | } 74 | 75 | } // namespace leveldb 76 | -------------------------------------------------------------------------------- /util/crc32c.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_CRC32C_H_ 6 | #define STORAGE_LEVELDB_UTIL_CRC32C_H_ 7 | 8 | #include 9 | #include 10 | 11 | namespace leveldb { 12 | namespace crc32c { 13 | 14 | // Return the crc32c of concat(A, data[0,n-1]) where init_crc is the 15 | // crc32c of some string A. Extend() is often used to maintain the 16 | // crc32c of a stream of data. 17 | uint32_t Extend(uint32_t init_crc, const char* data, size_t n); 18 | 19 | // Return the crc32c of data[0,n-1] 20 | inline uint32_t Value(const char* data, size_t n) { return Extend(0, data, n); } 21 | 22 | static const uint32_t kMaskDelta = 0xa282ead8ul; 23 | 24 | // Return a masked representation of crc. 25 | // 26 | // Motivation: it is problematic to compute the CRC of a string that 27 | // contains embedded CRCs. Therefore we recommend that CRCs stored 28 | // somewhere (e.g., in files) should be masked before being stored. 29 | inline uint32_t Mask(uint32_t crc) { 30 | // Rotate right by 15 bits and add a constant. 31 | return ((crc >> 15) | (crc << 17)) + kMaskDelta; 32 | } 33 | 34 | // Return the crc whose masked representation is masked_crc. 35 | inline uint32_t Unmask(uint32_t masked_crc) { 36 | uint32_t rot = masked_crc - kMaskDelta; 37 | return ((rot >> 17) | (rot << 15)); 38 | } 39 | 40 | } // namespace crc32c 41 | } // namespace leveldb 42 | 43 | #endif // STORAGE_LEVELDB_UTIL_CRC32C_H_ 44 | -------------------------------------------------------------------------------- /util/crc32c_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/crc32c.h" 6 | 7 | #include "gtest/gtest.h" 8 | 9 | namespace leveldb { 10 | namespace crc32c { 11 | 12 | TEST(CRC, StandardResults) { 13 | // From rfc3720 section B.4. 14 | char buf[32]; 15 | 16 | memset(buf, 0, sizeof(buf)); 17 | ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf))); 18 | 19 | memset(buf, 0xff, sizeof(buf)); 20 | ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf))); 21 | 22 | for (int i = 0; i < 32; i++) { 23 | buf[i] = i; 24 | } 25 | ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf))); 26 | 27 | for (int i = 0; i < 32; i++) { 28 | buf[i] = 31 - i; 29 | } 30 | ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf))); 31 | 32 | uint8_t data[48] = { 33 | 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 35 | 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 37 | }; 38 | ASSERT_EQ(0xd9963a56, Value(reinterpret_cast(data), sizeof(data))); 39 | } 40 | 41 | TEST(CRC, Values) { ASSERT_NE(Value("a", 1), Value("foo", 3)); } 42 | 43 | TEST(CRC, Extend) { 44 | ASSERT_EQ(Value("hello world", 11), Extend(Value("hello ", 6), "world", 5)); 45 | } 46 | 47 | TEST(CRC, Mask) { 48 | uint32_t crc = Value("foo", 3); 49 | ASSERT_NE(crc, Mask(crc)); 50 | ASSERT_NE(crc, Mask(Mask(crc))); 51 | ASSERT_EQ(crc, Unmask(Mask(crc))); 52 | ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc))))); 53 | } 54 | 55 | } // namespace crc32c 56 | } // namespace leveldb 57 | 58 | int main(int argc, char** argv) { 59 | testing::InitGoogleTest(&argc, argv); 60 | return RUN_ALL_TESTS(); 61 | } 62 | -------------------------------------------------------------------------------- /util/env.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/env.h" 6 | 7 | #include 8 | 9 | // This workaround can be removed when leveldb::Env::DeleteFile is removed. 10 | // See env.h for justification. 11 | #if defined(_WIN32) && defined(LEVELDB_DELETEFILE_UNDEFINED) 12 | #undef DeleteFile 13 | #endif 14 | 15 | namespace leveldb { 16 | 17 | Env::Env() = default; 18 | 19 | Env::~Env() = default; 20 | 21 | Status Env::NewAppendableFile(const std::string& fname, WritableFile** result) { 22 | return Status::NotSupported("NewAppendableFile", fname); 23 | } 24 | 25 | Status Env::RemoveDir(const std::string& dirname) { return DeleteDir(dirname); } 26 | Status Env::DeleteDir(const std::string& dirname) { return RemoveDir(dirname); } 27 | 28 | Status Env::RemoveFile(const std::string& fname) { return DeleteFile(fname); } 29 | Status Env::DeleteFile(const std::string& fname) { return RemoveFile(fname); } 30 | 31 | SequentialFile::~SequentialFile() = default; 32 | 33 | RandomAccessFile::~RandomAccessFile() = default; 34 | 35 | WritableFile::~WritableFile() = default; 36 | 37 | Logger::~Logger() = default; 38 | 39 | FileLock::~FileLock() = default; 40 | 41 | void Log(Logger* info_log, const char* format, ...) { 42 | if (info_log != nullptr) { 43 | std::va_list ap; 44 | va_start(ap, format); 45 | info_log->Logv(format, ap); 46 | va_end(ap); 47 | } 48 | } 49 | 50 | static Status DoWriteStringToFile(Env* env, const Slice& data, 51 | const std::string& fname, bool should_sync) { 52 | WritableFile* file; 53 | Status s = env->NewWritableFile(fname, &file); 54 | if (!s.ok()) { 55 | return s; 56 | } 57 | s = file->Append(data); 58 | if (s.ok() && should_sync) { 59 | s = file->Sync(); 60 | } 61 | if (s.ok()) { 62 | s = file->Close(); 63 | } 64 | delete file; // Will auto-close if we did not close above 65 | if (!s.ok()) { 66 | env->RemoveFile(fname); 67 | } 68 | return s; 69 | } 70 | 71 | Status WriteStringToFile(Env* env, const Slice& data, 72 | const std::string& fname) { 73 | return DoWriteStringToFile(env, data, fname, false); 74 | } 75 | 76 | Status WriteStringToFileSync(Env* env, const Slice& data, 77 | const std::string& fname) { 78 | return DoWriteStringToFile(env, data, fname, true); 79 | } 80 | 81 | Status ReadFileToString(Env* env, const std::string& fname, std::string* data) { 82 | data->clear(); 83 | SequentialFile* file; 84 | Status s = env->NewSequentialFile(fname, &file); 85 | if (!s.ok()) { 86 | return s; 87 | } 88 | static const int kBufferSize = 8192; 89 | char* space = new char[kBufferSize]; 90 | while (true) { 91 | Slice fragment; 92 | s = file->Read(kBufferSize, &fragment, space); 93 | if (!s.ok()) { 94 | break; 95 | } 96 | data->append(fragment.data(), fragment.size()); 97 | if (fragment.empty()) { 98 | break; 99 | } 100 | } 101 | delete[] space; 102 | delete file; 103 | return s; 104 | } 105 | 106 | EnvWrapper::~EnvWrapper() {} 107 | 108 | } // namespace leveldb 109 | -------------------------------------------------------------------------------- /util/env_posix_test_helper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_ENV_POSIX_TEST_HELPER_H_ 6 | #define STORAGE_LEVELDB_UTIL_ENV_POSIX_TEST_HELPER_H_ 7 | 8 | namespace leveldb { 9 | 10 | class EnvPosixTest; 11 | 12 | // A helper for the POSIX Env to facilitate testing. 13 | class EnvPosixTestHelper { 14 | private: 15 | friend class EnvPosixTest; 16 | 17 | // Set the maximum number of read-only files that will be opened. 18 | // Must be called before creating an Env. 19 | static void SetReadOnlyFDLimit(int limit); 20 | 21 | // Set the maximum number of read-only files that will be mapped via mmap. 22 | // Must be called before creating an Env. 23 | static void SetReadOnlyMMapLimit(int limit); 24 | }; 25 | 26 | } // namespace leveldb 27 | 28 | #endif // STORAGE_LEVELDB_UTIL_ENV_POSIX_TEST_HELPER_H_ 29 | -------------------------------------------------------------------------------- /util/env_windows_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "gtest/gtest.h" 6 | #include "leveldb/env.h" 7 | #include "port/port.h" 8 | #include "util/env_windows_test_helper.h" 9 | #include "util/testutil.h" 10 | 11 | namespace leveldb { 12 | 13 | static const int kMMapLimit = 4; 14 | 15 | class EnvWindowsTest : public testing::Test { 16 | public: 17 | static void SetFileLimits(int mmap_limit) { 18 | EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit); 19 | } 20 | 21 | EnvWindowsTest() : env_(Env::Default()) {} 22 | 23 | Env* env_; 24 | }; 25 | 26 | TEST_F(EnvWindowsTest, TestOpenOnRead) { 27 | // Write some test data to a single file that will be opened |n| times. 28 | std::string test_dir; 29 | ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir)); 30 | std::string test_file = test_dir + "/open_on_read.txt"; 31 | 32 | FILE* f = std::fopen(test_file.c_str(), "w"); 33 | ASSERT_TRUE(f != nullptr); 34 | const char kFileData[] = "abcdefghijklmnopqrstuvwxyz"; 35 | fputs(kFileData, f); 36 | std::fclose(f); 37 | 38 | // Open test file some number above the sum of the two limits to force 39 | // leveldb::WindowsEnv to switch from mapping the file into memory 40 | // to basic file reading. 41 | const int kNumFiles = kMMapLimit + 5; 42 | leveldb::RandomAccessFile* files[kNumFiles] = {0}; 43 | for (int i = 0; i < kNumFiles; i++) { 44 | ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(test_file, &files[i])); 45 | } 46 | char scratch; 47 | Slice read_result; 48 | for (int i = 0; i < kNumFiles; i++) { 49 | ASSERT_LEVELDB_OK(files[i]->Read(i, 1, &read_result, &scratch)); 50 | ASSERT_EQ(kFileData[i], read_result[0]); 51 | } 52 | for (int i = 0; i < kNumFiles; i++) { 53 | delete files[i]; 54 | } 55 | ASSERT_LEVELDB_OK(env_->RemoveFile(test_file)); 56 | } 57 | 58 | } // namespace leveldb 59 | 60 | int main(int argc, char** argv) { 61 | // All tests currently run with the same read-only file limits. 62 | leveldb::EnvWindowsTest::SetFileLimits(leveldb::kMMapLimit); 63 | testing::InitGoogleTest(&argc, argv); 64 | return RUN_ALL_TESTS(); 65 | } 66 | -------------------------------------------------------------------------------- /util/env_windows_test_helper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 (c) The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_ENV_WINDOWS_TEST_HELPER_H_ 6 | #define STORAGE_LEVELDB_UTIL_ENV_WINDOWS_TEST_HELPER_H_ 7 | 8 | namespace leveldb { 9 | 10 | class EnvWindowsTest; 11 | 12 | // A helper for the Windows Env to facilitate testing. 13 | class EnvWindowsTestHelper { 14 | private: 15 | friend class CorruptionTest; 16 | friend class EnvWindowsTest; 17 | 18 | // Set the maximum number of read-only files that will be mapped via mmap. 19 | // Must be called before creating an Env. 20 | static void SetReadOnlyMMapLimit(int limit); 21 | }; 22 | 23 | } // namespace leveldb 24 | 25 | #endif // STORAGE_LEVELDB_UTIL_ENV_WINDOWS_TEST_HELPER_H_ 26 | -------------------------------------------------------------------------------- /util/filter_policy.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/filter_policy.h" 6 | 7 | namespace leveldb { 8 | 9 | FilterPolicy::~FilterPolicy() {} 10 | 11 | } // namespace leveldb 12 | -------------------------------------------------------------------------------- /util/hash.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/hash.h" 6 | 7 | #include 8 | 9 | #include "util/coding.h" 10 | 11 | // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through 12 | // between switch labels. The real definition should be provided externally. 13 | // This one is a fallback version for unsupported compilers. 14 | #ifndef FALLTHROUGH_INTENDED 15 | #define FALLTHROUGH_INTENDED \ 16 | do { \ 17 | } while (0) 18 | #endif 19 | 20 | namespace leveldb { 21 | 22 | uint32_t Hash(const char* data, size_t n, uint32_t seed) { 23 | // Similar to murmur hash 24 | const uint32_t m = 0xc6a4a793; 25 | const uint32_t r = 24; 26 | const char* limit = data + n; 27 | uint32_t h = seed ^ (n * m); 28 | 29 | // Pick up four bytes at a time 30 | while (data + 4 <= limit) { 31 | uint32_t w = DecodeFixed32(data); 32 | data += 4; 33 | h += w; 34 | h *= m; 35 | h ^= (h >> 16); 36 | } 37 | 38 | // Pick up remaining bytes 39 | switch (limit - data) { 40 | case 3: 41 | h += static_cast(data[2]) << 16; 42 | FALLTHROUGH_INTENDED; 43 | case 2: 44 | h += static_cast(data[1]) << 8; 45 | FALLTHROUGH_INTENDED; 46 | case 1: 47 | h += static_cast(data[0]); 48 | h *= m; 49 | h ^= (h >> r); 50 | break; 51 | } 52 | return h; 53 | } 54 | 55 | } // namespace leveldb 56 | -------------------------------------------------------------------------------- /util/hash.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // Simple hash function used for internal data structures 6 | 7 | #ifndef STORAGE_LEVELDB_UTIL_HASH_H_ 8 | #define STORAGE_LEVELDB_UTIL_HASH_H_ 9 | 10 | #include 11 | #include 12 | 13 | namespace leveldb { 14 | 15 | uint32_t Hash(const char* data, size_t n, uint32_t seed); 16 | 17 | } // namespace leveldb 18 | 19 | #endif // STORAGE_LEVELDB_UTIL_HASH_H_ 20 | -------------------------------------------------------------------------------- /util/hash_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/hash.h" 6 | 7 | #include "gtest/gtest.h" 8 | 9 | namespace leveldb { 10 | 11 | TEST(HASH, SignedUnsignedIssue) { 12 | const uint8_t data1[1] = {0x62}; 13 | const uint8_t data2[2] = {0xc3, 0x97}; 14 | const uint8_t data3[3] = {0xe2, 0x99, 0xa5}; 15 | const uint8_t data4[4] = {0xe1, 0x80, 0xb9, 0x32}; 16 | const uint8_t data5[48] = { 17 | 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 18 | 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 19 | 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 21 | }; 22 | 23 | ASSERT_EQ(Hash(0, 0, 0xbc9f1d34), 0xbc9f1d34); 24 | ASSERT_EQ( 25 | Hash(reinterpret_cast(data1), sizeof(data1), 0xbc9f1d34), 26 | 0xef1345c4); 27 | ASSERT_EQ( 28 | Hash(reinterpret_cast(data2), sizeof(data2), 0xbc9f1d34), 29 | 0x5b663814); 30 | ASSERT_EQ( 31 | Hash(reinterpret_cast(data3), sizeof(data3), 0xbc9f1d34), 32 | 0x323c078f); 33 | ASSERT_EQ( 34 | Hash(reinterpret_cast(data4), sizeof(data4), 0xbc9f1d34), 35 | 0xed21633a); 36 | ASSERT_EQ( 37 | Hash(reinterpret_cast(data5), sizeof(data5), 0x12345678), 38 | 0xf333dabb); 39 | } 40 | 41 | } // namespace leveldb 42 | 43 | int main(int argc, char** argv) { 44 | testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /util/histogram.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_HISTOGRAM_H_ 6 | #define STORAGE_LEVELDB_UTIL_HISTOGRAM_H_ 7 | 8 | #include 9 | 10 | namespace leveldb { 11 | 12 | class Histogram { 13 | public: 14 | Histogram() {} 15 | ~Histogram() {} 16 | 17 | void Clear(); 18 | void Add(double value); 19 | void Merge(const Histogram& other); 20 | 21 | std::string ToString() const; 22 | 23 | private: 24 | enum { kNumBuckets = 154 }; 25 | 26 | double Median() const; 27 | double Percentile(double p) const; 28 | double Average() const; 29 | double StandardDeviation() const; 30 | 31 | static const double kBucketLimit[kNumBuckets]; 32 | 33 | double min_; 34 | double max_; 35 | double num_; 36 | double sum_; 37 | double sum_squares_; 38 | 39 | double buckets_[kNumBuckets]; 40 | }; 41 | 42 | } // namespace leveldb 43 | 44 | #endif // STORAGE_LEVELDB_UTIL_HISTOGRAM_H_ 45 | -------------------------------------------------------------------------------- /util/logging.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/logging.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "leveldb/env.h" 13 | #include "leveldb/slice.h" 14 | 15 | namespace leveldb { 16 | 17 | void AppendNumberTo(std::string* str, uint64_t num) { 18 | char buf[30]; 19 | std::snprintf(buf, sizeof(buf), "%llu", static_cast(num)); 20 | str->append(buf); 21 | } 22 | 23 | void AppendEscapedStringTo(std::string* str, const Slice& value) { 24 | for (size_t i = 0; i < value.size(); i++) { 25 | char c = value[i]; 26 | if (c >= ' ' && c <= '~') { 27 | str->push_back(c); 28 | } else { 29 | char buf[10]; 30 | std::snprintf(buf, sizeof(buf), "\\x%02x", 31 | static_cast(c) & 0xff); 32 | str->append(buf); 33 | } 34 | } 35 | } 36 | 37 | std::string NumberToString(uint64_t num) { 38 | std::string r; 39 | AppendNumberTo(&r, num); 40 | return r; 41 | } 42 | 43 | std::string EscapeString(const Slice& value) { 44 | std::string r; 45 | AppendEscapedStringTo(&r, value); 46 | return r; 47 | } 48 | 49 | bool ConsumeDecimalNumber(Slice* in, uint64_t* val) { 50 | // Constants that will be optimized away. 51 | constexpr const uint64_t kMaxUint64 = std::numeric_limits::max(); 52 | constexpr const char kLastDigitOfMaxUint64 = 53 | '0' + static_cast(kMaxUint64 % 10); 54 | 55 | uint64_t value = 0; 56 | 57 | // reinterpret_cast-ing from char* to uint8_t* to avoid signedness. 58 | const uint8_t* start = reinterpret_cast(in->data()); 59 | 60 | const uint8_t* end = start + in->size(); 61 | const uint8_t* current = start; 62 | for (; current != end; ++current) { 63 | const uint8_t ch = *current; 64 | if (ch < '0' || ch > '9') break; 65 | 66 | // Overflow check. 67 | // kMaxUint64 / 10 is also constant and will be optimized away. 68 | if (value > kMaxUint64 / 10 || 69 | (value == kMaxUint64 / 10 && ch > kLastDigitOfMaxUint64)) { 70 | return false; 71 | } 72 | 73 | value = (value * 10) + (ch - '0'); 74 | } 75 | 76 | *val = value; 77 | const size_t digits_consumed = current - start; 78 | in->remove_prefix(digits_consumed); 79 | return digits_consumed != 0; 80 | } 81 | 82 | } // namespace leveldb 83 | -------------------------------------------------------------------------------- /util/logging.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | // 5 | // Must not be included from any .h files to avoid polluting the namespace 6 | // with macros. 7 | 8 | #ifndef STORAGE_LEVELDB_UTIL_LOGGING_H_ 9 | #define STORAGE_LEVELDB_UTIL_LOGGING_H_ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "port/port.h" 16 | 17 | namespace leveldb { 18 | 19 | class Slice; 20 | class WritableFile; 21 | 22 | // Append a human-readable printout of "num" to *str 23 | void AppendNumberTo(std::string* str, uint64_t num); 24 | 25 | // Append a human-readable printout of "value" to *str. 26 | // Escapes any non-printable characters found in "value". 27 | void AppendEscapedStringTo(std::string* str, const Slice& value); 28 | 29 | // Return a human-readable printout of "num" 30 | std::string NumberToString(uint64_t num); 31 | 32 | // Return a human-readable version of "value". 33 | // Escapes any non-printable characters found in "value". 34 | std::string EscapeString(const Slice& value); 35 | 36 | // Parse a human-readable number from "*in" into *value. On success, 37 | // advances "*in" past the consumed number and sets "*val" to the 38 | // numeric value. Otherwise, returns false and leaves *in in an 39 | // unspecified state. 40 | bool ConsumeDecimalNumber(Slice* in, uint64_t* val); 41 | 42 | } // namespace leveldb 43 | 44 | #endif // STORAGE_LEVELDB_UTIL_LOGGING_H_ 45 | -------------------------------------------------------------------------------- /util/mutexlock.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_ 6 | #define STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_ 7 | 8 | #include "port/port.h" 9 | #include "port/thread_annotations.h" 10 | 11 | namespace leveldb { 12 | 13 | // Helper class that locks a mutex on construction and unlocks the mutex when 14 | // the destructor of the MutexLock object is invoked. 15 | // 16 | // Typical usage: 17 | // 18 | // void MyClass::MyMethod() { 19 | // MutexLock l(&mu_); // mu_ is an instance variable 20 | // ... some complex code, possibly with multiple return paths ... 21 | // } 22 | 23 | class SCOPED_LOCKABLE MutexLock { 24 | public: 25 | explicit MutexLock(port::Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { 26 | this->mu_->Lock(); 27 | } 28 | ~MutexLock() UNLOCK_FUNCTION() { this->mu_->Unlock(); } 29 | 30 | MutexLock(const MutexLock&) = delete; 31 | MutexLock& operator=(const MutexLock&) = delete; 32 | 33 | private: 34 | port::Mutex* const mu_; 35 | }; 36 | 37 | class SCOPED_LOCKABLE WLock { 38 | public: 39 | explicit WLock(port::SharedMutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { 40 | this->mu_->UniqueLock(); 41 | } 42 | ~WLock() UNLOCK_FUNCTION() { this->mu_->UniqueUnlock(); } 43 | 44 | WLock(const WLock&) = delete; 45 | WLock& operator=(const WLock&) = delete; 46 | 47 | private: 48 | port::SharedMutex* const mu_; 49 | }; 50 | 51 | } // namespace leveldb 52 | 53 | #endif // STORAGE_LEVELDB_UTIL_MUTEXLOCK_H_ 54 | -------------------------------------------------------------------------------- /util/no_destructor.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 6 | #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 7 | 8 | #include 9 | #include 10 | 11 | namespace leveldb { 12 | 13 | // Wraps an instance whose destructor is never called. 14 | // 15 | // This is intended for use with function-level static variables. 16 | template 17 | class NoDestructor { 18 | public: 19 | template 20 | explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { 21 | static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), 22 | "instance_storage_ is not large enough to hold the instance"); 23 | static_assert( 24 | alignof(decltype(instance_storage_)) >= alignof(InstanceType), 25 | "instance_storage_ does not meet the instance's alignment requirement"); 26 | new (&instance_storage_) 27 | InstanceType(std::forward(constructor_args)...); 28 | } 29 | 30 | ~NoDestructor() = default; 31 | 32 | NoDestructor(const NoDestructor&) = delete; 33 | NoDestructor& operator=(const NoDestructor&) = delete; 34 | 35 | InstanceType* get() { 36 | return reinterpret_cast(&instance_storage_); 37 | } 38 | 39 | private: 40 | typename std::aligned_storage::type instance_storage_; 42 | }; 43 | 44 | } // namespace leveldb 45 | 46 | #endif // STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ 47 | -------------------------------------------------------------------------------- /util/no_destructor_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/no_destructor.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "gtest/gtest.h" 12 | 13 | namespace leveldb { 14 | 15 | namespace { 16 | 17 | struct DoNotDestruct { 18 | public: 19 | DoNotDestruct(uint32_t a, uint64_t b) : a(a), b(b) {} 20 | ~DoNotDestruct() { std::abort(); } 21 | 22 | // Used to check constructor argument forwarding. 23 | uint32_t a; 24 | uint64_t b; 25 | }; 26 | 27 | constexpr const uint32_t kGoldenA = 0xdeadbeef; 28 | constexpr const uint64_t kGoldenB = 0xaabbccddeeffaabb; 29 | 30 | } // namespace 31 | 32 | TEST(NoDestructorTest, StackInstance) { 33 | NoDestructor instance(kGoldenA, kGoldenB); 34 | ASSERT_EQ(kGoldenA, instance.get()->a); 35 | ASSERT_EQ(kGoldenB, instance.get()->b); 36 | } 37 | 38 | TEST(NoDestructorTest, StaticInstance) { 39 | static NoDestructor instance(kGoldenA, kGoldenB); 40 | ASSERT_EQ(kGoldenA, instance.get()->a); 41 | ASSERT_EQ(kGoldenB, instance.get()->b); 42 | } 43 | 44 | } // namespace leveldb 45 | 46 | int main(int argc, char** argv) { 47 | testing::InitGoogleTest(&argc, argv); 48 | return RUN_ALL_TESTS(); 49 | } 50 | -------------------------------------------------------------------------------- /util/options.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/options.h" 6 | 7 | #include "leveldb/comparator.h" 8 | #include "leveldb/env.h" 9 | 10 | namespace leveldb { 11 | 12 | Options::Options() 13 | : comparator(BytewiseComparator()), 14 | env(Env::Default()), 15 | clean_write_buffer_size(128 << 10), 16 | clean_threshold(300 * 1024 * 1024), 17 | min_clean_threshold(clean_threshold / 5), 18 | log_dropCount_threshold(100), 19 | max_vlog_size(1024 * 1024 * 1024) {} 20 | } // namespace leveldb 21 | -------------------------------------------------------------------------------- /util/random.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_RANDOM_H_ 6 | #define STORAGE_LEVELDB_UTIL_RANDOM_H_ 7 | 8 | #include 9 | 10 | namespace leveldb { 11 | 12 | // A very simple random number generator. Not especially good at 13 | // generating truly random bits, but good enough for our needs in this 14 | // package. 15 | class Random { 16 | private: 17 | uint32_t seed_; 18 | 19 | public: 20 | explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) { 21 | // Avoid bad seeds. 22 | if (seed_ == 0 || seed_ == 2147483647L) { 23 | seed_ = 1; 24 | } 25 | } 26 | uint32_t Next() { 27 | static const uint32_t M = 2147483647L; // 2^31-1 28 | static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 29 | // We are computing 30 | // seed_ = (seed_ * A) % M, where M = 2^31-1 31 | // 32 | // seed_ must not be zero or M, or else all subsequent computed values 33 | // will be zero or M respectively. For all other values, seed_ will end 34 | // up cycling through every number in [1,M-1] 35 | uint64_t product = seed_ * A; 36 | 37 | // Compute (product % M) using the fact that ((x << 31) % M) == x. 38 | seed_ = static_cast((product >> 31) + (product & M)); 39 | // The first reduction may overflow by 1 bit, so we may need to 40 | // repeat. mod == M is not possible; using > allows the faster 41 | // sign-bit-based test. 42 | if (seed_ > M) { 43 | seed_ -= M; 44 | } 45 | return seed_; 46 | } 47 | // Returns a uniformly distributed value in the range [0..n-1] 48 | // REQUIRES: n > 0 49 | uint32_t Uniform(int n) { return Next() % n; } 50 | 51 | // Randomly returns true ~"1/n" of the time, and false otherwise. 52 | // REQUIRES: n > 0 53 | bool OneIn(int n) { return (Next() % n) == 0; } 54 | 55 | // Skewed: pick "base" uniformly from range [0,max_log] and then 56 | // return "base" random bits. The effect is to pick a number in the 57 | // range [0,2^max_log-1] with exponential bias towards smaller numbers. 58 | uint32_t Skewed(int max_log) { return Uniform(1 << Uniform(max_log + 1)); } 59 | }; 60 | 61 | } // namespace leveldb 62 | 63 | #endif // STORAGE_LEVELDB_UTIL_RANDOM_H_ 64 | -------------------------------------------------------------------------------- /util/status.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/status.h" 6 | 7 | #include 8 | 9 | #include "port/port.h" 10 | 11 | namespace leveldb { 12 | 13 | const char* Status::CopyState(const char* state) { 14 | uint32_t size; 15 | std::memcpy(&size, state, sizeof(size)); 16 | char* result = new char[size + 5]; 17 | std::memcpy(result, state, size + 5); 18 | return result; 19 | } 20 | 21 | Status::Status(Code code, const Slice& msg, const Slice& msg2) { 22 | assert(code != kOk); 23 | const uint32_t len1 = static_cast(msg.size()); 24 | const uint32_t len2 = static_cast(msg2.size()); 25 | const uint32_t size = len1 + (len2 ? (2 + len2) : 0); 26 | char* result = new char[size + 5]; 27 | std::memcpy(result, &size, sizeof(size)); 28 | result[4] = static_cast(code); 29 | std::memcpy(result + 5, msg.data(), len1); 30 | if (len2) { 31 | result[5 + len1] = ':'; 32 | result[6 + len1] = ' '; 33 | std::memcpy(result + 7 + len1, msg2.data(), len2); 34 | } 35 | state_ = result; 36 | } 37 | 38 | std::string Status::ToString() const { 39 | if (state_ == nullptr) { 40 | return "OK"; 41 | } else { 42 | char tmp[30]; 43 | const char* type; 44 | switch (code()) { 45 | case kOk: 46 | type = "OK"; 47 | break; 48 | case kNotFound: 49 | type = "NotFound: "; 50 | break; 51 | case kCorruption: 52 | type = "Corruption: "; 53 | break; 54 | case kNotSupported: 55 | type = "Not implemented: "; 56 | break; 57 | case kInvalidArgument: 58 | type = "Invalid argument: "; 59 | break; 60 | case kIOError: 61 | type = "IO error: "; 62 | break; 63 | default: 64 | std::snprintf(tmp, sizeof(tmp), 65 | "Unknown code(%d): ", static_cast(code())); 66 | type = tmp; 67 | break; 68 | } 69 | std::string result(type); 70 | uint32_t length; 71 | std::memcpy(&length, state_, sizeof(length)); 72 | result.append(state_ + 5, length); 73 | return result; 74 | } 75 | } 76 | 77 | } // namespace leveldb 78 | -------------------------------------------------------------------------------- /util/status_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "leveldb/status.h" 6 | 7 | #include 8 | 9 | #include "gtest/gtest.h" 10 | #include "leveldb/slice.h" 11 | 12 | namespace leveldb { 13 | 14 | TEST(Status, MoveConstructor) { 15 | { 16 | Status ok = Status::OK(); 17 | Status ok2 = std::move(ok); 18 | 19 | ASSERT_TRUE(ok2.ok()); 20 | } 21 | 22 | { 23 | Status status = Status::NotFound("custom NotFound status message"); 24 | Status status2 = std::move(status); 25 | 26 | ASSERT_TRUE(status2.IsNotFound()); 27 | ASSERT_EQ("NotFound: custom NotFound status message", status2.ToString()); 28 | } 29 | 30 | { 31 | Status self_moved = Status::IOError("custom IOError status message"); 32 | 33 | // Needed to bypass compiler warning about explicit move-assignment. 34 | Status& self_moved_reference = self_moved; 35 | self_moved_reference = std::move(self_moved); 36 | } 37 | } 38 | 39 | } // namespace leveldb 40 | 41 | int main(int argc, char** argv) { 42 | testing::InitGoogleTest(&argc, argv); 43 | return RUN_ALL_TESTS(); 44 | } 45 | -------------------------------------------------------------------------------- /util/testutil.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #include "util/testutil.h" 6 | 7 | #include 8 | 9 | #include "util/random.h" 10 | 11 | namespace leveldb { 12 | namespace test { 13 | 14 | Slice RandomString(Random* rnd, int len, std::string* dst) { 15 | dst->resize(len); 16 | for (int i = 0; i < len; i++) { 17 | (*dst)[i] = static_cast(' ' + rnd->Uniform(95)); // ' ' .. '~' 18 | } 19 | return Slice(*dst); 20 | } 21 | 22 | std::string RandomKey(Random* rnd, int len) { 23 | // Make sure to generate a wide variety of characters so we 24 | // test the boundary conditions for short-key optimizations. 25 | static const char kTestChars[] = {'\0', '\1', 'a', 'b', 'c', 26 | 'd', 'e', '\xfd', '\xfe', '\xff'}; 27 | std::string result; 28 | for (int i = 0; i < len; i++) { 29 | result += kTestChars[rnd->Uniform(sizeof(kTestChars))]; 30 | } 31 | return result; 32 | } 33 | 34 | Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len, 35 | std::string* dst) { 36 | int raw = static_cast(len * compressed_fraction); 37 | if (raw < 1) raw = 1; 38 | std::string raw_data; 39 | RandomString(rnd, raw, &raw_data); 40 | 41 | // Duplicate the random data until we have filled "len" bytes 42 | dst->clear(); 43 | while (dst->size() < len) { 44 | dst->append(raw_data); 45 | } 46 | dst->resize(len); 47 | return Slice(*dst); 48 | } 49 | 50 | } // namespace test 51 | } // namespace leveldb 52 | -------------------------------------------------------------------------------- /util/testutil.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 | 5 | #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_ 6 | #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_ 7 | 8 | #include "gmock/gmock.h" 9 | #include "gtest/gtest.h" 10 | #include "helpers/memenv/memenv.h" 11 | #include "leveldb/env.h" 12 | #include "leveldb/slice.h" 13 | #include "util/random.h" 14 | 15 | namespace leveldb { 16 | namespace test { 17 | 18 | MATCHER(IsOK, "") { return arg.ok(); } 19 | 20 | // Macros for testing the results of functions that return leveldb::Status or 21 | // absl::StatusOr (for any type T). 22 | #define EXPECT_LEVELDB_OK(expression) \ 23 | EXPECT_THAT(expression, leveldb::test::IsOK()) 24 | #define ASSERT_LEVELDB_OK(expression) \ 25 | ASSERT_THAT(expression, leveldb::test::IsOK()) 26 | 27 | // Returns the random seed used at the start of the current test run. 28 | inline int RandomSeed() { 29 | return testing::UnitTest::GetInstance()->random_seed(); 30 | } 31 | 32 | // Store in *dst a random string of length "len" and return a Slice that 33 | // references the generated data. 34 | Slice RandomString(Random* rnd, int len, std::string* dst); 35 | 36 | // Return a random key with the specified length that may contain interesting 37 | // characters (e.g. \x00, \xff, etc.). 38 | std::string RandomKey(Random* rnd, int len); 39 | 40 | // Store in *dst a string of length "len" that will compress to 41 | // "N*compressed_fraction" bytes and return a Slice that references 42 | // the generated data. 43 | Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len, 44 | std::string* dst); 45 | 46 | // A wrapper that allows injection of errors. 47 | class ErrorEnv : public EnvWrapper { 48 | public: 49 | bool writable_file_error_; 50 | int num_writable_file_errors_; 51 | 52 | ErrorEnv() 53 | : EnvWrapper(NewMemEnv(Env::Default())), 54 | writable_file_error_(false), 55 | num_writable_file_errors_(0) {} 56 | ~ErrorEnv() override { delete target(); } 57 | 58 | Status NewWritableFile(const std::string& fname, 59 | WritableFile** result) override { 60 | if (writable_file_error_) { 61 | ++num_writable_file_errors_; 62 | *result = nullptr; 63 | return Status::IOError(fname, "fake error"); 64 | } 65 | return target()->NewWritableFile(fname, result); 66 | } 67 | 68 | Status NewAppendableFile(const std::string& fname, 69 | WritableFile** result) override { 70 | if (writable_file_error_) { 71 | ++num_writable_file_errors_; 72 | *result = nullptr; 73 | return Status::IOError(fname, "fake error"); 74 | } 75 | return target()->NewAppendableFile(fname, result); 76 | } 77 | }; 78 | 79 | } // namespace test 80 | } // namespace leveldb 81 | 82 | #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_ 83 | --------------------------------------------------------------------------------