-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathmod.rs
More file actions
152 lines (128 loc) · 3.96 KB
/
Copy pathmod.rs
File metadata and controls
152 lines (128 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::any::Any;
use std::sync::Arc;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_session::ArcSwapMap;
use vortex_session::SessionExt;
use vortex_session::SessionGuard;
use vortex_session::SessionVar;
use vortex_session::registry::Id;
use crate::ArrayRef;
use crate::array::ArrayPlugin;
use crate::array::ArrayPluginRef;
use crate::arrays::Bool;
use crate::arrays::Chunked;
use crate::arrays::Constant;
use crate::arrays::Decimal;
use crate::arrays::Dict;
use crate::arrays::Extension;
use crate::arrays::FixedSizeList;
use crate::arrays::List;
use crate::arrays::ListView;
use crate::arrays::Map;
use crate::arrays::Masked;
use crate::arrays::Null;
use crate::arrays::PiecewiseSequence;
use crate::arrays::Primitive;
use crate::arrays::Struct;
use crate::arrays::Union;
use crate::arrays::VarBin;
use crate::arrays::VarBinView;
use crate::arrays::Variant;
/// Registry of array encodings.
pub type ArrayRegistry = ArcSwapMap<Id, ArrayPluginRef>;
#[derive(Clone, Debug)]
pub struct ArraySession {
/// The set of registered array encodings.
registry: ArrayRegistry,
}
impl ArraySession {
pub fn empty() -> ArraySession {
Self {
registry: ArrayRegistry::default(),
}
}
pub fn registry(&self) -> &ArrayRegistry {
&self.registry
}
/// Register a new array encoding, replacing any existing encoding with the same ID.
pub fn register<P: ArrayPlugin>(&self, plugin: P) {
self.registry
.insert(plugin.id(), Arc::new(plugin) as ArrayPluginRef);
}
}
impl Default for ArraySession {
fn default() -> Self {
let this = ArraySession {
registry: ArrayRegistry::default(),
};
// Register the canonical encodings.
this.register(Null);
this.register(Bool);
this.register(Primitive);
this.register(Decimal);
this.register(VarBinView);
this.register(ListView);
this.register(Map);
this.register(FixedSizeList);
this.register(Struct);
this.register(Union);
this.register(Variant);
this.register(Extension);
// Register the utility encodings.
this.register(Chunked);
this.register(Constant);
this.register(Dict);
this.register(List);
this.register(Masked);
this.register(PiecewiseSequence);
this.register(VarBin);
this
}
}
impl SessionVar for ArraySession {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
/// Session data for Vortex arrays.
pub trait ArraySessionExt: SessionExt {
/// Returns the array encoding registry.
fn arrays(&self) -> SessionGuard<'_, ArraySession> {
self.get::<ArraySession>()
}
/// Serialize an array using a plugin from the registry.
fn array_serialize(&self, array: &ArrayRef) -> VortexResult<Option<Vec<u8>>> {
let Some(plugin) = self.arrays().registry.get(&array.encoding_id()) else {
vortex_bail!(
"Array {} is not registered for serializations",
array.encoding_id()
);
};
plugin.serialize(array, &self.session())
}
}
impl<S: SessionExt> ArraySessionExt for S {}
#[cfg(test)]
mod tests {
use vortex_session::VortexSession;
use crate::ArrayVTable;
use crate::arrays::Bool;
use crate::session::ArraySession;
use crate::session::ArraySessionExt;
#[test]
fn array_session_default_registers_encodings() {
let session = VortexSession::empty().with::<ArraySession>();
assert!(session.arrays().registry().contains_key(&Bool.id()));
}
#[test]
fn empty_array_session_registers_no_encodings() {
let session = VortexSession::empty().with_some(ArraySession::empty());
assert!(!session.arrays().registry().contains_key(&Bool.id()));
}
}