-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathplugin.rs
More file actions
92 lines (81 loc) · 2.67 KB
/
plugin.rs
File metadata and controls
92 lines (81 loc) · 2.67 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::sync::Arc;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use crate::ArrayRef;
use crate::IntoArray;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::VTable;
use crate::buffer::BufferHandle;
use crate::dtype::DType;
use crate::serde::ArrayChildren;
/// Reference-counted array plugin.
pub type ArrayPluginRef = Arc<dyn ArrayPlugin>;
/// Registry trait for ID-based deserialization of arrays.
///
/// Plugins are registered in the session by their [`ArrayId`]. When a serialized array is
/// encountered, the session resolves the ID to the plugin and calls [`deserialize`] to reconstruct
/// the value as an [`ArrayRef`].
///
/// [`deserialize`]: ArrayPlugin::deserialize
pub trait ArrayPlugin: 'static + Send + Sync {
/// Returns the ID for this array encoding.
fn id(&self) -> ArrayId;
/// Serialize the array metadata.
///
/// This function will only be called for arrays where the encoding ID matches that of this
/// plugin.
fn serialize(&self, array: &ArrayRef, session: &VortexSession)
-> VortexResult<Option<Vec<u8>>>;
/// Deserialize an array from serialized components.
///
/// The returned array doesn't necessary have to match this plugin's encoding ID. This is
/// useful for implementing back-compat logic and deserializing arrays into the new version.
fn deserialize(
&self,
dtype: &DType,
len: usize,
metadata: &[u8],
buffers: &[BufferHandle],
children: &dyn ArrayChildren,
session: &VortexSession,
) -> VortexResult<ArrayRef>;
}
impl std::fmt::Debug for dyn ArrayPlugin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ArrayPlugin").field(&self.id()).finish()
}
}
impl<V: VTable> ArrayPlugin for V {
fn id(&self) -> ArrayId {
VTable::id(self)
}
fn serialize(
&self,
array: &ArrayRef,
session: &VortexSession,
) -> VortexResult<Option<Vec<u8>>> {
assert_eq!(
self.id(),
array.encoding_id(),
"Invoked for incorrect array ID"
);
V::serialize(array.as_::<V>(), session)
}
fn deserialize(
&self,
dtype: &DType,
len: usize,
metadata: &[u8],
buffers: &[BufferHandle],
children: &dyn ArrayChildren,
session: &VortexSession,
) -> VortexResult<ArrayRef> {
Ok(Array::<V>::try_from_parts(V::deserialize(
self, dtype, len, metadata, buffers, children, session,
)?)?
.into_array())
}
}