vortex_session/
registry.rs1use std::fmt::Debug;
8use std::ops::Deref;
9use std::sync::Arc;
10
11use arcref::ArcRef;
12use parking_lot::Mutex;
13use vortex_error::VortexExpect;
14use vortex_utils::aliases::dash_map::DashMap;
15
16pub type Id = ArcRef<str>;
18
19#[derive(Clone, Debug)]
21pub struct Registry<T>(Arc<DashMap<Id, T>>);
22
23impl<T> Default for Registry<T> {
24 fn default() -> Self {
25 Self(Default::default())
26 }
27}
28
29impl<T: Clone> Registry<T> {
30 pub fn empty() -> Self {
31 Self(Default::default())
32 }
33
34 pub fn ids(&self) -> impl Iterator<Item = Id> + '_ {
36 self.0.iter().map(|i| i.key().clone())
37 }
38
39 pub fn items(&self) -> impl Iterator<Item = T> + '_ {
41 self.0.iter().map(|i| i.value().clone())
42 }
43
44 pub fn find_many<'a>(
46 &self,
47 ids: impl IntoIterator<Item = &'a Id>,
48 ) -> impl Iterator<Item = Option<impl Deref<Target = T>>> {
49 ids.into_iter().map(|id| self.0.get(id))
50 }
51
52 pub fn find(&self, id: &Id) -> Option<T> {
54 self.0.get(id).as_deref().cloned()
55 }
56
57 pub fn register(&self, id: impl Into<Id>, item: impl Into<T>) {
59 self.0.insert(id.into(), item.into());
60 }
61
62 pub fn with(self, id: impl Into<Id>, item: impl Into<T>) -> Self {
64 self.register(id, item.into());
65 self
66 }
67}
68
69#[derive(Clone, Debug)]
79pub struct Context<T> {
80 ids: Arc<Mutex<Vec<Id>>>,
84 registry: Option<Registry<T>>,
86}
87
88impl<T> Default for Context<T> {
89 fn default() -> Self {
90 Self {
91 ids: Arc::new(Mutex::new(Vec::new())),
92 registry: None,
93 }
94 }
95}
96
97impl<T: Clone> Context<T> {
98 pub fn new(ids: Vec<Id>) -> Self {
100 Self {
101 ids: Arc::new(Mutex::new(ids)),
102 registry: None,
103 }
104 }
105
106 pub fn empty() -> Self {
108 Self::default()
109 }
110
111 pub fn with_registry(mut self, registry: Registry<T>) -> Self {
113 self.registry = Some(registry);
114 self
115 }
116
117 pub fn intern(&self, id: &Id) -> Option<u16> {
119 if let Some(registry) = &self.registry
120 && registry.find(id).is_none()
121 {
122 return None;
124 }
125
126 let mut ids = self.ids.lock();
127 if let Some(idx) = ids.iter().position(|e| e == id) {
128 return Some(u16::try_from(idx).vortex_expect("Cannot have more than u16::MAX items"));
129 }
130
131 let idx = ids.len();
132 assert!(
133 idx < u16::MAX as usize,
134 "Cannot have more than u16::MAX items"
135 );
136 ids.push(id.clone());
137 Some(u16::try_from(idx).vortex_expect("checked already"))
138 }
139
140 pub fn resolve(&self, idx: u16) -> Option<Id> {
142 self.ids.lock().get(idx as usize).cloned()
143 }
144
145 pub fn to_ids(&self) -> Vec<Id> {
147 self.ids.lock().clone()
148 }
149}