Skip to main content

Crate roughdb

Crate roughdb 

Source
Expand description

RoughDB — an embedded key-value store written in Rust, porting LevelDB to Rust.

RoughDB is an LSM-tree key-value store with a LevelDB-compatible on-disk format. It supports persistent (WAL + MANIFEST + SSTables) and in-memory operation, multi-level compaction, snapshots, and bidirectional iteration.

§Getting started

§Opening a database

use roughdb::{Db, Options};

let mut opts = Options::default();
opts.create_if_missing = true;

let db = Db::open("/tmp/my_db", opts)?;

Use Db::default for a lightweight in-memory database (no WAL, no flush):

let db = roughdb::Db::default();

§Reads and writes

db.put(b"hello", b"world")?;

match db.get(b"hello") {
    Ok(value) => println!("got: {}", String::from_utf8_lossy(&value)),
    Err(e) if e.is_not_found() => println!("not found"),
    Err(e) => return Err(e),
}

db.delete(b"hello")?;

§Atomic batch writes

use roughdb::{WriteBatch, WriteOptions};

let mut batch = WriteBatch::new();
batch.put(b"key1", b"val1");
batch.put(b"key2", b"val2");
batch.delete(b"old_key");

db.write(&WriteOptions::default(), &batch)?;

§Iterating over keys

Iterators start unpositioned — call DbIter::seek_to_first, DbIter::seek_to_last, or DbIter::seek before reading. Both forward and backward traversal are supported; direction switches are handled transparently.

use roughdb::ReadOptions;

let mut it = db.new_iterator(&ReadOptions::default())?;

it.seek_to_first();
while it.valid() {
    println!("{:?} = {:?}", it.key(), it.value());
    it.next();
}

§Snapshots

A snapshot pins the database to a specific point in time; reads through it see only writes that preceded the snapshot.

use roughdb::ReadOptions;

db.put(b"k", b"v1")?;
let snap = db.get_snapshot();
db.put(b"k", b"v2")?;

let opts = ReadOptions { snapshot: Some(&snap), ..ReadOptions::default() };
assert_eq!(db.get_with_options(&opts, b"k")?, b"v1");

// snap releases automatically here when it goes out of scope.

Re-exports§

pub use cache::BlockCache;
pub use error::Error;
pub use filter::BloomFilterPolicy;
pub use options::CompressionType;
pub use options::Options;
pub use options::WriteOptions;
pub use write_batch::Handler;
pub use write_batch::WriteBatch;

Modules§

cache
Block cache — an LRU cache of decompressed SSTable data blocks.
error
filter
options
write_batch

Structs§

Db
DbIter
User-facing iterator over a consistent snapshot of the database.
ReadOptions
Options that control read operations.
Snapshot
An immutable snapshot of the database state at a particular sequence number.