Lens
Key Value Operations
Key-value operations in Lens — point lookups, range scans, conditional puts, and batched commits over the B-tree backend.
Purpose
B-tree key/value database with batches, snapshots, sub-databases, iterators, cache, compaction, and metrics.
This page follows the real source shape for Lens and explains the workflow a developer is likely to use first.
Trust contract
Document which keys authorize the operation, what is signed, what is encrypted, what is deliberately public, and which policy hook can deny the call. Security examples should use deterministic test vectors where possible.
Primary types to know
AllowAllLensPolicy— models/lens/src/lens_policy.rsBatch— models/lens/src/batch.rsBlockCache— models/lens/src/block.rsBlockPointer— models/lens/src/node.rsCacheStats— models/lens/src/block.rsCompactionOptions— models/lens/src/config.rsKey— models/lens/src/node.rsLens— models/lens/src/lens.rsLensConfig— models/lens/src/config.rsLensMetrics— models/lens/src/metrics.rsLensMetricsSnapshot— models/lens/src/metrics.rsMetadata— models/lens/src/lens.rs
Example shape
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(())
}