Crate byodb_rust

Source
Expand description

§Build Your Own Database from Scratch (in Rust)

https://build-your-own.org/database/, but instead of Go, use Rust. This a personal project to learn both database internals and the Rust programming language.

§Example

let path = "/path/to/a/db/file";
let db: DB = DB::open_or_create(path)?;

// Perform reads in a read transaction.
{
    let r_txn: RTxn<'_, '_> = db.r_txn();
    for (k, v) in r_txn.in_order_iter() {
        println!("key: {k:?}, val: {v:?}");
    }
} // read transaction is dropped at the end of scope.

// Perform reads and writes in a read-write transaction.
{
    let mut rw_txn: RWTxn<'_, '_> = db.rw_txn();
    if rw_txn.get("some_key".as_bytes())?.is_some() {
        rw_txn.update("some_key".as_bytes(), "some_new_val".as_bytes())?;
    }
    // If rw_txn.commit() is not called b/c there was error in any of the
    // above steps, then when rw_txn is dropped, it is equivalent to doing
    // rw_txn.abort().
    rw_txn.commit();
}

Modules§

consts
Constants related to memory page size and offsets.
error
Errors returned by functions in this crate.

Structs§

DB
Represents the main database instance.
DBBuilder
Builder of a DB.
Txn
Represents a database transaction.

Type Aliases§

RTxn
A read-only transaction. It must live as long as the DB that created it (via DB::r_txn).
RWTxn
A read-write transaction. It must live as long as the DB that created it (via DB::rw_txn).
Result
A specialized Result type for database operations within this module.