Fjall provides a thread-safe BTreeMap-like API for key-value storage. A Database can contain multiple Keyspaces, where each keyspace is an isolated physical LSM-tree.
Key operations include:
insert(key, value): Write data.get(key): Retrieve data.remove(key): Delete data.prefix(prefix): Iterate over keys with a specific prefix.range(range): Iterate over a range of keys.persist(mode): Manually sync the journal to disk.
Note: Keys are stored in lexicographic order. For integer keys (like timestamps), use big-endian encoding to ensure predictable ordering.
use fjall::{Database, KeyspaceCreateOptions, PersistMode};
fn main() -> fjall::Result<()> {
// A database may contain multiple keyspaces
// You should probably only use a single database for your application
let db = Database::builder(path).open()?;
// TxDatabase::builder for transactional semantics
// Each keyspace is its own physical LSM-tree, and thus isolated from other keyspaces
let items = db.keyspace("my_items", KeyspaceCreateOptions::default)?;
// Write some data
items.insert("a", "hello")?;
// And retrieve it
let bytes = items.get("a")?;
// Or remove it again
items.remove("a")?;
// Search by prefix
for kv in items.prefix("prefix") {
// ...
}
// Search by range
for kv in items.range("a"..="z") {
// ...
}
// Iterators implement DoubleEndedIterator, so you can search backwards, too!
for kv in items.prefix("prefix").rev() {
// ...
}
// Sync the journal to disk to make sure data is definitely durable
// When the database is dropped, it will try to persist with `PersistMode::SyncAll` automatically
db.persist(PersistMode::SyncAll)
}