Skip to main content

gix_pack/
testing.rs

1use crate::find::Entry;
2
3/// An in-memory object database without pack locations.
4#[derive(Clone)]
5pub struct Memory {
6    objects: std::sync::Arc<gix_hashtable::HashMap<gix_hash::ObjectId, (gix_object::Kind, Vec<u8>)>>,
7}
8
9impl Memory {
10    /// Create a database containing all `objects`.
11    ///
12    /// Each item is `(id, (kind, data))`, where `data` is the uncompressed object body without a
13    /// loose-object header. IDs are trusted rather than recomputed. If an ID occurs more than once,
14    /// the last item wins.
15    pub fn new(objects: impl IntoIterator<Item = (gix_hash::ObjectId, (gix_object::Kind, Vec<u8>))>) -> Self {
16        Self {
17            objects: std::sync::Arc::new(objects.into_iter().collect()),
18        }
19    }
20}
21
22impl crate::Find for Memory {
23    fn contains(&self, id: &gix_hash::oid) -> bool {
24        self.objects.contains_key(id)
25    }
26
27    fn try_find_cached<'a>(
28        &self,
29        id: &gix_hash::oid,
30        buffer: &'a mut Vec<u8>,
31        _pack_cache: &mut dyn crate::cache::DecodeEntry,
32    ) -> Result<Option<(gix_object::Data<'a>, Option<crate::data::entry::Location>)>, gix_object::find::Error> {
33        Ok(self.objects.get(id).map(|(kind, data)| {
34            buffer.clear();
35            buffer.extend_from_slice(data);
36            (gix_object::Data::new(buffer, *kind, id.kind()), None)
37        }))
38    }
39
40    fn location_by_oid(&self, _id: &gix_hash::oid, _buf: &mut Vec<u8>) -> Option<crate::data::entry::Location> {
41        None
42    }
43
44    fn pack_offsets_and_oid(&self, _pack_id: u32) -> Option<Vec<(crate::data::Offset, gix_hash::ObjectId)>> {
45        None
46    }
47
48    fn entry_by_location(&self, _location: &crate::data::entry::Location) -> Option<Entry> {
49        None
50    }
51}