Skip to main content

webgraph_cli/check/
eq.rs

1/*
2 * SPDX-FileCopyrightText: 2024 Davide Cologni
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
5 */
6
7use crate::GlobalArgs;
8use anyhow::Result;
9use clap::Args;
10use dsi_bitstream::dispatch::factory::CodesReaderFactoryHelper;
11use dsi_bitstream::prelude::*;
12use dsi_progress_logger::prelude::*;
13use std::{path::PathBuf, process::exit};
14use webgraph::graphs::bvgraph::get_endianness;
15use webgraph::traits::{SequentialLabeling, graph};
16use webgraph::utils::MmapHelper;
17
18#[derive(Args, Debug)]
19#[command(name = "eq", about = "Checks that two graphs have the same contents, listed in the same order. Useful to check equality when two graphs are compressed with different parameters or with different algorithms (think about reference selection).", long_about = None)]
20pub struct CliArgs {
21    /// The basename of the first graph.
22    pub first_basename: PathBuf,
23    /// The basename of the second graph.
24    pub second_basename: PathBuf,
25}
26
27pub fn main(_global_args: GlobalArgs, args: CliArgs) -> Result<()> {
28    match get_endianness(&args.first_basename)?.as_str() {
29        #[cfg(feature = "be_bins")]
30        BE::NAME => compare_graphs::<BE>(args),
31        #[cfg(feature = "le_bins")]
32        LE::NAME => compare_graphs::<LE>(args),
33        e => panic!("Unknown endianness: {}", e),
34    }
35}
36
37pub fn compare_graphs<E: Endianness + 'static>(args: CliArgs) -> Result<()>
38where
39    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
40{
41    let first_graph =
42        webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.first_basename)
43            .endianness::<E>()
44            .load()?;
45    let second_graph =
46        webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.second_basename)
47            .endianness::<E>()
48            .load()?;
49
50    let mut pl = ProgressLogger::default();
51    pl.display_memory(true)
52        .item_name("compare graphs")
53        .expected_updates(Some(first_graph.num_nodes()));
54
55    pl.start("Start comparing the graphs...");
56
57    let result = graph::eq(&first_graph, &second_graph);
58    if let Err(eq_error) = result {
59        eprintln!("{}", eq_error);
60        exit(1);
61    }
62
63    pl.done();
64    Ok(())
65}