Skip to main content

vortex_layout/plan/physical/pipeline/
values.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Whole values, such as dictionary values, kept between the scans that share them.
5
6use std::sync::Arc;
7
8use parking_lot::Mutex;
9use rustc_hash::FxHashMap;
10use vortex_array::ArrayRef;
11
12use crate::plan::physical::PlanByPtr;
13use crate::plan::physical::PlanRef;
14
15/// Values a plan produces over its whole domain whatever rows it is compiled for, kept for the
16/// later scans that share this cache.
17///
18/// A dictionary's values are read whole by every reader of its codes, so readers sharing this
19/// cache can reuse them across filters, projections, and splits. The caller controls its lifetime;
20/// only whole values that a reader requested are retained.
21#[derive(Clone, Default)]
22pub struct ValueCache(Arc<Mutex<FxHashMap<PlanByPtr, ArrayRef>>>);
23
24impl ValueCache {
25    /// The values `plan` produced earlier.
26    pub(crate) fn get(&self, plan: &PlanRef) -> Option<ArrayRef> {
27        self.0.lock().get(&PlanByPtr(plan.clone())).cloned()
28    }
29
30    /// Records the values of `plan`, keeping the first when two race.
31    pub(crate) fn insert(&self, plan: &PlanRef, values: ArrayRef) -> ArrayRef {
32        self.0
33            .lock()
34            .entry(PlanByPtr(plan.clone()))
35            .or_insert(values)
36            .clone()
37    }
38}