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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::{cmp::Ordering, mem::size_of};
use crate::{
data,
index::{self, EntryIndex, PrefixLookupResult, FAN_LEN},
};
const N32_SIZE: usize = size_of::<u32>();
const N64_SIZE: usize = size_of::<u64>();
const V1_HEADER_SIZE: usize = FAN_LEN * N32_SIZE;
const V2_HEADER_SIZE: usize = N32_SIZE * 2 + FAN_LEN * N32_SIZE;
const N32_HIGH_BIT: u32 = 1 << 31;
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry {
pub oid: git_hash::ObjectId,
pub pack_offset: data::Offset,
pub crc32: Option<u32>,
}
impl index::File {
fn iter_v1(&self) -> impl Iterator<Item = Entry> + '_ {
match self.version {
index::Version::V1 => self.data[V1_HEADER_SIZE..]
.chunks(N32_SIZE + self.hash_len)
.take(self.num_objects as usize)
.map(|c| {
let (ofs, oid) = c.split_at(N32_SIZE);
Entry {
oid: git_hash::ObjectId::from(oid),
pack_offset: crate::read_u32(ofs) as u64,
crc32: None,
}
}),
_ => panic!("Cannot use iter_v1() on index of type {:?}", self.version),
}
}
fn iter_v2(&self) -> impl Iterator<Item = Entry> + '_ {
let pack64_offset = self.offset_pack_offset64_v2();
match self.version {
index::Version::V2 => izip!(
self.data[V2_HEADER_SIZE..].chunks(self.hash_len),
self.data[self.offset_crc32_v2()..].chunks(N32_SIZE),
self.data[self.offset_pack_offset_v2()..].chunks(N32_SIZE)
)
.take(self.num_objects as usize)
.map(move |(oid, crc32, ofs32)| Entry {
oid: git_hash::ObjectId::from(oid),
pack_offset: self.pack_offset_from_offset_v2(ofs32, pack64_offset),
crc32: Some(crate::read_u32(crc32)),
}),
_ => panic!("Cannot use iter_v2() on index of type {:?}", self.version),
}
}
pub fn oid_at_index(&self, index: EntryIndex) -> &git_hash::oid {
let index = index as usize;
let start = match self.version {
index::Version::V2 => V2_HEADER_SIZE + index * self.hash_len,
index::Version::V1 => V1_HEADER_SIZE + index * (N32_SIZE + self.hash_len) + N32_SIZE,
};
git_hash::oid::from_bytes_unchecked(&self.data[start..][..self.hash_len])
}
pub fn pack_offset_at_index(&self, index: EntryIndex) -> data::Offset {
let index = index as usize;
match self.version {
index::Version::V2 => {
let start = self.offset_pack_offset_v2() + index * N32_SIZE;
self.pack_offset_from_offset_v2(&self.data[start..][..N32_SIZE], self.offset_pack_offset64_v2())
}
index::Version::V1 => {
let start = V1_HEADER_SIZE + index * (N32_SIZE + self.hash_len);
crate::read_u32(&self.data[start..][..N32_SIZE]) as u64
}
}
}
pub fn crc32_at_index(&self, index: EntryIndex) -> Option<u32> {
let index = index as usize;
match self.version {
index::Version::V2 => {
let start = self.offset_crc32_v2() + index * N32_SIZE;
Some(crate::read_u32(&self.data[start..start + N32_SIZE]))
}
index::Version::V1 => None,
}
}
pub fn lookup(&self, id: impl AsRef<git_hash::oid>) -> Option<EntryIndex> {
let id = id.as_ref();
let first_byte = id.first_byte() as usize;
let mut upper_bound = self.fan[first_byte];
let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
while lower_bound < upper_bound {
let mid = (lower_bound + upper_bound) / 2;
let mid_sha = self.oid_at_index(mid);
use std::cmp::Ordering::*;
match id.cmp(mid_sha) {
Less => upper_bound = mid,
Equal => return Some(mid),
Greater => lower_bound = mid + 1,
}
}
None
}
pub fn lookup_prefix(&self, prefix: git_hash::Prefix) -> Option<PrefixLookupResult> {
let first_byte = prefix.as_oid().first_byte() as usize;
let mut upper_bound = self.fan[first_byte];
let mut lower_bound = if first_byte != 0 { self.fan[first_byte - 1] } else { 0 };
while lower_bound < upper_bound {
let mid = (lower_bound + upper_bound) / 2;
let mid_sha = self.oid_at_index(mid);
use std::cmp::Ordering::*;
match prefix.cmp_oid(mid_sha) {
Less => upper_bound = mid,
Equal => {
let next = mid + 1;
if next < self.num_objects && prefix.cmp_oid(self.oid_at_index(next)) == Ordering::Equal {
return Some(Err(()));
}
if mid != 0 && prefix.cmp_oid(self.oid_at_index(mid - 1)) == Ordering::Equal {
return Some(Err(()));
}
return Some(Ok(mid));
}
Greater => lower_bound = mid + 1,
}
}
None
}
pub fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = Entry> + 'a> {
match self.version {
index::Version::V2 => Box::new(self.iter_v2()),
index::Version::V1 => Box::new(self.iter_v1()),
}
}
pub fn sorted_offsets(&self) -> Vec<data::Offset> {
let mut ofs: Vec<_> = match self.version {
index::Version::V1 => self.iter().map(|e| e.pack_offset).collect(),
index::Version::V2 => {
let offset32_start = &self.data[self.offset_pack_offset_v2()..];
let pack_offset_64_start = self.offset_pack_offset64_v2();
offset32_start
.chunks(N32_SIZE)
.take(self.num_objects as usize)
.map(|offset| self.pack_offset_from_offset_v2(offset, pack_offset_64_start))
.collect()
}
};
ofs.sort_unstable();
ofs
}
#[inline]
fn offset_crc32_v2(&self) -> usize {
V2_HEADER_SIZE + self.num_objects as usize * self.hash_len
}
#[inline]
fn offset_pack_offset_v2(&self) -> usize {
self.offset_crc32_v2() + self.num_objects as usize * N32_SIZE
}
#[inline]
fn offset_pack_offset64_v2(&self) -> usize {
self.offset_pack_offset_v2() + self.num_objects as usize * N32_SIZE
}
#[inline]
fn pack_offset_from_offset_v2(&self, offset: &[u8], pack64_offset: usize) -> data::Offset {
debug_assert_eq!(self.version, index::Version::V2);
let ofs32 = crate::read_u32(offset);
if (ofs32 & N32_HIGH_BIT) == N32_HIGH_BIT {
let from = pack64_offset + (ofs32 ^ N32_HIGH_BIT) as usize * N64_SIZE;
crate::read_u64(&self.data[from..][..N64_SIZE])
} else {
ofs32 as u64
}
}
}