Skip to main content

gix_pack/cache/
mod.rs

1use std::ops::DerefMut;
2
3use gix_object::Kind;
4
5/// A trait to model putting objects at a given pack `offset` into a cache, and fetching them.
6///
7/// It is used to speed up [pack traversals][crate::index::File::traverse()].
8pub trait DecodeEntry {
9    /// Store a fully decoded object at `offset` of `kind` with `compressed_size` and `data` in the cache.
10    ///
11    /// It is up to the cache implementation whether that actually happens or not.
12    fn put(&mut self, pack_id: u32, offset: u64, data: &[u8], kind: gix_object::Kind, compressed_size: usize);
13    /// Attempt to fetch the object at `offset` and store its decoded bytes in `out`, as previously stored with [`DecodeEntry::put()`], and return
14    /// its (object `kind`, `decompressed_size`)
15    fn get(&mut self, pack_id: u32, offset: u64, out: &mut Vec<u8>) -> Option<(gix_object::Kind, usize)>;
16}
17
18/// A cache that stores nothing and retrieves nothing, thus it _never_ caches.
19#[derive(Default)]
20pub struct Never;
21
22impl DecodeEntry for Never {
23    fn put(&mut self, _pack_id: u32, _offset: u64, _data: &[u8], _kind: gix_object::Kind, _compressed_size: usize) {}
24    fn get(&mut self, _pack_id: u32, _offset: u64, _out: &mut Vec<u8>) -> Option<(gix_object::Kind, usize)> {
25        None
26    }
27}
28
29impl<T: DecodeEntry + ?Sized> DecodeEntry for Box<T> {
30    fn put(&mut self, pack_id: u32, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
31        self.deref_mut().put(pack_id, offset, data, kind, compressed_size);
32    }
33
34    fn get(&mut self, pack_id: u32, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
35        self.deref_mut().get(pack_id, offset, out)
36    }
37}
38
39/// A way of storing and retrieving entire objects to and from a cache.
40pub trait Object {
41    /// Put the object going by `id` of `kind` with `data` into the cache.
42    fn put(&mut self, id: gix_hash::ObjectId, kind: gix_object::Kind, data: &[u8]);
43
44    /// Try to retrieve the object named `id` and place its data into `out` if available and return `Some(kind)` if found.
45    fn get(&mut self, id: &gix_hash::ObjectId, out: &mut Vec<u8>) -> Option<gix_object::Kind>;
46}
47
48/// Various implementations of [`DecodeEntry`] using least-recently-used algorithms.
49#[cfg(any(feature = "pack-cache-lru-dynamic", feature = "pack-cache-lru-static"))]
50pub mod lru;
51
52pub mod object;
53
54/// Index-less parallel pack traversal.
55///
56/// Build a delta [`delta::Tree`] directly from a streaming pack scan with
57/// [`delta::Tree::from_offsets_in_pack()`] — no pre-built `.idx` — then resolve every
58/// object in parallel with [`delta::Tree::traverse()`]. This is the index-less companion
59/// to the idx-verified [`crate::index::File::traverse_with_index()`]; [`delta::traverse::Context`] and
60/// [`delta::traverse::Options`] configure the traversal.
61///
62/// ```no_run
63/// use gix_pack::cache::delta::Tree;
64/// use std::sync::atomic::AtomicBool;
65///
66/// # fn build_index_less(
67/// #     pack_path: &std::path::Path,
68/// #     offsets: Vec<gix_pack::data::Offset>,
69/// # ) -> Result<(), Box<dyn std::error::Error>> {
70/// // Build the delta tree straight from the pack — no `.idx` required:
71/// let tree = Tree::from_offsets_in_pack(
72///     pack_path,
73///     offsets.into_iter(),
74///     &|offset| *offset,
75///     &|_id| None,                     // self-contained pack: no ref-delta lookups
76///     &mut gix_features::progress::Discard,
77///     &AtomicBool::default(),
78///     gix_hash::Kind::default(),
79/// )?;
80/// // `tree.traverse(resolve, &pack, pack_end, inspect, Options { .. })` then
81/// // resolves every object in parallel across threads.
82/// let _ = tree;
83/// # Ok(()) }
84/// ```
85pub mod delta;
86
87/// Replaces content of the given `Vec` with the slice. The vec will have the same length
88/// as the slice. The vec can be either `&mut Vec` or `Vec`.
89/// Returns `None` if no memory could be allocated.
90#[cfg(any(
91    feature = "pack-cache-lru-static",
92    feature = "pack-cache-lru-dynamic",
93    feature = "object-cache-dynamic"
94))]
95fn set_vec_to_slice<V: std::borrow::BorrowMut<Vec<u8>>>(mut vec: V, source: &[u8]) -> Option<V> {
96    let out = vec.borrow_mut();
97    out.clear();
98    out.try_reserve(source.len()).ok()?;
99    out.extend_from_slice(source);
100    Some(vec)
101}