Skip to main content

xet_data/file_reconstruction/data_writer/
data_writer.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use bytes::Bytes;
5use xet_client::cas_types::FileRange;
6use xet_runtime::utils::adjustable_semaphore::AdjustableSemaphorePermit;
7
8use super::super::Result;
9
10/// A future that produces the data bytes to be written.
11#[cfg(not(target_family = "wasm"))]
12pub type DataFuture = Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'static>>;
13#[cfg(target_family = "wasm")]
14pub type DataFuture = Pin<Box<dyn Future<Output = Result<Bytes>> + 'static>>;
15
16#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
17#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
18pub trait DataWriter: Send + 'static {
19    /// Sets the data source for the next sequential term.
20    ///
21    /// The byte range must be sequential - its start must match the end of the
22    /// previous range (or 0 for the first call). The data future will be spawned
23    /// as a task and its result will be written when ready.
24    ///
25    /// SequentialWriter will ensure that the actual writes happen in order.
26    ///
27    /// An optional semaphore permit can be passed for rate limiting. The permit
28    /// will be released by the background writer after the data has been written.
29    async fn set_next_term_data_source(
30        &mut self,
31        byte_range: FileRange,
32        permit: Option<AdjustableSemaphorePermit>,
33        data_future: DataFuture,
34    ) -> Result<()>;
35
36    /// Consumes the writer, waits until all data has been written, and returns the
37    /// number of bytes written. Dropping the writer without calling `finish` cancels
38    /// the reconstruction via the shared run state.
39    async fn finish(mut self: Box<Self>) -> Result<u64>;
40}