Debian Codegraph @

lib/src/default_index/entry.rs

raw
1 // Copyright 2023 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::fmt::Debug;
16 use std::fmt::Formatter;
17 use std::hash::Hash;
18 use std::hash::Hasher;
19
20 use smallvec::SmallVec;
21
22 use super::composite::CompositeCommitIndex;
23 use super::composite::DynCommitIndexSegment;
24 use crate::backend::ChangeId;
25 use crate::backend::CommitId;
26 use crate::object_id::ObjectId as _;
27
28 /// Global commit index position.
29 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
30 pub(super) struct GlobalCommitPosition(pub(super) u32);
31
32 impl GlobalCommitPosition {
33 pub const MIN: Self = Self(u32::MIN);
34 pub const MAX: Self = Self(u32::MAX);
35 }
36
37 /// Local commit position within an index segment.
38 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
39 pub(super) struct LocalCommitPosition(pub(super) u32);
40
41 // SmallVec reuses two pointer-size fields as inline area, which meas we can
42 // inline up to 16 bytes (on 64-bit platform) for free.
43 pub(super) type SmallGlobalCommitPositionsVec = SmallVec<[GlobalCommitPosition; 4]>;
44 pub(super) type SmallLocalCommitPositionsVec = SmallVec<[LocalCommitPosition; 4]>;
45
46 #[derive(Clone)]
47 pub(super) struct CommitIndexEntry<'a> {
48 source: &'a DynCommitIndexSegment,
49 pos: GlobalCommitPosition,
50 /// Position within the source segment
51 local_pos: LocalCommitPosition,
52 }
53
54 impl Debug for CommitIndexEntry<'_> {
55 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56 f.debug_struct("CommitIndexEntry")
57 .field("pos", &self.pos)
58 .field("local_pos", &self.local_pos)
59 .field("commit_id", &self.commit_id().hex())
60 .finish()
61 }
62 }
63
64 impl PartialEq for CommitIndexEntry<'_> {
65 fn eq(&self, other: &Self) -> bool {
66 self.pos == other.pos
67 }
68 }
69
70 impl Eq for CommitIndexEntry<'_> {}
71
72 impl Hash for CommitIndexEntry<'_> {
73 fn hash<H: Hasher>(&self, state: &mut H) {
74 self.pos.hash(state);
75 }
76 }
77
78 impl<'a> CommitIndexEntry<'a> {
79 pub(super) fn new(
80 source: &'a DynCommitIndexSegment,
81 pos: GlobalCommitPosition,
82 local_pos: LocalCommitPosition,
83 ) -> Self {
84 Self {
85 source,
86 pos,
87 local_pos,
88 }
89 }
90
91 pub fn position(&self) -> GlobalCommitPosition {
92 self.pos
93 }
94
95 pub fn generation_number(&self) -> u32 {
96 self.source.generation_number(self.local_pos)
97 }
98
99 pub fn commit_id(&self) -> CommitId {
100 self.source.commit_id(self.local_pos)
101 }
102
103 pub fn change_id(&self) -> ChangeId {
104 self.source.change_id(self.local_pos)
105 }
106
107 pub fn num_parents(&self) -> u32 {
108 self.source.num_parents(self.local_pos)
109 }
110
111 pub fn parent_positions(&self) -> SmallGlobalCommitPositionsVec {
112 self.source.parent_positions(self.local_pos)
113 }
114
115 pub fn parents(&self) -> impl ExactSizeIterator<Item = CommitIndexEntry<'a>> + use<'a> {
116 let composite = CompositeCommitIndex::new(self.source);
117 self.parent_positions()
118 .into_iter()
119 .map(move |pos| composite.entry_by_pos(pos))
120 }
121 }