-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathpostscript.rs
More file actions
129 lines (116 loc) · 3.95 KB
/
Copy pathpostscript.rs
File metadata and controls
129 lines (116 loc) · 3.95 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use flatbuffers::FlatBufferBuilder;
use flatbuffers::Follow;
use flatbuffers::WIPOffset;
use vortex_buffer::Alignment;
use vortex_error::VortexError;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use vortex_flatbuffers::FlatBufferRoot;
use vortex_flatbuffers::ReadFlatBuffer;
use vortex_flatbuffers::WriteFlatBuffer;
use vortex_flatbuffers::footer as fb;
/// The postscript captures the locations and compression for the initial segments required for
/// reading a Vortex file.
pub(crate) struct Postscript {
pub(crate) dtype: Option<PostscriptSegment>,
pub(crate) layout: PostscriptSegment,
pub(crate) statistics: Option<PostscriptSegment>,
pub(crate) footer: PostscriptSegment,
}
impl FlatBufferRoot for Postscript {}
impl WriteFlatBuffer for Postscript {
type Target<'a> = fb::Postscript<'a>;
fn write_flatbuffer<'fb>(
&self,
fbb: &mut FlatBufferBuilder<'fb>,
) -> VortexResult<WIPOffset<Self::Target<'fb>>> {
let dtype = self
.dtype
.as_ref()
.map(|ps| ps.write_flatbuffer(fbb))
.transpose()?;
let layout = self.layout.write_flatbuffer(fbb)?;
let statistics = self
.statistics
.as_ref()
.map(|ps| ps.write_flatbuffer(fbb))
.transpose()?;
let footer = self.footer.write_flatbuffer(fbb)?;
Ok(fb::Postscript::create(
fbb,
&fb::PostscriptArgs {
dtype,
layout: Some(layout),
statistics,
footer: Some(footer),
},
))
}
}
impl ReadFlatBuffer for Postscript {
type Source<'a> = fb::Postscript<'a>;
type Error = VortexError;
fn read_flatbuffer<'buf>(
fb: &<Self::Source<'buf> as Follow<'buf>>::Inner,
) -> Result<Self, Self::Error> {
Ok(Self {
dtype: fb
.dtype()
.map(|ps| PostscriptSegment::read_flatbuffer(&ps))
.transpose()?,
layout: PostscriptSegment::read_flatbuffer(
&fb.layout()
.ok_or_else(|| vortex_err!("Postscript missing layout segment"))?,
)?,
statistics: fb
.statistics()
.map(|ps| PostscriptSegment::read_flatbuffer(&ps))
.transpose()?,
footer: PostscriptSegment::read_flatbuffer(
&fb.footer()
.ok_or_else(|| vortex_err!("Postscript missing footer segment"))?,
)?,
})
}
}
pub struct PostscriptSegment {
pub(crate) offset: u64,
pub(crate) length: u32,
pub(crate) alignment: Alignment,
}
impl FlatBufferRoot for PostscriptSegment {}
impl WriteFlatBuffer for PostscriptSegment {
type Target<'a> = fb::PostscriptSegment<'a>;
fn write_flatbuffer<'fb>(
&self,
fbb: &mut FlatBufferBuilder<'fb>,
) -> VortexResult<WIPOffset<Self::Target<'fb>>> {
Ok(fb::PostscriptSegment::create(
fbb,
&fb::PostscriptSegmentArgs {
offset: self.offset,
length: self.length,
alignment_exponent: self.alignment.exponent(),
_compression: None,
_encryption: None,
},
))
}
}
impl ReadFlatBuffer for PostscriptSegment {
type Source<'a> = fb::PostscriptSegment<'a>;
type Error = VortexError;
fn read_flatbuffer<'buf>(
fb: &<Self::Source<'buf> as Follow<'buf>>::Inner,
) -> Result<Self, Self::Error> {
Ok(PostscriptSegment {
offset: fb.offset(),
length: fb.length(),
// The alignment exponent comes from the file and may be corrupt, so validate it rather
// than panicking on a too-large shift (see issue #8819).
alignment: Alignment::try_from_exponent(fb.alignment_exponent())?,
})
}
}