use std::path::Path;
#[derive(Debug)]
enum Error {
Persy(persy::PersyError),
}
fn main() {
let create_segment;
if !Path::new("data.persy").exists() {
persy::Persy::create("data.persy").expect("create file correctly");
create_segment = true;
} else {
create_segment = false;
}
let persy = persy::Persy::open_with_recover("data.persy", persy::Config::new(), |id| {
String::from("ok_commit").into_bytes().eq(id)
})
.expect("open file correctly");
if create_segment {
let mut tx = persy.begin().expect("begin tx correctly");
tx.create_segment("data").expect("create segment correctly");
let prepared = tx.prepare().expect("prepare commit correctly");
prepared.commit().expect("tx commited correctly");
}
let good = vec![10; 20];
for (id, content) in persy.scan("data").expect("segment scan works") {
if good.eq(&content) {
println!("found some right recovered content");
} else {
panic!(
" panic !!! this data should haven't been recovered id:{:?} , {:?}",
id, content
);
}
}
let mut tx = persy
.begin_with(persy::TransactionConfig::new().set_transaction_id(String::from("ok_commit").into_bytes()))
.expect("begin tx correctly");
let val = vec![10; 20];
tx.insert("data", &val).expect("insert correctly");
let _prepared = tx.prepare().expect("prepare commit correctly");
let mut tx = persy
.begin_with(persy::TransactionConfig::new().set_transaction_id(String::from("no_commit").into_bytes()))
.expect("begin tx correctly");
let val = vec![10; 50];
tx.insert("data", &val).expect("insert correctly");
let _prepared = tx.prepare().expect("prepare commit correctly");
panic!("this panic on purpose without commit")
}
impl From<persy::PersyError> for Error {
fn from(err: persy::PersyError) -> Error {
Error::Persy(err)
}
}