vortex_io/file/read/
source.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use futures::FutureExt;
7use futures::future::{BoxFuture, LocalBoxFuture};
8use futures::stream::BoxStream;
9use vortex_error::VortexResult;
10
11use crate::file::IoRequest;
12use crate::runtime::Handle;
13
14/// A trait for types that can be opened as an `IoSource`.
15pub trait IntoReadSource {
16    fn into_read_source(self, handle: Handle) -> VortexResult<ReadSourceRef>;
17}
18
19pub type ReadSourceRef = Arc<dyn ReadSource>;
20
21/// An object-safe trait representing an open file-like I/O object for reading.
22pub trait ReadSource: Send + Sync {
23    /// The URI of this source, for logging and debugging purposes.
24    fn uri(&self) -> &Arc<str>;
25
26    /// The coalescing window to use for this source, if any.
27    fn coalesce_window(&self) -> Option<CoalesceWindow>;
28
29    /// Returns a shared future that resolves to the byte size of the underlying data source.
30    fn size(&self) -> BoxFuture<'static, VortexResult<u64>>;
31
32    /// Drive a stream of I/O requests to completion.
33    fn drive_send(
34        self: Arc<Self>,
35        requests: BoxStream<'static, IoRequest>,
36    ) -> BoxFuture<'static, ()>;
37
38    /// Drive a stream of I/O requests to completion on the local thread.
39    fn drive_local(
40        self: Arc<Self>,
41        requests: BoxStream<'static, IoRequest>,
42    ) -> LocalBoxFuture<'static, ()> {
43        self.drive_send(requests).boxed_local()
44    }
45}
46
47#[derive(Clone, Copy, Debug)]
48pub struct CoalesceWindow {
49    /// The maximum "empty" distance between two requests to consider them for coalescing.
50    pub distance: u64,
51    /// The maximum total size spanned by a coalesced request.
52    pub max_size: u64,
53}