swh_graph/stdlib/
mod.rs

1// Copyright (C) 2024  The Software Heritage developers
2// See the AUTHORS file at the top-level directory of this distribution
3// License: GNU General Public License version 3, or any later version
4// See top-level LICENSE file for more information
5
6//! Standard library to work on Software Heritage compressed graph in Rust
7
8use anyhow::{ensure, Result};
9
10use crate::graph::*;
11use crate::labels::{EdgeLabel, VisitStatus};
12use crate::properties;
13use crate::NodeType;
14
15/// Given a graph and an origin node in it, return the node id and timestamp
16/// (as a number of seconds since Epoch) of the most recent snapshot of that
17/// origin, if it exists.
18///
19/// Note: only visit with status `Full` are considered when selecting
20/// snapshots.
21pub fn find_latest_snp<G>(graph: &G, ori: NodeId) -> Result<Option<(NodeId, u64)>>
22where
23    G: SwhLabeledForwardGraph + SwhGraphWithProperties,
24    <G as SwhGraphWithProperties>::Maps: properties::Maps,
25{
26    let props = graph.properties();
27    let node_type = props.node_type(ori);
28    ensure!(
29        node_type == NodeType::Origin,
30        "Type of {ori} should be ori, but is {node_type} instead"
31    );
32    // Most recent snapshot thus far, as an optional (node_id, timestamp) pair
33    let mut latest_snp: Option<(usize, u64)> = None;
34    for (succ, labels) in graph.labeled_successors(ori) {
35        let node_type = props.node_type(succ);
36        if node_type != NodeType::Snapshot {
37            continue;
38        }
39        for label in labels {
40            if let EdgeLabel::Visit(visit) = label {
41                if visit.status() != VisitStatus::Full {
42                    continue;
43                }
44                let ts = visit.timestamp();
45                if let Some((_cur_snp, cur_ts)) = latest_snp {
46                    if ts > cur_ts {
47                        latest_snp = Some((succ, ts))
48                    }
49                } else {
50                    latest_snp = Some((succ, ts))
51                }
52            }
53        }
54    }
55    Ok(latest_snp)
56}
57
58mod fs;
59pub use fs::*;
60
61mod vcs;
62pub use vcs::*;
63
64mod visit;
65pub use visit::*;
66
67mod root_directory;
68pub use root_directory::find_root_dir;