use core::str;
use std::{sync::Arc, time::Duration};
use candystore::{CandyStore, Config, Result};
fn main() -> Result<()> {
_ = std::fs::remove_dir_all("/tmp/candy-dir");
let db = Arc::new(CandyStore::open("/tmp/candy-dir-mt", Config::default())?);
let db1 = db.clone();
let h1 = std::thread::spawn(move || -> Result<()> {
for i in 0..100 {
db1.set(format!("key{i}"), "thread 1")?;
std::thread::sleep(Duration::from_millis(1));
}
Ok(())
});
let db2 = db.clone();
let h2 = std::thread::spawn(move || -> Result<()> {
for i in 0..100 {
db2.set(format!("key{i}"), "thread 2")?;
std::thread::sleep(Duration::from_millis(1));
}
Ok(())
});
h1.join().unwrap()?;
h2.join().unwrap()?;
for res in db.iter_items() {
let (k, v) = res?;
println!(
"{} = {}",
str::from_utf8(&k).unwrap(),
str::from_utf8(&v).unwrap()
);
}
Ok(())
}