Lens
Key Value Operations
Byte-key put/get/delete operations against the current Lens root.
get returns Option<Vec<u8>>; put publishes an updated B-tree root and delete removes a key from the current tree. The root metadata is distinct from append-only historical nodes. The cache is an optimization, not the source of truth. These operations do not imply multi-operation atomic transactions or distributed key claims.
This example is checked against source signatures; it was not compiled or run during this documentation pass.
use lens::{Lens, LensConfig};
use std::sync::Arc;
use strand::{Strand, StrandConfig};
use tokio::sync::RwLock as AsyncRwLock;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Lens wraps an arc-shared Strand. The Strand can be opened against a temp
// directory for tests or against persistent storage for production.
let dir = tempfile::tempdir()?;
let strand = Strand::new(StrandConfig::new().with_storage(dir.path())).await?;
let lens = Lens::new(Arc::new(AsyncRwLock::new(strand)), LensConfig::default()).await?;
// put / get / delete are the three primitive KV operations on the B-tree.
lens.put(b"theme", b"dark").await?;
lens.put(b"locale", b"en-US").await?;
let theme = lens.get(b"theme").await?;
assert_eq!(theme.as_deref(), Some(b"dark".as_slice()));
lens.delete(b"theme").await?;
let theme_after = lens.get(b"theme").await?;
assert!(theme_after.is_none());
let metrics = lens.metrics_snapshot();
println!(
"puts={} deletes={} cache_hits={}",
metrics.puts_total,
metrics.deletes_total,
lens.cache_stats().hits,
);
Ok(())
}