Skip to main content

vortex_layout/segments/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Segment access abstractions for layouts.
5//!
6//! Layouts refer to byte ranges by [`SegmentId`]. A [`SegmentSource`] resolves those ids to buffer
7//! handles for readers, while a [`SegmentSink`] assigns ids when writers emit buffers.
8//! [`SegmentCache`] implementations can sit in front of sources to reuse segment bytes across
9//! scans.
10
11mod cache;
12mod shared;
13mod sink;
14mod source;
15
16#[cfg(any(test, feature = "_test-harness"))]
17mod test;
18
19use std::fmt::Display;
20use std::ops::Deref;
21
22pub use cache::*;
23pub use shared::*;
24pub use sink::*;
25pub use source::*;
26#[cfg(any(test, feature = "_test-harness"))]
27pub use test::*;
28use vortex_error::VortexError;
29
30/// Identifier for a single physical segment referenced by a layout.
31///
32/// Segment ids are local to a file or segment source. The file footer maps ids to physical offsets;
33/// custom storage systems may map them to object-store keys or other random-access locations.
34// TODO(ngates): should this be a `[u8]` instead? Allowing for arbitrary segment identifiers?
35#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct SegmentId(u32);
37
38impl From<u32> for SegmentId {
39    fn from(value: u32) -> Self {
40        Self(value)
41    }
42}
43
44impl TryFrom<usize> for SegmentId {
45    type Error = VortexError;
46
47    fn try_from(value: usize) -> Result<Self, Self::Error> {
48        Ok(Self::from(u32::try_from(value)?))
49    }
50}
51
52impl Deref for SegmentId {
53    type Target = u32;
54
55    fn deref(&self) -> &Self::Target {
56        &self.0
57    }
58}
59
60impl Display for SegmentId {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "SegmentId({})", self.0)
63    }
64}