1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{compound, data, loose, pack};
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("An error occurred while obtaining an object from the loose object store")]
Loose(#[from] loose::db::find::Error),
#[error("An error occurred while obtaining an object from the packed object store")]
Pack(#[from] pack::data::decode_entry::Error),
}
pub(crate) struct PackLocation {
pub pack_id: usize,
pub entry_index: u32,
}
impl compound::Db {
pub fn find<'a>(
&self,
id: impl AsRef<git_hash::oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<data::Object<'a>>, Error> {
let id = id.as_ref();
for pack in &self.packs {
if let Some(idx) = pack.internal_find_pack_index(id) {
let object = pack.internal_get_object_by_index(idx, buffer, pack_cache)?;
return Ok(Some(object));
}
}
if self.loose.contains(id) {
return self.loose.find(id, buffer).map_err(Into::into);
}
Ok(None)
}
pub(crate) fn internal_find(&self, id: impl AsRef<git_hash::oid>) -> Option<PackLocation> {
let id = id.as_ref();
for (pack_idx, pack) in self.packs.iter().enumerate() {
if let Some(idx) = pack.internal_find_pack_index(id) {
return Some(PackLocation {
pack_id: pack_idx,
entry_index: idx,
});
}
}
None
}
pub(crate) fn internal_get_packed_object_by_index<'a>(
&self,
pack_index: usize,
object_index: u32,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<data::Object<'a>, pack::data::decode_entry::Error> {
self.packs[pack_index].internal_get_object_by_index(object_index, buffer, pack_cache)
}
}