Skip to content

Commit

Permalink
ensure that we effectively generated trees and not graph
Browse files Browse the repository at this point in the history
  • Loading branch information
irevoire committed Jan 16, 2024
1 parent 2aab22f commit f12a7ce
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ impl<'t, D: Distance> Reader<'t, D> {
/// Verify that the whole reader is correctly formed:
/// - We can access all the items.
/// - All the tree nodes are part of a tree.
/// - No tree share a same tree node.
/// - No tree shares the same tree node.
/// - We're effectively working with trees and not graphs (i.e., an item or tree node cannot be linked twice in the tree)
#[cfg(any(test, feature = "assert_reader_validity"))]
pub fn assert_validity(&self, rtxn: &RoTxn) -> Result<()> {
// First, get all the items
Expand Down Expand Up @@ -402,6 +403,7 @@ impl<'t, D: Distance> Reader<'t, D> {
}

/// Return first the number of tree nodes and second the items accessible from a node.
/// And ensure that an item or tree node is never linked twice in the tree
#[cfg(any(test, feature = "assert_reader_validity"))]
fn gather_items_and_tree_ids(
&self,
Expand All @@ -421,9 +423,16 @@ impl<'t, D: Distance> Reader<'t, D> {
let left = self.gather_items_and_tree_ids(rtxn, left)?;
let right = self.gather_items_and_tree_ids(rtxn, right)?;

let total_trees_size = left.0.len() + right.0.len();
let total_items_size = left.1.len() + right.1.len();

let mut trees = left.0 | right.0;
let items = left.1 | right.1;

// We should never find the same tree node or item ID in a single tree.
assert_eq!(total_trees_size, trees.len());
assert_eq!(total_items_size, items.len());

trees.insert(node_id.item);

Ok((trees, items))
Expand Down

0 comments on commit f12a7ce

Please sign in to comment.