Debian Codegraph @

lib/src/default_index/mod.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 //! An on-disk index of the commits in a repository.
16 //!
17 //! Implements an index of the commits in a repository that conforms to the
18 //! trains in the [index module](crate::index). The index is stored on local
19 //! disk and contains an entry for every commit in the repository. See
20 //! [`DefaultReadonlyIndex`] and [`DefaultMutableIndex`].
21
22 mod bit_set;
23 mod changed_path;
24 mod composite;
25 mod entry;
26 mod mutable;
27 mod readonly;
28 mod rev_walk;
29 mod rev_walk_queue;
30 mod revset_engine;
31 mod revset_graph_iterator;
32 mod store;
33
34 pub use self::mutable::DefaultMutableIndex;
35 pub use self::readonly::ChangedPathIndexLevelStats;
36 pub use self::readonly::CommitIndexLevelStats;
37 pub use self::readonly::DefaultReadonlyIndex;
38 pub use self::readonly::DefaultReadonlyIndexRevset;
39 pub use self::readonly::IndexStats;
40 pub use self::readonly::ReadonlyIndexLoadError;
41 pub use self::store::DefaultChangedPathIndexProgress;
42 pub use self::store::DefaultIndexStore;
43 pub use self::store::DefaultIndexStoreError;
44 pub use self::store::DefaultIndexStoreInitError;
45
46 #[cfg(test)]
47 #[rustversion::attr(
48 since(1.89),
49 expect(clippy::cloned_ref_to_slice_refs, reason = "makes tests more readable")
50 )]
51 mod tests {
52 use std::cmp::Reverse;
53 use std::convert::Infallible;
54 use std::ops::Range;
55 use std::sync::Arc;
56
57 use itertools::Itertools as _;
58 use pollster::FutureExt as _;
59 use smallvec::smallvec_inline;
60 use test_case::test_case;
61
62 use super::changed_path::CompositeChangedPathIndex;
63 use super::composite::AsCompositeIndex as _;
64 use super::composite::CommitIndexSegment as _;
65 use super::composite::CompositeCommitIndex;
66 use super::composite::DynCommitIndexSegment;
67 use super::entry::GlobalCommitPosition;
68 use super::entry::SmallGlobalCommitPositionsVec;
69 use super::mutable::MutableCommitIndexSegment;
70 use super::readonly::ReadonlyCommitIndexSegment;
71 use super::*;
72 use crate::backend::ChangeId;
73 use crate::backend::CommitId;
74 use crate::default_index::entry::LocalCommitPosition;
75 use crate::default_index::entry::SmallLocalCommitPositionsVec;
76 use crate::default_index::readonly::FieldLengths;
77 use crate::index::Index as _;
78 use crate::object_id::HexPrefix;
79 use crate::object_id::PrefixResolution;
80 use crate::revset::PARENTS_RANGE_FULL;
81 use crate::tests::TestResult;
82 use crate::tests::new_temp_dir;
83
84 const TEST_FIELD_LENGTHS: FieldLengths = FieldLengths {
85 // TODO: align with commit_id_generator()?
86 commit_id: 3,
87 change_id: 16,
88 };
89
90 /// Generator of unique 16-byte CommitId excluding root id
91 fn commit_id_generator() -> impl FnMut() -> CommitId {
92 let mut iter = (1_u128..).map(|n| CommitId::new(n.to_le_bytes().into()));
93 move || iter.next().unwrap()
94 }
95
96 /// Generator of unique 16-byte ChangeId excluding root id
97 fn change_id_generator() -> impl FnMut() -> ChangeId {
98 let mut iter = (1_u128..).map(|n| ChangeId::new(n.to_le_bytes().into()));
99 move || iter.next().unwrap()
100 }
101
102 fn get_commit_index_stats(commits: &Arc<ReadonlyCommitIndexSegment>) -> IndexStats {
103 let changed_paths = CompositeChangedPathIndex::null();
104 let index = DefaultReadonlyIndex::from_segment(commits.clone(), changed_paths);
105 index.stats()
106 }
107
108 fn common_ancestors(
109 index: &DefaultMutableIndex,
110 set1: &[CommitId],
111 set2: &[CommitId],
112 ) -> Vec<CommitId> {
113 index.common_ancestors(set1, set2).block_on().unwrap()
114 }
115
116 fn is_ancestor(
117 index: &DefaultMutableIndex,
118 ancestor_id: &CommitId,
119 descendant_id: &CommitId,
120 ) -> bool {
121 index
122 .is_ancestor(ancestor_id, descendant_id)
123 .block_on()
124 .unwrap()
125 }
126
127 #[test_case(false; "memory")]
128 #[test_case(true; "file")]
129 fn index_empty(on_disk: bool) -> TestResult {
130 let temp_dir = new_temp_dir();
131 let mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
132 let index_segment: Box<DynCommitIndexSegment> = if on_disk {
133 let saved_index = mutable_segment.save_in(temp_dir.path())?;
134 // Stats are as expected
135 let stats = get_commit_index_stats(&saved_index);
136 assert_eq!(stats.num_commits, 0);
137 assert_eq!(stats.num_heads, 0);
138 assert_eq!(stats.max_generation_number, 0);
139 assert_eq!(stats.num_merges, 0);
140 assert_eq!(stats.num_changes, 0);
141 Box::new(Arc::try_unwrap(saved_index).unwrap())
142 } else {
143 Box::new(mutable_segment)
144 };
145 let index = CompositeCommitIndex::new(index_segment.as_ref());
146 assert_eq!(index.num_commits(), 0);
147
148 // Cannot find any commits
149 assert!(index.entry_by_id(&CommitId::from_hex("000000")).is_none());
150 assert!(index.entry_by_id(&CommitId::from_hex("aaa111")).is_none());
151 assert!(index.entry_by_id(&CommitId::from_hex("ffffff")).is_none());
152 Ok(())
153 }
154
155 #[test_case(false; "memory")]
156 #[test_case(true; "file")]
157 fn index_root_commit(on_disk: bool) -> TestResult {
158 let temp_dir = new_temp_dir();
159 let mut new_change_id = change_id_generator();
160 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
161 let id_0 = CommitId::from_hex("000000");
162 let change_id0 = new_change_id();
163 mutable_segment.add_commit_data(id_0.clone(), change_id0.clone(), &[]);
164 let index_segment: Box<DynCommitIndexSegment> = if on_disk {
165 let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap();
166 // Stats are as expected
167 let stats = get_commit_index_stats(&saved_index);
168 assert_eq!(stats.num_commits, 1);
169 assert_eq!(stats.num_heads, 1);
170 assert_eq!(stats.max_generation_number, 0);
171 assert_eq!(stats.num_merges, 0);
172 assert_eq!(stats.num_changes, 1);
173 Box::new(Arc::try_unwrap(saved_index).unwrap())
174 } else {
175 Box::new(mutable_segment)
176 };
177 let index = CompositeCommitIndex::new(index_segment.as_ref());
178 assert_eq!(index.num_commits(), 1);
179
180 // Can find only the root commit
181 assert_eq!(index.commit_id_to_pos(&id_0), Some(GlobalCommitPosition(0)));
182 assert_eq!(index.commit_id_to_pos(&CommitId::from_hex("aaaaaa")), None);
183 assert_eq!(index.commit_id_to_pos(&CommitId::from_hex("ffffff")), None);
184 // Check properties of root entry
185 let entry = index.entry_by_id(&id_0).unwrap();
186 assert_eq!(entry.position(), GlobalCommitPosition(0));
187 assert_eq!(entry.commit_id(), id_0);
188 assert_eq!(entry.change_id(), change_id0);
189 assert_eq!(entry.generation_number(), 0);
190 assert_eq!(entry.num_parents(), 0);
191 assert_eq!(
192 entry.parent_positions(),
193 SmallGlobalCommitPositionsVec::new()
194 );
195 assert_eq!(entry.parents().len(), 0);
196 Ok(())
197 }
198
199 #[test]
200 #[should_panic(expected = "parent commit is not indexed")]
201 fn index_missing_parent_commit() {
202 let mut new_change_id = change_id_generator();
203 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
204 let id_0 = CommitId::from_hex("000000");
205 let id_1 = CommitId::from_hex("111111");
206 index.add_commit_data(id_1, new_change_id(), &[id_0]);
207 }
208
209 #[test_case(false, false; "full in memory")]
210 #[test_case(false, true; "full on disk")]
211 #[test_case(true, false; "incremental in memory")]
212 #[test_case(true, true; "incremental on disk")]
213 fn index_multiple_commits(incremental: bool, on_disk: bool) -> TestResult {
214 let temp_dir = new_temp_dir();
215 let mut new_change_id = change_id_generator();
216 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
217 // 5
218 // |\
219 // 4 | 3
220 // | |/
221 // 1 2
222 // |/
223 // 0
224 let id_0 = CommitId::from_hex("000000");
225 let change_id0 = new_change_id();
226 let id_1 = CommitId::from_hex("111111");
227 let change_id1 = new_change_id();
228 let id_2 = CommitId::from_hex("222222");
229 let change_id2 = change_id1.clone();
230 mutable_segment.add_commit_data(id_0.clone(), change_id0, &[]);
231 mutable_segment.add_commit_data(id_1.clone(), change_id1.clone(), &[id_0.clone()]);
232 mutable_segment.add_commit_data(id_2.clone(), change_id2.clone(), &[id_0.clone()]);
233
234 // If testing incremental indexing, write the first three commits to one file
235 // now and build the remainder as another segment on top.
236 if incremental {
237 let initial_file = mutable_segment.save_in(temp_dir.path()).unwrap();
238 mutable_segment = MutableCommitIndexSegment::incremental(initial_file);
239 }
240
241 let id_3 = CommitId::from_hex("333333");
242 let change_id3 = new_change_id();
243 let id_4 = CommitId::from_hex("444444");
244 let change_id4 = new_change_id();
245 let id_5 = CommitId::from_hex("555555");
246 let change_id5 = change_id3.clone();
247 mutable_segment.add_commit_data(id_3.clone(), change_id3.clone(), &[id_2.clone()]);
248 mutable_segment.add_commit_data(id_4.clone(), change_id4, &[id_1.clone()]);
249 mutable_segment.add_commit_data(id_5.clone(), change_id5, &[id_4.clone(), id_2.clone()]);
250 let index_segment: Box<DynCommitIndexSegment> = if on_disk {
251 let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap();
252 // Stats are as expected
253 let stats = get_commit_index_stats(&saved_index);
254 assert_eq!(stats.num_commits, 6);
255 assert_eq!(stats.num_heads, 2);
256 assert_eq!(stats.max_generation_number, 3);
257 assert_eq!(stats.num_merges, 1);
258 assert_eq!(stats.num_changes, 4);
259 Box::new(Arc::try_unwrap(saved_index).unwrap())
260 } else {
261 Box::new(mutable_segment)
262 };
263 let index = CompositeCommitIndex::new(index_segment.as_ref());
264 assert_eq!(index.num_commits(), 6);
265
266 // Can find all the commits
267 let entry_0 = index.entry_by_id(&id_0).unwrap();
268 let entry_1 = index.entry_by_id(&id_1).unwrap();
269 let entry_2 = index.entry_by_id(&id_2).unwrap();
270 let entry_3 = index.entry_by_id(&id_3).unwrap();
271 let entry_4 = index.entry_by_id(&id_4).unwrap();
272 let entry_5 = index.entry_by_id(&id_5).unwrap();
273 // Check properties of some entries
274 assert_eq!(entry_0.position(), GlobalCommitPosition(0));
275 assert_eq!(entry_0.commit_id(), id_0);
276 assert_eq!(entry_1.position(), GlobalCommitPosition(1));
277 assert_eq!(entry_1.commit_id(), id_1);
278 assert_eq!(entry_1.change_id(), change_id1);
279 assert_eq!(entry_1.generation_number(), 1);
280 assert_eq!(entry_1.num_parents(), 1);
281 assert_eq!(
282 entry_1.parent_positions(),
283 smallvec_inline![GlobalCommitPosition(0)]
284 );
285 assert_eq!(entry_1.parents().len(), 1);
286 assert_eq!(
287 entry_1.parents().next().unwrap().position(),
288 GlobalCommitPosition(0)
289 );
290 assert_eq!(entry_2.position(), GlobalCommitPosition(2));
291 assert_eq!(entry_2.commit_id(), id_2);
292 assert_eq!(entry_2.change_id(), change_id2);
293 assert_eq!(entry_2.generation_number(), 1);
294 assert_eq!(entry_2.num_parents(), 1);
295 assert_eq!(
296 entry_2.parent_positions(),
297 smallvec_inline![GlobalCommitPosition(0)]
298 );
299 assert_eq!(entry_3.change_id(), change_id3);
300 assert_eq!(entry_3.generation_number(), 2);
301 assert_eq!(
302 entry_3.parent_positions(),
303 smallvec_inline![GlobalCommitPosition(2)]
304 );
305 assert_eq!(entry_4.position(), GlobalCommitPosition(4));
306 assert_eq!(entry_4.generation_number(), 2);
307 assert_eq!(entry_4.num_parents(), 1);
308 assert_eq!(
309 entry_4.parent_positions(),
310 smallvec_inline![GlobalCommitPosition(1)]
311 );
312 assert_eq!(entry_5.generation_number(), 3);
313 assert_eq!(entry_5.num_parents(), 2);
314 assert_eq!(
315 entry_5.parent_positions(),
316 smallvec_inline![GlobalCommitPosition(4), GlobalCommitPosition(2)]
317 );
318 assert_eq!(entry_5.parents().len(), 2);
319 assert_eq!(
320 entry_5.parents().next().unwrap().position(),
321 GlobalCommitPosition(4)
322 );
323 assert_eq!(
324 entry_5.parents().nth(1).unwrap().position(),
325 GlobalCommitPosition(2)
326 );
327 Ok(())
328 }
329
330 #[test_case(false; "in memory")]
331 #[test_case(true; "on disk")]
332 fn index_many_parents(on_disk: bool) -> TestResult {
333 let temp_dir = new_temp_dir();
334 let mut new_change_id = change_id_generator();
335 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
336 // 6
337 // /|\
338 // / | \
339 // / /|\ \
340 // 1 2 3 4 5
341 // \ \|/ /
342 // \ | /
343 // \|/
344 // 0
345 let id_0 = CommitId::from_hex("000000");
346 let id_1 = CommitId::from_hex("111111");
347 let id_2 = CommitId::from_hex("222222");
348 let id_3 = CommitId::from_hex("333333");
349 let id_4 = CommitId::from_hex("444444");
350 let id_5 = CommitId::from_hex("555555");
351 let id_6 = CommitId::from_hex("666666");
352 mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]);
353 mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
354 mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
355 mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[id_0.clone()]);
356 mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[id_0.clone()]);
357 mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[id_0]);
358 mutable_segment.add_commit_data(
359 id_6.clone(),
360 new_change_id(),
361 &[id_1, id_2, id_3, id_4, id_5],
362 );
363 let index_segment: Box<DynCommitIndexSegment> = if on_disk {
364 let saved_index = mutable_segment.save_in(temp_dir.path()).unwrap();
365 // Stats are as expected
366 let stats = get_commit_index_stats(&saved_index);
367 assert_eq!(stats.num_commits, 7);
368 assert_eq!(stats.num_heads, 1);
369 assert_eq!(stats.max_generation_number, 2);
370 assert_eq!(stats.num_merges, 1);
371 Box::new(Arc::try_unwrap(saved_index).unwrap())
372 } else {
373 Box::new(mutable_segment)
374 };
375 let index = CompositeCommitIndex::new(index_segment.as_ref());
376 assert_eq!(index.num_commits(), 7);
377
378 // The octopus merge has the right parents
379 let entry_6 = index.entry_by_id(&id_6).unwrap();
380 assert_eq!(entry_6.commit_id(), id_6.clone());
381 assert_eq!(entry_6.num_parents(), 5);
382 assert_eq!(
383 entry_6.parent_positions(),
384 smallvec_inline![
385 GlobalCommitPosition(1),
386 GlobalCommitPosition(2),
387 GlobalCommitPosition(3),
388 GlobalCommitPosition(4),
389 GlobalCommitPosition(5),
390 ]
391 );
392 assert_eq!(entry_6.generation_number(), 2);
393 Ok(())
394 }
395
396 #[test]
397 fn resolve_commit_id_prefix() -> TestResult {
398 let temp_dir = new_temp_dir();
399 let mut new_change_id = change_id_generator();
400 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
401
402 // Create some commits with different various common prefixes.
403 let id_0 = CommitId::from_hex("000000");
404 let id_1 = CommitId::from_hex("009999");
405 let id_2 = CommitId::from_hex("055488");
406 mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]);
407 mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]);
408 mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]);
409
410 // Write the first three commits to one file and build the remainder on top.
411 let initial_file = mutable_segment.save_in(temp_dir.path())?;
412 mutable_segment = MutableCommitIndexSegment::incremental(initial_file);
413
414 let id_3 = CommitId::from_hex("055444");
415 let id_4 = CommitId::from_hex("055555");
416 let id_5 = CommitId::from_hex("033333");
417 mutable_segment.add_commit_data(id_3, new_change_id(), &[]);
418 mutable_segment.add_commit_data(id_4, new_change_id(), &[]);
419 mutable_segment.add_commit_data(id_5, new_change_id(), &[]);
420
421 let index = mutable_segment.as_composite();
422
423 // Can find commits given the full hex number
424 assert_eq!(
425 index.resolve_commit_id_prefix(&HexPrefix::from_id(&id_0)),
426 PrefixResolution::SingleMatch(id_0)
427 );
428 assert_eq!(
429 index.resolve_commit_id_prefix(&HexPrefix::from_id(&id_1)),
430 PrefixResolution::SingleMatch(id_1)
431 );
432 assert_eq!(
433 index.resolve_commit_id_prefix(&HexPrefix::from_id(&id_2)),
434 PrefixResolution::SingleMatch(id_2)
435 );
436 // Test nonexistent commits
437 assert_eq!(
438 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("ffffff").unwrap()),
439 PrefixResolution::NoMatch
440 );
441 assert_eq!(
442 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("000001").unwrap()),
443 PrefixResolution::NoMatch
444 );
445 // Test ambiguous prefix
446 assert_eq!(
447 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("0").unwrap()),
448 PrefixResolution::AmbiguousMatch
449 );
450 // Test a globally unique prefix in initial part
451 assert_eq!(
452 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("009").unwrap()),
453 PrefixResolution::SingleMatch(CommitId::from_hex("009999"))
454 );
455 // Test a globally unique prefix in incremental part
456 assert_eq!(
457 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("03").unwrap()),
458 PrefixResolution::SingleMatch(CommitId::from_hex("033333"))
459 );
460 // Test a locally unique but globally ambiguous prefix
461 assert_eq!(
462 index.resolve_commit_id_prefix(&HexPrefix::try_from_hex("0554").unwrap()),
463 PrefixResolution::AmbiguousMatch
464 );
465 Ok(())
466 }
467
468 #[test]
469 #[expect(clippy::redundant_clone)] // allow id_n.clone()
470 fn neighbor_commit_ids() -> TestResult {
471 let temp_dir = new_temp_dir();
472 let mut new_change_id = change_id_generator();
473 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
474
475 // Create some commits with different various common prefixes.
476 let id_0 = CommitId::from_hex("000001");
477 let id_1 = CommitId::from_hex("009999");
478 let id_2 = CommitId::from_hex("055488");
479 mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]);
480 mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]);
481 mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]);
482
483 // Write the first three commits to one file and build the remainder on top.
484 let initial_file = mutable_segment.save_in(temp_dir.path())?;
485 mutable_segment = MutableCommitIndexSegment::incremental(initial_file.clone());
486
487 let id_3 = CommitId::from_hex("055444");
488 let id_4 = CommitId::from_hex("055555");
489 let id_5 = CommitId::from_hex("033333");
490 mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[]);
491 mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[]);
492 mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[]);
493
494 // Local lookup in readonly index, commit_id exists.
495 assert_eq!(
496 initial_file.resolve_neighbor_commit_ids(&id_0),
497 (None, Some(id_1.clone())),
498 );
499 assert_eq!(
500 initial_file.resolve_neighbor_commit_ids(&id_1),
501 (Some(id_0.clone()), Some(id_2.clone())),
502 );
503 assert_eq!(
504 initial_file.resolve_neighbor_commit_ids(&id_2),
505 (Some(id_1.clone()), None),
506 );
507
508 // Local lookup in readonly index, commit_id does not exist.
509 assert_eq!(
510 initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("000000")),
511 (None, Some(id_0.clone())),
512 );
513 assert_eq!(
514 initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("000002")),
515 (Some(id_0.clone()), Some(id_1.clone())),
516 );
517 assert_eq!(
518 initial_file.resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
519 (Some(id_2.clone()), None),
520 );
521
522 // Local lookup in mutable index, commit_id exists. id_5 < id_3 < id_4
523 assert_eq!(
524 mutable_segment.resolve_neighbor_commit_ids(&id_5),
525 (None, Some(id_3.clone())),
526 );
527 assert_eq!(
528 mutable_segment.resolve_neighbor_commit_ids(&id_3),
529 (Some(id_5.clone()), Some(id_4.clone())),
530 );
531 assert_eq!(
532 mutable_segment.resolve_neighbor_commit_ids(&id_4),
533 (Some(id_3.clone()), None),
534 );
535
536 // Local lookup in mutable index, commit_id does not exist. id_5 < id_3 < id_4
537 assert_eq!(
538 mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("033332")),
539 (None, Some(id_5.clone())),
540 );
541 assert_eq!(
542 mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("033334")),
543 (Some(id_5.clone()), Some(id_3.clone())),
544 );
545 assert_eq!(
546 mutable_segment.resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
547 (Some(id_4.clone()), None),
548 );
549
550 // Global lookup, commit_id exists. id_0 < id_1 < id_5 < id_3 < id_2 < id_4
551 let composite_index = CompositeCommitIndex::new(&mutable_segment);
552 assert_eq!(
553 composite_index.resolve_neighbor_commit_ids(&id_0),
554 (None, Some(id_1.clone())),
555 );
556 assert_eq!(
557 composite_index.resolve_neighbor_commit_ids(&id_1),
558 (Some(id_0.clone()), Some(id_5.clone())),
559 );
560 assert_eq!(
561 composite_index.resolve_neighbor_commit_ids(&id_5),
562 (Some(id_1.clone()), Some(id_3.clone())),
563 );
564 assert_eq!(
565 composite_index.resolve_neighbor_commit_ids(&id_3),
566 (Some(id_5.clone()), Some(id_2.clone())),
567 );
568 assert_eq!(
569 composite_index.resolve_neighbor_commit_ids(&id_2),
570 (Some(id_3.clone()), Some(id_4.clone())),
571 );
572 assert_eq!(
573 composite_index.resolve_neighbor_commit_ids(&id_4),
574 (Some(id_2.clone()), None),
575 );
576
577 // Global lookup, commit_id doesn't exist. id_0 < id_1 < id_5 < id_3 < id_2 <
578 // id_4
579 assert_eq!(
580 composite_index.resolve_neighbor_commit_ids(&CommitId::from_hex("000000")),
581 (None, Some(id_0.clone())),
582 );
583 assert_eq!(
584 composite_index.resolve_neighbor_commit_ids(&CommitId::from_hex("010000")),
585 (Some(id_1.clone()), Some(id_5.clone())),
586 );
587 assert_eq!(
588 composite_index.resolve_neighbor_commit_ids(&CommitId::from_hex("033334")),
589 (Some(id_5.clone()), Some(id_3.clone())),
590 );
591 assert_eq!(
592 composite_index.resolve_neighbor_commit_ids(&CommitId::from_hex("ffffff")),
593 (Some(id_4.clone()), None),
594 );
595 Ok(())
596 }
597
598 #[test]
599 fn shortest_unique_commit_id_prefix() -> TestResult {
600 let temp_dir = new_temp_dir();
601 let mut new_change_id = change_id_generator();
602 let mut mutable_segment = MutableCommitIndexSegment::full(TEST_FIELD_LENGTHS);
603
604 // Create some commits with different various common prefixes.
605 let id_0 = CommitId::from_hex("000001");
606 let id_1 = CommitId::from_hex("009999");
607 let id_2 = CommitId::from_hex("055488");
608 mutable_segment.add_commit_data(id_0.clone(), new_change_id(), &[]);
609 mutable_segment.add_commit_data(id_1.clone(), new_change_id(), &[]);
610 mutable_segment.add_commit_data(id_2.clone(), new_change_id(), &[]);
611
612 // Write the first three commits to one file and build the remainder on top.
613 let initial_file = mutable_segment.save_in(temp_dir.path())?;
614 mutable_segment = MutableCommitIndexSegment::incremental(initial_file);
615
616 let id_3 = CommitId::from_hex("055444");
617 let id_4 = CommitId::from_hex("055555");
618 let id_5 = CommitId::from_hex("033333");
619 mutable_segment.add_commit_data(id_3.clone(), new_change_id(), &[]);
620 mutable_segment.add_commit_data(id_4.clone(), new_change_id(), &[]);
621 mutable_segment.add_commit_data(id_5.clone(), new_change_id(), &[]);
622
623 let index = mutable_segment.as_composite();
624
625 // Public API: calculate shortest unique prefix len with known commit_id
626 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_0), 3);
627 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_1), 3);
628 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_2), 5);
629 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_3), 5);
630 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_4), 4);
631 assert_eq!(index.shortest_unique_commit_id_prefix_len(&id_5), 2);
632
633 // Public API: calculate shortest unique prefix len with unknown commit_id
634 assert_eq!(
635 index.shortest_unique_commit_id_prefix_len(&CommitId::from_hex("000002")),
636 6
637 );
638 assert_eq!(
639 index.shortest_unique_commit_id_prefix_len(&CommitId::from_hex("010000")),
640 2
641 );
642 assert_eq!(
643 index.shortest_unique_commit_id_prefix_len(&CommitId::from_hex("033334")),
644 6
645 );
646 assert_eq!(
647 index.shortest_unique_commit_id_prefix_len(&CommitId::from_hex("ffffff")),
648 1
649 );
650 Ok(())
651 }
652
653 #[test]
654 fn resolve_change_id_prefix() -> TestResult {
655 let temp_dir = new_temp_dir();
656 let mut new_commit_id = commit_id_generator();
657 let local_positions_vec = |positions: &[u32]| -> SmallLocalCommitPositionsVec {
658 positions.iter().copied().map(LocalCommitPosition).collect()
659 };
660 let index_positions_vec = |positions: &[u32]| -> SmallGlobalCommitPositionsVec {
661 positions
662 .iter()
663 .copied()
664 .map(GlobalCommitPosition)
665 .collect()
666 };
667
668 let id_0 = ChangeId::from_hex("00000001");
669 let id_1 = ChangeId::from_hex("00999999");
670 let id_2 = ChangeId::from_hex("05548888");
671 let id_3 = ChangeId::from_hex("05544444");
672 let id_4 = ChangeId::from_hex("05555555");
673 let id_5 = ChangeId::from_hex("05555333");
674
675 // Create some commits with different various common prefixes.
676 let mut mutable_segment = MutableCommitIndexSegment::full(FieldLengths {
677 commit_id: 16,
678 change_id: 4,
679 });
680 mutable_segment.add_commit_data(new_commit_id(), id_0.clone(), &[]);
681 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
682 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
683 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
684 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
685 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
686
687 // Write these commits to one file and build the remainder on top.
688 let initial_file = mutable_segment.save_in(temp_dir.path())?;
689 mutable_segment = MutableCommitIndexSegment::incremental(initial_file.clone());
690
691 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
692 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
693 mutable_segment.add_commit_data(new_commit_id(), id_4.clone(), &[]);
694 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
695 mutable_segment.add_commit_data(new_commit_id(), id_5.clone(), &[]);
696
697 // Local lookup in readonly index with the full hex digits
698 assert_eq!(
699 initial_file.resolve_change_id_prefix(&HexPrefix::from_id(&id_0)),
700 PrefixResolution::SingleMatch((id_0.clone(), local_positions_vec(&[0])))
701 );
702 assert_eq!(
703 initial_file.resolve_change_id_prefix(&HexPrefix::from_id(&id_1)),
704 PrefixResolution::SingleMatch((id_1.clone(), local_positions_vec(&[1, 3])))
705 );
706 assert_eq!(
707 initial_file.resolve_change_id_prefix(&HexPrefix::from_id(&id_2)),
708 PrefixResolution::SingleMatch((id_2.clone(), local_positions_vec(&[2, 4, 5])))
709 );
710
711 // Local lookup in mutable index with the full hex digits
712 assert_eq!(
713 mutable_segment.resolve_change_id_prefix(&HexPrefix::from_id(&id_1)),
714 PrefixResolution::SingleMatch((id_1.clone(), local_positions_vec(&[3])))
715 );
716 assert_eq!(
717 mutable_segment.resolve_change_id_prefix(&HexPrefix::from_id(&id_3)),
718 PrefixResolution::SingleMatch((id_3.clone(), local_positions_vec(&[0, 1])))
719 );
720 assert_eq!(
721 mutable_segment.resolve_change_id_prefix(&HexPrefix::from_id(&id_4)),
722 PrefixResolution::SingleMatch((id_4.clone(), local_positions_vec(&[2])))
723 );
724 assert_eq!(
725 mutable_segment.resolve_change_id_prefix(&HexPrefix::from_id(&id_5)),
726 PrefixResolution::SingleMatch((id_5.clone(), local_positions_vec(&[4])))
727 );
728
729 // Local lookup with locally unknown prefix
730 assert_eq!(
731 initial_file.resolve_change_id_prefix(&HexPrefix::try_from_hex("0555").unwrap()),
732 PrefixResolution::NoMatch
733 );
734 assert_eq!(
735 mutable_segment.resolve_change_id_prefix(&HexPrefix::try_from_hex("000").unwrap()),
736 PrefixResolution::NoMatch
737 );
738
739 // Local lookup with locally unique prefix
740 assert_eq!(
741 initial_file.resolve_change_id_prefix(&HexPrefix::try_from_hex("0554").unwrap()),
742 PrefixResolution::SingleMatch((id_2.clone(), local_positions_vec(&[2, 4, 5])))
743 );
744 assert_eq!(
745 mutable_segment.resolve_change_id_prefix(&HexPrefix::try_from_hex("0554").unwrap()),
746 PrefixResolution::SingleMatch((id_3.clone(), local_positions_vec(&[0, 1])))
747 );
748
749 // Local lookup with locally ambiguous prefix
750 assert_eq!(
751 initial_file.resolve_change_id_prefix(&HexPrefix::try_from_hex("00").unwrap()),
752 PrefixResolution::AmbiguousMatch
753 );
754 assert_eq!(
755 mutable_segment.resolve_change_id_prefix(&HexPrefix::try_from_hex("05555").unwrap()),
756 PrefixResolution::AmbiguousMatch
757 );
758
759 let index = mutable_segment.as_composite();
760
761 // Global lookup with the full hex digits
762 assert_eq!(
763 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_0)),
764 PrefixResolution::SingleMatch((id_0.clone(), index_positions_vec(&[0])))
765 );
766 assert_eq!(
767 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_1)),
768 PrefixResolution::SingleMatch((id_1.clone(), index_positions_vec(&[9, 3, 1])))
769 );
770 assert_eq!(
771 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_2)),
772 PrefixResolution::SingleMatch((id_2.clone(), index_positions_vec(&[5, 4, 2])))
773 );
774 assert_eq!(
775 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_3)),
776 PrefixResolution::SingleMatch((id_3.clone(), index_positions_vec(&[7, 6])))
777 );
778 assert_eq!(
779 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_4)),
780 PrefixResolution::SingleMatch((id_4.clone(), index_positions_vec(&[8])))
781 );
782 assert_eq!(
783 index.resolve_change_id_prefix(&HexPrefix::from_id(&id_5)),
784 PrefixResolution::SingleMatch((id_5.clone(), index_positions_vec(&[10])))
785 );
786
787 // Global lookup with unknown prefix
788 assert_eq!(
789 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("ffffffff").unwrap()),
790 PrefixResolution::NoMatch
791 );
792 assert_eq!(
793 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("00000002").unwrap()),
794 PrefixResolution::NoMatch
795 );
796
797 // Global lookup with globally unique prefix
798 assert_eq!(
799 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("000").unwrap()),
800 PrefixResolution::SingleMatch((id_0.clone(), index_positions_vec(&[0])))
801 );
802 assert_eq!(
803 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("055553").unwrap()),
804 PrefixResolution::SingleMatch((id_5.clone(), index_positions_vec(&[10])))
805 );
806
807 // Global lookup with globally unique prefix stored in both parts
808 assert_eq!(
809 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("009").unwrap()),
810 PrefixResolution::SingleMatch((id_1.clone(), index_positions_vec(&[9, 3, 1])))
811 );
812
813 // Global lookup with locally ambiguous prefix
814 assert_eq!(
815 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("00").unwrap()),
816 PrefixResolution::AmbiguousMatch
817 );
818 assert_eq!(
819 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("05555").unwrap()),
820 PrefixResolution::AmbiguousMatch
821 );
822
823 // Global lookup with locally unique but globally ambiguous prefix
824 assert_eq!(
825 index.resolve_change_id_prefix(&HexPrefix::try_from_hex("0554").unwrap()),
826 PrefixResolution::AmbiguousMatch
827 );
828 Ok(())
829 }
830
831 #[test]
832 fn neighbor_change_ids() -> TestResult {
833 let temp_dir = new_temp_dir();
834 let mut new_commit_id = commit_id_generator();
835
836 let id_0 = ChangeId::from_hex("00000001");
837 let id_1 = ChangeId::from_hex("00999999");
838 let id_2 = ChangeId::from_hex("05548888");
839 let id_3 = ChangeId::from_hex("05544444");
840 let id_4 = ChangeId::from_hex("05555555");
841 let id_5 = ChangeId::from_hex("05555333");
842
843 // Create some commits with different various common prefixes.
844 let mut mutable_segment = MutableCommitIndexSegment::full(FieldLengths {
845 commit_id: 16,
846 change_id: 4,
847 });
848 mutable_segment.add_commit_data(new_commit_id(), id_0.clone(), &[]);
849 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
850 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
851 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
852 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
853 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
854
855 // Write these commits to one file and build the remainder on top.
856 let initial_file = mutable_segment.save_in(temp_dir.path())?;
857 mutable_segment = MutableCommitIndexSegment::incremental(initial_file.clone());
858
859 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
860 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
861 mutable_segment.add_commit_data(new_commit_id(), id_4.clone(), &[]);
862 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
863 mutable_segment.add_commit_data(new_commit_id(), id_5.clone(), &[]);
864
865 // Local lookup in readonly index, change_id exists.
866 assert_eq!(
867 initial_file.resolve_neighbor_change_ids(&id_0),
868 (None, Some(id_1.clone())),
869 );
870 assert_eq!(
871 initial_file.resolve_neighbor_change_ids(&id_1),
872 (Some(id_0.clone()), Some(id_2.clone())),
873 );
874 assert_eq!(
875 initial_file.resolve_neighbor_change_ids(&id_2),
876 (Some(id_1.clone()), None),
877 );
878
879 // Local lookup in readonly index, change_id does not exist.
880 assert_eq!(
881 initial_file.resolve_neighbor_change_ids(&ChangeId::from_hex("00000000")),
882 (None, Some(id_0.clone())),
883 );
884 assert_eq!(
885 initial_file.resolve_neighbor_change_ids(&ChangeId::from_hex("00000002")),
886 (Some(id_0.clone()), Some(id_1.clone())),
887 );
888 assert_eq!(
889 initial_file.resolve_neighbor_change_ids(&ChangeId::from_hex("ffffffff")),
890 (Some(id_2.clone()), None),
891 );
892
893 // Local lookup in mutable index, change_id exists.
894 // id_1 < id_3 < id_5 < id_4
895 assert_eq!(
896 mutable_segment.resolve_neighbor_change_ids(&id_1),
897 (None, Some(id_3.clone())),
898 );
899 assert_eq!(
900 mutable_segment.resolve_neighbor_change_ids(&id_3),
901 (Some(id_1.clone()), Some(id_5.clone())),
902 );
903 assert_eq!(
904 mutable_segment.resolve_neighbor_change_ids(&id_5),
905 (Some(id_3.clone()), Some(id_4.clone())),
906 );
907 assert_eq!(
908 mutable_segment.resolve_neighbor_change_ids(&id_4),
909 (Some(id_5.clone()), None),
910 );
911
912 // Local lookup in mutable index, change_id does not exist.
913 // id_1 < id_3 < id_5 < id_4
914 assert_eq!(
915 mutable_segment.resolve_neighbor_change_ids(&ChangeId::from_hex("00999998")),
916 (None, Some(id_1.clone())),
917 );
918 assert_eq!(
919 mutable_segment.resolve_neighbor_change_ids(&ChangeId::from_hex("01000000")),
920 (Some(id_1.clone()), Some(id_3.clone())),
921 );
922 assert_eq!(
923 mutable_segment.resolve_neighbor_change_ids(&ChangeId::from_hex("05555332")),
924 (Some(id_3.clone()), Some(id_5.clone())),
925 );
926 assert_eq!(
927 mutable_segment.resolve_neighbor_change_ids(&ChangeId::from_hex("ffffffff")),
928 (Some(id_4.clone()), None),
929 );
930
931 let index = mutable_segment.as_composite();
932
933 // Global lookup, change_id exists.
934 // id_0 < id_1 < id_3 < id_2 < id_5 < id_4
935 assert_eq!(
936 index.resolve_neighbor_change_ids(&id_0),
937 (None, Some(id_1.clone())),
938 );
939 assert_eq!(
940 index.resolve_neighbor_change_ids(&id_1),
941 (Some(id_0.clone()), Some(id_3.clone())),
942 );
943 assert_eq!(
944 index.resolve_neighbor_change_ids(&id_3),
945 (Some(id_1.clone()), Some(id_2.clone())),
946 );
947 assert_eq!(
948 index.resolve_neighbor_change_ids(&id_2),
949 (Some(id_3.clone()), Some(id_5.clone())),
950 );
951 assert_eq!(
952 index.resolve_neighbor_change_ids(&id_5),
953 (Some(id_2.clone()), Some(id_4.clone())),
954 );
955 assert_eq!(
956 index.resolve_neighbor_change_ids(&id_4),
957 (Some(id_5.clone()), None),
958 );
959
960 // Global lookup, change_id doesn't exist.
961 // id_0 < id_1 < id_3 < id_2 < id_5 < id_4
962 assert_eq!(
963 index.resolve_neighbor_change_ids(&ChangeId::from_hex("00000000")),
964 (None, Some(id_0.clone())),
965 );
966 assert_eq!(
967 index.resolve_neighbor_change_ids(&ChangeId::from_hex("01000000")),
968 (Some(id_1.clone()), Some(id_3.clone())),
969 );
970 assert_eq!(
971 index.resolve_neighbor_change_ids(&ChangeId::from_hex("05544555")),
972 (Some(id_3.clone()), Some(id_2.clone())),
973 );
974 assert_eq!(
975 index.resolve_neighbor_change_ids(&ChangeId::from_hex("ffffffff")),
976 (Some(id_4.clone()), None),
977 );
978 Ok(())
979 }
980
981 #[test]
982 fn shortest_unique_change_id_prefix() -> TestResult {
983 let temp_dir = new_temp_dir();
984 let mut new_commit_id = commit_id_generator();
985
986 let id_0 = ChangeId::from_hex("00000001");
987 let id_1 = ChangeId::from_hex("00999999");
988 let id_2 = ChangeId::from_hex("05548888");
989 let id_3 = ChangeId::from_hex("05544444");
990 let id_4 = ChangeId::from_hex("05555555");
991 let id_5 = ChangeId::from_hex("05555333");
992
993 // Create some commits with different various common prefixes.
994 let mut mutable_segment = MutableCommitIndexSegment::full(FieldLengths {
995 commit_id: 16,
996 change_id: 4,
997 });
998 mutable_segment.add_commit_data(new_commit_id(), id_0.clone(), &[]);
999 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
1000 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
1001 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
1002 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
1003 mutable_segment.add_commit_data(new_commit_id(), id_2.clone(), &[]);
1004
1005 // Write these commits to one file and build the remainder on top.
1006 let initial_file = mutable_segment.save_in(temp_dir.path())?;
1007 mutable_segment = MutableCommitIndexSegment::incremental(initial_file.clone());
1008
1009 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
1010 mutable_segment.add_commit_data(new_commit_id(), id_3.clone(), &[]);
1011 mutable_segment.add_commit_data(new_commit_id(), id_4.clone(), &[]);
1012 mutable_segment.add_commit_data(new_commit_id(), id_1.clone(), &[]);
1013 mutable_segment.add_commit_data(new_commit_id(), id_5.clone(), &[]);
1014
1015 let index = mutable_segment.as_composite();
1016
1017 // Calculate shortest unique prefix len with known change_id
1018 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_0), 3);
1019 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_1), 3);
1020 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_2), 5);
1021 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_3), 5);
1022 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_4), 6);
1023 assert_eq!(index.shortest_unique_change_id_prefix_len(&id_5), 6);
1024
1025 // Calculate shortest unique prefix len with unknown change_id
1026 assert_eq!(
1027 index.shortest_unique_change_id_prefix_len(&ChangeId::from_hex("00000002")),
1028 8
1029 );
1030 assert_eq!(
1031 index.shortest_unique_change_id_prefix_len(&ChangeId::from_hex("01000000")),
1032 2
1033 );
1034 assert_eq!(
1035 index.shortest_unique_change_id_prefix_len(&ChangeId::from_hex("05555344")),
1036 7
1037 );
1038 assert_eq!(
1039 index.shortest_unique_change_id_prefix_len(&ChangeId::from_hex("ffffffff")),
1040 1
1041 );
1042 Ok(())
1043 }
1044
1045 #[test]
1046 fn test_is_ancestor() -> TestResult {
1047 let mut new_change_id = change_id_generator();
1048 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1049 // 5
1050 // |\
1051 // 4 | 3
1052 // | |/
1053 // 1 2
1054 // |/
1055 // 0
1056 let id_0 = CommitId::from_hex("000000");
1057 let id_1 = CommitId::from_hex("111111");
1058 let id_2 = CommitId::from_hex("222222");
1059 let id_3 = CommitId::from_hex("333333");
1060 let id_4 = CommitId::from_hex("444444");
1061 let id_5 = CommitId::from_hex("555555");
1062 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1063 index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
1064 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
1065 index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]);
1066 index.add_commit_data(id_4.clone(), new_change_id(), &[id_1.clone()]);
1067 index.add_commit_data(id_5.clone(), new_change_id(), &[id_4.clone(), id_2.clone()]);
1068
1069 assert!(is_ancestor(&index, &id_0, &id_0));
1070 assert!(is_ancestor(&index, &id_0, &id_1));
1071 assert!(is_ancestor(&index, &id_2, &id_3));
1072 assert!(is_ancestor(&index, &id_2, &id_5));
1073 assert!(is_ancestor(&index, &id_1, &id_5));
1074 assert!(is_ancestor(&index, &id_0, &id_5));
1075 assert!(!is_ancestor(&index, &id_1, &id_0));
1076 assert!(!is_ancestor(&index, &id_5, &id_3));
1077 assert!(!is_ancestor(&index, &id_3, &id_5));
1078 assert!(!is_ancestor(&index, &id_2, &id_4));
1079 assert!(!is_ancestor(&index, &id_4, &id_2));
1080 Ok(())
1081 }
1082
1083 #[test]
1084 fn test_common_ancestors() -> TestResult {
1085 let mut new_change_id = change_id_generator();
1086 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1087 // 5
1088 // |\
1089 // 4 |
1090 // | |
1091 // 1 2 3
1092 // | |/
1093 // |/
1094 // 0
1095 let id_0 = CommitId::from_hex("000000");
1096 let id_1 = CommitId::from_hex("111111");
1097 let id_2 = CommitId::from_hex("222222");
1098 let id_3 = CommitId::from_hex("333333");
1099 let id_4 = CommitId::from_hex("444444");
1100 let id_5 = CommitId::from_hex("555555");
1101 let id_6 = CommitId::from_hex("666666");
1102 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1103 index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
1104 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
1105 index.add_commit_data(id_3.clone(), new_change_id(), &[id_0.clone()]);
1106 index.add_commit_data(id_4.clone(), new_change_id(), &[id_1.clone()]);
1107 index.add_commit_data(id_5.clone(), new_change_id(), &[id_4.clone(), id_2.clone()]);
1108 index.add_commit_data(id_6.clone(), new_change_id(), &[id_4.clone()]);
1109
1110 assert_eq!(
1111 common_ancestors(&index, &[id_0.clone()], &[id_0.clone()]),
1112 vec![id_0.clone()]
1113 );
1114 assert_eq!(
1115 common_ancestors(&index, &[id_5.clone()], &[id_5.clone()]),
1116 vec![id_5.clone()]
1117 );
1118 assert_eq!(
1119 common_ancestors(&index, &[id_1.clone()], &[id_2.clone()]),
1120 vec![id_0.clone()]
1121 );
1122 assert_eq!(
1123 common_ancestors(&index, &[id_2.clone()], &[id_1.clone()]),
1124 vec![id_0.clone()]
1125 );
1126 assert_eq!(
1127 common_ancestors(&index, &[id_1.clone()], &[id_4.clone()]),
1128 vec![id_1.clone()]
1129 );
1130 assert_eq!(
1131 common_ancestors(&index, &[id_4.clone()], &[id_1.clone()]),
1132 vec![id_1.clone()]
1133 );
1134 assert_eq!(
1135 common_ancestors(&index, &[id_3.clone()], &[id_5.clone()]),
1136 vec![id_0.clone()]
1137 );
1138 assert_eq!(
1139 common_ancestors(&index, &[id_5.clone()], &[id_3.clone()]),
1140 vec![id_0.clone()]
1141 );
1142 assert_eq!(
1143 common_ancestors(&index, &[id_2.clone()], &[id_6.clone()]),
1144 vec![id_0.clone()]
1145 );
1146
1147 // With multiple commits in an input set
1148 assert_eq!(
1149 common_ancestors(&index, &[id_0.clone(), id_1.clone()], &[id_0.clone()]),
1150 vec![id_0.clone()]
1151 );
1152 assert_eq!(
1153 common_ancestors(&index, &[id_0.clone(), id_1.clone()], &[id_1.clone()]),
1154 vec![id_1.clone()]
1155 );
1156 assert_eq!(
1157 common_ancestors(&index, &[id_1.clone(), id_2.clone()], &[id_1.clone()]),
1158 vec![id_1.clone()]
1159 );
1160 assert_eq!(
1161 common_ancestors(&index, &[id_1.clone(), id_2.clone()], &[id_4]),
1162 vec![id_1.clone()]
1163 );
1164 assert_eq!(
1165 common_ancestors(&index, &[id_5.clone(), id_6.clone()], &[id_2.clone()]),
1166 &[id_2.clone()]
1167 );
1168 // Both 1 and 2 are returned since (5) expands to (2, 4), which expands
1169 // to (1,2) and matches the (1,2) of the first input set.
1170 assert_eq!(
1171 common_ancestors(&index, &[id_1.clone(), id_2.clone()], &[id_5]),
1172 vec![id_2.clone(), id_1.clone()]
1173 );
1174 assert_eq!(common_ancestors(&index, &[id_1, id_2], &[id_3]), vec![id_0]);
1175 Ok(())
1176 }
1177
1178 #[test]
1179 fn test_common_ancestors_criss_cross() -> TestResult {
1180 let mut new_change_id = change_id_generator();
1181 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1182 // 3 4
1183 // |X|
1184 // 1 2
1185 // |/
1186 // 0
1187 let id_0 = CommitId::from_hex("000000");
1188 let id_1 = CommitId::from_hex("111111");
1189 let id_2 = CommitId::from_hex("222222");
1190 let id_3 = CommitId::from_hex("333333");
1191 let id_4 = CommitId::from_hex("444444");
1192 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1193 index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
1194 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0]);
1195 index.add_commit_data(id_3.clone(), new_change_id(), &[id_1.clone(), id_2.clone()]);
1196 index.add_commit_data(id_4.clone(), new_change_id(), &[id_1.clone(), id_2.clone()]);
1197
1198 let mut common_ancestors = common_ancestors(&index, &[id_3], &[id_4]);
1199 common_ancestors.sort();
1200 assert_eq!(common_ancestors, vec![id_1, id_2]);
1201 Ok(())
1202 }
1203
1204 #[test]
1205 fn test_common_ancestors_merge_with_ancestor() -> TestResult {
1206 let mut new_change_id = change_id_generator();
1207 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1208 // 4 5
1209 // |\ /|
1210 // 1 2 3
1211 // \|/
1212 // 0
1213 let id_0 = CommitId::from_hex("000000");
1214 let id_1 = CommitId::from_hex("111111");
1215 let id_2 = CommitId::from_hex("222222");
1216 let id_3 = CommitId::from_hex("333333");
1217 let id_4 = CommitId::from_hex("444444");
1218 let id_5 = CommitId::from_hex("555555");
1219 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1220 index.add_commit_data(id_1, new_change_id(), &[id_0.clone()]);
1221 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
1222 index.add_commit_data(id_3, new_change_id(), &[id_0.clone()]);
1223 index.add_commit_data(id_4.clone(), new_change_id(), &[id_0.clone(), id_2.clone()]);
1224 index.add_commit_data(id_5.clone(), new_change_id(), &[id_0, id_2.clone()]);
1225
1226 let mut common_ancestors = common_ancestors(&index, &[id_4], &[id_5]);
1227 common_ancestors.sort();
1228 assert_eq!(common_ancestors, vec![id_2]);
1229 Ok(())
1230 }
1231
1232 #[test]
1233 fn test_heads() -> TestResult {
1234 let mut new_change_id = change_id_generator();
1235 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1236 // 5
1237 // |\
1238 // 4 | 3
1239 // | |/
1240 // 1 2
1241 // |/
1242 // 0
1243 let id_0 = CommitId::from_hex("000000");
1244 let id_1 = CommitId::from_hex("111111");
1245 let id_2 = CommitId::from_hex("222222");
1246 let id_3 = CommitId::from_hex("333333");
1247 let id_4 = CommitId::from_hex("444444");
1248 let id_5 = CommitId::from_hex("555555");
1249 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1250 index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
1251 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
1252 index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]);
1253 index.add_commit_data(id_4.clone(), new_change_id(), &[id_1.clone()]);
1254 index.add_commit_data(id_5.clone(), new_change_id(), &[id_4.clone(), id_2.clone()]);
1255
1256 // Empty input
1257 assert!(index.heads(&mut [].iter()).block_on()?.is_empty());
1258 // Single head
1259 assert_eq!(
1260 index.heads(&mut [id_4.clone()].iter()).block_on()?,
1261 vec![id_4.clone()]
1262 );
1263 // Single head and parent
1264 assert_eq!(
1265 index.heads(&mut [id_4.clone(), id_1].iter()).block_on()?,
1266 vec![id_4.clone()]
1267 );
1268 // Single head and grand-parent
1269 assert_eq!(
1270 index.heads(&mut [id_4.clone(), id_0].iter()).block_on()?,
1271 vec![id_4.clone()]
1272 );
1273 // Multiple heads
1274 assert_eq!(
1275 index
1276 .heads(&mut [id_3.clone(), id_4.clone()].iter())
1277 .block_on()?,
1278 vec![id_4.clone(), id_3.clone()]
1279 );
1280 // Duplicated inputs
1281 assert_eq!(
1282 index
1283 .heads(&mut [id_4.clone(), id_3.clone(), id_4.clone()].iter())
1284 .block_on()?,
1285 vec![id_4.clone(), id_3.clone()]
1286 );
1287 // Merge commit and ancestors
1288 assert_eq!(
1289 index.heads(&mut [id_5.clone(), id_2].iter()).block_on()?,
1290 vec![id_5.clone()]
1291 );
1292 // Merge commit and other commit
1293 assert_eq!(
1294 index
1295 .heads(&mut [id_5.clone(), id_3.clone()].iter())
1296 .block_on()?,
1297 vec![id_5.clone(), id_3.clone()]
1298 );
1299
1300 assert_eq!(
1301 index.all_heads_for_gc()?.collect_vec(),
1302 vec![id_3.clone(), id_5.clone()]
1303 );
1304 Ok(())
1305 }
1306
1307 #[test]
1308 fn test_heads_range_with_filter() -> TestResult {
1309 let mut new_change_id = change_id_generator();
1310 let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS);
1311 // 5
1312 // |\
1313 // 4 | 3
1314 // | |/
1315 // 1 2
1316 // |/
1317 // 0
1318 let id_0 = CommitId::from_hex("000000");
1319 let id_1 = CommitId::from_hex("111111");
1320 let id_2 = CommitId::from_hex("222222");
1321 let id_3 = CommitId::from_hex("333333");
1322 let id_4 = CommitId::from_hex("444444");
1323 let id_5 = CommitId::from_hex("555555");
1324 index.add_commit_data(id_0.clone(), new_change_id(), &[]);
1325 index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]);
1326 index.add_commit_data(id_2.clone(), new_change_id(), &[id_0.clone()]);
1327 index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]);
1328 index.add_commit_data(id_4.clone(), new_change_id(), &[id_1.clone()]);
1329 index.add_commit_data(id_5.clone(), new_change_id(), &[id_4.clone(), id_2.clone()]);
1330
1331 // Helper function to convert commit IDs to/from index positions and call
1332 // `heads_from_range_and_filter`.
1333 let heads_range = |roots: &[&CommitId],
1334 heads: &[&CommitId],
1335 parents_range: &Range<u32>,
1336 filter: &dyn Fn(CommitId) -> bool|
1337 -> Vec<CommitId> {
1338 let roots = roots
1339 .iter()
1340 .map(|id| index.as_composite().commits().commit_id_to_pos(id).unwrap())
1341 .sorted_by_key(|&pos| Reverse(pos))
1342 .collect_vec();
1343 let heads = heads
1344 .iter()
1345 .map(|id| index.as_composite().commits().commit_id_to_pos(id).unwrap())
1346 .sorted_by_key(|&pos| Reverse(pos))
1347 .collect_vec();
1348 index
1349 .as_composite()
1350 .commits()
1351 .heads_from_range_and_filter::<Infallible>(roots, heads, parents_range, |pos| {
1352 Ok(filter(
1353 index.as_composite().commits().entry_by_pos(pos).commit_id(),
1354 ))
1355 })
1356 .unwrap()
1357 .into_iter()
1358 .map(|pos| index.as_composite().commits().entry_by_pos(pos).commit_id())
1359 .collect_vec()
1360 };
1361
1362 // heads(::none())
1363 assert!(heads_range(&[], &[], &PARENTS_RANGE_FULL, &|_| true).is_empty());
1364 // heads(all())
1365 assert_eq!(
1366 heads_range(&[], &[&id_5, &id_3], &PARENTS_RANGE_FULL, &|_| true),
1367 vec![id_5.clone(), id_3.clone()]
1368 );
1369 // heads(~5)
1370 assert_eq!(
1371 heads_range(&[], &[&id_5, &id_3], &PARENTS_RANGE_FULL, &|id| id != id_5),
1372 vec![id_4.clone(), id_3.clone()]
1373 );
1374 // heads(5..)
1375 assert_eq!(
1376 heads_range(&[&id_5], &[&id_5, &id_3], &PARENTS_RANGE_FULL, &|_| true),
1377 vec![id_3.clone()]
1378 );
1379 // heads(5.. ~ 3)
1380 assert!(
1381 heads_range(&[&id_5], &[&id_5, &id_3], &PARENTS_RANGE_FULL, &|id| id
1382 != id_3)
1383 .is_empty()
1384 );
1385 // heads(2..4)
1386 assert_eq!(
1387 heads_range(&[&id_2], &[&id_4], &PARENTS_RANGE_FULL, &|_| true),
1388 vec![id_4.clone()]
1389 );
1390 // heads(2..4 ~ 4)
1391 assert_eq!(
1392 heads_range(&[&id_2], &[&id_4], &PARENTS_RANGE_FULL, &|id| id != id_4),
1393 vec![id_1.clone()]
1394 );
1395 // heads((3 | 1).. ~ 5)
1396 assert_eq!(
1397 heads_range(
1398 &[&id_3, &id_1],
1399 &[&id_5, &id_3],
1400 &PARENTS_RANGE_FULL,
1401 &|id| id != id_5
1402 ),
1403 vec![id_4.clone()]
1404 );
1405 // heads(::(5 | 4) ~ 5)
1406 assert_eq!(
1407 heads_range(&[], &[&id_5, &id_4], &PARENTS_RANGE_FULL, &|id| id != id_5),
1408 vec![id_4.clone(), id_2.clone()]
1409 );
1410 // heads(::(5 | 5))
1411 assert_eq!(
1412 heads_range(&[], &[&id_5, &id_5], &PARENTS_RANGE_FULL, &|_| true),
1413 vec![id_5.clone()]
1414 );
1415 // heads(::5 ~ 5)
1416 assert_eq!(
1417 heads_range(&[], &[&id_5], &PARENTS_RANGE_FULL, &|id| id != id_5),
1418 vec![id_4.clone(), id_2.clone()]
1419 );
1420 // heads(first_ancestors(5) ~ 5)
1421 assert_eq!(
1422 heads_range(&[], &[&id_5], &(0..1), &|id| id != id_5),
1423 vec![id_4.clone()]
1424 );
1425 // heads(ancestors(5, parents_range=1..2) ~ 5)
1426 assert_eq!(
1427 heads_range(&[], &[&id_5], &(1..2), &|id| id != id_5),
1428 vec![id_2.clone()]
1429 );
1430 Ok(())
1431 }
1432 }