Skip to main content

webgraph_cli/check/
ef.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
4 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR MIT
7 */
8
9use crate::LogIntervalArg;
10use anyhow::Result;
11use clap::Parser;
12use dsi_bitstream::dispatch::factory::CodesReaderFactoryHelper;
13use dsi_bitstream::prelude::*;
14use dsi_progress_logger::prelude::*;
15use epserde::prelude::*;
16use log::info;
17use std::path::PathBuf;
18use value_traits::slices::SliceByValue;
19use webgraph::graphs::bvgraph::{EF, EF_EXTENSION, OFFSETS_EXTENSION, PROPERTIES_EXTENSION};
20use webgraph::prelude::*;
21
22#[derive(Parser, Debug)]
23#[command(name = "ef", about = "Checks that the \".ef\" file (and \".offsets\" if present) is consistent with the graph.", long_about = None, next_line_help = true)]
24pub struct CliArgs {
25    /// The basename of the graph.​
26    pub basename: PathBuf,
27
28    #[clap(flatten)]
29    pub log_interval: LogIntervalArg,
30}
31
32pub fn main(args: CliArgs) -> Result<()> {
33    dispatch_endianness!(&args.basename, |E| check_ef::<E>(args))
34}
35
36pub fn check_ef<E: Endianness + 'static>(args: CliArgs) -> Result<()>
37where
38    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
39    for<'a> LoadModeCodesReader<'a, E, Mmap>: BitSeek,
40{
41    let (num_nodes, _, _) =
42        parse_properties::<E>(args.basename.with_extension(PROPERTIES_EXTENSION))?;
43
44    // Creates the offsets file
45    let of_file_path = args.basename.with_extension(OFFSETS_EXTENSION);
46
47    let ef = unsafe { EF::mmap(args.basename.with_extension(EF_EXTENSION), Flags::default()) }?;
48    let ef = ef.uncase();
49
50    let mut pl = progress_logger![
51        display_memory = true,
52        item_name = "offset",
53        expected_updates = Some(num_nodes),
54        log_interval = args.log_interval.log_interval,
55    ];
56
57    // if the offset files exists, read it to build elias-fano
58    if of_file_path.exists() {
59        // create a bit reader on the file
60        let mut reader = buf_bit_reader::from_path::<BE, u32>(of_file_path)?;
61        // progress bar
62        pl.start("Checking offsets file against Elias–Fano...");
63        // read the graph a write the offsets
64        let mut offset = 0;
65        for node_id in 0..num_nodes + 1 {
66            // write where
67            offset += reader.read_gamma()?;
68            // read ef
69            let ef_res = ef.index_value(node_id as _);
70            assert_eq!(offset, ef_res, "node_id: {}", node_id);
71            // decode the next nodes so we know where the next node_id starts
72            pl.light_update();
73        }
74    } else {
75        info!("No offsets file, checking against graph file only");
76    }
77
78    let mut pl = progress_logger![
79        display_memory = true,
80        item_name = "offset",
81        expected_updates = Some(num_nodes),
82        log_interval = args.log_interval.log_interval,
83    ];
84
85    let seq_graph =
86        webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.basename)
87            .endianness::<E>()
88            .load()?;
89    // otherwise directly read the graph
90    // progress bar
91    pl.start("Checking graph against Elias–Fano...");
92    // read the graph a write the offsets
93    let mut degs_iter = seq_graph.offset_deg_iter();
94    for (node, (new_offset, _degree)) in (&mut degs_iter).enumerate() {
95        // decode the next nodes so we know where the next node_id starts
96        // read ef
97        let ef_res = ef.index_value(node as _);
98        assert_eq!(new_offset, ef_res, "node_id: {}", node);
99        pl.light_update();
100    }
101    // The final entry is the total length of the graph bitstream.
102    let ef_res = ef.index_value(num_nodes as _);
103    assert_eq!(
104        degs_iter.get_pos(),
105        ef_res,
106        "final offset (node_id: {num_nodes})"
107    );
108    pl.done();
109    Ok(())
110}