reddb-io-server 1.1.1

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Structural Graph Algorithms
//!
//! Algorithms for analyzing graph structure:
//! - HITS: Hubs and Authorities
//! - Strongly Connected Components (Tarjan's)
//! - Weakly Connected Components
//! - Triangle Counting
//! - Clustering Coefficient

use std::collections::{HashMap, HashSet, VecDeque};

use super::super::graph_store::GraphStore;

// ============================================================================
// HITS (Hubs and Authorities)
// ============================================================================

/// HITS algorithm: Identifies hubs and authorities
///
/// - Authorities: Nodes that are pointed to by many hubs (valuable targets)
/// - Hubs: Nodes that point to many authorities (good pivot points)
pub struct HITS {
    /// Maximum iterations
    pub max_iterations: usize,
    /// Convergence threshold
    pub epsilon: f64,
}

impl Default for HITS {
    fn default() -> Self {
        Self {
            max_iterations: 100,
            epsilon: 1e-6,
        }
    }
}

/// Result of HITS computation
#[derive(Debug, Clone)]
pub struct HITSResult {
    /// Node ID → hub score
    pub hub_scores: HashMap<String, f64>,
    /// Node ID → authority score
    pub authority_scores: HashMap<String, f64>,
    /// Iterations until convergence
    pub iterations: usize,
    /// Whether converged
    pub converged: bool,
}

impl HITSResult {
    /// Get top N hubs
    pub fn top_hubs(&self, n: usize) -> Vec<(String, f64)> {
        let mut sorted: Vec<_> = self
            .hub_scores
            .iter()
            .map(|(k, v)| (k.clone(), *v))
            .collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sorted.truncate(n);
        sorted
    }

    /// Get top N authorities
    pub fn top_authorities(&self, n: usize) -> Vec<(String, f64)> {
        let mut sorted: Vec<_> = self
            .authority_scores
            .iter()
            .map(|(k, v)| (k.clone(), *v))
            .collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sorted.truncate(n);
        sorted
    }
}

impl HITS {
    pub fn new() -> Self {
        Self::default()
    }

    /// Compute HITS hub and authority scores
    pub fn compute(&self, graph: &GraphStore) -> HITSResult {
        let nodes: Vec<String> = graph.iter_nodes().map(|n| n.id.clone()).collect();
        let n = nodes.len();

        if n == 0 {
            return HITSResult {
                hub_scores: HashMap::new(),
                authority_scores: HashMap::new(),
                iterations: 0,
                converged: true,
            };
        }

        // Build adjacency
        let mut outgoing: HashMap<String, Vec<String>> = HashMap::new();
        let mut incoming: HashMap<String, Vec<String>> = HashMap::new();

        for node in &nodes {
            let out: Vec<String> = graph
                .outgoing_edges(node)
                .into_iter()
                .map(|(_, t, _)| t)
                .collect();
            outgoing.insert(node.clone(), out);

            let inc: Vec<String> = graph
                .incoming_edges(node)
                .into_iter()
                .map(|(_, s, _)| s)
                .collect();
            incoming.insert(node.clone(), inc);
        }

        // Initialize scores
        let init = 1.0 / (n as f64).sqrt();
        let mut hub: HashMap<String, f64> = nodes.iter().map(|id| (id.clone(), init)).collect();
        let mut auth: HashMap<String, f64> = nodes.iter().map(|id| (id.clone(), init)).collect();

        let mut converged = false;
        let mut iterations = 0;

        for iter in 0..self.max_iterations {
            iterations = iter + 1;

            // Update authority scores: auth(p) = sum of hub(q) for all q that point to p
            let mut new_auth: HashMap<String, f64> = HashMap::new();
            for node in &nodes {
                let sum: f64 = incoming
                    .get(node)
                    .map(|inc| inc.iter().map(|s| hub.get(s).copied().unwrap_or(0.0)).sum())
                    .unwrap_or(0.0);
                new_auth.insert(node.clone(), sum);
            }

            // Normalize authority scores
            let auth_norm: f64 = new_auth.values().map(|v| v * v).sum::<f64>().sqrt();
            if auth_norm > 0.0 {
                for v in new_auth.values_mut() {
                    *v /= auth_norm;
                }
            }

            // Update hub scores: hub(p) = sum of auth(q) for all q that p points to
            let mut new_hub: HashMap<String, f64> = HashMap::new();
            for node in &nodes {
                let sum: f64 = outgoing
                    .get(node)
                    .map(|out| {
                        out.iter()
                            .map(|t| new_auth.get(t).copied().unwrap_or(0.0))
                            .sum()
                    })
                    .unwrap_or(0.0);
                new_hub.insert(node.clone(), sum);
            }

            // Normalize hub scores
            let hub_norm: f64 = new_hub.values().map(|v| v * v).sum::<f64>().sqrt();
            if hub_norm > 0.0 {
                for v in new_hub.values_mut() {
                    *v /= hub_norm;
                }
            }

            // Check convergence
            let hub_diff: f64 = nodes
                .iter()
                .map(|id| {
                    (hub.get(id).copied().unwrap_or(0.0) - new_hub.get(id).copied().unwrap_or(0.0))
                        .abs()
                })
                .sum();

            hub = new_hub;
            auth = new_auth;

            if hub_diff < self.epsilon {
                converged = true;
                break;
            }
        }

        HITSResult {
            hub_scores: hub,
            authority_scores: auth,
            iterations,
            converged,
        }
    }
}

// ============================================================================
// Strongly Connected Components (Tarjan's Algorithm)
// ============================================================================

/// Strongly connected components using Tarjan's algorithm
///
/// In a directed graph, an SCC is a maximal set of nodes where every node
/// is reachable from every other node.
pub struct StronglyConnectedComponents;

/// Result of SCC computation
#[derive(Debug, Clone)]
pub struct SCCResult {
    /// List of strongly connected components (sets of node IDs)
    pub components: Vec<Vec<String>>,
    /// Number of SCCs
    pub count: usize,
}

impl SCCResult {
    /// Get the largest SCC
    pub fn largest(&self) -> Option<&Vec<String>> {
        self.components.iter().max_by_key(|c| c.len())
    }

    /// Find which SCC a node belongs to
    pub fn component_of(&self, node_id: &str) -> Option<usize> {
        self.components
            .iter()
            .position(|c| c.contains(&node_id.to_string()))
    }
}

impl StronglyConnectedComponents {
    /// Find all strongly connected components using Tarjan's algorithm
    pub fn find(graph: &GraphStore) -> SCCResult {
        let nodes: Vec<String> = graph.iter_nodes().map(|n| n.id.clone()).collect();

        let mut index_counter = 0;
        let mut stack: Vec<String> = Vec::new();
        let mut on_stack: HashSet<String> = HashSet::new();
        let mut indices: HashMap<String, usize> = HashMap::new();
        let mut lowlinks: HashMap<String, usize> = HashMap::new();
        let mut components: Vec<Vec<String>> = Vec::new();

        fn strongconnect(
            graph: &GraphStore,
            node: &str,
            index_counter: &mut usize,
            stack: &mut Vec<String>,
            on_stack: &mut HashSet<String>,
            indices: &mut HashMap<String, usize>,
            lowlinks: &mut HashMap<String, usize>,
            components: &mut Vec<Vec<String>>,
        ) {
            indices.insert(node.to_string(), *index_counter);
            lowlinks.insert(node.to_string(), *index_counter);
            *index_counter += 1;
            stack.push(node.to_string());
            on_stack.insert(node.to_string());

            for (_, neighbor, _) in graph.outgoing_edges(node) {
                if !indices.contains_key(&neighbor) {
                    // Neighbor not visited, recurse
                    strongconnect(
                        graph,
                        &neighbor,
                        index_counter,
                        stack,
                        on_stack,
                        indices,
                        lowlinks,
                        components,
                    );
                    let neighbor_ll = *lowlinks.get(&neighbor).unwrap();
                    let node_ll = lowlinks.get_mut(node).unwrap();
                    *node_ll = (*node_ll).min(neighbor_ll);
                } else if on_stack.contains(&neighbor) {
                    // Neighbor on stack, update lowlink
                    let neighbor_idx = *indices.get(&neighbor).unwrap();
                    let node_ll = lowlinks.get_mut(node).unwrap();
                    *node_ll = (*node_ll).min(neighbor_idx);
                }
            }

            // If node is root of SCC
            if lowlinks.get(node) == indices.get(node) {
                let mut component = Vec::new();
                loop {
                    let w = stack.pop().unwrap();
                    on_stack.remove(&w);
                    component.push(w.clone());
                    if w == node {
                        break;
                    }
                }
                components.push(component);
            }
        }

        for node in &nodes {
            if !indices.contains_key(node) {
                strongconnect(
                    graph,
                    node,
                    &mut index_counter,
                    &mut stack,
                    &mut on_stack,
                    &mut indices,
                    &mut lowlinks,
                    &mut components,
                );
            }
        }

        // Sort components by size descending
        components.sort_by_key(|b| std::cmp::Reverse(b.len()));

        SCCResult {
            count: components.len(),
            components,
        }
    }
}

// ============================================================================
// Weakly Connected Components
// ============================================================================

/// Weakly connected components - treats directed graph as undirected
///
/// A weakly connected component is a set of nodes where there is a path
/// between any two nodes when ignoring edge direction.
pub struct WeaklyConnectedComponents;

/// Result of weakly connected components
#[derive(Debug, Clone)]
pub struct WCCResult {
    /// Each component as a list of node IDs
    pub components: Vec<Vec<String>>,
    /// Number of components
    pub count: usize,
    /// Node ID → component index
    pub node_to_component: HashMap<String, usize>,
}

impl WCCResult {
    /// Get the largest component
    pub fn largest(&self) -> Option<&Vec<String>> {
        self.components.iter().max_by_key(|c| c.len())
    }

    /// Get nodes in the same component as the given node
    pub fn component_of(&self, node: &str) -> Option<&Vec<String>> {
        self.node_to_component
            .get(node)
            .and_then(|&i| self.components.get(i))
    }
}

impl WeaklyConnectedComponents {
    /// Find all weakly connected components
    pub fn find(graph: &GraphStore) -> WCCResult {
        let nodes: Vec<String> = graph.iter_nodes().map(|n| n.id.clone()).collect();

        // Build undirected adjacency
        let mut neighbors: HashMap<String, Vec<String>> = HashMap::new();
        for node in &nodes {
            let mut nbrs: Vec<String> = Vec::new();
            for (_, target, _) in graph.outgoing_edges(node) {
                if target != *node {
                    nbrs.push(target);
                }
            }
            for (_, source, _) in graph.incoming_edges(node) {
                if source != *node && !nbrs.contains(&source) {
                    nbrs.push(source);
                }
            }
            neighbors.insert(node.clone(), nbrs);
        }

        // BFS to find components
        let mut visited: HashSet<String> = HashSet::new();
        let mut components: Vec<Vec<String>> = Vec::new();
        let mut node_to_component: HashMap<String, usize> = HashMap::new();

        for node in &nodes {
            if visited.contains(node) {
                continue;
            }

            let mut component: Vec<String> = Vec::new();
            let mut queue: VecDeque<String> = VecDeque::new();
            queue.push_back(node.clone());
            visited.insert(node.clone());

            while let Some(current) = queue.pop_front() {
                component.push(current.clone());

                if let Some(nbrs) = neighbors.get(&current) {
                    for nbr in nbrs {
                        if !visited.contains(nbr) {
                            visited.insert(nbr.clone());
                            queue.push_back(nbr.clone());
                        }
                    }
                }
            }

            let component_idx = components.len();
            for n in &component {
                node_to_component.insert(n.clone(), component_idx);
            }
            components.push(component);
        }

        // Sort by size descending
        let mut indexed: Vec<(usize, Vec<String>)> = components.into_iter().enumerate().collect();
        indexed.sort_by_key(|b| std::cmp::Reverse(b.1.len()));

        // Rebuild with new indices
        let mut new_node_to_component: HashMap<String, usize> = HashMap::new();
        let new_components: Vec<Vec<String>> = indexed
            .into_iter()
            .enumerate()
            .map(|(new_idx, (_, comp))| {
                for n in &comp {
                    new_node_to_component.insert(n.clone(), new_idx);
                }
                comp
            })
            .collect();

        WCCResult {
            count: new_components.len(),
            components: new_components,
            node_to_component: new_node_to_component,
        }
    }
}

// ============================================================================
// Triangle Counting
// ============================================================================

/// Count triangles in the graph
///
/// Triangles indicate tightly connected clusters.
/// High triangle count in attack graphs suggests multiple redundant paths.
pub struct TriangleCounting;

/// Result of triangle counting
#[derive(Debug, Clone)]
pub struct TriangleResult {
    /// Total number of triangles
    pub count: usize,
    /// Node ID → number of triangles containing this node
    pub per_node: HashMap<String, usize>,
    /// The triangles themselves (as triples of node IDs)
    pub triangles: Vec<(String, String, String)>,
}

impl TriangleCounting {
    /// Count all triangles in the graph (treating as undirected)
    pub fn count(graph: &GraphStore) -> TriangleResult {
        let nodes: Vec<String> = graph.iter_nodes().map(|n| n.id.clone()).collect();

        // Build undirected adjacency
        let mut neighbors: HashMap<String, HashSet<String>> = HashMap::new();
        for node in &nodes {
            let mut nbrs: HashSet<String> = HashSet::new();
            for (_, target, _) in graph.outgoing_edges(node) {
                nbrs.insert(target);
            }
            for (_, source, _) in graph.incoming_edges(node) {
                nbrs.insert(source);
            }
            neighbors.insert(node.clone(), nbrs);
        }

        let mut triangles: Vec<(String, String, String)> = Vec::new();
        let mut per_node: HashMap<String, usize> = nodes.iter().map(|n| (n.clone(), 0)).collect();

        // For each node, check if any two neighbors are connected
        for node in &nodes {
            if let Some(node_nbrs) = neighbors.get(node) {
                let nbr_list: Vec<&String> = node_nbrs.iter().collect();
                for i in 0..nbr_list.len() {
                    for j in (i + 1)..nbr_list.len() {
                        let a = nbr_list[i];
                        let b = nbr_list[j];

                        // Check if a and b are connected
                        if neighbors.get(a).map(|s| s.contains(b)).unwrap_or(false) {
                            // Sort to avoid duplicates
                            let mut triple = vec![node.clone(), a.clone(), b.clone()];
                            triple.sort();

                            // Check if we've already found this triangle
                            let is_new = !triangles.iter().any(|(x, y, z)| {
                                let mut existing = vec![x.clone(), y.clone(), z.clone()];
                                existing.sort();
                                existing == triple
                            });

                            if is_new {
                                triangles.push((
                                    triple[0].clone(),
                                    triple[1].clone(),
                                    triple[2].clone(),
                                ));
                                *per_node.entry(triple[0].clone()).or_insert(0) += 1;
                                *per_node.entry(triple[1].clone()).or_insert(0) += 1;
                                *per_node.entry(triple[2].clone()).or_insert(0) += 1;
                            }
                        }
                    }
                }
            }
        }

        TriangleResult {
            count: triangles.len(),
            per_node,
            triangles,
        }
    }
}

// ============================================================================
// Clustering Coefficient
// ============================================================================

/// Local and global clustering coefficient
///
/// Measures how much neighbors of a node are connected to each other.
/// High clustering = tightly connected neighborhood = potential attack surface.
pub struct ClusteringCoefficient;

/// Result of clustering coefficient computation
#[derive(Debug, Clone)]
pub struct ClusteringResult {
    /// Node ID → local clustering coefficient (0 to 1)
    pub local: HashMap<String, f64>,
    /// Global clustering coefficient (average of local)
    pub global: f64,
}

impl ClusteringResult {
    /// Get nodes with highest clustering
    pub fn top(&self, n: usize) -> Vec<(String, f64)> {
        let mut sorted: Vec<_> = self.local.iter().map(|(k, v)| (k.clone(), *v)).collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sorted.truncate(n);
        sorted
    }
}

impl ClusteringCoefficient {
    /// Compute local and global clustering coefficients
    pub fn compute(graph: &GraphStore) -> ClusteringResult {
        let nodes: Vec<String> = graph.iter_nodes().map(|n| n.id.clone()).collect();

        // Build undirected adjacency
        let mut neighbors: HashMap<String, HashSet<String>> = HashMap::new();
        for node in &nodes {
            let mut nbrs: HashSet<String> = HashSet::new();
            for (_, target, _) in graph.outgoing_edges(node) {
                if target != *node {
                    nbrs.insert(target);
                }
            }
            for (_, source, _) in graph.incoming_edges(node) {
                if source != *node {
                    nbrs.insert(source);
                }
            }
            neighbors.insert(node.clone(), nbrs);
        }

        let mut local: HashMap<String, f64> = HashMap::new();

        for node in &nodes {
            if let Some(node_nbrs) = neighbors.get(node) {
                let k = node_nbrs.len();
                if k < 2 {
                    local.insert(node.clone(), 0.0);
                    continue;
                }

                // Count edges between neighbors
                let mut edges_between = 0;
                let nbr_list: Vec<&String> = node_nbrs.iter().collect();
                for i in 0..nbr_list.len() {
                    for j in (i + 1)..nbr_list.len() {
                        if neighbors
                            .get(nbr_list[i])
                            .map(|s| s.contains(nbr_list[j]))
                            .unwrap_or(false)
                        {
                            edges_between += 1;
                        }
                    }
                }

                // Local clustering coefficient = 2 * edges_between / (k * (k-1))
                let max_edges = k * (k - 1) / 2;
                let cc = if max_edges > 0 {
                    edges_between as f64 / max_edges as f64
                } else {
                    0.0
                };
                local.insert(node.clone(), cc);
            } else {
                local.insert(node.clone(), 0.0);
            }
        }

        // Global clustering coefficient (average of local)
        let global = if !local.is_empty() {
            local.values().sum::<f64>() / local.len() as f64
        } else {
            0.0
        };

        ClusteringResult { local, global }
    }
}