use beam::{Node, Value};
use tokio::time::{Duration, timeout};
const REPLAY_SENTINEL: &str = "__beam_replay_complete__";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut db = Node::new();
db.batch_put(vec![
(
vec!["users".into(), "alice".into()],
Value::Text("Alice".into()),
),
(
vec!["users".into(), "bob".into()],
Value::Text("Bob".into()),
),
(
vec!["users".into(), "carol".into()],
Value::Text("Carol".into()),
),
])
.await?;
let mut sub = db.get("users").map();
let mut children = Vec::new();
let _ = timeout(Duration::from_secs(5), async {
while let Ok((key, value)) = sub.recv().await {
if key == REPLAY_SENTINEL {
break;
}
children.push((key, value));
}
})
.await;
children.sort_by(|a, b| a.0.cmp(&b.0));
println!("Children under 'users':");
for (key, value) in &children {
println!(" {} = {}", key, value.to_string());
}
assert_eq!(children.len(), 3);
assert_eq!(children[0].0, "alice");
assert_eq!(children[1].0, "bob");
assert_eq!(children[2].0, "carol");
db.stop();
Ok(())
}