This repository has no description
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(
13 &mut self,
14 pack_id: u32,
15 offset: u64,
16 data: &[u8],
17 kind: gix_object::Kind,
18 compressed_size: usize,
19 );
20 /// Attempt to fetch the object at `offset` and store its decoded bytes in `out`, as previously stored with [`DecodeEntry::put()`], and return
21 /// its (object `kind`, `decompressed_size`)
22 fn get(
23 &mut self,
24 pack_id: u32,
25 offset: u64,
26 out: &mut Vec<u8>,
27 ) -> Option<(gix_object::Kind, usize)>;
28}
29
30/// A cache that stores nothing and retrieves nothing, thus it _never_ caches.
31#[derive(Default)]
32pub struct Never;
33
34impl DecodeEntry for Never {
35 fn put(
36 &mut self,
37 _pack_id: u32,
38 _offset: u64,
39 _data: &[u8],
40 _kind: gix_object::Kind,
41 _compressed_size: usize,
42 ) {
43 }
44 fn get(
45 &mut self,
46 _pack_id: u32,
47 _offset: u64,
48 _out: &mut Vec<u8>,
49 ) -> Option<(gix_object::Kind, usize)> {
50 None
51 }
52}
53
54impl<T: DecodeEntry + ?Sized> DecodeEntry for Box<T> {
55 fn put(&mut self, pack_id: u32, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
56 self.deref_mut()
57 .put(pack_id, offset, data, kind, compressed_size);
58 }
59
60 fn get(&mut self, pack_id: u32, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
61 self.deref_mut().get(pack_id, offset, out)
62 }
63}
64
65/// A way of storing and retrieving entire objects to and from a cache.
66pub trait Object {
67 /// Put the object going by `id` of `kind` with `data` into the cache.
68 fn put(&mut self, id: gix_hash::ObjectId, kind: gix_object::Kind, data: &[u8]);
69
70 /// Try to retrieve the object named `id` and place its data into `out` if available and return `Some(kind)` if found.
71 fn get(&mut self, id: &gix_hash::ObjectId, out: &mut Vec<u8>) -> Option<gix_object::Kind>;
72}
73
74/// Various implementations of [`DecodeEntry`] using least-recently-used algorithms.
75#[cfg(any(feature = "pack-cache-lru-dynamic", feature = "pack-cache-lru-static"))]
76pub mod lru;
77
78pub mod object;
79
80///
81pub mod delta;
82
83/// Replaces content of the given `Vec` with the slice. The vec will have the same length
84/// as the slice. The vec can be either `&mut Vec` or `Vec`.
85/// Returns `None` if no memory could be allocated.
86#[cfg(any(
87 feature = "pack-cache-lru-static",
88 feature = "pack-cache-lru-dynamic",
89 feature = "object-cache-dynamic"
90))]
91fn set_vec_to_slice<V: std::borrow::BorrowMut<Vec<u8>>>(mut vec: V, source: &[u8]) -> Option<V> {
92 let out = vec.borrow_mut();
93 out.clear();
94 out.try_reserve(source.len()).ok()?;
95 out.extend_from_slice(source);
96 Some(vec)
97}