vortex_layout/
encoding.rs1use std::any::Any;
5use std::fmt::Debug;
6use std::fmt::Display;
7use std::fmt::Formatter;
8
9use arcref::ArcRef;
10use vortex_array::DeserializeMetadata;
11use vortex_array::dtype::DType;
12use vortex_error::VortexExpect;
13use vortex_error::VortexResult;
14use vortex_session::VortexSession;
15use vortex_session::registry::ReadContext;
16
17use crate::LayoutId;
18use crate::LayoutRef;
19use crate::VTable;
20use crate::children::LayoutChildren;
21use crate::segments::SegmentId;
22
23pub type LayoutEncodingId = LayoutId;
25pub type LayoutVTableRef = ArcRef<dyn LayoutVTablePlugin>;
27pub type LayoutEncodingRef = LayoutVTableRef;
29
30pub struct LayoutDeserializeArgs<'a> {
32 pub session: &'a VortexSession,
34 pub array_read_ctx: &'a ReadContext,
36 pub dtype: &'a DType,
38 pub row_count: u64,
40 pub segment_ids: Vec<SegmentId>,
42 pub children: &'a dyn LayoutChildren,
44}
45
46pub struct LayoutBuildContext<'a> {
48 pub session: &'a VortexSession,
50 pub array_read_ctx: &'a ReadContext,
52}
53
54pub trait LayoutVTablePlugin: 'static + Send + Sync + Debug {
56 fn as_any(&self) -> &dyn Any;
58 fn id(&self) -> LayoutEncodingId;
60 fn build(
62 &self,
63 dtype: &DType,
64 row_count: u64,
65 metadata: &[u8],
66 segment_ids: Vec<SegmentId>,
67 children: &dyn LayoutChildren,
68 build_ctx: &LayoutBuildContext<'_>,
69 ) -> VortexResult<LayoutRef>;
70}
71
72pub use LayoutVTablePlugin as LayoutEncoding;
74
75impl<V: VTable> LayoutVTablePlugin for V {
76 fn as_any(&self) -> &dyn Any {
77 self
78 }
79
80 fn id(&self) -> LayoutEncodingId {
81 VTable::id(self)
82 }
83
84 fn build(
85 &self,
86 dtype: &DType,
87 row_count: u64,
88 metadata: &[u8],
89 segment_ids: Vec<SegmentId>,
90 children: &dyn LayoutChildren,
91 build_ctx: &LayoutBuildContext<'_>,
92 ) -> VortexResult<LayoutRef> {
93 let metadata = <V::Metadata as DeserializeMetadata>::deserialize(metadata)?;
94 Ok(V::build(
95 self,
96 dtype,
97 row_count,
98 &metadata,
99 segment_ids,
100 children,
101 build_ctx,
102 )?
103 .into_layout())
104 }
105}
106
107impl Display for dyn LayoutVTablePlugin + '_ {
108 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
109 write!(f, "{}", self.id())
110 }
111}
112
113impl PartialEq for dyn LayoutVTablePlugin + '_ {
114 fn eq(&self, other: &Self) -> bool {
115 self.id() == other.id()
116 }
117}
118
119impl Eq for dyn LayoutVTablePlugin + '_ {}
120
121impl dyn LayoutVTablePlugin + '_ {
122 pub fn is<V: VTable>(&self) -> bool {
124 self.as_opt::<V>().is_some()
125 }
126
127 pub fn as_<V: VTable>(&self) -> &V {
129 self.as_opt::<V>()
130 .vortex_expect("layout encoding type mismatch")
131 }
132
133 pub fn as_opt<V: VTable>(&self) -> Option<&V> {
135 self.as_any().downcast_ref()
136 }
137}