use crate::part::idcol::{get_varint, put_varint};
use crate::types::{AttrValue, BodyOp, Content, ContentHash, PieceHash, Record, BODY_CONTENT};
use anyhow::{bail, Context, Result};
use std::fs::File;
use std::path::Path;
pub type CarvedRecord = (Record, Vec<(PieceHash, Vec<u8>)>);
pub type FramedRecord = (Record, Vec<(PieceHash, Vec<u8>)>, bool);
pub const FRAME_TAG: u8 = 0x57;
pub const TOMB_TAG: u8 = 0x58;
pub const BATCH_COMMIT_TAG: u8 = 0x59;
pub const BATCH_FRAME_TAG: u8 = 0x5A;
pub const BATCH_TOMB_TAG: u8 = 0x5B;
pub const RECORD_V2_TAG: u8 = 0x5C;
pub const BATCH_RECORD_V2_TAG: u8 = 0x5D;
const HDR: usize = 13; const CRC: usize = 4;
pub struct Frame {
pub tomb: bool,
pub seq: u64,
pub record: Record,
pub novel: Vec<(PieceHash, Vec<u8>)>,
}
fn put_bytes(out: &mut Vec<u8>, b: &[u8]) {
put_varint(out, b.len() as u64);
out.extend_from_slice(b);
}
fn get_bytes<'a>(b: &'a [u8], at: &mut usize) -> Result<&'a [u8]> {
let n = usize::try_from(get_varint(b, at)?)
.context("wal: byte-string length exceeds this platform's address space")?;
if n > b.len() - *at {
bail!("wal: byte string runs past the frame");
}
let s = &b[*at..*at + n];
*at += n;
Ok(s)
}
fn take<'a>(b: &'a [u8], at: &mut usize, n: usize) -> Result<&'a [u8]> {
if n > b.len() - *at {
bail!("wal: field of {n} bytes runs past the frame");
}
let s = &b[*at..*at + n];
*at += n;
Ok(s)
}
fn put_ops(out: &mut Vec<u8>, ops: &[BodyOp]) {
put_varint(out, ops.len() as u64);
for op in ops {
match op {
BodyOp::Lit(b) => {
out.push(0);
put_bytes(out, b);
}
BodyOp::Piece { hash, len } => {
out.push(1);
out.extend_from_slice(&hash.0);
put_varint(out, *len as u64);
}
}
}
}
fn get_ops(b: &[u8], at: &mut usize) -> Result<Vec<BodyOp>> {
let n_ops = usize::try_from(get_varint(b, at)?)
.context("wal: content-op count exceeds this platform's address space")?;
let mut ops = Vec::with_capacity(n_ops.min(b.len()));
for _ in 0..n_ops {
let tag = *b.get(*at).ok_or_else(|| anyhow::anyhow!("wal: truncated content op"))?;
*at += 1;
match tag {
0 => ops.push(BodyOp::Lit(get_bytes(b, at)?.to_vec())),
1 => {
let mut h = [0u8; 32];
h.copy_from_slice(take(b, at, 32)?);
let len = u32::try_from(get_varint(b, at)?)
.context("wal: piece length exceeds the format's u32 limit")?;
ops.push(BodyOp::Piece { hash: PieceHash(h), len });
}
t => bail!("wal: unknown content op tag {t}"),
}
}
Ok(ops)
}
fn put_attrs(out: &mut Vec<u8>, attrs: &[(String, AttrValue)]) {
put_varint(out, attrs.len() as u64);
for (k, v) in attrs {
put_bytes(out, k.as_bytes());
out.push(v.type_tag());
match v {
AttrValue::Str(s) => put_bytes(out, s.as_bytes()),
AttrValue::Int(i) => out.extend_from_slice(&i.to_le_bytes()),
AttrValue::Float(f) => out.extend_from_slice(&f.to_bits().to_le_bytes()),
AttrValue::Bool(b) => out.push(u8::from(*b)),
AttrValue::UInt(i) => out.extend_from_slice(&i.to_le_bytes()),
AttrValue::Bytes(bytes) => put_bytes(out, bytes),
AttrValue::TimestampNs(ns) => out.extend_from_slice(&ns.to_le_bytes()),
AttrValue::Null => {}
}
}
}
fn get_attrs(b: &[u8], at: &mut usize, revision_two: bool) -> Result<Vec<(String, AttrValue)>> {
let n_attrs = usize::try_from(get_varint(b, at)?)
.context("wal: attribute count exceeds this platform's address space")?;
let mut attrs = Vec::with_capacity(n_attrs.min(b.len()));
for _ in 0..n_attrs {
let key = String::from_utf8(get_bytes(b, at)?.to_vec())?;
let tag = *b.get(*at).ok_or_else(|| anyhow::anyhow!("wal: truncated attr"))?;
*at += 1;
let v = match tag {
0 => AttrValue::Str(String::from_utf8(get_bytes(b, at)?.to_vec())?),
1 => AttrValue::Int(i64::from_le_bytes(take(b, at, 8)?.try_into().unwrap())),
2 => AttrValue::Float(f64::from_bits(u64::from_le_bytes(
take(b, at, 8)?.try_into().unwrap(),
))),
3 => AttrValue::Bool(take(b, at, 1)?[0] != 0),
4 if revision_two => {
AttrValue::UInt(u64::from_le_bytes(take(b, at, 8)?.try_into().unwrap()))
}
5 if revision_two => AttrValue::Bytes(get_bytes(b, at)?.to_vec()),
6 if revision_two => {
AttrValue::TimestampNs(i64::from_le_bytes(take(b, at, 8)?.try_into().unwrap()))
}
7 if revision_two => AttrValue::Null,
t => bail!("wal: unknown attr type tag {t}"),
};
attrs.push((key, v));
}
Ok(attrs)
}
fn put_novel(out: &mut Vec<u8>, novel: &[(PieceHash, Vec<u8>)]) {
put_varint(out, novel.len() as u64);
for (h, bytes) in novel {
out.extend_from_slice(&h.0);
put_bytes(out, bytes);
}
}
fn get_novel(b: &[u8], at: &mut usize) -> Result<Vec<(PieceHash, Vec<u8>)>> {
let n_novel = usize::try_from(get_varint(b, at)?)
.context("wal: novel-piece count exceeds this platform's address space")?;
let mut novel = Vec::with_capacity(n_novel.min(b.len() / 33 + 1));
for _ in 0..n_novel {
let mut h = [0u8; 32];
h.copy_from_slice(take(b, at, 32)?);
novel.push((PieceHash(h), get_bytes(b, at)?.to_vec()));
}
Ok(novel)
}
pub fn encode_record(out: &mut Vec<u8>, r: &Record, novel: &[(PieceHash, Vec<u8>)]) -> Result<()> {
crate::types::validate_contents(&r.contents)?;
if r.id.is_empty() {
bail!("record id must not be empty");
}
put_bytes(out, r.id.as_bytes());
put_varint(out, r.contents.len() as u64);
let mut contents: Vec<&Content> = r.contents.iter().collect();
contents.sort_by(|a, b| a.name.as_bytes().cmp(b.name.as_bytes()));
for content in contents {
put_bytes(out, content.name.as_bytes());
match content.identity {
Some(identity) => {
out.push(1);
out.extend_from_slice(&identity.0);
}
None => out.push(0),
}
put_ops(out, &content.ops);
}
put_attrs(out, &r.attrs);
put_novel(out, novel);
Ok(())
}
#[cfg(test)]
fn encode_record_v1(out: &mut Vec<u8>, r: &Record, novel: &[(PieceHash, Vec<u8>)]) {
put_bytes(out, r.id.as_bytes());
put_ops(out, r.body().unwrap_or(&[]));
put_attrs(out, &r.attrs);
put_novel(out, novel);
}
pub fn decode_record(b: &[u8]) -> Result<CarvedRecord> {
let mut at = 0usize;
let id = String::from_utf8(get_bytes(b, &mut at)?.to_vec())?;
let n_contents = usize::try_from(get_varint(b, &mut at)?)
.context("wal: content count exceeds this platform's address space")?;
let mut contents = Vec::with_capacity(n_contents.min(b.len()));
for _ in 0..n_contents {
let name = String::from_utf8(get_bytes(b, &mut at)?.to_vec())?;
let marker =
*b.get(at).ok_or_else(|| anyhow::anyhow!("wal: content identity marker is missing"))?;
at += 1;
let identity = match marker {
0 => None,
1 => {
let mut digest = [0u8; 32];
digest.copy_from_slice(take(b, &mut at, 32)?);
Some(ContentHash(digest))
}
marker => bail!("wal: unknown content identity marker {marker}"),
};
let mut content = Content::new(name, get_ops(b, &mut at)?);
content.identity = identity;
contents.push(content);
}
let attrs = get_attrs(b, &mut at, true)?;
let novel = get_novel(b, &mut at)?;
if at != b.len() {
bail!("wal: record has {} trailing bytes", b.len() - at);
}
Ok((Record::new(id, contents, attrs)?, novel))
}
fn decode_record_v1(b: &[u8]) -> Result<CarvedRecord> {
let mut at = 0usize;
let id = String::from_utf8(get_bytes(b, &mut at)?.to_vec())?;
let body = get_ops(b, &mut at)?;
let attrs = get_attrs(b, &mut at, false)?;
let novel = get_novel(b, &mut at)?;
if at != b.len() {
bail!("wal: legacy record has {} trailing bytes", b.len() - at);
}
Ok((Record::new(id, vec![Content::new(BODY_CONTENT, body)], attrs)?, novel))
}
pub struct Wal {
f: File,
path: std::path::PathBuf,
file_len: u64,
buf: Vec<u8>,
scratch: Vec<u8>,
read_limits: crate::read_limits::ReadLimits,
frame_count: u64,
}
pub(crate) struct ReplayState {
pub frames: Vec<Frame>,
pub physical_frames: u64,
pub valid_bytes: u64,
}
const BUF_FLUSH: usize = 1 << 20;
impl Wal {
pub fn open(path: &Path) -> Result<Wal> {
let read_limits = crate::read_limits::ReadLimits::default();
let replay = Self::replay_state_with_limits(path, read_limits)?;
Self::open_recovered(path, read_limits, replay.physical_frames, replay.valid_bytes)
}
pub(crate) fn open_recovered(
path: &Path,
read_limits: crate::read_limits::ReadLimits,
frame_count: u64,
valid_bytes: u64,
) -> Result<Wal> {
let read_limits = read_limits.validate()?;
read_limits.admit_wal_frames(frame_count)?;
let (f, created) = crate::vfs::open_or_create(path)
.with_context(|| format!("open wal {}", path.display()))?;
if created {
if let Some(parent) = path.parent() {
crate::vfs::sync_dir(parent).with_context(|| {
format!("fsync {} after creating the wal", parent.display())
})?;
}
}
let file_len = f.metadata()?.len();
if valid_bytes > file_len {
bail!("WAL recovery boundary {valid_bytes} runs past the file's {file_len} bytes");
}
if valid_bytes < file_len {
crate::vfs::set_len(&f, path, valid_bytes)?;
crate::vfs::sync_file(&f, path)?;
}
Ok(Wal {
f,
path: path.to_path_buf(),
file_len: valid_bytes,
buf: Vec::new(),
scratch: Vec::new(),
read_limits,
frame_count,
})
}
pub(crate) fn admit_additional_frames(&self, additional: u64) -> Result<()> {
let proposed = self
.frame_count
.checked_add(additional)
.ok_or_else(|| anyhow::anyhow!("WAL frame count overflow"))?;
self.read_limits.admit_wal_frames(proposed)?;
Ok(())
}
pub fn frame_count(&self) -> u64 {
self.frame_count
}
fn flush_buf(&mut self) -> Result<()> {
if self.buf.is_empty() {
return Ok(());
}
crate::vfs::write_all_at(&self.f, &self.path, &self.buf, self.file_len)?;
self.file_len += self.buf.len() as u64;
self.buf.clear();
Ok(())
}
fn append_frame(&mut self, tag: u8, seq: u64, payload: &[u8]) -> Result<()> {
if payload.len() as u64 > u32::MAX as u64 {
bail!("wal frame payload of {} bytes exceeds the u32 length field", payload.len());
}
self.admit_additional_frames(1)?;
let mut hdr = Vec::with_capacity(HDR);
hdr.push(tag);
hdr.extend_from_slice(&seq.to_le_bytes());
hdr.extend_from_slice(&(payload.len() as u32).to_le_bytes());
let mut crc = crc32fast::Hasher::new();
crc.update(&hdr);
crc.update(payload);
let c = crc.finalize();
self.buf.extend_from_slice(&hdr);
self.buf.extend_from_slice(payload);
self.buf.extend_from_slice(&c.to_le_bytes());
if self.buf.len() >= BUF_FLUSH {
self.flush_buf()?;
}
self.frame_count += 1;
Ok(())
}
pub fn append_tomb(&mut self, seq: u64, id: &str) -> Result<()> {
let mut scratch = std::mem::take(&mut self.scratch);
scratch.clear();
scratch.extend_from_slice(id.as_bytes());
let r = self.append_frame(TOMB_TAG, seq, &scratch);
self.scratch = scratch;
r
}
pub fn append(&mut self, seq: u64, r: &Record, novel: &[(PieceHash, Vec<u8>)]) -> Result<()> {
let mut scratch = std::mem::take(&mut self.scratch);
scratch.clear();
let encoded = encode_record(&mut scratch, r, novel);
let res = encoded.and_then(|()| self.append_frame(RECORD_V2_TAG, seq, &scratch));
self.scratch = scratch;
res
}
pub fn append_batch(&mut self, seq: u64, items: &[FramedRecord]) -> Result<()> {
self.admit_additional_frames(items.len() as u64 + 1)?;
for (r, novel, tomb) in items {
let mut scratch = std::mem::take(&mut self.scratch);
scratch.clear();
let tag = if *tomb {
scratch.extend_from_slice(r.id.as_bytes());
BATCH_TOMB_TAG
} else {
encode_record(&mut scratch, r, novel)?;
BATCH_RECORD_V2_TAG
};
let res = self.append_frame(tag, seq, &scratch);
self.scratch = scratch;
res?;
}
let mut count = Vec::with_capacity(4);
put_varint(&mut count, items.len() as u64);
self.append_frame(BATCH_COMMIT_TAG, seq, &count)
}
pub fn sync(&mut self) -> Result<()> {
self.flush_buf()?;
crate::vfs::sync_file(&self.f, &self.path).context("fsync wal")?;
Ok(())
}
pub fn truncate(&mut self) -> Result<()> {
self.buf.clear();
crate::vfs::set_len(&self.f, &self.path, 0)?;
crate::vfs::sync_file(&self.f, &self.path)?;
self.file_len = 0;
self.frame_count = 0;
Ok(())
}
pub fn bytes(&self) -> u64 {
self.file_len + self.buf.len() as u64
}
pub fn replay(path: &Path) -> Result<Vec<Frame>> {
Self::replay_with_limits(path, crate::read_limits::ReadLimits::default())
}
pub fn replay_with_limits(
path: &Path,
read_limits: crate::read_limits::ReadLimits,
) -> Result<Vec<Frame>> {
Ok(Self::replay_state_with_limits(path, read_limits)?.frames)
}
pub(crate) fn replay_state_with_limits(
path: &Path,
read_limits: crate::read_limits::ReadLimits,
) -> Result<ReplayState> {
let read_limits = read_limits.validate()?;
let f = match File::open(path) {
Ok(f) => f,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(ReplayState { frames: Vec::new(), physical_frames: 0, valid_bytes: 0 });
}
Err(error) => return Err(error.into()),
};
let len = f.metadata()?.len();
let mut out = Vec::new();
let mut pending: Vec<Frame> = Vec::new();
let mut pending_start: Option<(u64, u64)> = None;
let mut physical_frames = 0u64;
let mut off = 0u64;
let mut hdr = [0u8; HDR];
loop {
if off + HDR as u64 + CRC as u64 > len
|| crate::sys::read_exact_at(&f, &mut hdr, off).is_err()
{
break;
}
let known = matches!(
hdr[0],
FRAME_TAG
| TOMB_TAG
| BATCH_COMMIT_TAG
| BATCH_FRAME_TAG
| BATCH_TOMB_TAG
| RECORD_V2_TAG
| BATCH_RECORD_V2_TAG
);
let seq = u64::from_le_bytes(hdr[1..9].try_into().unwrap());
let plen = u32::from_le_bytes(hdr[9..13].try_into().unwrap()) as usize;
let end = off + HDR as u64 + plen as u64 + CRC as u64;
if end > len {
break;
}
read_limits.admit_wal_frames(physical_frames.saturating_add(1))?;
read_limits.admit("WAL frame", plen as u64, plen as u64)?;
let mut payload = vec![0u8; plen];
if crate::sys::read_exact_at(&f, &mut payload, off + HDR as u64).is_err() {
break;
}
let mut cb = [0u8; CRC];
if crate::sys::read_exact_at(&f, &mut cb, off + HDR as u64 + plen as u64).is_err() {
break;
}
let mut crc = crc32fast::Hasher::new();
crc.update(&hdr);
crc.update(&payload);
if crc.finalize() != u32::from_le_bytes(cb) {
break; }
if !known {
bail!(
"wal frame tag {:#04x} is not a type this build knows, and it checksums — \
refusing rather than discarding committed records",
hdr[0]
);
}
match hdr[0] {
BATCH_COMMIT_TAG => {
let mut at = 0usize;
let Ok(n) = get_varint(&payload, &mut at) else { break };
if at != payload.len() {
break; }
let n = n as usize;
if n > pending.len() {
bail!(
"wal batch commit seals {n} frames but only {} precede it",
pending.len()
);
}
let start = pending.len() - n;
out.extend(pending.drain(..).skip(start));
pending_start = None;
}
TOMB_TAG | BATCH_TOMB_TAG => {
let Ok(id) = String::from_utf8(payload) else { break };
let fr = Frame {
seq,
tomb: true,
record: Record { id, contents: Vec::new(), attrs: Vec::new() },
novel: Vec::new(),
};
if hdr[0] == BATCH_TOMB_TAG {
if pending.is_empty() {
pending_start = Some((off, physical_frames));
}
pending.push(fr);
} else {
pending.clear();
pending_start = None;
out.push(fr);
}
}
FRAME_TAG | BATCH_FRAME_TAG | RECORD_V2_TAG | BATCH_RECORD_V2_TAG => {
let decoded = if matches!(hdr[0], FRAME_TAG | BATCH_FRAME_TAG) {
decode_record_v1(&payload)
} else {
decode_record(&payload)
};
match decoded {
Ok((record, novel)) => {
let fr = Frame { seq, tomb: false, record, novel };
if matches!(hdr[0], BATCH_FRAME_TAG | BATCH_RECORD_V2_TAG) {
if pending.is_empty() {
pending_start = Some((off, physical_frames));
}
pending.push(fr);
} else {
pending.clear();
pending_start = None;
out.push(fr);
}
}
Err(_) => break,
}
}
_ => unreachable!("known WAL tag was not handled"),
}
physical_frames += 1;
off = end;
}
let (valid_bytes, physical_frames) = pending_start.unwrap_or((off, physical_frames));
Ok(ReplayState { frames: out, physical_frames, valid_bytes })
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::OpenOptions;
use std::io::Write;
fn rec() -> (Record, Vec<(PieceHash, Vec<u8>)>) {
let bytes = b"a novel piece".to_vec();
let h = PieceHash::of(&bytes);
(
Record {
id: "genai:aé#in".into(),
contents: vec![Content::new(
BODY_CONTENT,
vec![
BodyOp::Lit(b"[".to_vec()),
BodyOp::Piece { hash: h, len: bytes.len() as u32 },
],
)],
attrs: vec![
("k".into(), AttrValue::Str("v".into())),
("k".into(), AttrValue::Int(-5)),
("f".into(), AttrValue::Float(f64::from_bits(0x7ff8_0000_0000_0001))),
("z".into(), AttrValue::Float(-0.0)),
("b".into(), AttrValue::Bool(true)),
("u".into(), AttrValue::UInt(u64::MAX)),
("raw".into(), AttrValue::Bytes(vec![0, 0xff, 1])),
("at".into(), AttrValue::TimestampNs(i64::MIN)),
("none".into(), AttrValue::Null),
],
},
vec![(h, bytes)],
)
}
#[test]
fn replay_admits_a_complete_frame_before_payload_allocation() {
let p = std::env::temp_dir().join(format!(
"turndb-wal-read-limit-{}-{}",
std::process::id(),
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
));
let mut wal = Wal::open(&p).unwrap();
wal.append_tomb(1, &"x".repeat(128)).unwrap();
wal.sync().unwrap();
drop(wal);
let error = match Wal::replay_with_limits(
&p,
crate::read_limits::ReadLimits {
max_stored_frame_bytes: 64,
max_decoded_frame_bytes: 64,
..crate::read_limits::ReadLimits::default()
},
) {
Ok(_) => panic!("strict replay must refuse the complete larger frame"),
Err(error) => error,
};
assert_eq!(crate::error::classify(&error), crate::error::ErrorClass::ResourceExhausted);
let _ = std::fs::remove_file(p);
}
#[test]
fn record_wire_roundtrips_exactly() {
let (r, novel) = rec();
let mut b = Vec::new();
encode_record(&mut b, &r, &novel).unwrap();
let (got, gn) = decode_record(&b).unwrap();
assert_eq!(got, r, "record must replay exactly, attr order and dupes included");
assert_eq!(gn.len(), 1);
assert_eq!(gn[0].1, novel[0].1);
match (&got.attrs[2].1, &r.attrs[2].1) {
(AttrValue::Float(a), AttrValue::Float(b)) => assert_eq!(a.to_bits(), b.to_bits()),
_ => panic!(),
}
}
#[test]
fn two_record_generations_replay_together() {
let d = std::env::temp_dir().join(format!("turndb-wal-mixed-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
let p = d.join("wal");
let (mut legacy, novel) = rec();
legacy.attrs.truncate(5); let identified = Record::new(
"v2",
vec![Content::identified(
"response",
vec![BodyOp::Lit(b"identified".to_vec())],
ContentHash::of(b"identified"),
)],
vec![],
)
.unwrap();
let (mut current, current_novel) = rec();
current.id = "v2-full".into();
{
let mut w = Wal::open(&p).unwrap();
let mut old_payload = Vec::new();
encode_record_v1(&mut old_payload, &legacy, &novel);
w.append_frame(FRAME_TAG, 1, &old_payload).unwrap();
w.append(1, &identified, &[]).unwrap();
w.append(1, ¤t, ¤t_novel).unwrap();
w.sync().unwrap();
}
let replay = Wal::replay(&p).unwrap();
assert_eq!(replay.len(), 3);
assert_eq!(replay[0].record.contents, legacy.contents);
assert_eq!(replay[0].record.contents[0].name, BODY_CONTENT);
assert_eq!(replay[1].record, identified);
assert_eq!(replay[1].record.contents[0].identity, Some(ContentHash::of(b"identified")));
assert_eq!(replay[2].record, current);
std::fs::remove_dir_all(&d).ok();
}
#[test]
fn log_replays_and_stops_at_a_torn_tail() {
let d = std::env::temp_dir().join(format!("turndb-wal-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
let p = d.join("wal");
let (r, novel) = rec();
{
let mut w = Wal::open(&p).unwrap();
for s in 1..=5 {
w.append(s, &r, &novel).unwrap();
}
w.sync().unwrap();
}
assert_eq!(Wal::replay(&p).unwrap().len(), 5);
{
let mut f = OpenOptions::new().append(true).open(&p).unwrap();
f.write_all(&[FRAME_TAG, 9, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0]).unwrap();
f.write_all(b"short").unwrap();
f.sync_all().unwrap();
}
let frames = Wal::replay(&p).unwrap();
assert_eq!(frames.len(), 5, "a torn tail is the end of the log, not an error");
assert_eq!(frames[4].seq, 5);
std::fs::remove_dir_all(&d).ok();
}
#[test]
fn a_batch_replays_all_or_nothing() {
let d = std::env::temp_dir().join(format!("turndb-walbatch-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
let p = d.join("wal");
let mk = |id: &str| {
let (mut r, n) = rec();
r.id = id.into();
(r, n)
};
let (a, na) = mk("a");
let (b, nb) = mk("b");
{
let mut w = Wal::open(&p).unwrap();
w.append(1, &a, &na).unwrap();
let tomb = Record { id: "z".into(), contents: Vec::new(), attrs: Vec::new() };
w.append_batch(
2,
&[
(a.clone(), na.clone(), false),
(tomb, Vec::new(), true),
(b.clone(), nb.clone(), false),
],
)
.unwrap();
w.sync().unwrap();
}
let f = Wal::replay(&p).unwrap();
assert_eq!(f.len(), 4, "standalone + three sealed members");
assert!(f[2].tomb && f[2].record.id == "z");
assert_eq!(f[3].record.id, "b");
let flen = std::fs::metadata(&p).unwrap().len();
let fh = OpenOptions::new().write(true).open(&p).unwrap();
fh.set_len(flen - (HDR as u64 + 1 + CRC as u64)).unwrap();
let f = Wal::replay(&p).unwrap();
assert_eq!(f.len(), 1, "an unsealed batch must not replay a prefix of itself");
{
let mut w = Wal::open(&p).unwrap();
w.append_batch(3, &[(b.clone(), nb.clone(), false)]).unwrap();
w.sync().unwrap();
}
let f = Wal::replay(&p).unwrap();
assert_eq!(
f.len(),
2,
"abandoned members stay discarded: {:?}",
f.iter().map(|x| &x.record.id).collect::<Vec<_>>()
);
assert_eq!(f[1].record.id, "b");
let p2 = d.join("wal2");
{
let mut w = Wal::open(&p2).unwrap();
let mut count = Vec::new();
put_varint(&mut count, 5);
w.append_frame(BATCH_COMMIT_TAG, 1, &count).unwrap();
w.sync().unwrap();
}
assert!(Wal::replay(&p2).is_err(), "a marker sealing absent frames must refuse the log");
std::fs::remove_dir_all(&d).ok();
}
#[test]
fn truncate_empties_the_log() {
let d = std::env::temp_dir().join(format!("turndb-wal2-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
let p = d.join("wal");
let (r, novel) = rec();
let mut w = Wal::open(&p).unwrap();
w.append(1, &r, &novel).unwrap();
w.sync().unwrap();
assert!(w.bytes() > 0);
w.truncate().unwrap();
assert_eq!(w.bytes(), 0);
assert!(Wal::replay(&p).unwrap().is_empty());
std::fs::remove_dir_all(&d).ok();
}
}