use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use super::ContentMeta;
pub const BUDGET_DEFAULT: usize = 512 << 20;
pub const BUDGET_MIN: usize = 1 << 20;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Kind {
Section(String),
Nums(String),
Rids(usize),
ContentRids(usize),
ContentMeta,
Dict(usize),
BinaryDict(usize),
Ids,
}
#[derive(Clone)]
pub enum Held {
Bytes(Arc<Vec<u8>>),
Nums(Arc<Vec<u64>>),
Rids(Arc<Vec<u32>>),
Strings(Arc<Vec<String>>),
ByteStrings(Arc<Vec<Vec<u8>>>),
ContentMeta(Arc<Vec<ContentMeta>>),
}
impl Held {
fn weight(&self) -> usize {
match self {
Held::Bytes(v) => v.len(),
Held::Nums(v) => v.len() * 8,
Held::Rids(v) => v.len() * 4,
Held::Strings(v) => v.iter().map(|s| s.len() + std::mem::size_of::<String>()).sum(),
Held::ByteStrings(v) => {
v.iter().map(|s| s.len() + std::mem::size_of::<Vec<u8>>()).sum()
}
Held::ContentMeta(v) => {
v.iter().map(|m| m.name.len() + std::mem::size_of::<ContentMeta>()).sum()
}
}
}
}
struct Inner {
bytes: usize,
clock: u64,
map: HashMap<(u64, Kind), (u64, Held)>,
}
pub struct SectionCache {
budget: usize,
inner: Mutex<Inner>,
}
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
pub fn next_part_id() -> u64 {
NEXT_ID.fetch_add(1, Ordering::Relaxed)
}
impl SectionCache {
pub fn new(budget: usize) -> SectionCache {
SectionCache {
budget: budget.max(BUDGET_MIN),
inner: Mutex::new(Inner { bytes: 0, clock: 0, map: HashMap::new() }),
}
}
pub fn shared() -> Arc<SectionCache> {
Arc::new(SectionCache::new(BUDGET_DEFAULT))
}
pub fn global() -> Arc<SectionCache> {
static G: std::sync::OnceLock<Arc<SectionCache>> = std::sync::OnceLock::new();
G.get_or_init(|| Arc::new(SectionCache::new(BUDGET_DEFAULT))).clone()
}
pub fn get(&self, part: u64, k: &Kind) -> Option<Held> {
let mut g = self.inner.lock().unwrap();
g.clock += 1;
let c = g.clock;
let e = g.map.get_mut(&(part, k.clone()))?;
e.0 = c;
Some(e.1.clone())
}
pub fn put(&self, part: u64, k: Kind, v: Held) -> Held {
let w = v.weight();
let mut g = self.inner.lock().unwrap();
while g.bytes + w > self.budget && !g.map.is_empty() {
let victim = g.map.iter().min_by_key(|(_, (t, _))| *t).map(|(key, _)| key.clone());
let Some(victim) = victim else { break };
if let Some((_, gone)) = g.map.remove(&victim) {
g.bytes -= gone.weight();
}
}
g.clock += 1;
let c = g.clock;
g.bytes += w;
if let Some((_, old)) = g.map.insert((part, k), (c, v.clone())) {
g.bytes -= old.weight();
}
v
}
pub fn forget(&self, part: u64) {
let mut g = self.inner.lock().unwrap();
let keys: Vec<(u64, Kind)> = g.map.keys().filter(|(p, _)| *p == part).cloned().collect();
for k in keys {
if let Some((_, gone)) = g.map.remove(&k) {
g.bytes -= gone.weight();
}
}
}
pub fn bytes(&self) -> usize {
self.inner.lock().unwrap().bytes
}
pub fn budget(&self) -> usize {
self.budget
}
pub fn entries(&self) -> usize {
self.inner.lock().unwrap().map.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bytes(n: usize) -> Held {
Held::Bytes(Arc::new(vec![0u8; n]))
}
#[test]
fn stays_inside_its_budget() {
let c = SectionCache::new(4 << 20);
for i in 0..64 {
c.put(1, Kind::Section(format!("s{i}")), bytes(1 << 20));
assert!(c.bytes() <= 4 << 20, "budget exceeded at insert {i}: {}", c.bytes());
}
}
#[test]
fn an_oversized_entry_is_still_admitted() {
let c = SectionCache::new(1 << 20);
c.put(1, Kind::Section("big".into()), bytes(8 << 20));
assert!(c.get(1, &Kind::Section("big".into())).is_some());
}
#[test]
fn re_inserting_a_key_does_not_leak_budget() {
let c = SectionCache::new(64 << 20);
for _ in 0..50 {
c.put(7, Kind::Rids(3), Held::Rids(Arc::new(vec![0u32; 1 << 18])));
}
assert_eq!(c.entries(), 1);
assert_eq!(c.bytes(), (1 << 18) * 4, "a displaced value must stop being counted");
}
#[test]
fn forgetting_a_part_releases_exactly_its_own_bytes() {
let c = SectionCache::new(64 << 20);
c.put(1, Kind::Section("a".into()), bytes(1 << 20));
c.put(2, Kind::Section("a".into()), bytes(1 << 20));
let both = c.bytes();
c.forget(1);
assert_eq!(c.bytes(), both / 2);
assert!(c.get(1, &Kind::Section("a".into())).is_none());
assert!(c.get(2, &Kind::Section("a".into())).is_some(), "the other part is untouched");
}
#[test]
fn eviction_never_invalidates_a_live_handle() {
let c = SectionCache::new(2 << 20);
let held = c.put(1, Kind::Section("keep".into()), bytes(1 << 20));
for i in 0..16 {
c.put(1, Kind::Section(format!("push{i}")), bytes(1 << 20));
}
assert!(c.get(1, &Kind::Section("keep".into())).is_none(), "it should have been evicted");
match held {
Held::Bytes(v) => assert_eq!(v.len(), 1 << 20),
_ => panic!("wrong kind"),
}
}
#[test]
fn part_ids_are_unique() {
let a = next_part_id();
let b = next_part_id();
assert_ne!(a, b);
}
}