1use crate::LogIntervalArg;
8use anyhow::{Context, Result};
9use clap::Parser;
10use dsi_bitstream::dispatch::factory::CodesReaderFactoryHelper;
11use dsi_bitstream::prelude::*;
12use dsi_progress_logger::prelude::*;
13use epserde::ser::Serialize;
14use std::io::BufWriter;
15use std::path::PathBuf;
16use webgraph::prelude::*;
17use webgraph::utils::MmapHelper;
18
19#[derive(Parser, Debug)]
20#[command(name = "csr", about = "Converts a graph to CSR format and serializes it with ε-serde.", long_about = None, next_line_help = true)]
21pub struct CliArgs {
22 pub basename: PathBuf,
24
25 pub dst: Option<PathBuf>,
27
28 #[clap(flatten)]
29 pub log_interval: LogIntervalArg,
30}
31
32pub fn main(args: CliArgs) -> Result<()> {
33 dispatch_endianness!(&args.basename, |E| to_csr::<E>(args))
34}
35
36pub fn to_csr<E: Endianness + 'static>(args: CliArgs) -> Result<()>
37where
38 MmapHelper<u32>: CodesReaderFactoryHelper<E>,
39{
40 let mut pl = progress_logger![
41 display_memory = true,
42 log_interval = args.log_interval.log_interval,
43 ];
44
45 log::info!("Loading graph from {}...", args.basename.display());
46 let graph = webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.basename)
47 .endianness::<E>()
48 .load()?;
49
50 pl.start("Converting to CSR...");
51 let csr = CsrGraph::from_seq_graph(&graph);
52 pl.done();
53
54 drop(graph);
55
56 let csr_path = args
57 .dst
58 .unwrap_or_else(|| args.basename.clone())
59 .with_extension(CSR_EXTENSION);
60 log::info!("Writing CSR to {}...", csr_path.display());
61 let mut file = BufWriter::new(
62 std::fs::File::create(&csr_path)
63 .with_context(|| format!("Could not create {}", csr_path.display()))?,
64 );
65 unsafe {
66 csr.serialize(&mut file)
67 .with_context(|| format!("Could not serialize CSR to {}", csr_path.display()))
68 }?;
69
70 log::info!("Completed.");
71 Ok(())
72}