-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathencoding.rs
More file actions
139 lines (116 loc) · 3.49 KB
/
encoding.rs
File metadata and controls
139 lines (116 loc) · 3.49 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::any::Any;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use arcref::ArcRef;
use vortex_array::DeserializeMetadata;
use vortex_array::dtype::DType;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;
use vortex_session::registry::Id;
use vortex_session::registry::ReadContext;
use crate::IntoLayout;
use crate::LayoutChildren;
use crate::LayoutRef;
use crate::VTable;
use crate::segments::SegmentId;
/// A unique identifier for a layout encoding.
pub type LayoutEncodingId = Id;
pub type LayoutEncodingRef = ArcRef<dyn LayoutEncoding>;
pub trait LayoutEncoding: 'static + Send + Sync + Debug + private::Sealed {
fn as_any(&self) -> &dyn Any;
fn id(&self) -> LayoutEncodingId;
fn build(
&self,
dtype: &DType,
row_count: u64,
metadata: &[u8],
segment_ids: Vec<SegmentId>,
children: &dyn LayoutChildren,
ctx: &ReadContext,
) -> VortexResult<LayoutRef>;
}
#[repr(transparent)]
pub struct LayoutEncodingAdapter<V: VTable>(V::Encoding);
impl<V: VTable> LayoutEncoding for LayoutEncodingAdapter<V> {
fn as_any(&self) -> &dyn Any {
self
}
fn id(&self) -> LayoutEncodingId {
V::id(&self.0)
}
fn build(
&self,
dtype: &DType,
row_count: u64,
metadata: &[u8],
segment_ids: Vec<SegmentId>,
children: &dyn LayoutChildren,
ctx: &ReadContext,
) -> VortexResult<LayoutRef> {
let metadata = <V::Metadata as DeserializeMetadata>::deserialize(metadata)?;
let layout = V::build(
&self.0,
dtype,
row_count,
&metadata,
segment_ids,
children,
ctx,
)?;
// Validate that the builder function returned the expected values.
if layout.row_count() != row_count {
vortex_panic!(
"Layout row count mismatch: {} != {}",
layout.row_count(),
row_count
);
}
if layout.dtype() != dtype {
vortex_panic!("Layout dtype mismatch: {} != {}", layout.dtype(), dtype);
}
Ok(layout.into_layout())
}
}
impl<V: VTable> Debug for LayoutEncodingAdapter<V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LayoutEncoding")
.field("id", &self.id())
.finish()
}
}
impl Display for dyn LayoutEncoding + '_ {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.id())
}
}
impl PartialEq for dyn LayoutEncoding + '_ {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
}
}
impl Eq for dyn LayoutEncoding + '_ {}
impl dyn LayoutEncoding + '_ {
pub fn is<V: VTable>(&self) -> bool {
self.as_opt::<V>().is_some()
}
pub fn as_<V: VTable>(&self) -> &V::Encoding {
self.as_opt::<V>()
.vortex_expect("LayoutEncoding is not of the expected type")
}
pub fn as_opt<V: VTable>(&self) -> Option<&V::Encoding> {
self.as_any()
.downcast_ref::<LayoutEncodingAdapter<V>>()
.map(|e| &e.0)
}
}
mod private {
use super::*;
use crate::layouts::foreign::ForeignLayoutEncoding;
pub trait Sealed {}
impl<V: VTable> Sealed for LayoutEncodingAdapter<V> {}
impl Sealed for ForeignLayoutEncoding {}
}