Skip to main content

vortex_layout/layouts/indexed/
session.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Session registry of index kinds.
5
6use std::any::Any;
7
8use vortex_session::ArcSwapMap;
9use vortex_session::SessionExt;
10use vortex_session::SessionGuard;
11use vortex_session::SessionVar;
12use vortex_session::registry::Id;
13
14use crate::layouts::indexed::index::IndexId;
15use crate::layouts::indexed::index::IndexVTableRef;
16
17/// Registry of index kinds, keyed by their stable [`IndexId`].
18type IndexRegistry = ArcSwapMap<Id, IndexVTableRef>;
19
20/// Session state holding the registered index kinds.
21///
22/// Empty by default: index kinds live in their own crates and are registered explicitly, the same
23/// way layout encodings are. A kind that is not registered is simply ignored on read — its child is
24/// dropped from consideration and the data child answers the query directly.
25#[derive(Clone, Debug, Default)]
26pub struct IndexSession {
27    registry: IndexRegistry,
28}
29
30impl IndexSession {
31    /// Register an index kind, replacing any existing kind with the same id.
32    pub fn register(&self, index: IndexVTableRef) {
33        self.registry.insert(index.id(), index);
34    }
35
36    /// Find a registered index kind by id.
37    pub fn find(&self, id: &IndexId) -> Option<IndexVTableRef> {
38        self.registry.get(id)
39    }
40
41    /// The underlying registry.
42    pub fn registry(&self) -> &IndexRegistry {
43        &self.registry
44    }
45}
46
47impl SessionVar for IndexSession {
48    fn as_any(&self) -> &dyn Any {
49        self
50    }
51
52    fn as_any_mut(&mut self) -> &mut dyn Any {
53        self
54    }
55}
56
57/// Extension trait for reaching the index-kind registry from a session.
58pub trait IndexSessionExt: SessionExt {
59    /// Returns the index-kind registry.
60    fn indexes(&self) -> SessionGuard<'_, IndexSession> {
61        self.get::<IndexSession>()
62    }
63}
64
65impl<S: SessionExt> IndexSessionExt for S {}