use std::time::Duration;
use beam::adapters::RedbStorage;
use beam::{Config, Node, Value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = std::env::temp_dir().join(format!("beam-example-{}.redb", std::process::id()));
let _ = std::fs::remove_file(&db_path);
let path_str = db_path.to_string_lossy().to_string();
println!("Phase 1: Writing data to persistent storage...");
{
let config = Config::default();
let mut db = Node::new_with_config(
config.clone(),
vec![Box::new(RedbStorage::new_with_config(
config, &path_str, None,
))],
vec![],
);
db.get("name").put(Value::Text("BEAM".into())).await?;
println!("Wrote: name = \"BEAM\"");
println!("Flushing storage (write barrier)...");
db.flush_storage(Some(Duration::from_secs(5))).await?;
println!("Flush acknowledged — data is on disk.");
db.stop();
tokio::time::sleep(Duration::from_millis(200)).await;
}
println!("\nPhase 2: Reopening database in a new node...");
{
let config = Config::default();
let mut db = Node::new_with_config(
config.clone(),
vec![Box::new(RedbStorage::new_with_config(
config, &path_str, None,
))],
vec![],
);
let value = db.get("name").once(Some(Duration::from_secs(3))).await;
match value {
Some(Value::Text(s)) => {
println!("Read back: name = {:?}", s);
assert_eq!(s, "BEAM", "persisted value should match");
println!("Persistence confirmed — data survived node restart.");
}
other => panic!("Expected Value::Text(\"BEAM\"), got {:?}", other),
}
db.stop();
}
let _ = std::fs::remove_file(&db_path);
std::process::exit(0);
}