gix_pack/data/output/
bytes.rs

1use std::io::Write;
2
3use gix_features::hash;
4
5use crate::data::output;
6use crate::exact_vec;
7
8/// The error returned by `next()` in the [`FromEntriesIter`] iterator.
9#[allow(missing_docs)]
10#[derive(Debug, thiserror::Error)]
11pub enum Error<E>
12where
13    E: std::error::Error + 'static,
14{
15    #[error(transparent)]
16    Io(#[from] std::io::Error),
17    #[error(transparent)]
18    Input(E),
19}
20
21/// An implementation of [`Iterator`] to write [encoded entries][output::Entry] to an inner implementation each time
22/// `next()` is called.
23pub struct FromEntriesIter<I, W> {
24    /// An iterator for input [`output::Entry`] instances
25    pub input: I,
26    /// A way of writing encoded bytes.
27    output: hash::Write<W>,
28    /// Our trailing hash when done writing all input entries
29    trailer: Option<gix_hash::ObjectId>,
30    /// The amount of objects in the iteration and the version of the packfile to be written.
31    /// Will be `None` to signal the header was written already.
32    header_info: Option<(crate::data::Version, u32)>,
33    /// The pack data version with which pack entries should be written.
34    entry_version: crate::data::Version,
35    /// The amount of written bytes thus far
36    written: u64,
37    /// Required to quickly find offsets by object IDs, as future objects may refer to those in the past to become a delta offset base.
38    /// It stores the pack offsets at which objects begin.
39    /// Additionally we store if an object was invalid, and if so we will not write it nor will we allow delta objects to it.
40    pack_offsets_and_validity: Vec<(u64, bool)>,
41    /// If we are done, no additional writes will occur
42    is_done: bool,
43}
44
45impl<I, W, E> FromEntriesIter<I, W>
46where
47    I: Iterator<Item = Result<Vec<output::Entry>, E>>,
48    W: std::io::Write,
49    E: std::error::Error + 'static,
50{
51    /// Create a new instance reading [entries][output::Entry] from an `input` iterator and write pack data bytes to
52    /// `output` writer, resembling a pack of `version` with exactly `num_entries` amount of objects contained in it.
53    /// `object_hash` is the kind of hash to use for the pack checksum and maybe other places, depending on the version.
54    ///
55    /// The input chunks are expected to be sorted already. You can use the [`InOrderIter`][gix_features::parallel::InOrderIter] to assure
56    /// this happens on the fly holding entire chunks in memory as long as needed for them to be dispensed in order.
57    ///
58    /// # Panics
59    ///
60    /// Not all combinations of `object_hash` and `version` are supported currently triggering assertion errors.
61    pub fn new(
62        input: I,
63        output: W,
64        num_entries: u32,
65        version: crate::data::Version,
66        object_hash: gix_hash::Kind,
67    ) -> Self {
68        assert!(
69            matches!(version, crate::data::Version::V2),
70            "currently only pack version 2 can be written",
71        );
72        FromEntriesIter {
73            input,
74            output: hash::Write::new(output, object_hash),
75            trailer: None,
76            entry_version: version,
77            pack_offsets_and_validity: exact_vec(num_entries as usize),
78            written: 0,
79            header_info: Some((version, num_entries)),
80            is_done: false,
81        }
82    }
83
84    /// Consume this instance and return the `output` implementation.
85    ///
86    /// _Note_ that the `input` iterator can be moved out of this instance beforehand.
87    pub fn into_write(self) -> W {
88        self.output.inner
89    }
90
91    /// Returns the trailing hash over all written entries once done.
92    /// It's `None` if we are not yet done writing.
93    pub fn digest(&self) -> Option<gix_hash::ObjectId> {
94        self.trailer
95    }
96
97    fn next_inner(&mut self) -> Result<u64, Error<E>> {
98        let previous_written = self.written;
99        if let Some((version, num_entries)) = self.header_info.take() {
100            let header_bytes = crate::data::header::encode(version, num_entries);
101            self.output.write_all(&header_bytes[..])?;
102            self.written += header_bytes.len() as u64;
103        }
104        match self.input.next() {
105            Some(entries) => {
106                for entry in entries.map_err(Error::Input)? {
107                    if entry.is_invalid() {
108                        self.pack_offsets_and_validity.push((0, false));
109                        continue;
110                    };
111                    self.pack_offsets_and_validity.push((self.written, true));
112                    let header = entry.to_entry_header(self.entry_version, |index| {
113                        let (base_offset, is_valid_object) = self.pack_offsets_and_validity[index];
114                        if !is_valid_object {
115                            unreachable!("if you see this the object database is correct as a delta refers to a non-existing object")
116                        }
117                        self.written - base_offset
118                    });
119                    self.written += header.write_to(entry.decompressed_size as u64, &mut self.output)? as u64;
120                    self.written += std::io::copy(&mut &*entry.compressed_data, &mut self.output)?;
121                }
122            }
123            None => {
124                let digest = self.output.hash.clone().digest();
125                self.output.inner.write_all(&digest[..])?;
126                self.written += digest.len() as u64;
127                self.output.inner.flush()?;
128                self.is_done = true;
129                self.trailer = Some(gix_hash::ObjectId::from(digest));
130            }
131        };
132        Ok(self.written - previous_written)
133    }
134}
135
136impl<I, W, E> Iterator for FromEntriesIter<I, W>
137where
138    I: Iterator<Item = Result<Vec<output::Entry>, E>>,
139    W: std::io::Write,
140    E: std::error::Error + 'static,
141{
142    /// The amount of bytes written to `out` if `Ok` or the error `E` received from the input.
143    type Item = Result<u64, Error<E>>;
144
145    fn next(&mut self) -> Option<Self::Item> {
146        if self.is_done {
147            return None;
148        }
149        Some(match self.next_inner() {
150            Err(err) => {
151                self.is_done = true;
152                Err(err)
153            }
154            Ok(written) => Ok(written),
155        })
156    }
157}