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");
persy.create_segment(&mut tx, "data").expect("create segment correctly");
let prepared = persy.prepare_commit(tx).expect("prepare commit correctly");
persy.commit(prepared).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_id(String::from("ok_commit").into_bytes())
.expect("begin tx correctly");
let val = vec![10; 20];
persy.insert_record(&mut tx, "data", &val).expect("insert correctly");
let _prepared = persy.prepare_commit(tx).expect("prepare commit correctly");
let mut tx = persy
.begin_id(String::from("no_commit").into_bytes())
.expect("begin tx correctly");
let val = vec![10; 50];
persy.insert_record(&mut tx, "data", &val).expect("insert correctly");
let _prepared = persy.prepare_commit(tx).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)
}
}