Key-value stores

RecipeCratesCategories
heedheedcat-database
rocksdbrocksdbcat-database

heed

heed heed-crates.io heed-github heed-lib.rs cat-data-structures cat-database

A fully typed LMDB (mdb.master) wrapper with minimum overhead. LMDB is a high performant, light-weight, embedded key-value database library.


// // `heed` and `heed3` are high-level wrappers of LMDB.
// // Lightning Memory-Mapped Database (LMDB) is an embedded transactional
// // database in the form of a key-value store.

// // `heed` is a wrapper around LMDB on the mdb.master branch,
// // `heed3` derives from the heed wrapper but on the mdb.master3 branch.
// // The heed3 crate will be stable once the LMDB version on the mdb.master3
// // branch will be officially released. It features encryption-at-rest and
// // checksumming features that the heed crate doesn't.

// use heed::types::*;
// use heed::{Database, EnvOpenOptions};

// fn main() -> anyhow::Result<()> {
//     // Open a database

//    let env = unsafe {
//         EnvOpenOptions::new()
//             .map_size(1024 * 1024 * 10) // 10 MiB
//             .max_dbs(10)
//             .open("my_database")?
//     };

//     // Create a database within the environment
//     let db: Database<u32, String> = env.create_database(b"my_database",
// None)?;

//     // Write data to the database
//     let mut wtxn = env.write_txn()?;
//     db.put(&mut wtxn, &1, &"Hello, world!".to_string())?;
//     wtxn.commit()?;

//     // Read data from the database
//     let mut rtxn = env.read_txn()?;
//     let value = db.get(&rtxn, &1)?.unwrap();
//     println!("Value: {}", value);

//     Ok(())
// }

rocksdb

rocksdb rocksdb-crates.io rocksdb-github rocksdb-lib.rs cat-database

Rust wrapper for Facebook's RocksDB embeddable database. RocksDB is a high performance database for key-value data.


// Rust wrapper for Facebook's RocksDB embeddable database

// Ensure you have the RocksDB C++ library installed on your system, because the
// Rust rocksdb crate is a wrapper around the native C++ RocksDB library.
// If you're on Ubuntu, you can install it with:
// sudo apt-get install librocksdb-dev
// Requirements: Clang and LLVM

use rocksdb::DB;
use rocksdb::Options;

fn main() -> anyhow::Result<()> {
    // Create a new temporary directory to store the database,
    // which will be deleted when tempdir goes out of scope
    let tempdir = tempfile::Builder::new()
        .prefix("rocksdb_storage")
        .tempdir()
        .expect("Failed to create temporary directory for rocksdb storage");
    let path = tempdir.path();
    // In real life, use e.g.: let path = "my_rocksdb_path";
    {
        // Open a RocksDB database
        // (this will create a new database if it doesn't exist)
        let db = DB::open_default(path)?;

        // Insert some key-value pairs
        db.put(b"key1", b"value1")?;
        db.put(b"key2", b"value2")?;

        // Retrieve a value by its key
        match db.get(b"key1")? {
            Some(value) => println!(
                "Found key1 with value: {}",
                String::from_utf8_lossy(&value)
            ),
            None => println!("key1 not found"),
        }

        // Delete a key-value pair
        db.delete(b"key1")?;

        // Try to get the deleted key
        match db.get(b"key1")? {
            Some(value) => println!(
                "Found key1 with value: {}",
                String::from_utf8_lossy(&value)
            ),
            None => println!("key1 not found after deletion"),
        }
    }
    let _ = DB::destroy(&Options::default(), path);

    Ok(())
}