graphannis_core/graph/storage/
legacy.rs

1//! Legacy structures of graph storages. Old versions of graph storages need to
2//! be kept for compatibility reasons, but are not further developed. If
3//! possible, only the legacy data structure is kept, the graph storage is
4//! converted into a newer version and there is no specific implementation for
5//! the old data structure.
6
7use std::collections::HashMap;
8
9use rustc_hash::FxHashMap;
10
11use crate::{
12    annostorage::inmemory::AnnoStorageImpl,
13    types::{Edge, NodeID, NumValue},
14};
15
16use super::{
17    GraphStatistic,
18    linear::RelativePosition,
19    prepost::{OrderVecEntry, PrePost},
20};
21
22/// Some general statistical numbers specific to a graph component
23#[derive(Serialize, Deserialize, Clone)]
24pub(crate) struct GraphStatisticV1 {
25    /// True if the component contains any cycle.
26    pub cyclic: bool,
27
28    /// True if the component consists of [rooted trees](https://en.wikipedia.org/wiki/Tree_(graph_theory)).
29    pub rooted_tree: bool,
30
31    /// Number of nodes in this graph storage (both source and target nodes).
32    pub nodes: usize,
33
34    /// Average fan out.
35    pub avg_fan_out: f64,
36    /// Max fan-out of 99% of the data.
37    pub fan_out_99_percentile: usize,
38
39    /// Max inverse fan-out of 99% of the data.
40    pub inverse_fan_out_99_percentile: usize,
41
42    /// Maximal number of children of a node.
43    pub max_fan_out: usize,
44    /// Maximum length from a root node to a terminal node.
45    pub max_depth: usize,
46
47    /// Only valid for acyclic graphs: the average number of times a DFS will visit each node.
48    pub dfs_visit_ratio: f64,
49}
50
51impl From<GraphStatisticV1> for GraphStatistic {
52    fn from(value: GraphStatisticV1) -> Self {
53        let root_nodes = if value.nodes > 0 { 1 } else { 0 };
54        Self {
55            cyclic: value.cyclic,
56            rooted_tree: value.rooted_tree,
57            nodes: value.nodes,
58            root_nodes,
59            avg_fan_out: value.avg_fan_out,
60            fan_out_99_percentile: value.fan_out_99_percentile,
61            inverse_fan_out_99_percentile: value.inverse_fan_out_99_percentile,
62            max_fan_out: value.max_fan_out,
63            max_depth: value.max_depth,
64            dfs_visit_ratio: value.dfs_visit_ratio,
65        }
66    }
67}
68
69/// An adjacency list based storage that uses the [`GraphStatisticV1`]
70#[derive(Serialize, Deserialize, Clone)]
71pub(crate) struct AdjacencyListStorageV1 {
72    pub(crate) edges: HashMap<NodeID, Vec<NodeID>>,
73    pub(crate) inverse_edges: HashMap<NodeID, Vec<NodeID>>,
74    pub(crate) annos: AnnoStorageImpl<Edge>,
75    pub(crate) stats: Option<GraphStatisticV1>,
76}
77
78/// An adjacency list based storage that uses the [`GraphStatisticV1`] and is
79/// optimized for graphs where almost all nodes have an outgoing edge.
80#[derive(Serialize, Deserialize, Clone)]
81pub(crate) struct DenseAdjacencyListStorageV1 {
82    pub(crate) edges: Vec<Option<NodeID>>,
83    pub(crate) inverse_edges: HashMap<NodeID, Vec<NodeID>>,
84    pub(crate) annos: AnnoStorageImpl<Edge>,
85    pub(crate) stats: Option<GraphStatisticV1>,
86}
87
88/// A graph storage for linar graphs that uses the [`GraphStatisticV1`].
89#[derive(Serialize, Deserialize, Clone)]
90pub(crate) struct LinearGraphStorageV1<PosT: NumValue> {
91    pub(crate) node_to_pos: HashMap<NodeID, RelativePosition<PosT>>,
92    pub(crate) node_chains: HashMap<NodeID, Vec<NodeID>>,
93    pub(crate) annos: AnnoStorageImpl<Edge>,
94    pub(crate) stats: Option<GraphStatisticV1>,
95}
96
97/// A graph storage for trees that uses the [`GraphStatisticV1`] and indexes graphs using the pre/post order.
98#[derive(Serialize, Deserialize, Clone)]
99pub(crate) struct PrePostOrderStorageV1<OrderT: NumValue, LevelT: NumValue> {
100    pub(crate) node_to_order: FxHashMap<NodeID, Vec<PrePost<OrderT, LevelT>>>,
101    pub(crate) order_to_node: Vec<OrderVecEntry<OrderT, LevelT>>,
102    pub(crate) annos: AnnoStorageImpl<Edge>,
103    pub(crate) stats: Option<GraphStatisticV1>,
104}