RocksDB.NET (rocksdb-sharp)

repository·master·Indexed 42 days ago

https://github.com/warrenfalk/rocksdb-sharp

C# bindings for the RocksDB key-value database, providing a wrapper around the native C++ library via the RocksDB C API. It offers three levels of abstraction: low-level direct C API access, mid-level marshaling helpers, and a high-level idiomatic C# class hierarchy for operations like Put, Get, and Remove.

Tokens
808
Snippets
4
Records
6
Agent score
3%

What's inside RocksDB.NET

  1. Requirements and Platform Constraints

    master

    Architecture

    • 64-bit only: RocksDb is supported only in 64-bit mode. While 32-bit compilation is possible via specific patches, it is untested, unsupported, and not recommended for production. On Mac, you should use 64-bit Mono.

    Dependencies

    • Linux and Mac: The libsnappy library must be installed on the system.
  2. How rocksdb-sharp is structured

    master

    rocksdb-sharp is a multi-level binding for the native RocksDB C++ library via its C API. It consists of three layers:

    1. Low level: Direct access to the native C API functions.
    2. Mid level: Helper wrappers to assist with marshaling and exception handling.
    3. High level: An idiomatic C# class hierarchy for easy consumption.
  3. Install rocksdb-sharp via NuGet

    master

    To use rocksdb-sharp in your project, install the managed library via NuGet. By default, the managed library expects the unmanaged native library to be present on the machine at runtime. If you want to bundle the native library with your project, you should also install the RocksDbNative package.

    install-package RocksDbSharp
    # Optionally install the native binaries as well:
    install-package RocksDbNative
  4. Build the native RocksDB library on Windows

    master

    If you need to build the native rocksdb.dll yourself on Windows, follow these steps:

    Prerequisites:

    • Git for Windows (specifically the git bash environment)
    • CMake
    • Visual Studio 2017 (or newer)

    Instructions:

    1. Open the "Developer Command Prompt for VS2017".
    2. Run bash.exe from Git.
    3. Navigate to the native-build folder within the repository.
    4. Execute ./build-rocksdb.sh.

    This process creates rocksdb.dll and places it in the location expected by the solution file.

    # Inside the native-build folder after running bash.exe
    ./build-rocksdb.sh
  5. High-level usage of RocksDb in C#

    master

    The library provides an idiomatic C# class hierarchy for ease of use. You can perform basic key-value operations like Put, Get, and Remove using strings or byte arrays. The library is designed to minimize buffer copying when using byte arrays. Always ensure DbOptions are configured (e.g., SetCreateIfMissing(true)) and wrap the RocksDb instance in a using block to ensure proper disposal of the unmanaged resources.

    var options = new DbOptions()
        .SetCreateIfMissing(true);
    using (var db = RocksDb.Open(options, path))
    {
        // Using strings below, but can also use byte arrays for both keys and values
        // much care has been taken to minimize buffer copying
        db.Put("key", "value");
        string value = db.Get("key");
        db.Remove("key");
    }