reddb-io-server 1.1.2

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
//! FlatMap Steps
//!
//! Steps that expand traversers 1:N (one input produces N outputs).
//!
//! # Steps
//!
//! - `out()`: Follow outgoing edges
//! - `in()`: Follow incoming edges
//! - `both()`: Follow both directions
//! - `outE()`: Get outgoing edges
//! - `inE()`: Get incoming edges
//! - `bothE()`: Get all adjacent edges
//! - `outV()`, `inV()`, `bothV()`, `otherV()`: Edge vertex accessors

use super::{Step, StepResult, Traverser, TraverserRequirement, TraverserValue};
use std::any::Any;

/// Trait for flatmap steps (1:N expansion)
pub trait FlatMapStep: Step {
    /// Expand a single traverser to multiple
    fn flat_map(&self, traverser: &Traverser) -> Vec<Traverser>;
}

/// Direction for edge traversal
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    /// Outgoing edges
    Out,
    /// Incoming edges
    In,
    /// Both directions
    Both,
}

/// Generic vertex step - traverses edges to adjacent vertices
#[derive(Debug, Clone)]
pub struct VertexStep {
    id: String,
    labels: Vec<String>,
    /// Direction to traverse
    direction: Direction,
    /// Edge labels to follow (empty = all)
    edge_labels: Vec<String>,
    /// Return edges instead of vertices
    return_edges: bool,
}

impl VertexStep {
    /// Create out() step
    pub fn out(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("out_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Out,
            edge_labels,
            return_edges: false,
        }
    }

    /// Create in() step
    pub fn in_(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("in_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::In,
            edge_labels,
            return_edges: false,
        }
    }

    /// Create both() step
    pub fn both(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("both_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Both,
            edge_labels,
            return_edges: false,
        }
    }

    /// Create outE() step
    pub fn out_e(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("outE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Out,
            edge_labels,
            return_edges: true,
        }
    }

    /// Create inE() step
    pub fn in_e(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("inE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::In,
            edge_labels,
            return_edges: true,
        }
    }

    /// Create bothE() step
    pub fn both_e(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("bothE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Both,
            edge_labels,
            return_edges: true,
        }
    }

    /// Get direction
    pub fn direction(&self) -> Direction {
        self.direction
    }

    /// Get edge labels
    pub fn edge_labels(&self) -> &[String] {
        &self.edge_labels
    }

    /// Check if returning edges
    pub fn returns_edges(&self) -> bool {
        self.return_edges
    }

    /// Set step ID
    pub fn with_id(mut self, id: String) -> Self {
        self.id = id;
        self
    }
}

impl Step for VertexStep {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> &str {
        match (self.direction, self.return_edges) {
            (Direction::Out, false) => "OutStep",
            (Direction::In, false) => "InStep",
            (Direction::Both, false) => "BothStep",
            (Direction::Out, true) => "OutEStep",
            (Direction::In, true) => "InEStep",
            (Direction::Both, true) => "BothEStep",
        }
    }

    fn labels(&self) -> &[String] {
        &self.labels
    }

    fn add_label(&mut self, label: String) {
        if !self.labels.contains(&label) {
            self.labels.push(label);
        }
    }

    fn requirements(&self) -> &[TraverserRequirement] {
        &[]
    }

    fn process_traverser(&self, traverser: Traverser) -> StepResult {
        let new_traversers = self.flat_map(&traverser);
        StepResult::emit_many(new_traversers)
    }

    fn reset(&mut self) {}

    fn clone_step(&self) -> Box<dyn Step> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl FlatMapStep for VertexStep {
    fn flat_map(&self, traverser: &Traverser) -> Vec<Traverser> {
        // In real implementation, this would query the graph store
        // For now, return empty - execution engine will populate
        Vec::new()
    }
}

/// Out step - convenience alias
pub type OutStep = VertexStep;

/// In step - convenience alias
pub type InStep = VertexStep;

/// Both step - convenience alias
pub type BothStep = VertexStep;

/// Edge step - get edges from current element
#[derive(Debug, Clone)]
pub struct EdgeStep {
    id: String,
    labels: Vec<String>,
    /// Direction
    direction: Direction,
    /// Edge labels
    edge_labels: Vec<String>,
}

impl EdgeStep {
    /// Create outE() step
    pub fn out(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("outE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Out,
            edge_labels,
        }
    }

    /// Create inE() step
    pub fn in_(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("inE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::In,
            edge_labels,
        }
    }

    /// Create bothE() step
    pub fn both(edge_labels: Vec<String>) -> Self {
        Self {
            id: format!("bothE_{}", edge_labels.join("_")),
            labels: Vec::new(),
            direction: Direction::Both,
            edge_labels,
        }
    }
}

impl Step for EdgeStep {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> &str {
        match self.direction {
            Direction::Out => "OutEStep",
            Direction::In => "InEStep",
            Direction::Both => "BothEStep",
        }
    }

    fn labels(&self) -> &[String] {
        &self.labels
    }

    fn add_label(&mut self, label: String) {
        if !self.labels.contains(&label) {
            self.labels.push(label);
        }
    }

    fn requirements(&self) -> &[TraverserRequirement] {
        &[]
    }

    fn process_traverser(&self, traverser: Traverser) -> StepResult {
        let new_traversers = self.flat_map(&traverser);
        StepResult::emit_many(new_traversers)
    }

    fn reset(&mut self) {}

    fn clone_step(&self) -> Box<dyn Step> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl FlatMapStep for EdgeStep {
    fn flat_map(&self, _traverser: &Traverser) -> Vec<Traverser> {
        // Would query graph store for edges
        Vec::new()
    }
}

/// Edge vertex accessor - gets vertex from edge
#[derive(Debug, Clone)]
pub struct EdgeVertexStep {
    id: String,
    labels: Vec<String>,
    /// Which vertex to get
    direction: Direction,
}

impl EdgeVertexStep {
    /// Create outV() step - get target vertex
    pub fn out_v() -> Self {
        Self {
            id: "outV_0".to_string(),
            labels: Vec::new(),
            direction: Direction::Out,
        }
    }

    /// Create inV() step - get source vertex
    pub fn in_v() -> Self {
        Self {
            id: "inV_0".to_string(),
            labels: Vec::new(),
            direction: Direction::In,
        }
    }

    /// Create bothV() step - get both vertices
    pub fn both_v() -> Self {
        Self {
            id: "bothV_0".to_string(),
            labels: Vec::new(),
            direction: Direction::Both,
        }
    }

    /// Create otherV() step - get opposite vertex from traversal
    pub fn other_v() -> Self {
        // otherV is context-dependent - needs path info
        Self {
            id: "otherV_0".to_string(),
            labels: Vec::new(),
            direction: Direction::Both, // Direction determined at runtime
        }
    }
}

impl Step for EdgeVertexStep {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> &str {
        match self.direction {
            Direction::Out => "OutVStep",
            Direction::In => "InVStep",
            Direction::Both => "BothVStep",
        }
    }

    fn labels(&self) -> &[String] {
        &self.labels
    }

    fn add_label(&mut self, label: String) {
        if !self.labels.contains(&label) {
            self.labels.push(label);
        }
    }

    fn requirements(&self) -> &[TraverserRequirement] {
        &[]
    }

    fn process_traverser(&self, traverser: Traverser) -> StepResult {
        // Get edge and extract vertex
        if let TraverserValue::Edge {
            id: _,
            source,
            target,
            label: _,
        } = traverser.value()
        {
            let new_traversers = match self.direction {
                Direction::Out => {
                    vec![traverser.clone_with_value(TraverserValue::Vertex(target.clone()))]
                }
                Direction::In => {
                    vec![traverser.clone_with_value(TraverserValue::Vertex(source.clone()))]
                }
                Direction::Both => vec![
                    traverser.clone_with_value(TraverserValue::Vertex(source.clone())),
                    traverser.clone_with_value(TraverserValue::Vertex(target.clone())),
                ],
            };
            StepResult::emit_many(new_traversers)
        } else {
            StepResult::Filter
        }
    }

    fn reset(&mut self) {}

    fn clone_step(&self) -> Box<dyn Step> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl FlatMapStep for EdgeVertexStep {
    fn flat_map(&self, traverser: &Traverser) -> Vec<Traverser> {
        // Delegate to process_traverser
        match self.process_traverser(traverser.clone()) {
            StepResult::Emit(t) => t,
            _ => Vec::new(),
        }
    }
}

/// Properties step - get all properties as map
#[derive(Debug, Clone)]
pub struct PropertiesStep {
    id: String,
    labels: Vec<String>,
    /// Property keys to get (empty = all)
    keys: Vec<String>,
}

impl PropertiesStep {
    /// Create properties() step
    pub fn new() -> Self {
        Self {
            id: "properties_0".to_string(),
            labels: Vec::new(),
            keys: Vec::new(),
        }
    }

    /// Create properties(keys) step
    pub fn with_keys(keys: Vec<String>) -> Self {
        Self {
            id: format!("properties_{}", keys.join("_")),
            labels: Vec::new(),
            keys,
        }
    }
}

impl Default for PropertiesStep {
    fn default() -> Self {
        Self::new()
    }
}

impl Step for PropertiesStep {
    fn id(&self) -> &str {
        &self.id
    }

    fn name(&self) -> &str {
        "PropertiesStep"
    }

    fn labels(&self) -> &[String] {
        &self.labels
    }

    fn add_label(&mut self, label: String) {
        if !self.labels.contains(&label) {
            self.labels.push(label);
        }
    }

    fn requirements(&self) -> &[TraverserRequirement] {
        &[]
    }

    fn process_traverser(&self, traverser: Traverser) -> StepResult {
        let new_traversers = self.flat_map(&traverser);
        StepResult::emit_many(new_traversers)
    }

    fn reset(&mut self) {}

    fn clone_step(&self) -> Box<dyn Step> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl FlatMapStep for PropertiesStep {
    fn flat_map(&self, traverser: &Traverser) -> Vec<Traverser> {
        // Would extract properties from vertex/edge
        // For now, return single property traverser per key
        if let TraverserValue::Map(map) = traverser.value() {
            let mut result = Vec::new();
            for (key, value) in map {
                if self.keys.is_empty() || self.keys.contains(key) {
                    result.push(
                        traverser
                            .clone_with_value(TraverserValue::Property(key.clone(), value.clone())),
                    );
                }
            }
            result
        } else {
            Vec::new()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_out_step() {
        let step = VertexStep::out(vec!["knows".to_string()]);
        assert_eq!(step.direction(), Direction::Out);
        assert_eq!(step.edge_labels(), &["knows"]);
        assert!(!step.returns_edges());
    }

    #[test]
    fn test_in_step() {
        let step = VertexStep::in_(vec![]);
        assert_eq!(step.direction(), Direction::In);
        assert!(step.edge_labels().is_empty());
    }

    #[test]
    fn test_both_step() {
        let step = VertexStep::both(vec!["connects".to_string(), "knows".to_string()]);
        assert_eq!(step.direction(), Direction::Both);
        assert_eq!(step.edge_labels().len(), 2);
    }

    #[test]
    fn test_out_e_step() {
        let step = VertexStep::out_e(vec![]);
        assert_eq!(step.direction(), Direction::Out);
        assert!(step.returns_edges());
    }

    #[test]
    fn test_edge_vertex_step() {
        let edge = TraverserValue::Edge {
            id: "e1".to_string(),
            source: "v1".to_string(),
            target: "v2".to_string(),
            label: "knows".to_string(),
        };
        let traverser = Traverser::with_value(edge);

        // outV should return target
        let out_v = EdgeVertexStep::out_v();
        let result = out_v.process_traverser(traverser.clone());
        if let StepResult::Emit(t) = result {
            assert_eq!(t.len(), 1);
            assert!(matches!(t[0].value(), TraverserValue::Vertex(id) if id == "v2"));
        }

        // inV should return source
        let in_v = EdgeVertexStep::in_v();
        let result = in_v.process_traverser(traverser.clone());
        if let StepResult::Emit(t) = result {
            assert_eq!(t.len(), 1);
            assert!(matches!(t[0].value(), TraverserValue::Vertex(id) if id == "v1"));
        }

        // bothV should return both
        let both_v = EdgeVertexStep::both_v();
        let result = both_v.process_traverser(traverser);
        if let StepResult::Emit(t) = result {
            assert_eq!(t.len(), 2);
        }
    }

    #[test]
    fn test_properties_step() {
        let step = PropertiesStep::with_keys(vec!["name".to_string(), "age".to_string()]);
        assert_eq!(step.name(), "PropertiesStep");
    }
}