use super::{cursor::TermCursor, phrase::AnyCursor};
pub(super) fn term_cursor_bytes(cursors: &[TermCursor]) -> u64 {
cursors.iter().map(|c| c.bytes.len() as u64).sum()
}
pub(super) fn atom_cursor_bytes(atoms: &[AnyCursor]) -> u64 {
atoms
.iter()
.map(|a| match a {
AnyCursor::Term(c) => c.bytes.len() as u64,
AnyCursor::Phrase(p) => p
.members
.iter()
.map(|m| m.cursor.bytes.len() as u64 + m.positions.len() as u64)
.sum(),
})
.sum()
}
pub(super) fn term_cursor_ranges(cursors: &[TermCursor]) -> u64 {
cursors
.iter()
.filter(|c| !c.bytes.is_empty())
.map(|c| 1 + u64::from(c.header_probed))
.sum()
}
pub(super) fn atom_planned_ranges(atoms: &[AnyCursor]) -> u64 {
let term_ranges = |c: &TermCursor| {
if c.bytes.is_empty() {
0
} else {
1 + u64::from(c.header_probed)
}
};
atoms
.iter()
.map(|a| match a {
AnyCursor::Term(c) => term_ranges(c),
AnyCursor::Phrase(p) => p
.members
.iter()
.map(|m| term_ranges(&m.cursor) + u64::from(!m.positions.is_empty()))
.sum(),
})
.sum()
}
#[derive(Debug, Default, Clone, Copy)]
pub struct MatchWork {
pub postings_bytes: u64,
pub planned_ranges: u64,
pub kernel_cpu_ns: u64,
}
impl MatchWork {
pub(super) fn for_cursors(cursors: &[TermCursor]) -> Self {
Self {
postings_bytes: term_cursor_bytes(cursors),
planned_ranges: term_cursor_ranges(cursors),
kernel_cpu_ns: 0,
}
}
pub(super) fn for_atoms(atoms: &[AnyCursor]) -> Self {
Self {
postings_bytes: atom_cursor_bytes(atoms),
planned_ranges: atom_planned_ranges(atoms),
kernel_cpu_ns: 0,
}
}
pub fn merge(&mut self, other: MatchWork) {
self.postings_bytes += other.postings_bytes;
self.planned_ranges += other.planned_ranges;
self.kernel_cpu_ns += other.kernel_cpu_ns;
}
}