/
find.rs
103 lines (94 loc) · 3.53 KB
/
find.rs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::pack::bundle::Location;
use crate::{compound, data::Object, find::PackEntry, linked, pack};
use git_hash::oid;
use std::convert::TryInto;
impl crate::Find for linked::Db {
type Error = compound::find::Error;
fn find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
let id = id.as_ref();
for db in self.dbs.iter() {
match db.internal_find(id) {
Some(compound::find::PackLocation {
bundle_index: pack_id,
entry_index,
}) => {
return db
.internal_get_packed_object_by_index(pack_id, entry_index, buffer, pack_cache)
.map(Some)
.map_err(Into::into)
}
None => {
if db.loose.contains(id) {
return db.loose.find(id, buffer).map_err(Into::into);
}
}
}
}
Ok(None)
}
fn location_by_id(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<pack::bundle::Location> {
let id = id.as_ref();
for db in self.dbs.iter() {
if let Some(compound::find::PackLocation {
bundle_index,
entry_index,
}) = db.internal_find(id)
{
let bundle = &db.bundles[bundle_index];
let pack_offset = bundle.index.pack_offset_at_index(entry_index);
let entry = bundle.pack.entry(pack_offset);
buf.resize(entry.decompressed_size.try_into().expect("representable szie"), 0);
return bundle
.pack
.decompress_entry(&entry, buf)
.ok()
.map(|entry_size| pack::bundle::Location {
pack_id: bundle.pack.id,
index_file_id: entry_index,
entry_size: entry.header_size() + entry_size,
});
}
}
None
}
fn pack_entry_by_location(&self, location: &pack::bundle::Location) -> Option<PackEntry<'_>> {
self.dbs
.iter()
.find_map(|db| db.bundles.iter().find(|p| p.pack.id == location.pack_id))
.map(|b| (b, location))
.and_then(|(bundle, l)| {
let crc32 = bundle.index.crc32_at_index(l.index_file_id);
let pack_offset = bundle.index.pack_offset_at_index(l.index_file_id);
bundle
.pack
.entry_slice(l.entry_range(pack_offset))
.map(|data| PackEntry {
data,
crc32,
version: bundle.pack.version(),
})
})
}
}
impl crate::Find for &linked::Db {
type Error = compound::find::Error;
fn find<'a>(
&self,
id: impl AsRef<oid>,
buffer: &'a mut Vec<u8>,
pack_cache: &mut impl pack::cache::DecodeEntry,
) -> Result<Option<Object<'a>>, Self::Error> {
(*self).find(id, buffer, pack_cache)
}
fn location_by_id(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<Location> {
(*self).location_by_id(id, buf)
}
fn pack_entry_by_location(&self, location: &pack::bundle::Location) -> Option<PackEntry<'_>> {
(*self).pack_entry_by_location(location)
}
}