bookdata/cli/
cluster_books.rs

1//! Book clustering command.
2use std::thread::{scope, Scope, ScopedJoinHandle};
3
4use petgraph::algo::kosaraju_scc;
5
6use crate::graph::model::*;
7use crate::graph::*;
8use crate::prelude::*;
9
10/// Run the book clustering algorithm.
11#[derive(Args, Debug)]
12#[command(name = "cluster-books")]
13pub struct ClusterBooks {
14    #[arg(long = "save-graph")]
15    save_graph: Option<PathBuf>,
16}
17
18impl Command for ClusterBooks {
19    fn exec(&self) -> Result<()> {
20        let cfg = load_config()?;
21        let mut graph = construct_graph(&cfg)?;
22
23        info!("computing connected components");
24        let clusters = kosaraju_scc(&graph);
25
26        info!("computed {} clusters", clusters.len());
27
28        info!("adding cluster annotations");
29        for ci in 0..clusters.len() {
30            let verts = &clusters[ci];
31            let vids: Vec<_> = verts
32                .iter()
33                .map(|v| graph.node_weight(*v).unwrap())
34                .collect();
35            let cluster = vids.iter().map(|b| b.code).min().unwrap();
36            for v in verts {
37                let node = graph.node_weight_mut(*v).unwrap();
38                node.cluster = cluster;
39            }
40        }
41
42        scope(|scope| -> Result<()> {
43            let sthread = self.maybe_save_graph(scope, &graph);
44
45            save_graph_cluster_data(&graph, clusters)?;
46
47            if let Some(h) = sthread {
48                info!("waiting for background save to finish");
49                let br = h.join().expect("thread join failed");
50                br?;
51            }
52            Ok(())
53        })?;
54
55        Ok(())
56    }
57}
58
59impl ClusterBooks {
60    fn maybe_save_graph<'scope, 'env>(
61        &'env self,
62        scope: &'scope Scope<'scope, 'env>,
63        graph: &'env IdGraph,
64    ) -> Option<ScopedJoinHandle<'scope, Result<()>>> {
65        if let Some(gf) = &self.save_graph {
66            Some(scope.spawn(move || {
67                info!("saving graph to {} (in background)", gf.display());
68                let res = save_graph(&graph, gf);
69                if let Err(e) = &res {
70                    error!("error saving graph: {}", e);
71                }
72                res
73            }))
74        } else {
75            None
76        }
77    }
78}