use serde::{Serialize, Deserialize};
use crc32fast::Hasher;
pub const PAGE_MAGIC: u32 = 0x53484442;
pub const PAGE_VERSION: u16 = 1;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PageHeader {
pub magic: u32,
pub version: u16,
pub checksum: u32,
pub min_id: String,
pub max_id: String,
pub num_records: u32,
pub page_seqno: u64,
}
impl PageHeader {
pub fn new(
min_id: String,
max_id: String,
num_records: u32,
page_seqno: u64,
) -> Self {
Self {
magic: PAGE_MAGIC,
version: PAGE_VERSION,
checksum: 0, min_id,
max_id,
num_records,
page_seqno,
}
}
pub fn compute_checksum(payload: &[u8]) -> u32 {
let mut hasher = Hasher::new();
hasher.update(payload);
hasher.finalize()
}
pub fn validate(&self) -> Result<(), String> {
if self.magic != PAGE_MAGIC {
return Err("Invalid page magic".into());
}
if self.version != PAGE_VERSION {
return Err("Unsupported page version".into());
}
if self.min_id > self.max_id {
return Err("min_id > max_id".into());
}
Ok(())
}
}