use std::collections::HashMap;
use super::registry::BuiltRegistry;
#[derive(Clone, Copy, Debug)]
pub struct SegmentedCheckLimits {
pub rows: u64,
pub bytes: u64,
}
impl Default for SegmentedCheckLimits {
fn default() -> Self {
Self {
rows: 10_000_000,
bytes: 1 << 30,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum UncheckedReason {
TargetTooLarge { physical_entries: u64 },
ExpectedRowsExceeded { rows: u64 },
BytesExceeded { bytes: u64 },
}
pub(super) const ENTRY_OVERHEAD: u64 = 96;
pub(super) fn entry_bytes(bytes: usize) -> u64 {
(bytes as u64).saturating_add(ENTRY_OVERHEAD)
}
pub(super) struct SegBudget {
limits: SegmentedCheckLimits,
segmented: HashMap<&'static str, ()>,
rows: HashMap<&'static str, u64>,
total_bytes: u64,
unchecked: HashMap<&'static str, UncheckedReason>,
}
impl SegBudget {
pub(super) fn new(reg: &BuiltRegistry) -> Self {
let mut b = Self {
limits: reg.segmented_check_limits,
segmented: HashMap::new(),
rows: HashMap::new(),
total_bytes: 0,
unchecked: HashMap::new(),
};
for name in super::expected::declared_targets(reg) {
if let Some(t) = super::expected::target_decl(reg, name)
&& matches!(
t.index.target_kind(),
crate::projection::TargetKind::Segmented { .. }
)
{
b.segmented.insert(name, ());
let physical_entries = t.index.physical_entries().unwrap_or(0);
if physical_entries > b.limits.rows {
b.unchecked
.insert(name, UncheckedReason::TargetTooLarge { physical_entries });
}
}
}
b
}
pub(super) fn is_segmented(&self, name: &str) -> bool {
self.segmented.contains_key(name)
}
pub(super) fn is_unchecked(&self, name: &str) -> bool {
self.unchecked.contains_key(name)
}
pub(super) fn charge(&mut self, name: &'static str, expected: bool, bytes: usize) -> bool {
if !self.is_segmented(name) {
return true;
}
if self.is_unchecked(name) {
return false;
}
if expected {
let r = self.rows.entry(name).or_default();
*r = r.saturating_add(1);
if *r > self.limits.rows {
self.unchecked
.insert(name, UncheckedReason::ExpectedRowsExceeded { rows: *r });
return false;
}
}
let next = self.total_bytes.saturating_add(entry_bytes(bytes));
if next > self.limits.bytes {
self.unchecked
.insert(name, UncheckedReason::BytesExceeded { bytes: next });
return false;
}
self.total_bytes = next;
true
}
pub(super) fn release(&mut self, bytes: u64) {
self.total_bytes = self.total_bytes.saturating_sub(bytes);
}
pub(super) fn unchecked(&self) -> &HashMap<&'static str, UncheckedReason> {
&self.unchecked
}
}