gix_pack/multi_index/access.rs
1use std::{
2 ops::Range,
3 path::{Path, PathBuf},
4};
5
6use crate::{
7 data,
8 index::PrefixLookupResult,
9 multi_index::{EntryIndex, File, PackIndex, Version},
10};
11
12/// Represents an entry within a multi index file, effectively mapping object [`IDs`][gix_hash::ObjectId] to pack data
13/// files and the offset within.
14#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub struct Entry {
17 /// The ID of the object.
18 pub oid: gix_hash::ObjectId,
19 /// The offset to the object's header in the pack data file.
20 pub pack_offset: data::Offset,
21 /// The index of the pack matching our [`File::index_names()`] slice.
22 pub pack_index: PackIndex,
23}
24
25/// Access methods
26impl File {
27 /// Returns the version of the multi-index file.
28 pub fn version(&self) -> Version {
29 self.version
30 }
31 /// Returns the path from which the multi-index file was loaded.
32 ///
33 /// Note that it might have changed in the mean time, or might have been removed as well.
34 pub fn path(&self) -> &Path {
35 &self.path
36 }
37 /// Returns the amount of indices stored in this multi-index file. It's the same as [File::index_names().len()][File::index_names()],
38 /// and returned as one past the highest known index.
39 pub fn num_indices(&self) -> PackIndex {
40 self.num_indices
41 }
42 /// Returns the total amount of objects available for lookup, and returned as one past the highest known entry index
43 pub fn num_objects(&self) -> EntryIndex {
44 self.num_objects
45 }
46 /// Returns the kind of hash function used for object ids available in this index.
47 pub fn object_hash(&self) -> gix_hash::Kind {
48 self.object_hash
49 }
50 /// Returns the checksum over the entire content of the file (excluding the checksum itself).
51 ///
52 /// It can be used to validate it didn't change after creation.
53 pub fn checksum(&self) -> gix_hash::ObjectId {
54 gix_hash::ObjectId::from_bytes_or_panic(&self.data[self.data.len() - self.hash_len..])
55 }
56 /// Return all names of index files (`*.idx`) whose objects we contain.
57 ///
58 /// The corresponding pack can be found by replacing the `.idx` extension with `.pack`.
59 pub fn index_names(&self) -> &[PathBuf] {
60 &self.index_names
61 }
62}
63
64impl File {
65 /// Return the object id at the given `index`, which ranges from 0 to [File::num_objects()].
66 pub fn oid_at_index(&self, index: EntryIndex) -> &gix_hash::oid {
67 debug_assert!(index < self.num_objects, "index out of bounds");
68 let index: usize = index as usize;
69 let start = self.lookup_ofs + index * self.hash_len;
70 gix_hash::oid::from_bytes_unchecked(&self.data[start..][..self.hash_len])
71 }
72
73 /// Given a `prefix`, find an object that matches it uniquely within this index and return `Some(Ok(entry_index))`.
74 /// If there is more than one object matching the object `Some(Err(())` is returned.
75 ///
76 /// Finally, if no object matches the index, the return value is `None`.
77 ///
78 /// Pass `candidates` to obtain the set of entry-indices matching `prefix`, with the same return value as
79 /// one would have received if it remained `None`. It will be empty if no object matched the `prefix`.
80 ///
81 // NOTE: pretty much the same things as in `index::File::lookup`, change things there
82 // as well.
83 pub fn lookup_prefix(
84 &self,
85 prefix: gix_hash::Prefix,
86 candidates: Option<&mut Range<EntryIndex>>,
87 ) -> Option<PrefixLookupResult> {
88 crate::index::access::lookup_prefix(
89 prefix,
90 candidates,
91 &self.fan,
92 &|idx| self.oid_at_index(idx),
93 self.num_objects,
94 )
95 }
96
97 /// Find the index ranging from 0 to [File::num_objects()] that belongs to data associated with `id`, or `None` if it wasn't found.
98 ///
99 /// Use this index for finding additional information via [`File::pack_id_and_pack_offset_at_index()`].
100 pub fn lookup(&self, id: impl AsRef<gix_hash::oid>) -> Option<EntryIndex> {
101 crate::index::access::lookup(id.as_ref(), &self.fan, &|idx| self.oid_at_index(idx))
102 }
103
104 /// Given the `index` ranging from 0 to [File::num_objects()], return the pack index and its absolute offset into the pack.
105 ///
106 /// The pack-index refers to an entry in the [`index_names`][File::index_names()] list, from which the pack can be derived.
107 pub fn pack_id_and_pack_offset_at_index(&self, index: EntryIndex) -> (PackIndex, data::Offset) {
108 const OFFSET_ENTRY_SIZE: usize = 4 + 4;
109 let index = index as usize;
110 let start = self.offsets_ofs + index * OFFSET_ENTRY_SIZE;
111
112 const HIGH_BIT: u32 = 1 << 31;
113
114 let pack_index = crate::read_u32(&self.data[start..][..4]);
115 let offset = &self.data[start + 4..][..4];
116 let ofs32 = crate::read_u32(offset);
117 let pack_offset = if (ofs32 & HIGH_BIT) == HIGH_BIT {
118 // We determine if large offsets are actually larger than 4GB and if not, we don't use the high-bit to signal anything
119 // but allow the presence of the large-offset chunk to signal what's happening.
120 if let Some(offsets_64) = self.large_offsets_ofs {
121 let from = offsets_64 + (ofs32 ^ HIGH_BIT) as usize * 8;
122 crate::read_u64(&self.data[from..][..8])
123 } else {
124 u64::from(ofs32)
125 }
126 } else {
127 u64::from(ofs32)
128 };
129 (pack_index, pack_offset)
130 }
131
132 /// Return an iterator over all entries within this file.
133 pub fn iter(&self) -> impl Iterator<Item = Entry> + '_ {
134 (0..self.num_objects).map(move |idx| {
135 let (pack_index, pack_offset) = self.pack_id_and_pack_offset_at_index(idx);
136 Entry {
137 oid: self.oid_at_index(idx).to_owned(),
138 pack_offset,
139 pack_index,
140 }
141 })
142 }
143}