webgraph_cli/to/
endianness.rs1use crate::{LogIntervalArg, create_parent_dir};
9use anyhow::{Context, Result};
10use clap::Parser;
11use dsi_bitstream::prelude::*;
12use dsi_progress_logger::prelude::*;
13use log::info;
14use std::path::PathBuf;
15use webgraph::prelude::*;
16
17#[derive(Parser, Debug)]
18#[command(name = "endianness", about = "Inverts the endianness of a graph in the BV format.", long_about = None, next_line_help = true)]
19pub struct CliArgs {
20 pub src: PathBuf,
22 pub dst: PathBuf,
24
25 #[clap(flatten)]
26 pub log_interval: LogIntervalArg,
27}
28
29macro_rules! impl_convert {
30 ($args:expr, $src:ty, $dst:ty) => {{
31 info!(
32 "The source graph was {}-endian, converting to {}-endian",
33 <$src>::NAME,
34 <$dst>::NAME
35 );
36
37 let src_properties_path = $args.src.with_extension(PROPERTIES_EXTENSION);
38 let dst_properties_path = $args.dst.with_extension(PROPERTIES_EXTENSION);
39 let (num_nodes, num_arcs, comp_flags) = parse_properties::<$src>(&src_properties_path)?;
40
41 let mut pl = progress_logger![
42 display_memory = true,
43 item_name = "node",
44 expected_updates = Some(num_nodes as usize),
45 log_interval = $args.log_interval.log_interval,
46 ];
47
48 let seq_graph = BvGraphSeq::with_basename(&$args.src)
49 .endianness::<$src>()
50 .load()
51 .with_context(|| format!("Could not load graph {}", $args.src.display()))?;
52
53 let target_graph_path = $args.dst.with_extension(GRAPH_EXTENSION);
54 let writer = buf_bit_writer::from_path::<$dst, usize>(&target_graph_path)
55 .with_context(|| format!("Could not create {}", target_graph_path.display()))?;
56 let encoder = <DynCodesEncoder<$dst, _>>::new(writer, &comp_flags)?;
57
58 let mut offsets_writer =
61 OffsetsWriter::from_path($args.dst.with_extension(OFFSETS_EXTENSION), true)?;
62
63 pl.start("Inverting endianness...");
64
65 let mut iter = seq_graph
66 .offset_deg_iter()
67 .map_decoder(move |decoder| Converter {
68 decoder,
69 encoder,
70 offset: 0,
71 });
72
73 let mut offset = 0;
74 for _ in 0..num_nodes {
75 iter.next_degree()?;
76 let new_offset = iter.get_decoder().offset;
77 offsets_writer.push((new_offset - offset) as u64)?;
79 offset = new_offset;
80 pl.light_update();
81 }
82 let bitstream_len = iter.get_decoder().offset;
83 pl.done();
84 offsets_writer.flush().context("Could not flush offsets")?;
85
86 std::fs::write(
90 &dst_properties_path,
91 comp_flags.to_properties::<$dst>(&CompStats {
92 num_nodes,
93 num_arcs,
94 written_bits: bitstream_len as u64,
95 ..CompStats::default()
96 })?,
97 )
98 .with_context(|| {
99 format!(
100 "Could not write properties to {}",
101 dst_properties_path.display()
102 )
103 })?;
104
105 Ok(())
106 }};
107}
108
109pub fn main(args: CliArgs) -> Result<()> {
110 create_parent_dir(&args.dst)?;
111
112 match get_endianness(&args.src)?.as_str() {
113 #[cfg(feature = "be_bins")]
114 BE::NAME => impl_convert!(args, BE, LE),
115 #[cfg(feature = "le_bins")]
116 LE::NAME => impl_convert!(args, LE, BE),
117 e => anyhow::bail!("Unknown endianness: {}", e),
118 }
119}