#[cfg(test)]
mod tests {
use crate::wal::Wal;
use crate::wal::tests::helpers::*;
use tempfile::TempDir;
#[test]
fn one_append_and_replay() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("000000.log");
let wal = Wal::open(path.to_str().unwrap(), None).unwrap();
let insert = vec![MemTableRecord {
key: b"a".to_vec(),
value: Some(b"v1".to_vec()),
timestamp: 1,
deleted: false,
}];
for record in &insert {
wal.append(record).unwrap();
}
let replayed = collect_iter(&wal).unwrap();
assert_eq!(insert, replayed);
}
#[test]
fn many_appends_and_replay() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("000000.log");
let wal = Wal::open(path.to_str().unwrap(), None).unwrap();
let insert = vec![
ManifestRecord {
id: 0,
path: "/db/table-0".to_string(),
creation_timestamp: 100,
},
ManifestRecord {
id: 1,
path: "/db/table-1".to_string(),
creation_timestamp: 101,
},
ManifestRecord {
id: 2,
path: "/db/table-2".to_string(),
creation_timestamp: 102,
},
];
for record in &insert {
wal.append(record).unwrap();
}
let replayed = collect_iter(&wal).unwrap();
assert_eq!(insert, replayed);
}
#[test]
fn append_replay_and_truncate() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("000000.log");
let mut wal = Wal::open(path.to_str().unwrap(), None).unwrap();
let insert = vec![
MemTableRecord {
key: b"a".to_vec(),
value: Some(b"v1".to_vec()),
timestamp: 1,
deleted: false,
},
MemTableRecord {
key: b"b".to_vec(),
value: Some(b"v2".to_vec()),
timestamp: 2,
deleted: false,
},
MemTableRecord {
key: b"c".to_vec(),
value: Some(b"v3".to_vec()),
timestamp: 3,
deleted: false,
},
];
for record in &insert {
wal.append(record).unwrap();
}
let replayed = collect_iter(&wal).unwrap();
assert_eq!(insert, replayed);
wal.truncate().unwrap();
let replayed = collect_iter(&wal).unwrap();
assert_eq!(replayed.len(), 0);
}
#[test]
fn full_cycle_write_truncate_rewrite() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("000000.log");
let mut wal = Wal::open(path.to_str().unwrap(), None).unwrap();
let batch1 = vec![
ManifestRecord {
id: 0,
path: "/db/table-0".to_string(),
creation_timestamp: 100,
},
ManifestRecord {
id: 1,
path: "/db/table-1".to_string(),
creation_timestamp: 101,
},
];
let batch2 = vec![
ManifestRecord {
id: 100,
path: "/db/table-100".to_string(),
creation_timestamp: 1000,
},
ManifestRecord {
id: 101,
path: "/db/table-101".to_string(),
creation_timestamp: 1001,
},
ManifestRecord {
id: 102,
path: "/db/table-102".to_string(),
creation_timestamp: 1002,
},
];
for record in &batch1 {
wal.append(record).unwrap();
}
let replayed = collect_iter(&wal).unwrap();
assert_eq!(batch1, replayed);
wal.truncate().unwrap();
let replayed = collect_iter(&wal).unwrap();
assert_eq!(replayed.len(), 0);
for record in &batch2 {
wal.append(record).unwrap();
}
let replayed = collect_iter(&wal).unwrap();
assert_eq!(batch2, replayed);
wal.truncate().unwrap();
let replayed = collect_iter(&wal).unwrap();
assert_eq!(replayed.len(), 0);
}
}