From e0206a82f242cc2911a4cb982b5df3392a00690a Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 8 Dec 2023 14:07:45 +0900 Subject: [PATCH] index: extract merge_in() function that works on segment types Prepares for splitting MutableIndexImpl into segment and index wrapper types. --- lib/src/default_index_store.rs | 65 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index 8429d0f62..7b56ba9e7 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -506,6 +506,39 @@ impl MutableIndexImpl { } } + fn merge_in(&mut self, other: Arc) { + let mut maybe_own_ancestor = self.parent_file.clone(); + let mut maybe_other_ancestor = Some(other); + let mut files_to_add = vec![]; + loop { + if maybe_other_ancestor.is_none() { + break; + } + let other_ancestor = maybe_other_ancestor.as_ref().unwrap(); + if maybe_own_ancestor.is_none() { + files_to_add.push(other_ancestor.clone()); + maybe_other_ancestor = other_ancestor.parent_file.clone(); + continue; + } + let own_ancestor = maybe_own_ancestor.as_ref().unwrap(); + if own_ancestor.name == other_ancestor.name { + break; + } + if own_ancestor.as_composite().num_commits() + < other_ancestor.as_composite().num_commits() + { + files_to_add.push(other_ancestor.clone()); + maybe_other_ancestor = other_ancestor.parent_file.clone(); + } else { + maybe_own_ancestor = own_ancestor.parent_file.clone(); + } + } + + for file in files_to_add.iter().rev() { + self.add_commits_from(file.as_ref()); + } + } + fn serialize(self) -> Vec { assert_eq!(self.graph.len(), self.lookup.len()); @@ -708,37 +741,7 @@ impl MutableIndex for MutableIndexImpl { .as_any() .downcast_ref::() .expect("index to merge in must be a DefaultReadonlyIndex"); - - let mut maybe_own_ancestor = self.parent_file.clone(); - let mut maybe_other_ancestor = Some(other.0.clone()); - let mut files_to_add = vec![]; - loop { - if maybe_other_ancestor.is_none() { - break; - } - let other_ancestor = maybe_other_ancestor.as_ref().unwrap(); - if maybe_own_ancestor.is_none() { - files_to_add.push(other_ancestor.clone()); - maybe_other_ancestor = other_ancestor.parent_file.clone(); - continue; - } - let own_ancestor = maybe_own_ancestor.as_ref().unwrap(); - if own_ancestor.name == other_ancestor.name { - break; - } - if own_ancestor.as_composite().num_commits() - < other_ancestor.as_composite().num_commits() - { - files_to_add.push(other_ancestor.clone()); - maybe_other_ancestor = other_ancestor.parent_file.clone(); - } else { - maybe_own_ancestor = own_ancestor.parent_file.clone(); - } - } - - for file in files_to_add.iter().rev() { - self.add_commits_from(file.as_ref()); - } + self.merge_in(other.0.clone()); } }