use crate::{
ObjectLogError,
native_v2::codec::{FrameKind, LeafEntry, PAGE_BYTES},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NativeReadRequirements {
logical_identity_bytes: u64,
physical_identity_bytes: u64,
identity_pages: u64,
validation_pages: u64,
frame_bytes: u64,
hydration_reads: u64,
hydration_bytes: u64,
}
impl NativeReadRequirements {
pub(super) fn from_entry(entry: &LeafEntry) -> Result<Self, ObjectLogError> {
entry.end()?;
let overflow =
|| ObjectLogError::BudgetExceeded("native read requirements overflow".into());
let identity_pages = entry
.logical_id
.entries
.checked_add(entry.object_id.entries)
.ok_or_else(overflow)?;
let validation_pages = u64::from(entry.frame_kind == FrameKind::Coupled);
let pages = identity_pages
.checked_add(validation_pages)
.ok_or_else(overflow)?;
let frame_bytes = u64::from(entry.byte_start)
.checked_add(u64::from(entry.byte_len))
.ok_or_else(overflow)?;
let hydration_reads = pages.checked_add(1).ok_or_else(overflow)?;
let hydration_bytes = pages
.checked_mul(PAGE_BYTES as u64)
.and_then(|bytes| bytes.checked_add(frame_bytes))
.ok_or_else(overflow)?;
Ok(Self {
logical_identity_bytes: entry.logical_id.end,
physical_identity_bytes: entry.object_id.end,
identity_pages,
validation_pages,
frame_bytes,
hydration_reads,
hydration_bytes,
})
}
pub fn logical_identity_bytes(&self) -> u64 {
self.logical_identity_bytes
}
pub fn physical_identity_bytes(&self) -> u64 {
self.physical_identity_bytes
}
pub fn identity_pages(&self) -> u64 {
self.identity_pages
}
pub fn validation_pages(&self) -> u64 {
self.validation_pages
}
pub fn frame_bytes(&self) -> u64 {
self.frame_bytes
}
pub fn hydration_reads(&self) -> u64 {
self.hydration_reads
}
pub fn hydration_bytes(&self) -> u64 {
self.hydration_bytes
}
}
#[cfg(test)]
mod tests;