use std::rc::Rc;
use crate::metrics::{InTxBatchEnd, InTxStats};
use crate::overlay::ExecGraph;
use crate::row::Row;
pub(super) struct BatchProfile<'a> {
graph: &'a ExecGraph<'a>,
marks: Option<BatchMarks>,
}
struct BatchMarks {
stats: Rc<InTxStats>,
scratch: u64,
wal: u64,
}
#[derive(Clone, Copy, Default)]
pub(super) struct BatchPeaks {
scratch: u64,
wal: u64,
}
impl BatchMarks {
fn scratch_delta(&self, graph: &ExecGraph<'_>) -> u64 {
graph.scratch_created_bytes().saturating_sub(self.scratch)
}
fn wal_delta(&self, graph: &ExecGraph<'_>) -> u64 {
graph.redo_reserved_bytes().saturating_sub(self.wal)
}
}
impl<'a> BatchProfile<'a> {
pub(super) fn open(cell: Option<&Rc<InTxStats>>, graph: &'a ExecGraph<'a>) -> Self {
let marks = cell.map(|stats| BatchMarks {
stats: Rc::clone(stats),
scratch: graph.scratch_created_bytes(),
wal: graph.redo_reserved_bytes(),
});
Self { graph, marks }
}
pub(super) fn read_peaks(&self) -> BatchPeaks {
match &self.marks {
Some(marks) => BatchPeaks {
scratch: marks.scratch_delta(self.graph),
wal: marks.wal_delta(self.graph),
},
None => BatchPeaks::default(),
}
}
pub(super) fn committed(&self, peaks: BatchPeaks, input: &[Row], output: &[Row]) {
if let Some(marks) = &self.marks {
marks.stats.note_batch(InTxBatchEnd::Committed {
input_bytes: crate::workspace::retained_rows_of(input),
output_bytes: crate::workspace::retained_rows_of(output),
scratch_bytes: peaks.scratch,
wal_bytes: peaks.wal,
});
}
}
pub(super) fn rolled_back(&self, input: &[Row]) {
if let Some(marks) = &self.marks {
marks.stats.note_batch(InTxBatchEnd::RolledBack {
input_bytes: crate::workspace::retained_rows_of(input),
scratch_bytes: marks.scratch_delta(self.graph),
});
}
}
}