1use crate::LogIntervalArg;
10use anyhow::{Context, 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::fs::File;
18use std::path::PathBuf;
19use sux::prelude::*;
20use webgraph::prelude::*;
21
22#[derive(Parser, Debug, Clone)]
23#[command(name = "ef", about = "Builds the Elias–Fano representation of the offsets of a graph.", long_about = None, next_line_help = true)]
24pub struct CliArgs {
25 pub basename: PathBuf,
27 pub number_of_nodes: Option<usize>,
30
31 #[clap(flatten)]
32 pub log_interval: LogIntervalArg,
33}
34
35pub fn main(args: CliArgs) -> Result<()> {
36 let ef_path = args.basename.with_extension(EF_EXTENSION);
37 if ef_path.exists() && ef_path.metadata()?.permissions().readonly() {
38 return Err(anyhow::anyhow!(
39 "The file is not writable: {}",
40 ef_path.display()
41 ));
42 }
43
44 dispatch_endianness!(&args.basename, |E| build_elias_fano::<E>(args))
45}
46
47fn build_elias_fano<E: Endianness + 'static>(args: CliArgs) -> Result<()>
48where
49 MmapHelper<u32>: CodesReaderFactoryHelper<E>,
50 for<'a> LoadModeCodesReader<'a, E, Mmap>: BitSeek,
51{
52 let mut pl = progress_logger![
53 display_memory = true,
54 item_name = "node",
55 log_interval = args.log_interval.log_interval
56 ];
57
58 let basename = &args.basename;
59 let of_file_path = basename.with_extension(OFFSETS_EXTENSION);
60
61 let bitstream_path = [GRAPH_EXTENSION, LABELS_EXTENSION]
62 .map(|ext| basename.with_extension(ext))
63 .into_iter()
64 .find(|path| path.exists())
65 .with_context(|| {
66 format!(
67 "Neither {} nor {} exist",
68 basename.with_extension(GRAPH_EXTENSION).display(),
69 basename.with_extension(LABELS_EXTENSION).display()
70 )
71 })?;
72 info!("Bitstream: '{}'", bitstream_path.display());
73 let file_len = 8 * std::fs::metadata(&bitstream_path)
74 .with_context(|| {
75 format!(
76 "Could not read the metadata of {}",
77 bitstream_path.display()
78 )
79 })?
80 .len();
81 info!("Bitstream file size: {} bits", file_len);
82
83 let num_nodes = match args.number_of_nodes {
84 Some(num_nodes) => num_nodes,
85 None => {
86 let properties_path = basename.with_extension(PROPERTIES_EXTENSION);
87 info!(
88 "Reading num_nodes from properties file at '{}'",
89 properties_path.display()
90 );
91 let f = File::open(&properties_path).with_context(|| {
92 format!(
93 "Could not open properties file: {}",
94 properties_path.display()
95 )
96 })?;
97 java_properties::read(std::io::BufReader::new(f))?
98 .get("nodes")
99 .with_context(|| format!("Missing 'nodes' in {}", properties_path.display()))?
100 .parse()?
101 }
102 };
103 pl.expected_updates(num_nodes);
104
105 let ef = if of_file_path.exists() {
106 info!(
107 "Building Elias–Fano from offsets at '{}'",
108 of_file_path.display()
109 );
110 pl.start("Building Elias–Fano from offsets...");
111 let ef = build_ef(num_nodes, file_len, &of_file_path, &mut pl)?;
112 pl.done();
113 ef
114 } else {
115 info!("No offsets file, reading the graph to build Elias–Fano");
116 let seq_graph = BvGraphSeq::with_basename(basename)
117 .endianness::<E>()
118 .load()
119 .with_context(|| format!("Could not load graph at {}", basename.display()))?;
120 pl.start("Building Elias–Fano from graph...");
121 let mut efb = EliasFanoBuilder::new(num_nodes + 1, file_len);
122 let mut iter = seq_graph.offset_deg_iter();
123 for (offset, _degree) in iter.by_ref() {
124 efb.push(offset as _);
125 pl.light_update();
126 }
127 efb.push(iter.get_pos() as _);
128 pl.done();
129 info!("Building the index over the high bits...");
130 ef_from_builder(efb)?
131 };
132
133 let ef_path = basename.with_extension(EF_EXTENSION);
134 info!("Writing Elias–Fano to '{}'", ef_path.display());
135 unsafe { ef.store(&ef_path) }
136 .with_context(|| format!("Could not serialize Elias–Fano to {}", ef_path.display()))?;
137
138 Ok(())
139}