Skip to main content

vortex_io/segments/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Segments: the blobs a source names before a scan runs, and the sources that read them.
5
6mod file;
7mod read;
8mod shared;
9
10use std::fmt;
11use std::fmt::Display;
12use std::ops::Deref;
13use std::sync::Arc;
14
15pub use file::*;
16use futures::future::BoxFuture;
17pub use shared::*;
18use vortex_array::buffer::BufferHandle;
19use vortex_buffer::Alignment;
20use vortex_error::VortexError;
21use vortex_error::VortexResult;
22
23/// Identifier for a single physical segment referenced by a layout.
24///
25/// Segment ids are local to a source. A Vortex file's footer maps ids to physical offsets; other
26/// formats assign ids to the blobs their metadata names, and custom storage systems may map them
27/// to object-store keys or other random-access locations.
28#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct SegmentId(u32);
30
31impl From<u32> for SegmentId {
32    fn from(value: u32) -> Self {
33        Self(value)
34    }
35}
36
37impl TryFrom<usize> for SegmentId {
38    type Error = VortexError;
39
40    fn try_from(value: usize) -> Result<Self, Self::Error> {
41        Ok(Self::from(u32::try_from(value)?))
42    }
43}
44
45impl Deref for SegmentId {
46    type Target = u32;
47
48    fn deref(&self) -> &Self::Target {
49        &self.0
50    }
51}
52
53impl Display for SegmentId {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(f, "SegmentId({})", self.0)
56    }
57}
58
59/// Static future resolving to a segment byte buffer.
60pub type SegmentFuture = BoxFuture<'static, VortexResult<BufferHandle>>;
61
62/// Reads segments by id.
63///
64/// Implementations may issue asynchronous file reads, object-store requests, cache lookups, or
65/// in-memory buffer slices. Returned futures must be independent and safe to poll concurrently.
66pub trait SegmentSource: 'static + Send + Sync {
67    /// Request a segment, returning a future that will eventually resolve to the segment data.
68    fn request(&self, id: SegmentId) -> SegmentFuture;
69}
70
71impl SegmentSource for Arc<dyn SegmentSource> {
72    fn request(&self, id: SegmentId) -> SegmentFuture {
73        self.as_ref().request(id)
74    }
75}
76
77/// Where a segment's bytes live in its source.
78#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub struct SegmentLocation {
80    /// Byte offset of the segment from the start of the source.
81    pub offset: u64,
82    /// Length of the segment in bytes.
83    pub length: u32,
84    /// Alignment the segment's bytes must have once delivered.
85    pub alignment: Alignment,
86}