|
|
@ -13,7 +13,7 @@ use std::collections::HashMap; |
|
|
|
use std::collections::HashSet; |
|
|
|
use std::collections::HashSet; |
|
|
|
use std::fmt; |
|
|
|
use std::fmt; |
|
|
|
|
|
|
|
|
|
|
|
use fastbloom_rs::{BloomFilter as Filter, Membership}; |
|
|
|
use bloomfilter::Bloom; |
|
|
|
use zeroize::Zeroize; |
|
|
|
use zeroize::Zeroize; |
|
|
|
|
|
|
|
|
|
|
|
use crate::errors::*; |
|
|
|
use crate::errors::*; |
|
|
@ -51,6 +51,7 @@ impl BranchV0 { |
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
pub struct DagNode { |
|
|
|
pub struct DagNode { |
|
|
|
pub future: HashSet<ObjectId>, |
|
|
|
pub future: HashSet<ObjectId>, |
|
|
|
|
|
|
|
pub past: HashSet<ObjectId>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct Dag<'a>(&'a HashMap<Digest, DagNode>); |
|
|
|
struct Dag<'a>(&'a HashMap<Digest, DagNode>); |
|
|
@ -58,7 +59,7 @@ struct Dag<'a>(&'a HashMap<Digest, DagNode>); |
|
|
|
impl fmt::Display for DagNode { |
|
|
|
impl fmt::Display for DagNode { |
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
|
for fu in self.future.iter() { |
|
|
|
for fu in self.future.iter() { |
|
|
|
write!(f, "{}", fu)?; |
|
|
|
write!(f, "{} ", fu)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
@ -77,16 +78,29 @@ impl DagNode { |
|
|
|
fn new() -> Self { |
|
|
|
fn new() -> Self { |
|
|
|
Self { |
|
|
|
Self { |
|
|
|
future: HashSet::new(), |
|
|
|
future: HashSet::new(), |
|
|
|
|
|
|
|
past: HashSet::new(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
fn collapse(id: &ObjectId, dag: &HashMap<ObjectId, DagNode>) -> Vec<ObjectId> { |
|
|
|
fn collapse( |
|
|
|
let mut res = vec![*id]; |
|
|
|
id: &ObjectId, |
|
|
|
|
|
|
|
dag: &HashMap<ObjectId, DagNode>, |
|
|
|
|
|
|
|
already_in: &mut HashSet<ObjectId>, |
|
|
|
|
|
|
|
) -> Vec<ObjectId> { |
|
|
|
let this = dag.get(id).unwrap(); |
|
|
|
let this = dag.get(id).unwrap(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if this.past.len() > 1 && !this.past.is_subset(already_in) { |
|
|
|
|
|
|
|
// we postpone it
|
|
|
|
|
|
|
|
// log_debug!("postponed {}", id);
|
|
|
|
|
|
|
|
vec![] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let mut res = vec![*id]; |
|
|
|
|
|
|
|
already_in.insert(*id); |
|
|
|
for child in this.future.iter() { |
|
|
|
for child in this.future.iter() { |
|
|
|
res.append(&mut Self::collapse(child, dag)); |
|
|
|
res.append(&mut Self::collapse(child, dag, already_in)); |
|
|
|
} |
|
|
|
} |
|
|
|
res |
|
|
|
res |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Branch { |
|
|
|
impl Branch { |
|
|
@ -165,7 +179,7 @@ impl Branch { |
|
|
|
missing: &mut Option<&mut HashSet<ObjectId>>, |
|
|
|
missing: &mut Option<&mut HashSet<ObjectId>>, |
|
|
|
future: Option<ObjectId>, |
|
|
|
future: Option<ObjectId>, |
|
|
|
theirs_found: &mut Option<&mut HashSet<ObjectId>>, |
|
|
|
theirs_found: &mut Option<&mut HashSet<ObjectId>>, |
|
|
|
theirs_filter: &Option<Filter>, |
|
|
|
theirs_filter: &Option<Bloom<ObjectId>>, |
|
|
|
) -> Result<(), ObjectParseError> { |
|
|
|
) -> Result<(), ObjectParseError> { |
|
|
|
let id = cobj.id(); |
|
|
|
let id = cobj.id(); |
|
|
|
|
|
|
|
|
|
|
@ -173,7 +187,7 @@ impl Branch { |
|
|
|
// load deps, stop at the root(including it in visited) or if this is a commit object from known_heads
|
|
|
|
// load deps, stop at the root(including it in visited) or if this is a commit object from known_heads
|
|
|
|
|
|
|
|
|
|
|
|
let found_in_filter = if let Some(filter) = theirs_filter { |
|
|
|
let found_in_filter = if let Some(filter) = theirs_filter { |
|
|
|
filter.contains(id.slice()) |
|
|
|
filter.check(&id) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
false |
|
|
|
false |
|
|
|
}; |
|
|
|
}; |
|
|
@ -185,12 +199,14 @@ impl Branch { |
|
|
|
past.future.insert(f); |
|
|
|
past.future.insert(f); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
let mut insert = DagNode::new(); |
|
|
|
let mut new_node_to_insert = DagNode::new(); |
|
|
|
if let Some(f) = future { |
|
|
|
if let Some(f) = future { |
|
|
|
insert.future.insert(f); |
|
|
|
new_node_to_insert.future.insert(f); |
|
|
|
} |
|
|
|
} |
|
|
|
visited.insert(id, insert); |
|
|
|
let pasts = cobj.acks_and_nacks(); |
|
|
|
for past_id in cobj.acks_and_nacks() { |
|
|
|
new_node_to_insert.past.extend(pasts.iter().cloned()); |
|
|
|
|
|
|
|
visited.insert(id, new_node_to_insert); |
|
|
|
|
|
|
|
for past_id in pasts { |
|
|
|
match Object::load(past_id, None, store) { |
|
|
|
match Object::load(past_id, None, store) { |
|
|
|
Ok(o) => { |
|
|
|
Ok(o) => { |
|
|
|
Self::load_causal_past( |
|
|
|
Self::load_causal_past( |
|
|
@ -258,9 +274,14 @@ impl Branch { |
|
|
|
|
|
|
|
|
|
|
|
let theirs: HashSet<ObjectId> = theirs.keys().into_iter().cloned().collect(); |
|
|
|
let theirs: HashSet<ObjectId> = theirs.keys().into_iter().cloned().collect(); |
|
|
|
|
|
|
|
|
|
|
|
let filter = known_commits |
|
|
|
let filter = if let Some(filter) = known_commits.as_ref() { |
|
|
|
.as_ref() |
|
|
|
Some( |
|
|
|
.map(|their_filter| Filter::from_u8_array(their_filter.f.as_slice(), their_filter.k)); |
|
|
|
serde_bare::from_slice(filter.filter()) |
|
|
|
|
|
|
|
.map_err(|_| ObjectParseError::FilterDeserializationError)?, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
None |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// collect all commits reachable from target_heads
|
|
|
|
// collect all commits reachable from target_heads
|
|
|
|
// up to the root or until encountering a commit from theirs
|
|
|
|
// up to the root or until encountering a commit from theirs
|
|
|
@ -292,15 +313,22 @@ impl Branch { |
|
|
|
let all = HashSet::from_iter(visited.keys()); |
|
|
|
let all = HashSet::from_iter(visited.keys()); |
|
|
|
let first_generation = all.difference(&next_generations); |
|
|
|
let first_generation = all.difference(&next_generations); |
|
|
|
|
|
|
|
|
|
|
|
let mut result = Vec::with_capacity(visited.len()); |
|
|
|
let mut already_in: HashSet<ObjectId> = HashSet::new(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sub_dag_to_send_size = visited.len(); |
|
|
|
|
|
|
|
let mut result = Vec::with_capacity(sub_dag_to_send_size); |
|
|
|
for first in first_generation { |
|
|
|
for first in first_generation { |
|
|
|
result.append(&mut DagNode::collapse(first, &visited)); |
|
|
|
result.append(&mut DagNode::collapse(first, &visited, &mut already_in)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// #[cfg(debug_assertions)]
|
|
|
|
if result.len() != sub_dag_to_send_size || already_in.len() != sub_dag_to_send_size { |
|
|
|
// for _res in result.iter() {
|
|
|
|
return Err(ObjectParseError::MalformedDag); |
|
|
|
// log_debug!("sending missing commit {}", _res);
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)] |
|
|
|
|
|
|
|
for _res in result.iter() { |
|
|
|
|
|
|
|
log_debug!("sending missing commit {}", _res); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Ok(result) |
|
|
|
Ok(result) |
|
|
|
} |
|
|
|
} |
|
|
@ -310,7 +338,7 @@ impl Branch { |
|
|
|
#[cfg(test)] |
|
|
|
#[cfg(test)] |
|
|
|
mod test { |
|
|
|
mod test { |
|
|
|
|
|
|
|
|
|
|
|
//use fastbloom_rs::{BloomFilter as Filter, FilterBuilder, Membership};
|
|
|
|
//use use bloomfilter::Bloom;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::branch::*; |
|
|
|
use crate::branch::*; |
|
|
|
|
|
|
|
|
|
|
|