use super::*;
use crate::cache::SegmentBytes;
use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;
const SCOPE_SLOTS: usize = 4;
const SCOPE_CAPACITY_DIVISOR: u64 = 4;
pub(super) struct PointSegment {
pub(super) descriptor: CanonicalSegmentDescriptor,
pub(super) bytes: SegmentBytes,
pub(super) directory: Option<SegmentBytes>,
}
impl PointSegment {
fn contains(&self, kind: CanonicalSegmentKind, id: u64) -> bool {
self.descriptor.kind == kind
&& self.descriptor.min_record_id <= id
&& id <= self.descriptor.max_record_id
}
fn retained_bytes(&self) -> u64 {
let directory = self
.directory
.as_ref()
.map_or(0, |directory| directory.len());
(self.bytes.len() as u64).saturating_add(directory as u64)
}
}
struct ScopeSlot {
reader: u64,
segment: Rc<PointSegment>,
}
struct ScopeState {
depth: usize,
slots: Vec<ScopeSlot>,
}
thread_local! {
static SCOPE: RefCell<ScopeState> = const {
RefCell::new(ScopeState {
depth: 0,
slots: Vec::new(),
})
};
static DESCRIPTOR_SEEKS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
#[must_use = "point reads reuse segments only while the scope is open"]
pub struct CanonicalPointReadScope {
_thread_bound: PhantomData<*const ()>,
}
impl CanonicalPointReadScope {
pub fn enter() -> Self {
let _ = SCOPE.try_with(|scope| {
let mut scope = scope.borrow_mut();
scope.depth = scope.depth.saturating_add(1);
});
Self {
_thread_bound: PhantomData,
}
}
}
impl Drop for CanonicalPointReadScope {
fn drop(&mut self) {
let released = SCOPE
.try_with(|scope| {
let mut scope = scope.borrow_mut();
scope.depth = scope.depth.saturating_sub(1);
if scope.depth == 0 {
std::mem::take(&mut scope.slots)
} else {
Vec::new()
}
})
.unwrap_or_default();
drop(released);
}
}
#[doc(hidden)]
pub fn point_descriptor_seeks_for_test() -> u64 {
DESCRIPTOR_SEEKS.with(std::cell::Cell::get)
}
pub(super) fn record_descriptor_seek() {
DESCRIPTOR_SEEKS.with(|seeks| seeks.set(seeks.get().saturating_add(1)));
}
pub(super) fn retained_segment(
reader: u64,
kind: CanonicalSegmentKind,
id: u64,
) -> Option<Rc<PointSegment>> {
SCOPE
.try_with(|scope| {
let mut scope = scope.borrow_mut();
if scope.depth == 0 {
return None;
}
let index = scope
.slots
.iter()
.rposition(|slot| slot.reader == reader && slot.segment.contains(kind, id))?;
let last = scope.slots.len() - 1;
if index != last {
scope.slots[index..].rotate_left(1);
}
Some(Rc::clone(&scope.slots[last].segment))
})
.ok()
.flatten()
}
pub(super) fn retain_segment(reader: u64, segment: &Rc<PointSegment>, cache_capacity_bytes: u64) {
if !segment.bytes.is_cache_resident()
|| segment
.directory
.as_ref()
.is_some_and(|directory| !directory.is_cache_resident())
{
return;
}
let budget = cache_capacity_bytes / SCOPE_CAPACITY_DIVISOR;
let evicted = SCOPE
.try_with(|scope| {
let mut scope = scope.borrow_mut();
if scope.depth == 0 || segment.retained_bytes() > budget {
return Vec::new();
}
let mut evicted = Vec::new();
if let Some(index) = scope.slots.iter().position(|slot| {
slot.reader == reader
&& slot.segment.descriptor.segment_id == segment.descriptor.segment_id
}) {
evicted.push(scope.slots.remove(index));
}
let mut retained = scope
.slots
.iter()
.map(|slot| slot.segment.retained_bytes())
.fold(segment.retained_bytes(), u64::saturating_add);
while !scope.slots.is_empty() && (scope.slots.len() >= SCOPE_SLOTS || retained > budget)
{
let slot = scope.slots.remove(0);
retained = retained.saturating_sub(slot.segment.retained_bytes());
evicted.push(slot);
}
scope.slots.push(ScopeSlot {
reader,
segment: Rc::clone(segment),
});
evicted
})
.unwrap_or_default();
drop(evicted);
}
#[cfg(test)]
pub(super) fn retained_segments() -> usize {
SCOPE.with(|scope| scope.borrow().slots.len())
}