extern crate persy;
use persy::Persy;
use persy::PersyError;
use persy::Config;
use persy::TxStrategy;
use std::fs;
#[test]
fn test_insert_read_commit_record() {
{
Persy::create("./irc.persy").unwrap();
}
{
let persy = Persy::open("./irc.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let bytes = String::from("something").into_bytes();
let read_after = persy.read_record("test", &id).unwrap();
assert_eq!(read_after, Some(bytes));
}
fs::remove_file("./irc.persy").unwrap();
}
#[test]
fn test_insert_update_record() {
{
Persy::create("./iru.persy").unwrap();
}
{
let persy = Persy::open("./iru.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let rec_data: String = "something".into();
let bytes = rec_data.into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let rec_1: String = "newthing".into();
let bytes_1 = rec_1.into_bytes();
let mut tx1 = persy.begin().unwrap();
persy.update_record(&mut tx1, "test", &id, &bytes_1).unwrap();
let read_after = persy.read_record_tx(&mut tx1, "test", &id).unwrap();
assert_eq!(read_after, Some(bytes_1));
let finalizer = persy.prepare_commit(tx1).unwrap();
persy.commit(finalizer).unwrap();
}
fs::remove_file("./iru.persy").unwrap();
}
#[test]
fn test_insert_delete_record() {
{
Persy::create("./ird.persy").unwrap();
}
{
let persy = Persy::open("./ird.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let rec_data: String = "something".into();
let bytes = rec_data.into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let mut tx1 = persy.begin().unwrap();
persy.delete_record(&mut tx1, "test", &id).unwrap();
let read_after = persy.read_record_tx(&mut tx1, "test", &id).unwrap();
assert_eq!(read_after, None);
let finalizer = persy.prepare_commit(tx1).unwrap();
persy.commit(finalizer).unwrap();
}
fs::remove_file("./ird.persy").unwrap();
}
#[test]
fn test_insert_scan_records() {
{
Persy::create("./irs.persy").unwrap();
}
{
let persy = Persy::open("./irs.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let rec_data: String = "something".into();
let bytes = rec_data.into_bytes();
persy.insert_record(&mut tx, "test", &bytes).unwrap();
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let iter = persy.scan_records("test").unwrap().into_iter();
assert_eq!(iter.count(), 1);
let rec = persy.scan_records("test").unwrap().into_iter().next().unwrap();
assert_eq!(bytes, rec.content);
}
fs::remove_file("./irs.persy").unwrap();
}
#[test]
fn test_insert_update_same_tx() {
{
Persy::create("./ius.persy").unwrap();
}
{
let persy = Persy::open("./ius.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let bytes = String::from("data1").into_bytes();
persy.update_record(&mut tx, "test", &id, &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let read_after = persy.read_record("test", &id).unwrap();
let bytes = String::from("data1").into_bytes();
assert_eq!(read_after, Some(bytes));
}
fs::remove_file("./ius.persy").unwrap();
}
#[test]
fn test_double_update_same_tx() {
{
Persy::create("./iuus.persy").unwrap();
}
{
let persy = Persy::open("./iuus.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let mut tx = persy.begin().unwrap();
let bytes = String::from("first").into_bytes();
persy.update_record(&mut tx, "test", &id, &bytes).unwrap();
let bytes = String::from("second").into_bytes();
persy.update_record(&mut tx, "test", &id, &bytes).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, Some(bytes));
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let read_after = persy.read_record("test", &id).unwrap();
let bytes = String::from("second").into_bytes();
assert_eq!(read_after, Some(bytes));
}
fs::remove_file("./iuus.persy").unwrap();
}
#[test]
fn test_insert_update_delete_same_tx() {
{
Persy::create("./iuds.persy").unwrap();
}
{
let persy = Persy::open("./iuds.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let bytes = String::from("first").into_bytes();
persy.update_record(&mut tx, "test", &id, &bytes).unwrap();
persy.delete_record(&mut tx, "test", &id).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, None);
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let read_after = persy.read_record("test", &id).unwrap();
assert_eq!(read_after, None);
}
fs::remove_file("./iuds.persy").unwrap();
}
#[test]
fn test_update_delete_same_tx() {
{
Persy::create("./uds.persy").unwrap();
}
{
let persy = Persy::open("./uds.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let id = persy.insert_record(&mut tx, "test", &bytes).unwrap();
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let mut tx = persy.begin().unwrap();
let bytes = String::from("first").into_bytes();
persy.update_record(&mut tx, "test", &id, &bytes).unwrap();
persy.delete_record(&mut tx, "test", &id).unwrap();
let read_opt = persy.read_record_tx(&mut tx, "test", &id).unwrap();
assert_eq!(read_opt, None);
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let read_after = persy.read_record("test", &id).unwrap();
assert_eq!(read_after, None);
}
fs::remove_file("./uds.persy").unwrap();
}
#[test]
fn test_insert_100_same_tx() {
{
Persy::create("./i100.persy").unwrap();
}
{
let persy = Persy::open("./i100.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
for _ in [0; 100].iter() {
persy.insert_record(&mut tx, "test", &bytes).unwrap();
}
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let mut count = 0;
for _ in persy.scan_records("test").unwrap() {
count += 1;
}
assert_eq!(count, 100);
}
fs::remove_file("./i100.persy").unwrap();
}
#[test]
fn test_create_insert_scan_same_tx() {
{
Persy::create("./cistx.persy").unwrap();
}
{
let persy = Persy::open("./cistx.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
for _ in [0; 50].iter() {
persy.insert_record(&mut tx, "test", &bytes).unwrap();
}
let mut count = 0;
for rec in persy.scan_records_tx(&mut tx, "test").unwrap() {
assert_eq!(rec.content, bytes);
count += 1;
}
assert_eq!(count, 50);
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
}
fs::remove_file("./cistx.persy").unwrap();
}
#[test]
fn test_insert_100_and_scan_same_tx() {
{
Persy::create("./i100tx.persy").unwrap();
}
{
let persy = Persy::open("./i100tx.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
for _ in [0; 50].iter() {
persy.insert_record(&mut tx, "test", &bytes).unwrap();
}
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let mut tx = persy.begin().unwrap();
for _ in [0; 50].iter() {
persy.insert_record(&mut tx, "test", &bytes).unwrap();
}
let mut count = 0;
for rec in persy.scan_records_tx(&mut tx, "test").unwrap() {
assert_eq!(rec.content, bytes);
count += 1;
}
assert_eq!(count, 100);
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
}
fs::remove_file("./i100tx.persy").unwrap();
}
#[test]
fn test_insert_100_update_and_scan_same_tx() {
{
Persy::create("./i100u_tx.persy").unwrap();
}
{
let persy = Persy::open("./i100u_tx.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "test").unwrap();
let bytes = String::from("something").into_bytes();
let mut inserted = Vec::new();
for _ in [0; 50].iter() {
inserted.push(persy.insert_record(&mut tx, "test", &bytes).unwrap());
}
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
let bytes_other = String::from("other").into_bytes();
let mut tx = persy.begin().unwrap();
for _ in [0; 50].iter() {
persy.insert_record(&mut tx, "test", &bytes).unwrap();
}
for id in inserted.iter() {
persy.update_record(&mut tx, "test", id, &bytes_other).unwrap();
}
let mut count = 0;
for rec in persy.scan_records_tx(&mut tx, "test").unwrap() {
if inserted.contains(&rec.id) {
assert_eq!(rec.content, bytes_other);
} else {
assert_eq!(rec.content, bytes);
}
count += 1;
}
assert_eq!(count, 100);
let finalizer = persy.prepare_commit(tx).unwrap();
persy.commit(finalizer).unwrap();
}
fs::remove_file("./i100u_tx.persy").unwrap();
}
#[test]
fn test_scan_tx_segment_not_exist() {
{
Persy::create("./sne_tx.persy").unwrap();
}
{
let persy = Persy::open("./sne_tx.persy", Config::new()).unwrap();
let mut tx = persy.begin().unwrap();
let res = persy.scan_records_tx(&mut tx, "test");
if let Err(x) = res {
match x {
PersyError::SegmentNotFound => {}
_ => assert!(false),
}
} else {
assert!(false);
}
}
fs::remove_file("./sne_tx.persy").unwrap();
}
#[test]
fn test_concurrency_version_on_write() {
{
Persy::create("./cvow.persy").unwrap();
}
{
let mut config = Config::new();
config.change_tx_strategy(TxStrategy::VersionOnWrite);
let persy = Persy::open("./cvow.persy", config).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "seg").unwrap();
let bytes = String::from("aaa").into_bytes();
let id = persy.insert_record(&mut tx, "seg", &bytes).unwrap();
let prep = persy.prepare_commit(tx).unwrap();
persy.commit(prep).unwrap();
let bytes2 = String::from("bbb").into_bytes();
let mut tx = persy.begin().unwrap();
persy.update_record(&mut tx, "seg", &id, &bytes2).unwrap();
let bytes3 = String::from("cccc").into_bytes();
let mut tx1 = persy.begin().unwrap();
persy.update_record(&mut tx1, "seg", &id, &bytes3).unwrap();
let prep = persy.prepare_commit(tx1).unwrap();
persy.commit(prep).unwrap();
let to_fail = persy.prepare_commit(tx);
assert!(to_fail.is_err());
let val = persy.read_record("seg", &id).unwrap().unwrap();
assert_eq!(val, bytes3);
}
fs::remove_file("./cvow.persy").unwrap();
}
#[test]
fn test_concurrency_last_win() {
{
Persy::create("./cvol.persy").unwrap();
}
{
let mut config = Config::new();
config.change_tx_strategy(TxStrategy::LastWin);
let persy = Persy::open("./cvol.persy", config).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "seg").unwrap();
let bytes = String::from("aaa").into_bytes();
let id = persy.insert_record(&mut tx, "seg", &bytes).unwrap();
let prep = persy.prepare_commit(tx).unwrap();
persy.commit(prep).unwrap();
let bytes2 = String::from("bbb").into_bytes();
let mut tx = persy.begin().unwrap();
persy.update_record(&mut tx, "seg", &id, &bytes2).unwrap();
let bytes3 = String::from("cccc").into_bytes();
let mut tx1 = persy.begin().unwrap();
persy.update_record(&mut tx1, "seg", &id, &bytes3).unwrap();
let prep = persy.prepare_commit(tx1).unwrap();
persy.commit(prep).unwrap();
let prep = persy.prepare_commit(tx).unwrap();
persy.commit(prep).unwrap();
let val = persy.read_record("seg", &id).unwrap().unwrap();
assert_eq!(val, bytes2);
}
fs::remove_file("./cvol.persy").unwrap();
}
#[test]
fn test_concurrency_version_on_read() {
{
Persy::create("./cvor.persy").unwrap();
}
{
let mut config = Config::new();
config.change_tx_strategy(TxStrategy::VersionOnRead);
let persy = Persy::open("./cvor.persy", config).unwrap();
let mut tx = persy.begin().unwrap();
persy.create_segment(&mut tx, "seg").unwrap();
let bytes = String::from("aaa").into_bytes();
let id = persy.insert_record(&mut tx, "seg", &bytes).unwrap();
let prep = persy.prepare_commit(tx).unwrap();
persy.commit(prep).unwrap();
let bytes2 = String::from("bbb").into_bytes();
let mut tx = persy.begin().unwrap();
let _ = persy.read_record_tx(&mut tx, "seg", &id).unwrap();
let bytes3 = String::from("cccc").into_bytes();
let mut tx1 = persy.begin().unwrap();
persy.update_record(&mut tx1, "seg", &id, &bytes3).unwrap();
let prep = persy.prepare_commit(tx1).unwrap();
persy.commit(prep).unwrap();
persy.update_record(&mut tx, "seg", &id, &bytes2).unwrap();
let to_fail = persy.prepare_commit(tx);
assert!(to_fail.is_err());
let val = persy.read_record("seg", &id).unwrap().unwrap();
assert_eq!(val, bytes3);
}
fs::remove_file("./cvor.persy").unwrap();
}
#[test]
fn test_scan_segment_not_exist() {
{
Persy::create("./sne.persy").unwrap();
}
{
let persy = Persy::open("./sne.persy", Config::new()).unwrap();
let res = persy.scan_records("test");
if let Err(x) = res {
match x {
PersyError::SegmentNotFound => {}
_ => assert!(false),
}
} else {
assert!(false);
}
}
fs::remove_file("./sne.persy").unwrap();
}