dag: make parents_and_head include roots

Summary:
An alternative to D29363808 (e396cab669). The benefit is that parents_and_head is used by
both the client and the server. So we don't need to duplicate D29363808 (e396cab669) in
Mononoke code.

Reviewed By: andll

Differential Revision: D29365079

fbshipit-source-id: bca60ba2b3df477929d8e72b2363e5a0f744b35d
This commit is contained in:
Jun Wu 2021-06-24 17:52:57 -07:00 committed by Facebook GitHub Bot
parent ac63c8df22
commit ed7c8931fa
2 changed files with 24 additions and 9 deletions

View File

@ -68,8 +68,9 @@ impl PreparedFlatSegments {
self.segments.extend(rhs.segments);
}
/// Return list of all (unique) parents + head of this flat segments list.
/// Return set of all (unique) parents + head + roots of flat segments.
pub fn parents_and_head(&self) -> BTreeSet<Id> {
// Parents
let mut s: BTreeSet<Id> = self
.segments
.iter()
@ -77,9 +78,30 @@ impl PreparedFlatSegments {
.flatten()
.copied()
.collect();
// Head
if let Some(h) = self.head_id() {
s.insert(h);
}
// Roots
let id_set: BTreeSet<(Id, Id)> = self.segments.iter().map(|s| (s.low, s.high)).collect();
let contains = |id: Id| -> bool {
for &(low, high) in id_set.range(..=(id, Id::MAX)).rev() {
if id >= low && id <= high {
return true;
}
if id < low {
break;
}
}
false
};
for seg in &self.segments {
let pids: Vec<Id> = seg.parents.iter().copied().collect();
if pids.iter().all(|&p| !contains(p)) {
// seg.low is a root.
s.insert(seg.low);
}
}
s
}

View File

@ -688,15 +688,8 @@ where
let new_ancestors = self.dag.ancestors(new.into())?;
let missing_set = new_ancestors.difference(&old_ancestors);
let roots = self.dag.roots(missing_set.clone())?;
let flat_segments = self.dag.idset_to_flat_segments(missing_set)?;
let mut ids: Vec<_> = flat_segments
.parents_and_head()
.into_iter()
.chain(roots.iter())
.collect();
ids.sort_unstable();
ids.dedup();
let ids: Vec<_> = flat_segments.parents_and_head().into_iter().collect();
let idmap: HashMap<Id, VertexName> = {
tracing::debug!("pull: {} vertexes in idmap", ids.len());