Skip to main content

reddb_server/application/
graph.rs

1use crate::application::ports::RuntimeGraphPort;
2use crate::runtime::{
3    RuntimeGraphCentralityAlgorithm, RuntimeGraphCentralityResult, RuntimeGraphClusteringResult,
4    RuntimeGraphCommunityAlgorithm, RuntimeGraphCommunityResult, RuntimeGraphComponentsMode,
5    RuntimeGraphComponentsResult, RuntimeGraphCyclesResult, RuntimeGraphDirection,
6    RuntimeGraphHitsResult, RuntimeGraphNeighborhoodResult, RuntimeGraphPathAlgorithm,
7    RuntimeGraphPathResult, RuntimeGraphProjection, RuntimeGraphPropertiesResult,
8    RuntimeGraphTopologicalSortResult, RuntimeGraphTraversalResult, RuntimeGraphTraversalStrategy,
9};
10use crate::RedDBResult;
11
12#[derive(Debug, Clone)]
13pub struct GraphNeighborhoodInput {
14    pub node: String,
15    pub direction: RuntimeGraphDirection,
16    pub max_depth: usize,
17    pub edge_labels: Option<Vec<String>>,
18    pub projection: Option<RuntimeGraphProjection>,
19}
20
21#[derive(Debug, Clone)]
22pub struct GraphTraversalInput {
23    pub source: String,
24    pub direction: RuntimeGraphDirection,
25    pub max_depth: usize,
26    pub strategy: RuntimeGraphTraversalStrategy,
27    pub edge_labels: Option<Vec<String>>,
28    pub projection: Option<RuntimeGraphProjection>,
29}
30
31#[derive(Debug, Clone)]
32pub struct GraphShortestPathInput {
33    pub source: String,
34    pub target: String,
35    pub direction: RuntimeGraphDirection,
36    pub algorithm: RuntimeGraphPathAlgorithm,
37    pub edge_labels: Option<Vec<String>>,
38    pub projection: Option<RuntimeGraphProjection>,
39}
40
41#[derive(Debug, Clone)]
42pub struct GraphComponentsInput {
43    pub mode: RuntimeGraphComponentsMode,
44    pub min_size: usize,
45    pub projection: Option<RuntimeGraphProjection>,
46}
47
48#[derive(Debug, Clone)]
49pub struct GraphCentralityInput {
50    pub algorithm: RuntimeGraphCentralityAlgorithm,
51    pub top_k: usize,
52    pub normalize: bool,
53    pub max_iterations: Option<usize>,
54    pub epsilon: Option<f64>,
55    pub alpha: Option<f64>,
56    pub projection: Option<RuntimeGraphProjection>,
57}
58
59#[derive(Debug, Clone)]
60pub struct GraphCommunitiesInput {
61    pub algorithm: RuntimeGraphCommunityAlgorithm,
62    pub min_size: usize,
63    pub max_iterations: Option<usize>,
64    pub resolution: Option<f64>,
65    pub projection: Option<RuntimeGraphProjection>,
66}
67
68#[derive(Debug, Clone)]
69pub struct GraphClusteringInput {
70    pub top_k: usize,
71    pub include_triangles: bool,
72    pub projection: Option<RuntimeGraphProjection>,
73}
74
75#[derive(Debug, Clone)]
76pub struct GraphPersonalizedPageRankInput {
77    pub seeds: Vec<String>,
78    pub top_k: usize,
79    pub alpha: Option<f64>,
80    pub epsilon: Option<f64>,
81    pub max_iterations: Option<usize>,
82    pub projection: Option<RuntimeGraphProjection>,
83}
84
85#[derive(Debug, Clone)]
86pub struct GraphHitsInput {
87    pub top_k: usize,
88    pub epsilon: Option<f64>,
89    pub max_iterations: Option<usize>,
90    pub projection: Option<RuntimeGraphProjection>,
91}
92
93#[derive(Debug, Clone)]
94pub struct GraphCyclesInput {
95    pub max_length: usize,
96    pub max_cycles: usize,
97    pub projection: Option<RuntimeGraphProjection>,
98}
99
100#[derive(Debug, Clone)]
101pub struct GraphTopologicalSortInput {
102    pub projection: Option<RuntimeGraphProjection>,
103}
104
105#[derive(Debug, Clone)]
106pub struct GraphPropertiesInput {
107    pub projection: Option<RuntimeGraphProjection>,
108}
109
110pub struct GraphUseCases<'a, P: ?Sized> {
111    runtime: &'a P,
112}
113
114impl<'a, P: RuntimeGraphPort + ?Sized> GraphUseCases<'a, P> {
115    pub fn new(runtime: &'a P) -> Self {
116        Self { runtime }
117    }
118
119    pub fn neighborhood(
120        &self,
121        input: GraphNeighborhoodInput,
122    ) -> RedDBResult<RuntimeGraphNeighborhoodResult> {
123        self.runtime.graph_neighborhood(
124            &input.node,
125            input.direction,
126            input.max_depth,
127            input.edge_labels,
128            input.projection,
129        )
130    }
131
132    pub fn traverse(&self, input: GraphTraversalInput) -> RedDBResult<RuntimeGraphTraversalResult> {
133        self.runtime.graph_traverse(
134            &input.source,
135            input.direction,
136            input.max_depth,
137            input.strategy,
138            input.edge_labels,
139            input.projection,
140        )
141    }
142
143    pub fn shortest_path(
144        &self,
145        input: GraphShortestPathInput,
146    ) -> RedDBResult<RuntimeGraphPathResult> {
147        self.runtime.graph_shortest_path(
148            &input.source,
149            &input.target,
150            input.direction,
151            input.algorithm,
152            input.edge_labels,
153            input.projection,
154        )
155    }
156
157    pub fn components(
158        &self,
159        input: GraphComponentsInput,
160    ) -> RedDBResult<RuntimeGraphComponentsResult> {
161        self.runtime
162            .graph_components(input.mode, input.min_size, input.projection)
163    }
164
165    pub fn centrality(
166        &self,
167        input: GraphCentralityInput,
168    ) -> RedDBResult<RuntimeGraphCentralityResult> {
169        self.runtime.graph_centrality(
170            input.algorithm,
171            input.top_k,
172            input.normalize,
173            input.max_iterations,
174            input.epsilon,
175            input.alpha,
176            input.projection,
177        )
178    }
179
180    pub fn communities(
181        &self,
182        input: GraphCommunitiesInput,
183    ) -> RedDBResult<RuntimeGraphCommunityResult> {
184        self.runtime.graph_communities(
185            input.algorithm,
186            input.min_size,
187            input.max_iterations,
188            input.resolution,
189            input.projection,
190        )
191    }
192
193    pub fn clustering(
194        &self,
195        input: GraphClusteringInput,
196    ) -> RedDBResult<RuntimeGraphClusteringResult> {
197        self.runtime
198            .graph_clustering(input.top_k, input.include_triangles, input.projection)
199    }
200
201    pub fn personalized_pagerank(
202        &self,
203        input: GraphPersonalizedPageRankInput,
204    ) -> RedDBResult<RuntimeGraphCentralityResult> {
205        self.runtime.graph_personalized_pagerank(
206            input.seeds,
207            input.top_k,
208            input.alpha,
209            input.epsilon,
210            input.max_iterations,
211            input.projection,
212        )
213    }
214
215    pub fn hits(&self, input: GraphHitsInput) -> RedDBResult<RuntimeGraphHitsResult> {
216        self.runtime.graph_hits(
217            input.top_k,
218            input.epsilon,
219            input.max_iterations,
220            input.projection,
221        )
222    }
223
224    pub fn cycles(&self, input: GraphCyclesInput) -> RedDBResult<RuntimeGraphCyclesResult> {
225        self.runtime
226            .graph_cycles(input.max_length, input.max_cycles, input.projection)
227    }
228
229    pub fn topological_sort(
230        &self,
231        input: GraphTopologicalSortInput,
232    ) -> RedDBResult<RuntimeGraphTopologicalSortResult> {
233        self.runtime.graph_topological_sort(input.projection)
234    }
235
236    pub fn properties(
237        &self,
238        input: GraphPropertiesInput,
239    ) -> RedDBResult<RuntimeGraphPropertiesResult> {
240        self.runtime.graph_properties(input.projection)
241    }
242
243    pub fn resolve_projection(
244        &self,
245        name: Option<&str>,
246        inline: Option<RuntimeGraphProjection>,
247    ) -> RedDBResult<Option<RuntimeGraphProjection>> {
248        self.runtime.resolve_graph_projection(name, inline)
249    }
250}