use serde::{Deserialize, Serialize};
use crate::redo::RedoOp;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitRecord {
pub lsn: u64,
pub commit_ts: u64,
pub ops: Vec<RedoOp>,
}
impl CommitRecord {
pub const LSN_KEY: &'static str = "{\"lsn\":";
pub const COMMIT_TS_KEY: &'static str = ",\"commit_ts\":";
pub const OPS_KEY: &'static str = ",\"ops\":";
pub const U64_DECIMAL_MAX: usize = 20;
pub const HEADER_MAX: usize = Self::LSN_KEY.len()
+ Self::COMMIT_TS_KEY.len()
+ Self::OPS_KEY.len()
+ 2 * Self::U64_DECIMAL_MAX;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_header_spellings_match_the_serialized_field_names() {
let record = CommitRecord {
lsn: u64::MAX,
commit_ts: u64::MAX,
ops: Vec::new(),
};
let json = serde_json::to_string(&record).unwrap();
let header = format!(
"{}{}{}{}{}",
CommitRecord::LSN_KEY,
u64::MAX,
CommitRecord::COMMIT_TS_KEY,
u64::MAX,
CommitRecord::OPS_KEY
);
assert_eq!(json, format!("{header}[]}}"));
assert_eq!(header.len(), CommitRecord::HEADER_MAX);
assert_eq!(u64::MAX.to_string().len(), CommitRecord::U64_DECIMAL_MAX);
}
}