1use std::fmt::Display;
5use std::sync::Arc;
6
7use itertools::Itertools;
8use parking_lot::RwLock;
9use vortex_error::{VortexExpect, VortexResult, vortex_err};
10use vortex_utils::aliases::hash_map::HashMap;
11
12use crate::EncodingRef;
13use crate::arrays::{
14 BoolEncoding, ChunkedEncoding, ConstantEncoding, DecimalEncoding, ExtensionEncoding,
15 FixedSizeListEncoding, ListEncoding, NullEncoding, PrimitiveEncoding, StructEncoding,
16 VarBinEncoding, VarBinViewEncoding,
17};
18
19pub type ArrayContext = VTableContext<EncodingRef>;
22pub type ArrayRegistry = VTableRegistry<EncodingRef>;
23
24impl ArrayRegistry {
25 pub fn canonical_only() -> Self {
26 let mut this = Self::empty();
27
28 this.register_many([
30 EncodingRef::new_ref(NullEncoding.as_ref()) as EncodingRef,
31 EncodingRef::new_ref(BoolEncoding.as_ref()),
32 EncodingRef::new_ref(PrimitiveEncoding.as_ref()),
33 EncodingRef::new_ref(DecimalEncoding.as_ref()),
34 EncodingRef::new_ref(StructEncoding.as_ref()),
35 EncodingRef::new_ref(ListEncoding.as_ref()),
36 EncodingRef::new_ref(FixedSizeListEncoding.as_ref()),
37 EncodingRef::new_ref(VarBinEncoding.as_ref()),
38 EncodingRef::new_ref(VarBinViewEncoding.as_ref()),
39 EncodingRef::new_ref(ExtensionEncoding.as_ref()),
40 ]);
41
42 this.register_many([
44 EncodingRef::new_ref(ConstantEncoding.as_ref()) as EncodingRef,
45 EncodingRef::new_ref(ChunkedEncoding.as_ref()),
46 ]);
47
48 this
49 }
50}
51
52#[derive(Debug, Clone)]
55pub struct VTableContext<T>(Arc<RwLock<Vec<T>>>);
56
57impl<T: Clone + Eq> VTableContext<T> {
58 pub fn empty() -> Self {
59 Self(Arc::new(RwLock::new(Vec::new())))
60 }
61
62 pub fn with(self, encoding: T) -> Self {
63 {
64 let mut write = self.0.write();
65 if write.iter().all(|e| e != &encoding) {
66 write.push(encoding);
67 }
68 }
69 self
70 }
71
72 pub fn with_many<E: IntoIterator<Item = T>>(self, items: E) -> Self {
73 items.into_iter().fold(self, |ctx, e| ctx.with(e))
74 }
75
76 pub fn encodings(&self) -> Vec<T> {
77 self.0.read().clone()
78 }
79
80 pub fn encoding_idx(&self, encoding: &T) -> u16 {
82 let mut write = self.0.write();
83 if let Some(idx) = write.iter().position(|e| e == encoding) {
84 return u16::try_from(idx).vortex_expect("Cannot have more than u16::MAX encodings");
85 }
86 assert!(
87 write.len() < u16::MAX as usize,
88 "Cannot have more than u16::MAX encodings"
89 );
90 write.push(encoding.clone());
91 u16::try_from(write.len() - 1).vortex_expect("checked already")
92 }
93
94 pub fn lookup_encoding(&self, idx: u16) -> Option<T> {
96 self.0.read().get(idx as usize).cloned()
97 }
98}
99
100#[derive(Clone, Debug)]
105pub struct VTableRegistry<T>(HashMap<String, T>);
106
107impl<T: Clone + Display + Eq> VTableRegistry<T> {
110 pub fn empty() -> Self {
111 Self(Default::default())
112 }
113
114 pub fn new_context<'a>(
116 &self,
117 encoding_ids: impl Iterator<Item = &'a str>,
118 ) -> VortexResult<VTableContext<T>> {
119 let mut ctx = VTableContext::<T>::empty();
120 for id in encoding_ids {
121 let encoding = self.0.get(id).ok_or_else(|| {
122 vortex_err!(
123 "Array encoding {} not found in registry {}",
124 id,
125 self.0.values().join(", ")
126 )
127 })?;
128 ctx = ctx.with(encoding.clone());
129 }
130 Ok(ctx)
131 }
132
133 pub fn vtables(&self) -> impl Iterator<Item = &T> + '_ {
135 self.0.values()
136 }
137
138 pub fn get(&self, id: &str) -> Option<&T> {
140 self.0.get(id)
141 }
142
143 pub fn register(&mut self, encoding: T) {
145 self.0.insert(encoding.to_string(), encoding);
146 }
147
148 pub fn register_many<I: IntoIterator<Item = T>>(&mut self, encodings: I) {
150 self.0
151 .extend(encodings.into_iter().map(|e| (e.to_string(), e)));
152 }
153}