### Build LevelDB on POSIX with CMake Source: https://github.com/google/leveldb/blob/main/README.md Quick start commands to build LevelDB on POSIX systems using CMake. Ensure you are in the build directory. ```bash mkdir -p build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build . ``` -------------------------------- ### Install Package Configuration Files Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Installs the generated LevelDB package configuration files to the installation directory. This allows other CMake projects to find and use the installed LevelDB library. ```cmake install( EXPORT leveldbTargets NAMESPACE leveldb:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) install( FILES "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) ``` -------------------------------- ### Build LevelDB with CMake Source: https://context7.com/google/leveldb/llms.txt Instructions for cloning LevelDB with submodules and building it using CMake on POSIX and Windows systems. Includes running tests and installation. ```bash # Clone with submodules git clone --recurse-submodules https://github.com/google/leveldb.git cd leveldb # POSIX Release build mkdir -p build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. cmake --build . # Run tests ctest --output-on-failure # Windows (Visual Studio 2017) cmake -G "Visual Studio 15" .. # x86 cmake -G "Visual Studio 15 Win64" .. # x64 devenv /build Debug leveldb.sln # Install cmake --install . --prefix /usr/local # Installs: libleveldb.a (or .so), include/leveldb/*.h, ``` -------------------------------- ### LevelDB C API Example Source: https://context7.com/google/leveldb/llms.txt Demonstrates opening a database, writing, reading, batch operations, and iterating using the LevelDB C API. Ensure to free allocated memory and resources. ```c #include "leveldb/c.h" #include #include #include #include int main() { char* err = NULL; // Open leveldb_options_t* opts = leveldb_options_create(); leveldb_options_set_create_if_missing(opts, 1); leveldb_filterpolicy_t* fp = leveldb_filterpolicy_create_bloom(10); leveldb_options_set_filter_policy(opts, fp); leveldb_t* db = leveldb_open(opts, "/tmp/c_db", &err); assert(err == NULL); // Write leveldb_writeoptions_t* wopts = leveldb_writeoptions_create(); leveldb_put(db, wopts, "hello", 5, "world", 5, &err); assert(err == NULL); // Read leveldb_readoptions_t* ropts = leveldb_readoptions_create(); size_t vlen; char* val = leveldb_get(db, ropts, "hello", 5, &vlen, &err); assert(err == NULL && val != NULL); printf("Got: %.*s\n", (int)vlen, val); // Got: world leveldb_free(val); // Batch write leveldb_writebatch_t* batch = leveldb_writebatch_create(); leveldb_writebatch_put(batch, "k1", 2, "v1", 2); leveldb_writebatch_put(batch, "k2", 2, "v2", 2); leveldb_writebatch_delete(batch, "hello", 5); leveldb_write(db, wopts, batch, &err); assert(err == NULL); leveldb_writebatch_destroy(batch); // Iterate leveldb_iterator_t* it = leveldb_create_iterator(db, ropts); for (leveldb_iter_seek_to_first(it); leveldb_iter_valid(it); leveldb_iter_next(it)) { size_t klen, len; const char* k = leveldb_iter_key(it, &klen); const char* v = leveldb_iter_value(it, &len); printf("%.*s -> %.*s\n", (int)klen, k, (int)len, v); } leveldb_iter_destroy(it); // Cleanup leveldb_readoptions_destroy(ropts); leveldb_writeoptions_destroy(wopts); leveldb_close(db); leveldb_filterpolicy_destroy(fp); leveldb_options_destroy(opts); return 0; } ``` -------------------------------- ### LevelDB C API Usage Example Source: https://context7.com/google/leveldb/llms.txt Demonstrates common operations using the LevelDB C API, including opening a database, writing, reading, batch operations, and iteration. ```APIDOC ## C API (`leveldb/c.h`) — Stable ABI for non-C++ consumers The C API wraps all core functionality behind opaque pointer types and plain C functions. All errors are reported via `char** errptr` (NULL on success, malloc'd string on error — free with `leveldb_free`). Suitable for FFI use from Python, Go, Rust, Java, etc. ```c #include "leveldb/c.h" #include #include #include #include int main() { char* err = NULL; // Open leveldb_options_t* opts = leveldb_options_create(); leveldb_options_set_create_if_missing(opts, 1); leveldb_filterpolicy_t* fp = leveldb_filterpolicy_create_bloom(10); leveldb_options_set_filter_policy(opts, fp); leveldb_t* db = leveldb_open(opts, "/tmp/c_db", &err); assert(err == NULL); // Write leveldb_writeoptions_t* wopts = leveldb_writeoptions_create(); leveldb_put(db, wopts, "hello", 5, "world", 5, &err); assert(err == NULL); // Read leveldb_readoptions_t* ropts = leveldb_readoptions_create(); size_t vlen; char* val = leveldb_get(db, ropts, "hello", 5, &vlen, &err); assert(err == NULL && val != NULL); printf("Got: %.*s\n", (int)vlen, val); // Got: world leveldb_free(val); // Batch write leveldb_writebatch_t* batch = leveldb_writebatch_create(); leveldb_writebatch_put(batch, "k1", 2, "v1", 2); leveldb_writebatch_put(batch, "k2", 2, "v2", 2); leveldb_writebatch_delete(batch, "hello", 5); leveldb_write(db, wopts, batch, &err); assert(err == NULL); leveldb_writebatch_destroy(batch); // Iterate leveldb_iterator_t* it = leveldb_create_iterator(db, ropts); for (leveldb_iter_seek_to_first(it); leveldb_iter_valid(it); leveldb_iter_next(it)) { size_t klen, len; const char* k = leveldb_iter_key(it, &klen); const char* v = leveldb_iter_value(it, &len); printf("%.*s -> %.*s\n", (int)klen, k, (int)len, v); } leveldb_iter_destroy(it); // Cleanup leveldb_readoptions_destroy(ropts); leveldb_writeoptions_destroy(wopts); leveldb_close(db); leveldb_filterpolicy_destroy(fp); leveldb_options_destroy(opts); return 0; } ``` ``` -------------------------------- ### Install LevelDB Public Headers Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Installs the public header files for LevelDB into the specified include directory. This makes the LevelDB API accessible to other projects that link against it. ```cmake install( FILES "${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h" "${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/leveldb" ) ``` -------------------------------- ### Install LevelDB Targets Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Installs the LevelDB library targets, including executables, libraries, and archives, to their respective destinations based on CMake installation variables. This ensures the built library and tools are placed correctly on the system. ```cmake install(TARGETS leveldb EXPORT leveldbTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) ``` -------------------------------- ### Format C++ Code with clang-format Source: https://github.com/google/leveldb/blob/main/README.md Use this command to format C++ files according to the Google C++ Style Guide. Replace with the actual file path. ```bash clang-format -i --style=file ``` -------------------------------- ### Perform Reads and Writes in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Demonstrates basic database operations: getting a value, putting a new value, and deleting a key. Ensure proper error handling with the returned status. ```c++ std::string value; leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value); if (s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value); if (s.ok()) s = db->Delete(leveldb::WriteOptions(), key1); ``` -------------------------------- ### Configure Package Configuration Files Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Configures the package configuration files (Config.cmake and ConfigVersion.cmake) for LevelDB. These files are used by CMake to find and use the installed LevelDB package in other projects. ```cmake include(CMakePackageConfigHelpers) configure_package_config_file( "cmake/${PROJECT_NAME}Config.cmake.in" "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" ) write_basic_package_version_file( "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" COMPATIBILITY SameMajorVersion ) ``` -------------------------------- ### Get Approximate Sizes of Key Ranges in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Calculates the approximate disk space used by specified key ranges. Useful for understanding storage allocation without performing full scans. ```c++ leveldb::Range ranges[2]; ranges[0] = leveldb::Range("a", "c"); ranges[1] = leveldb::Range("x", "z"); uint64_t sizes[2]; db->GetApproximateSizes(ranges, 2, sizes); ``` -------------------------------- ### Implement Custom Filter Policy in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Provides a custom filter policy that ignores trailing spaces when creating filters, ensuring compatibility with comparators that have the same behavior. This example uses a built-in Bloom filter internally after trimming keys. ```c++ class CustomFilterPolicy : public leveldb::FilterPolicy { private: leveldb::FilterPolicy* builtin_policy_; public: CustomFilterPolicy() : builtin_policy_(leveldb::NewBloomFilterPolicy(10)) {} ~CustomFilterPolicy() { delete builtin_policy_; } const char* Name() const { return "IgnoreTrailingSpacesFilter"; } void CreateFilter(const leveldb::Slice* keys, int n, std::string* dst) const { // Use builtin bloom filter code after removing trailing spaces std::vector trimmed(n); for (int i = 0; i < n; i++) { trimmed[i] = RemoveTrailingSpaces(keys[i]); } builtin_policy_->CreateFilter(trimmed.data(), n, dst); } }; ``` -------------------------------- ### Define Custom Two-Part Comparator in C++ Source: https://github.com/google/leveldb/blob/main/doc/index.md Implement a custom comparator for LevelDB by subclassing `leveldb::Comparator`. This example defines a comparator that sorts keys based on two integer parts, first by the primary part and then by the secondary part for tie-breaking. Ensure `ParseKey` is defined elsewhere to extract these parts. ```c++ class TwoPartComparator : public leveldb::Comparator { public: int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const { int a1, a2, b1, b2; ParseKey(a, &a1, &a2); ParseKey(b, &b1, &b2); if (a1 < b1) return -1; if (a1 > b1) return +1; if (a2 < b2) return -1; if (a2 > b2) return +1; return 0; } const char* Name() const { return "TwoPartComparator"; } void FindShortestSeparator(std::string*, const leveldb::Slice&) const {} void FindShortSuccessor(std::string*) const {} }; ``` -------------------------------- ### Configure Bloom Filter for LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Associates a Bloom filter with the database to reduce disk reads for Get() calls. Recommended for applications with large working sets and frequent random reads. Ensure compatibility with custom comparators. ```c++ leveldb::Options options; options.filter_policy = NewBloomFilterPolicy(10); leveldb::DB* db; leveldb::DB::Open(options, "/tmp/testdb", &db); ... use the database ... delete db; delete options.filter_policy; ``` -------------------------------- ### Open or Create a LevelDB Database Source: https://context7.com/google/leveldb/llms.txt Use `DB::Open` to open an existing database or create a new one if `options.create_if_missing` is true. Ensure to delete the `DB` pointer when done. ```cpp #include #include #include "leveldb/db.h" int main() { leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; // create if not present options.error_if_exists = false; // don't fail if it already exists leveldb::Status status = leveldb::DB::Open(options, "/tmp/mydb", &db); if (!status.ok()) { std::cerr << "Failed to open DB: " << status.ToString() << "\n"; return 1; } // ... use db ... delete db; return 0; } // Expected: opens (or creates) /tmp/mydb successfully. ``` -------------------------------- ### NewMemEnv: In-Memory Environment for LevelDB Testing Source: https://context7.com/google/leveldb/llms.txt Creates an `Env` implementation that stores all database files in memory, delegating non-file tasks to a base `Env`. Ideal for fast, isolated unit tests without disk I/O. ```cpp #include "leveldb/db.h" #include "leveldb/env.h" #include "memenv/memenv.h" #include void memenv_example() { leveldb::Env* mem_env = leveldb::NewMemEnv(leveldb::Env::Default()); leveldb::Options opts; opts.create_if_missing = true; opts.env = mem_env; leveldb::DB* db; leveldb::Status s = leveldb::DB::Open(opts, "/test_db", &db); assert(s.ok()); db->Put(leveldb::WriteOptions(), "hello", "world"); std::string value; db->Get(leveldb::ReadOptions(), "hello", &value); assert(value == "world"); delete db; delete mem_env; // All data is gone; no files written to disk. } ``` -------------------------------- ### Buggy Slice Usage Example Source: https://github.com/google/leveldb/blob/main/doc/index.md Illustrates a common pitfall when using `leveldb::Slice`: the backing storage must remain valid for the lifetime of the Slice. This example is buggy because the `std::string` `str` goes out of scope, invalidating the `slice`. ```c++ leveldb::Slice slice; if (...) { std::string str = ...; slice = str; } Use(slice); ``` -------------------------------- ### Delete Key from LevelDB Source: https://context7.com/google/leveldb/llms.txt Use `DB::Delete` to remove a key-value pair. It is not an error to delete a non-existent key. The example includes verification of deletion. ```cpp #include "leveldb/db.h" #include void delete_example(leveldb::DB* db) { leveldb::WriteOptions wopts; leveldb::Status s = db->Delete(wopts, "user:1001"); assert(s.ok()); // Verifying deletion: std::string value; s = db->Get(leveldb::ReadOptions(), "user:1001", &value); assert(s.IsNotFound()); // confirmed deleted } ``` -------------------------------- ### NewMemEnv Source: https://context7.com/google/leveldb/llms.txt Creates an in-memory environment for testing LevelDB databases, ideal for unit tests as it avoids disk I/O. ```APIDOC ## `NewMemEnv` — In-memory environment for testing ### Description `NewMemEnv` (from `helpers/memenv/memenv.h`) returns an `Env` implementation that stores all database files in memory. It delegates non-file tasks (threads, timing) to the provided base `Env`. Ideal for unit tests: fast, isolated, no disk I/O. ### Method Signature `leveldb::Env* NewMemEnv(leveldb::Env* base_env); ` ### Parameters #### Path Parameters - **base_env** (`leveldb::Env*`) - Required - The base environment to delegate non-file operations to. ### Example Usage ```cpp #include "leveldb/db.h" #include "leveldb/env.h" #include "memenv/memenv.h" #include void memenv_example() { leveldb::Env* mem_env = leveldb::NewMemEnv(leveldb::Env::Default()); leveldb::Options opts; opts.create_if_missing = true; opts.env = mem_env; leveldb::DB* db; leveldb::Status s = leveldb::DB::Open(opts, "/test_db", &db); assert(s.ok()); db->Put(leveldb::WriteOptions(), "hello", "world"); std::string value; db->Get(leveldb::ReadOptions(), "hello", &value); assert(value == "world"); delete db; delete mem_env; // All data is gone; no files written to disk. } ``` ``` -------------------------------- ### Configure Google Benchmark for LevelDB Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Sets up Google Benchmark for building LevelDB benchmarks, disabling testing and exceptions for the benchmark library. ```cmake set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE) add_subdirectory("third_party/benchmark") ``` -------------------------------- ### Open a LevelDB Database Source: https://github.com/google/leveldb/blob/main/doc/index.md Opens a LevelDB database, creating it if it does not exist. Ensure the 'leveldb/db.h' header is included. ```c++ #include #include "leveldb/db.h" leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); assert(status.ok()); ... ``` -------------------------------- ### Configuring Database Options Source: https://context7.com/google/leveldb/llms.txt The Options struct allows extensive configuration of LevelDB behavior, including creation policies, performance tuning (write buffer, open files, block size), caching, compression, and filter policies. Remember to manage the lifetime of allocated cache and filter policy objects. ```cpp #include "leveldb/db.h" #include "leveldb/cache.h" #include "leveldb/filter_policy.h" leveldb::DB* open_configured_db(const std::string& path) { leveldb::Options opts; // Creation / safety opts.create_if_missing = true; opts.paranoid_checks = false; // set true for corruption detection // Performance tuning opts.write_buffer_size = 64 * 1024 * 1024; // 64 MB write buffer opts.max_open_files = 500; opts.block_size = 8 * 1024; // 8 KB blocks // Block cache (uncompressed; 128 MB) opts.block_cache = leveldb::NewLRUCache(128 * 1024 * 1024); // Compression opts.compression = leveldb::kSnappyCompression; // default; or kZstdCompression // Bloom filter: ~1% false-positive rate, reduces disk seeks opts.filter_policy = leveldb::NewBloomFilterPolicy(10); leveldb::DB* db; leveldb::Status s = leveldb::DB::Open(opts, path, &db); assert(s.ok()); return db; // Caller must delete db, opts.block_cache, opts.filter_policy when done. } ``` -------------------------------- ### DB::Open Source: https://context7.com/google/leveldb/llms.txt Opens or creates a LevelDB database at the specified filesystem path. It returns a `DB*` pointer on success and requires the caller to manage its deletion. ```APIDOC ## DB::Open ### Description Opens the database stored at the filesystem path `name`, creating it if `options.create_if_missing` is true. Returns `Status::OK()` and stores a heap-allocated `DB*` in `*dbptr` on success. The caller is responsible for deleting `*dbptr` when done. ### Method `leveldb::DB::Open(const Options& options, const std::string& name, DB** dbptr)` ### Parameters #### Path Parameters - `name` (string) - Required - The filesystem path to the database. #### Request Body - `options` (leveldb::Options) - Required - Options for opening the database, such as `create_if_missing` and `error_if_exists`. - `dbptr` (leveldb::DB**) - Output - Pointer to store the opened `DB` instance. ### Request Example ```cpp leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; options.error_if_exists = false; leveldb::Status status = leveldb::DB::Open(options, "/tmp/mydb", &db); if (!status.ok()) { // Handle error } // ... use db ... delete db; ``` ### Response #### Success Response (Status::OK()) - `dbptr` will point to a valid `leveldb::DB` instance. #### Response Example `Status::OK()` indicates successful opening. ``` -------------------------------- ### Estimate Key Range Disk Usage with GetApproximateSizes Source: https://context7.com/google/leveldb/llms.txt Employ `DB::GetApproximateSizes` to obtain an estimate of the disk space consumed by specified key ranges. This is helpful for partitioning data or understanding storage footprints. Note that recently written data not yet compacted might be excluded from these estimates. ```cpp #include "leveldb/db.h" #include void approximate_sizes_example(leveldb::DB* db) { leveldb::Range ranges[2]; ranges[0] = leveldb::Range("a", "m"); // keys [a, m) ranges[1] = leveldb::Range("m", "z"); // keys [m, z) uint64_t sizes[2]; db->GetApproximateSizes(ranges, 2, sizes); std::cout << "Range [a,m): ~" << sizes[0] << " bytes\n"; std::cout << "Range [m,z): ~" << sizes[1] << " bytes\n"; } ``` -------------------------------- ### Iterate Through Keys in a Specific Range in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md This variation of iteration allows processing keys within a specified range [start, limit). The loop continues as long as the iterator is valid and the current key is less than the limit. ```c++ for (it->Seek(start); it->Valid() && it->key().ToString() < limit; it->Next()) { ... ``` -------------------------------- ### LevelDB Slice Usage Source: https://context7.com/google/leveldb/llms.txt Demonstrates creating and using `Slice` objects for keys and values in LevelDB operations. Ensure the backing storage for slices outlives their usage. ```cpp #include "leveldb/slice.h" #include #include void slice_example(leveldb::DB* db) { // Construct from string literal leveldb::Slice key1("hello"); // Construct from std::string (no copy) std::string s = "world"; leveldb::Slice key2(s); // Construct from pointer + length (binary keys) const char raw[] = {0x01, 0x02, 0x03}; leveldb::Slice binary_key(raw, 3); assert(key1.size() == 5); assert(key1[0] == 'h'); assert(key1.starts_with("hel")); // Three-way comparison assert(key1.compare(key2) < 0); // "hello" < "world" // Convert back to std::string (makes a copy) std::string copy = key1.ToString(); // Use slices with DB operations db->Put(leveldb::WriteOptions(), key1, key2); // WARNING: key2 and s must stay alive until Put returns. } ``` -------------------------------- ### Configure GoogleTest for LevelDB Tests Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Sets up GoogleTest for building LevelDB tests, including forcing shared CRT on Windows and managing GoogleMock/GoogleTest subdirectories. ```cmake set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(install_gtest OFF) set(install_gmock OFF) set(build_gmock ON) add_subdirectory("third_party/googletest") ``` -------------------------------- ### Open LevelDB with Custom Comparator in C++ Source: https://github.com/google/leveldb/blob/main/doc/index.md Demonstrates how to open a LevelDB database using a custom comparator. Set the `comparator` field in `leveldb::Options` to an instance of your custom comparator before calling `leveldb::DB::Open`. Ensure `create_if_missing` is set appropriately. ```c++ TwoPartComparator cmp; leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; options.comparator = &cmp; leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); ``` -------------------------------- ### Compile LevelDB Windows Solution Source: https://github.com/google/leveldb/blob/main/README.md Command to compile the LevelDB Windows solution from the command line using devenv. ```cmd devenv /build Debug leveldb.sln ``` -------------------------------- ### Build Database Benchmark for LevelDB Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Uses the leveldb_benchmark function to build 'benchmarks/db_bench.cc' as a benchmark executable, only if shared libraries are not being built. ```cmake if(NOT BUILD_SHARED_LIBS) leveldb_benchmark("benchmarks/db_bench.cc") endif(NOT BUILD_SHARED_LIBS) ``` -------------------------------- ### Configure LRU Cache for LevelDB in C++ Source: https://github.com/google/leveldb/blob/main/doc/index.md Set up a LevelDB block cache by creating an LRU cache with a specified size using `leveldb::NewLRUCache` and assigning it to `options.block_cache`. Remember to delete the cache and the DB instance when they are no longer needed. The cache stores uncompressed data. ```c++ #include "leveldb/cache.h" leveldb::Options options; options.block_cache = leveldb::NewLRUCache(100 * 1048576); // 100MB cache leveldb::DB* db; leveldb::DB::Open(options, name, &db); ... use the db ... delete db delete options.block_cache; ``` -------------------------------- ### Write Key-Value Pairs to LevelDB Source: https://context7.com/google/leveldb/llms.txt Use `DB::Put` to store a key-value pair. Set `wopts.sync = true` for guaranteed durability, which is slower but survives machine reboots. ```cpp #include "leveldb/db.h" #include // Assumes db is already open. void write_example(leveldb::DB* db) { leveldb::WriteOptions wopts; wopts.sync = false; // fast, async write leveldb::Status s = db->Put(wopts, "user:1001", "Alice"); assert(s.ok()); // Durable write (slower, survives machine crash) wopts.sync = true; s = db->Put(wopts, "user:1002", "Bob"); assert(s.ok()); } ``` -------------------------------- ### Generate Visual Studio Project for LevelDB on Windows Source: https://github.com/google/leveldb/blob/main/README.md Commands to generate Visual Studio project files for LevelDB on Windows using CMake. Specify Win64 for 64-bit builds. ```cmd mkdir build cd build cmake -G "Visual Studio 15" .. cmake -G "Visual Studio 15 Win64" .. ``` -------------------------------- ### CompactRange: Manual Compaction for Key Ranges in LevelDB Source: https://context7.com/google/leveldb/llms.txt Triggers compaction for a specified key range or the entire database. Pass `nullptr` for both `begin` and `end` to compact the whole database. Intended for advanced users familiar with LSM-tree internals. ```cpp #include "leveldb/db.h" void compact_example(leveldb::DB* db) { // Compact a specific key range leveldb::Slice begin("user:"); leveldb::Slice end("user;"); // first byte > ':' to capture all "user:" keys db->CompactRange(&begin, &end); // Compact the entire database db->CompactRange(nullptr, nullptr); } ``` -------------------------------- ### Custom Environment Implementation for LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Implement a custom `leveldb::Env` to control file operations, such as introducing artificial delays. This is useful for managing LevelDB's impact on system resources. ```c++ class SlowEnv : public leveldb::Env { ... implementation of the Env interface ... }; SlowEnv env; leveldb::Options options; options.env = &env; Status s = leveldb::DB::Open(options, ...); ``` -------------------------------- ### Build C Test for LevelDB Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Uses the leveldb_test function to build and add 'db/c_test.c' as a test executable. ```cmake leveldb_test("db/c_test.c") ``` -------------------------------- ### Clone LevelDB Repository Source: https://github.com/google/leveldb/blob/main/README.md Use this command to clone the LevelDB repository and its submodules. ```bash git clone --recurse-submodules https://github.com/google/leveldb.git ``` -------------------------------- ### Build LevelDB Utility Executable Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Adds an executable target for leveldbutil, linking it against the LevelDB library. ```cmake add_executable(leveldbutil "db/leveldbutil.cc" ) target_link_libraries(leveldbutil leveldb) ``` -------------------------------- ### Create and Use a Snapshot for Consistent Reads in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Snapshots provide consistent read-only views. Create a snapshot using `DB::GetSnapshot()`, apply updates, then read using an iterator with the snapshot. Remember to release the snapshot when it's no longer needed. ```c++ leveldb::ReadOptions options; options.snapshot = db->GetSnapshot(); ... apply some updates to db ... leveldb::Iterator* iter = db->NewIterator(options); ... read using iter to view the state when the snapshot was created ... delete iter; db->ReleaseSnapshot(options.snapshot); ``` -------------------------------- ### Iterating Through Database Entries Source: https://context7.com/google/leveldb/llms.txt Use DB::NewIterator to obtain an iterator for scanning key-value pairs in sorted order. Iterators support forward, backward, and seek-based positioning. Remember to delete the iterator when done. ```cpp #include "leveldb/db.h" #include "leveldb/iterator.h" #include void iterate_example(leveldb::DB* db) { leveldb::ReadOptions ropts; leveldb::Iterator* it = db->NewIterator(ropts); // Forward scan of all entries for (it->SeekToFirst(); it->Valid(); it->Next()) { std::cout << it->key().ToString() << " -> " << it->value().ToString() << "\n"; } // Seek to a specific prefix for (it->Seek("user:"); it->Valid(); it->Next()) { leveldb::Slice key = it->key(); if (!key.starts_with("user:")) break; std::cout << "User entry: " << key.ToString() << "\n"; } // Reverse scan for (it->SeekToLast(); it->Valid(); it->Prev()) { std::cout << it->key().ToString() << "\n"; } if (!it->status().ok()) { std::cerr << "Iterator error: " << it->status().ToString() << "\n"; } delete it; } ``` -------------------------------- ### Build Environment Tests for LevelDB Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Conditionally builds environment tests ('util/env_windows_test.cc' or 'util/env_posix_test.cc') based on the operating system and if shared libraries are not being built. ```cmake if (WIN32) leveldb_test("util/env_windows_test.cc") else (WIN32) leveldb_test("util/env_posix_test.cc") endif (WIN32) ``` -------------------------------- ### Convert String Literals and std::string to LevelDB Slice Source: https://github.com/google/leveldb/blob/main/doc/index.md Demonstrates how to create `leveldb::Slice` objects from C++ string literals and `std::string` objects. Slices are efficient as they avoid copying data. ```c++ leveldb::Slice s1 = "hello"; std::string str("world"); leveldb::Slice s2 = str; ``` -------------------------------- ### Implement Custom Comparator for Key Ordering Source: https://context7.com/google/leveldb/llms.txt Define a custom `Comparator` to control the sort order of keys. The comparator's name is stored persistently, and changing it upon re-opening the database will result in an error. Ensure your comparator implementation is thread-safe. The `FindShortestSeparator` and `FindShortSuccessor` methods can be left unimplemented if not needed for optimization. ```cpp #include "leveldb/comparator.h" #include "leveldb/db.h" #include #include // Sorts keys as big-endian 64-bit integers class Uint64Comparator : public leveldb::Comparator { public: int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const override { uint64_t ia, ib; memcpy(&ia, a.data(), sizeof(uint64_t)); memcpy(&ib, b.data(), sizeof(uint64_t)); if (ia < ib) return -1; if (ia > ib) return +1; return 0; } const char* Name() const override { return "Uint64Comparator"; } void FindShortestSeparator(std::string*, const leveldb::Slice&) const override {} void FindShortSuccessor(std::string*) const override {} }; void custom_comparator_example() { Uint64Comparator cmp; leveldb::Options opts; opts.create_if_missing = true; opts.comparator = &cmp; leveldb::DB* db; leveldb::Status s = leveldb::DB::Open(opts, "/tmp/int_db", &db); assert(s.ok()); uint64_t k = 42; db->Put(leveldb::WriteOptions(), leveldb::Slice((char*)&k, 8), "forty-two"); delete db; } ``` -------------------------------- ### Convert LevelDB Slice to std::string Source: https://github.com/google/leveldb/blob/main/doc/index.md Shows how to convert a `leveldb::Slice` back into a `std::string` for easier manipulation or output. ```c++ std::string str = s1.ToString(); assert(str == std::string("hello")); ``` -------------------------------- ### Atomic Batch Writes with WriteBatch Source: https://context7.com/google/leveldb/llms.txt Use WriteBatch to group multiple Put and Delete operations into a single atomic transaction. This is more efficient for bulk operations and guarantees all or none of the updates are applied. The Append method can merge batches. ```cpp #include "leveldb/db.h" #include "leveldb/write_batch.h" #include void batch_example(leveldb::DB* db) { // Move value from key1 to key2 atomically std::string value; leveldb::Status s = db->Get(leveldb::ReadOptions(), "src_key", &value); assert(s.ok()); leveldb::WriteBatch batch; batch.Delete("src_key"); // remove old batch.Put("dst_key", value); // insert new batch.Put("audit_key", "moved"); // extra metadata leveldb::WriteOptions wopts; wopts.sync = true; // durable commit s = db->Write(wopts, &batch); assert(s.ok()); // Appending another batch leveldb::WriteBatch extra; extra.Put("counter", "1"); batch.Append(extra); // merge extra into batch } ``` -------------------------------- ### DB::CompactRange Source: https://context7.com/google/leveldb/llms.txt Triggers manual compaction of the underlying storage for a key range. Pass `nullptr` for both `begin` and `end` to compact the entire database. Intended for advanced users who understand the LSM-tree internals. ```APIDOC ## `DB::CompactRange` — Manual compaction ### Description Triggers compaction of the underlying storage for a key range, discarding deleted and overwritten versions and merging SSTable files. Pass `nullptr` for both `begin` and `end` to compact the entire database. Intended for advanced users who understand the LSM-tree internals. ### Method Signature `void CompactRange(const Slice* begin, const Slice* end);` ### Parameters #### Path Parameters - **begin** (`const Slice*`) - Optional - Pointer to the beginning of the key range to compact. If `nullptr`, the compaction starts from the beginning of the database. - **end** (`const Slice*`) - Optional - Pointer to the end of the key range to compact. If `nullptr`, the compaction ends at the end of the database. ### Example Usage ```cpp #include "leveldb/db.h" void compact_example(leveldb::DB* db) { // Compact a specific key range leveldb::Slice begin("user:"); leveldb::Slice end("user;"); // first byte > ':' to capture all "user:" keys db->CompactRange(&begin, &end); // Compact the entire database db->CompactRange(nullptr, nullptr); } ``` ``` -------------------------------- ### Check for SQLite3 Library Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Checks if the SQLite3 library is available on the system. ```cmake check_library_exists(sqlite3 sqlite3_open "" HAVE_SQLITE3) ``` -------------------------------- ### DB::Put Source: https://context7.com/google/leveldb/llms.txt Stores a key-value pair in the database. The `sync` option in `WriteOptions` can be used to ensure durability before the call returns. ```APIDOC ## DB::Put ### Description Stores the mapping `key → value` in the database. Returns `Status::OK()` on success. Set `options.sync = true` to guarantee durability before the call returns (equivalent to `write()` + `fsync()`). Without `sync`, the write survives process crashes but not machine reboots. ### Method `leveldb::Status DB::Put(const WriteOptions& options, const Slice& key, const Slice& value)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `options` (leveldb::WriteOptions) - Options for the write operation, including `sync`. - `key` (Slice) - The key to store. - `value` (Slice) - The value to associate with the key. ### Request Example ```cpp leveldb::WriteOptions wopts; wopts.sync = false; // fast, async write leveldb::Status s = db->Put(wopts, "user:1001", "Alice"); assert(s.ok()); wopts.sync = true; // Durable write s = db->Put(wopts, "user:1002", "Bob"); assert(s.ok()); ``` ### Response #### Success Response (Status::OK()) Indicates the key-value pair was successfully stored. #### Response Example `Status::OK()` ``` -------------------------------- ### Define a Generic LevelDB Benchmark Function Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt A CMake function to define and build individual benchmark executables for LevelDB, linking them with necessary libraries and setting compile definitions. ```cmake function(leveldb_benchmark bench_file) get_filename_component(bench_target_name "${bench_file}" NAME_WE) add_executable("${bench_target_name}" "") target_sources("${bench_target_name}" PRIVATE "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h" "util/histogram.cc" "util/histogram.h" "util/testutil.cc" "util/testutil.h" "${bench_file}" ) target_link_libraries("${bench_target_name}" leveldb gmock gtest benchmark) target_compile_definitions("${bench_target_name}" PRIVATE ${LEVELDB_PLATFORM_NAME}=1 ) if (NOT HAVE_CXX17_HAS_INCLUDE) target_compile_definitions("${bench_target_name}" PRIVATE LEVELDB_HAS_PORT_CONFIG_H=1 ) endif (NOT HAVE_CXX17_HAS_INCLUDE) endfunction(leveldb_benchmark) ``` -------------------------------- ### Apply Compiler Options for GoogleTest Warnings Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Applies a compiler option to suppress 'missing field initializers' warnings for gtest and gmock targets if LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS is defined. ```cmake if(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS) set_property(TARGET gtest APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers) set_property(TARGET gmock APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers) endif(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS) ``` -------------------------------- ### Define LevelDB Test Executable Source: https://github.com/google/leveldb/blob/main/CMakeLists.txt Defines the main executable for LevelDB tests and specifies its source files, including generated port_config.h. ```cmake add_executable(leveldb_tests "") target_sources(leveldb_tests PRIVATE "util/status_test.cc" "util/no_destructor_test.cc" "util/testutil.cc" "util/testutil.h" ) target_link_libraries(leveldb_tests leveldb gmock gtest gtest_main) target_compile_definitions(leveldb_tests PRIVATE ${LEVELDB_PLATFORM_NAME}=1 ) if (NOT HAVE_CXX17_HAS_INCLUDE) target_compile_definitions(leveldb_tests PRIVATE LEVELDB_HAS_PORT_CONFIG_H=1 ) endif(NOT HAVE_CXX17_HAS_INCLUDE) ``` -------------------------------- ### DumpFile: Inspect Raw LevelDB Storage Files Source: https://context7.com/google/leveldb/llms.txt Decodes LevelDB internal files (log, SSTable, MANIFEST) and appends human-readable text to a `WritableFile`. Useful for debugging storage corruption or inspecting SSTable content. ```cpp #include "leveldb/dumpfile.h" #include "leveldb/env.h" #include void dumpfile_example() { leveldb::Env* env = leveldb::Env::Default(); // Open an output file to receive the dump leveldb::WritableFile* out; env->NewWritableFile("/tmp/dump_output.txt", &out); // Dump an SSTable file leveldb::Status s = leveldb::DumpFile(env, "/tmp/mydb/000042.ldb", out); if (!s.ok()) { std::cerr << "DumpFile error: " << s.ToString() << "\n"; } out->Close(); delete out; // /tmp/dump_output.txt now contains human-readable SSTable content } ``` -------------------------------- ### Iterate Through All Key-Value Pairs in LevelDB Source: https://github.com/google/leveldb/blob/main/doc/index.md Use this snippet to print all key-value pairs in a database. Ensure to check the iterator's status after the loop and delete the iterator when done. ```c++ leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); for (it->SeekToFirst(); it->Valid(); it->Next()) { cout << it->key().ToString() << ": " << it->value().ToString() << endl; } assert(it->status().ok()); // Check for any errors found during the scan delete it; ``` -------------------------------- ### DestroyDB / RepairDB Source: https://context7.com/google/leveldb/llms.txt Functions for managing the database lifecycle. `DestroyDB` permanently deletes database files, while `RepairDB` attempts to recover corrupted databases. ```APIDOC ## `DestroyDB` / `RepairDB` — Database lifecycle management ### Description `DestroyDB` permanently deletes all database files at the given path. `RepairDB` attempts to recover a corrupted or unopenable database, recovering as much data as possible (some data may be lost). Both should be used with care. ### Method Signatures `leveldb::Status DestroyDB(const std::string& name, const Options& options); leveldb::Status RepairDB(const std::string& name, const Options& options); ` ### Parameters #### Path Parameters - **name** (`const std::string&`) - Required - The path to the database directory. - **options** (`const Options&`) - Required - Options for the operation. ### Example Usage ```cpp #include "leveldb/db.h" #include void lifecycle_example() { leveldb::Options opts; // Attempt to repair a corrupted database leveldb::Status s = leveldb::RepairDB("/tmp/mydb", opts); if (!s.ok()) { // repair failed; data may be unrecoverable } // Permanently destroy a database s = leveldb::DestroyDB("/tmp/old_db", opts); assert(s.ok()); } ``` ```