From b12c92c9d4219f7a3746424405e64a14bcc404dd Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 5 Sep 2022 12:02:54 -0400 Subject: [PATCH] Iterator over missing nodes for a set of search roots --- src/dag/iter.rs | 53 ++++++++++++++++++++++++++++++++++++++ src/{dag.rs => dag/mod.rs} | 22 ++++++++++++---- 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/dag/iter.rs rename src/{dag.rs => dag/mod.rs} (94%) diff --git a/src/dag/iter.rs b/src/dag/iter.rs new file mode 100644 index 0000000..1d5a2f6 --- /dev/null +++ b/src/dag/iter.rs @@ -0,0 +1,53 @@ +// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use std::collections::BTreeSet; + +use super::Merkle; +use crate::hash::HashWriter; +use crate::node::Node; +use crate::store::{Result, Store}; + +pub struct Gap<'dag, S, HW> +where + S: Store, + HW: HashWriter, +{ + dag: &'dag Merkle, + search_nodes: BTreeSet>, +} + +impl<'dag, S, HW> Gap<'dag, S, HW> +where + S: Store, + HW: HashWriter, +{ + pub fn new(dag: &'dag Merkle, search_nodes: BTreeSet>) -> Self { + Self { dag, search_nodes } + } + + pub fn next(&mut self) -> Result>>> { + let nodes = self + .dag + .find_next_non_descendant_nodes(&self.search_nodes)?; + self.search_nodes = BTreeSet::new(); + for id in nodes.iter().map(|n| n.id().to_vec()) { + self.search_nodes.insert(id); + } + if nodes.len() > 0 { + Ok(Some(nodes)) + } else { + Ok(None) + } + } +} diff --git a/src/dag.rs b/src/dag/mod.rs similarity index 94% rename from src/dag.rs rename to src/dag/mod.rs index 396d50b..a5bcb26 100644 --- a/src/dag.rs +++ b/src/dag/mod.rs @@ -21,6 +21,9 @@ use crate::{ store::{Result, Store, StoreError}, }; +mod iter; +pub use iter::*; + /// Node comparison values. In a given Merkle DAG a Node can come `After`, `Before`, be `Equivalent`, or `Uncomparable`. /// If the two nodes have the same id they are eqivalent. If two nodes are not part of the same sub graph within the DAG /// then they are Uncomparable. If one node is an ancestor of another DAG then that node comes before the other. If the @@ -152,26 +155,35 @@ where }) } + pub fn gap_fill_iter<'dag, 'iter>( + &'dag self, + search_nodes: BTreeSet>, + ) -> Gap<'iter, S, HW> + where + 'dag: 'iter, + { + Gap::new(self, search_nodes) + } + /// Find the immediate next non descendant nodes in this graph for the given `search_nodes`. pub fn find_next_non_descendant_nodes( &self, search_nodes: &BTreeSet>, ) -> Result>> { - let mut stack: Vec> = dbg!(self.roots.iter().cloned().collect()); - dbg!(search_nodes); + let mut stack: Vec> = self.roots.iter().cloned().collect(); let mut ids = BTreeSet::new(); while !stack.is_empty() { - let node_id = dbg!(stack.pop().unwrap()); + let node_id = stack.pop().unwrap(); let node = self.get_node_by_id(node_id.as_slice())?.unwrap(); let deps = node.dependency_ids(); - if dbg!(deps.len()) == 0 { + if deps.len() == 0 { // This is a leaf node which means it's the beginning of a sub graph // the search_nodes_are not part of. ids.insert(node.id().to_owned()); } for dep in deps { // We found one of the search roots. - if dbg!(search_nodes.contains(dep.as_slice())) { + if search_nodes.contains(dep.as_slice()) { // This means that the previous node is a parent of the search_roots. ids.insert(node.id().to_owned()); continue;