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