Skip to main content

nodedb_graph/
traversal_options.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Per-query graph traversal configuration.
4
5/// Largest accepted value for any graph-DSL depth parameter
6/// (`DEPTH`, `MAX_DEPTH`, `EXPANSION_DEPTH`).
7///
8/// Enforced at every ingress (pgwire, native protocol) and at the
9/// engine boundary as defence-in-depth so a single statement cannot
10/// saturate `cross_core_bfs`, `csr.shortest_path`, or the subgraph
11/// materializer with an unbounded fan-out per hop.
12pub const MAX_GRAPH_TRAVERSAL_DEPTH: usize = 64;
13
14use serde::{Deserialize, Serialize};
15
16/// Per-query graph traversal configuration.
17#[derive(
18    Debug,
19    Clone,
20    PartialEq,
21    Eq,
22    Serialize,
23    Deserialize,
24    zerompk::ToMessagePack,
25    zerompk::FromMessagePack,
26)]
27pub struct GraphTraversalOptions {
28    /// Cap on total visited nodes across all shards.
29    ///
30    /// Once this limit is reached, no further node exploration occurs.
31    /// Default: 100_000
32    pub max_visited: usize,
33}
34
35impl Default for GraphTraversalOptions {
36    fn default() -> Self {
37        Self {
38            max_visited: 100_000,
39        }
40    }
41}
42
43impl GraphTraversalOptions {
44    /// Create a new `GraphTraversalOptions` with default values.
45    pub fn new() -> Self {
46        Self::default()
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use sonic_rs;
54
55    #[test]
56    fn default_options_have_expected_values() {
57        let opts = GraphTraversalOptions::default();
58        assert_eq!(opts.max_visited, 100_000);
59    }
60
61    #[test]
62    fn new_returns_defaults() {
63        let opts = GraphTraversalOptions::new();
64        assert_eq!(opts, GraphTraversalOptions::default());
65    }
66
67    #[test]
68    fn serialization_roundtrip() {
69        let opts = GraphTraversalOptions {
70            max_visited: 50_000,
71        };
72        let json = sonic_rs::to_string(&opts).unwrap();
73        let deserialized: GraphTraversalOptions = sonic_rs::from_str(&json).unwrap();
74        assert_eq!(opts, deserialized);
75    }
76}