Skip to main content

webgraph_cli/check/
eq.rs

1/*
2 * SPDX-FileCopyrightText: 2024 Davide Cologni
3 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
4 *
5 * SPDX-License-Identifier: Apache-2.0 OR MIT
6 */
7
8use anyhow::Result;
9use clap::Args;
10use dsi_bitstream::dispatch::factory::CodesReaderFactoryHelper;
11use dsi_bitstream::prelude::*;
12use std::{path::PathBuf, process::exit};
13use webgraph::traits::graph;
14use webgraph::utils::MmapHelper;
15
16#[derive(Args, Debug)]
17#[command(
18    name = "eq",
19    about = "Checks that two graphs have the same contents, listed in the same order.",
20    long_about = "Checks that two graphs have the same contents, listed in the same order. Useful to verify equality when two graphs are compressed with different parameters or algorithms (e.g., reference selection).",
21    next_line_help = true
22)]
23pub struct CliArgs {
24    /// The basename of the first graph.​
25    pub first_basename: PathBuf,
26    /// The basename of the second graph.​
27    pub second_basename: PathBuf,
28}
29
30pub fn main(args: CliArgs) -> Result<()> {
31    dispatch_endianness!(&args.first_basename, |E| compare_graphs::<E>(args))
32}
33
34pub fn compare_graphs<E: Endianness + 'static>(args: CliArgs) -> Result<()>
35where
36    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
37{
38    let first_graph =
39        webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.first_basename)
40            .endianness::<E>()
41            .load()?;
42    let second_graph =
43        webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.second_basename)
44            .endianness::<E>()
45            .load()?;
46
47    log::info!("Comparing graphs...");
48    let result = graph::eq(&first_graph, &second_graph);
49    if let Err(eq_error) = result {
50        eprintln!("{}", eq_error);
51        exit(1);
52    }
53    log::info!("Graphs are equal.");
54
55    Ok(())
56}