use std::sync::Arc;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use vortex_array::expr::BoundExpression;
use vortex_array::expr::ExactBoundExpr;
use super::io::SegmentRanges;
use crate::plan::physical::PlanRef;
use crate::plan::physical::pipeline::ValueCache;
#[derive(Clone, Default)]
pub struct ScanCache(Arc<Inner>);
#[derive(Default)]
struct Inner {
root: Mutex<Option<PlanRef>>,
projections: Mutex<FxHashMap<ExactBoundExpr, PlanRef>>,
ranges: Mutex<Option<SegmentRanges>>,
values: ValueCache,
}
impl ScanCache {
pub(super) fn root(&self) -> Option<PlanRef> {
self.0.root.lock().clone()
}
pub(super) fn insert_root(&self, root: PlanRef) -> PlanRef {
self.0.root.lock().get_or_insert(root).clone()
}
pub(super) fn projection(&self, projection: &BoundExpression) -> Option<PlanRef> {
self.0
.projections
.lock()
.get(&ExactBoundExpr(projection.clone()))
.cloned()
}
pub(super) fn insert_projection(&self, projection: BoundExpression, plan: PlanRef) -> PlanRef {
self.0
.projections
.lock()
.entry(ExactBoundExpr(projection))
.or_insert(plan)
.clone()
}
pub(super) fn ranges(&self) -> Option<SegmentRanges> {
self.0.ranges.lock().clone()
}
pub(super) fn insert_ranges(&self, ranges: SegmentRanges) -> SegmentRanges {
Arc::clone(self.0.ranges.lock().get_or_insert(ranges))
}
pub(super) fn values(&self) -> ValueCache {
self.0.values.clone()
}
}