linkrs_query/binder/
query_graph.rs1use linkrs_core::types::semantic::ValueType;
2use linkrs_core::types::EdgeDirection;
3use std::collections::HashMap;
4use std::sync::Arc;
5
6#[derive(Debug, Clone)]
7pub struct BoundTagRef {
8 pub tag_name: Arc<str>,
9 pub properties: HashMap<String, ValueType>,
10}
11
12#[derive(Debug, Clone)]
13pub struct BoundEdgeTypeRef {
14 pub edge_type_name: Arc<str>,
15 pub properties: HashMap<String, ValueType>,
16}
17
18#[derive(Debug, Clone)]
19pub struct BoundNodePattern {
20 pub variable: String,
21 pub tags: Vec<BoundTagRef>,
22}
23
24#[derive(Debug, Clone)]
25pub struct BoundEdgePattern {
26 pub variable: String,
27 pub edge_types: Vec<BoundEdgeTypeRef>,
28 pub direction: EdgeDirection,
29 pub src_variable: String,
30 pub dst_variable: String,
31}
32
33#[derive(Debug, Clone)]
34pub struct QueryGraph {
35 pub nodes: Vec<BoundNodePattern>,
36 pub edges: Vec<BoundEdgePattern>,
37 node_index: HashMap<String, usize>,
38 edge_index: HashMap<String, usize>,
39}
40
41impl QueryGraph {
42 pub fn new() -> Self {
43 Self {
44 nodes: Vec::new(),
45 edges: Vec::new(),
46 node_index: HashMap::new(),
47 edge_index: HashMap::new(),
48 }
49 }
50
51 pub fn add_node(&mut self, node: BoundNodePattern) {
52 let idx = self.nodes.len();
53 self.node_index.insert(node.variable.clone(), idx);
54 self.nodes.push(node);
55 }
56
57 pub fn add_edge(&mut self, edge: BoundEdgePattern) {
58 let idx = self.edges.len();
59 self.edge_index.insert(edge.variable.clone(), idx);
60 self.edges.push(edge);
61 }
62
63 pub fn find_node(&self, variable: &str) -> Option<&BoundNodePattern> {
64 self.node_index
65 .get(variable)
66 .and_then(|&idx| self.nodes.get(idx))
67 }
68
69 pub fn find_edge(&self, variable: &str) -> Option<&BoundEdgePattern> {
70 self.edge_index
71 .get(variable)
72 .and_then(|&idx| self.edges.get(idx))
73 }
74
75 pub fn node_count(&self) -> usize {
76 self.nodes.len()
77 }
78
79 pub fn edge_count(&self) -> usize {
80 self.edges.len()
81 }
82}
83
84impl Default for QueryGraph {
85 fn default() -> Self {
86 Self::new()
87 }
88}