Lens
Iteration
Bounded forward/reverse scans over the root pinned when a stream is created.
create_read_stream_with_options(start, end, options).await pins the selected live or snapshot root. next().await returns Result<Option<(Key, Value)>>. For forward traversal, start is the lower bound and end is the upper bound. For reverse traversal, start is the upper starting key and end is the lower stopping key. An unknown snapshot is an error; an empty snapshot remains empty.
This example is checked against source signatures; it was not compiled or run during this documentation pass.
use lens::{Direction, Lens, LensConfig, ReadOptions};
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>> {
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?;
// Populate the tree so the iterator has something interesting to walk.
for label in ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot"] {
lens.put(label.as_bytes(), label.as_bytes()).await?;
}
// Reverse-direction range scan with a half-open bound and a row limit.
let opts = ReadOptions {
direction: Direction::Reverse,
include_start: true,
include_end: false,
limit: Some(3),
snapshot: None,
};
let mut stream = lens
.create_read_stream_with_options(Some(b"echo"), Some(b"bravo"), opts)
.await?;
let mut seen = 0usize;
while let Some((key, value)) = stream.next().await? {
println!(
"row {:?} -> {} bytes",
std::str::from_utf8(key.as_bytes()).unwrap_or("?"),
value.data.len(),
);
seen += 1;
}
println!("emitted {seen} rows in reverse order");
Ok(())
}