use std::collections::BinaryHeap;
use std::path::Path;
#[derive(Debug, Default)]
pub struct TierManager {
pub hot_rows: Vec<u64>,
pub ram_budget_bytes: u64,
}
impl TierManager {
pub fn from_counts_dump(
path: &Path,
budget_bytes: u64,
row_bytes: u64,
) -> std::io::Result<Self> {
let text = std::fs::read_to_string(path)?;
let mut heap: BinaryHeap<(u64, u64)> = BinaryHeap::new();
for line in text.lines() {
let mut it = line.split_whitespace();
let (rowid, count) = match (it.next(), it.next()) {
(Some(a), Some(b)) => (a.parse().unwrap_or(0), b.parse().unwrap_or(0)),
_ => (0, 0),
};
if rowid == 0 {
continue;
}
heap.push((count, rowid));
}
let max_rows = (budget_bytes / row_bytes.max(1)) as usize;
let mut hot_rows = Vec::with_capacity(max_rows.min(heap.len()));
while hot_rows.len() < max_rows {
match heap.pop() {
Some((_, rowid)) => hot_rows.push(rowid),
None => break,
}
}
hot_rows.sort_unstable();
Ok(Self {
hot_rows,
ram_budget_bytes: budget_bytes,
})
}
pub fn is_hot(&self, rowid: u64) -> bool {
self.hot_rows.binary_search(&rowid).is_ok()
}
}