Skip to main content

array_format/delta/
mod.rs

1mod allocator;
2pub mod cache;
3mod immutable;
4mod mutable;
5
6pub use allocator::{AllocatorOutput, DeltaAllocator};
7pub use cache::DeltaCache;
8pub use immutable::DeltaImmutable;
9pub use mutable::DeltaMutable;
10
11use bytes::Bytes;
12
13use crate::{Error, Result, storage::Storage};
14
15pub struct Delta<D> {
16    pub inner: D,
17}
18
19// ── Streaming helper ─────────────────────────────────────────────────
20
21const STREAM_CHUNK_SIZE: usize = 1024 * 1024; // 1 MiB
22
23/// Streams `file` in 1 MiB chunks to `storage`, then appends `suffix`.
24/// The file must already be seeked to position 0.
25pub(crate) async fn write_file_then_bytes(
26    file: &mut tokio::fs::File,
27    file_size: u64,
28    suffix: &[u8],
29    storage: &dyn Storage,
30) -> Result<()> {
31    use tokio::io::AsyncReadExt;
32    let mut writer = storage.write_multipart().await?;
33    let mut remaining = file_size as usize;
34    while remaining > 0 {
35        let to_read = remaining.min(STREAM_CHUNK_SIZE);
36        let mut chunk = vec![0u8; to_read];
37        file.read_exact(&mut chunk).await.map_err(Error::Io)?;
38        writer.write_chunk(Bytes::from(chunk)).await?;
39        remaining -= to_read;
40    }
41    if !suffix.is_empty() {
42        writer.write_chunk(Bytes::copy_from_slice(suffix)).await?;
43    }
44    writer.complete().await
45}