use relate_core::tokens::{peak_families, peak_hold, WorkspaceCharge};
use relate_core::Result;
use crate::overlay::{ExecGraph, RetainedTotal};
use crate::row::Row;
use crate::workspace::RetainedRows;
pub(super) struct GroupBill {
retained: RetainedTotal,
workspace: WorkspaceCharge,
}
pub(super) struct InputBill {
held: Option<(RetainedRows, WorkspaceCharge)>,
}
impl InputBill {
pub(super) fn open(rows: &[Row]) -> Result<Self> {
if !relate_core::tokens::workspace_accounting() {
return Ok(Self { held: None });
}
let retained = RetainedRows::measure(rows);
let charge = WorkspaceCharge::retain(retained.bytes())?;
peak_hold(peak_families::AGGREGATION);
Ok(Self {
held: Some((retained, charge)),
})
}
pub(super) fn release(&mut self, row: &Row) -> Result<()> {
let Some((retained, charge)) = &mut self.held else {
return Ok(());
};
retained.release_row(row);
charge.resize(retained.bytes())
}
}
impl GroupBill {
pub(super) fn open(graph: &ExecGraph<'_>) -> Result<Self> {
Ok(Self {
retained: graph.retain_bytes(0)?,
workspace: WorkspaceCharge::empty(),
})
}
pub(super) fn add(&mut self, graph: &ExecGraph<'_>, bytes: u64) -> Result<()> {
self.retained.add_bytes(graph, bytes)?;
let total = self.workspace.bytes().saturating_add(bytes);
self.workspace.resize_with(|| total)?;
peak_hold(peak_families::AGGREGATION);
Ok(())
}
#[cfg(test)]
pub(super) fn total(&self) -> u64 {
self.retained.total()
}
}