interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
//! Traversal explanation types for `explain()`.
//!
//! Provides structured descriptions of traversal pipelines without executing them.
//! Used for debugging, logging, and understanding query plans.

use std::fmt;

use crate::index::IndexSpec;
use crate::traversal::step::DynStep;
use crate::traversal::traverser::TraversalSource;
use crate::value::Value;

// =============================================================================
// StepCategory
// =============================================================================

/// Category of traversal step for classification in `explain()` output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepCategory {
    /// Source steps: V(), E(), inject()
    Source,
    /// Navigation: out(), in_(), both(), outE(), etc.
    Navigation,
    /// Filter: has(), hasLabel(), where_(), limit(), etc.
    Filter,
    /// Transform: values(), valueMap(), id(), label(), etc.
    Transform,
    /// Aggregation: group(), groupCount(), fold(), count(), etc.
    Aggregation,
    /// Branch: union(), coalesce(), choose(), etc.
    Branch,
    /// Side Effect: aggregate(), store(), sideEffect(), etc.
    SideEffect,
    /// Modulator: as_(), by(), etc.
    Modulator,
    /// Unknown/custom steps
    Other,
}

impl fmt::Display for StepCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Source => write!(f, "Source"),
            Self::Navigation => write!(f, "Navigation"),
            Self::Filter => write!(f, "Filter"),
            Self::Transform => write!(f, "Transform"),
            Self::Aggregation => write!(f, "Aggregation"),
            Self::Branch => write!(f, "Branch"),
            Self::SideEffect => write!(f, "SideEffect"),
            Self::Modulator => write!(f, "Modulator"),
            Self::Other => write!(f, "Other"),
        }
    }
}

// =============================================================================
// StepExplanation
// =============================================================================

/// Explanation of a single traversal step.
#[derive(Debug, Clone)]
pub struct StepExplanation {
    /// Step name (from `dyn_name()`)
    pub name: &'static str,
    /// Zero-based index in the pipeline
    pub index: usize,
    /// Whether this step blocks streaming execution
    pub is_barrier: bool,
    /// Step category
    pub category: StepCategory,
    /// Optional human-readable description of step configuration
    pub description: Option<String>,
    /// Index hint: which index (if any) covers this step's filter key
    pub index_hint: Option<String>,
    /// Whether this step has a filter key (for showing [no index] hint)
    pub has_filter_key: bool,
}

// =============================================================================
// TraversalExplanation
// =============================================================================

/// Structured description of a traversal pipeline.
///
/// Returned by `explain()` on `BoundTraversal` and `Traversal`.
/// Does not execute the traversal — only inspects its structure.
///
/// # Example
///
/// ```ignore
/// let explanation = g.v().out().has_label("person").explain();
/// println!("{}", explanation);
/// ```
#[derive(Debug, Clone)]
pub struct TraversalExplanation {
    /// Source description (e.g., "V()", "V(1,2,3)", "E()")
    pub source: Option<String>,
    /// Ordered list of step explanations
    pub steps: Vec<StepExplanation>,
    /// Whether any step is a barrier
    pub has_barriers: bool,
    /// Total number of steps
    pub step_count: usize,
}

impl TraversalExplanation {
    /// Build an explanation from a traversal source and steps.
    pub fn from_steps(
        source: Option<&TraversalSource>,
        steps: &[Box<dyn DynStep>],
        indexes: &[IndexSpec],
        text_indexes: &[String],
    ) -> Self {
        let step_explanations: Vec<StepExplanation> = steps
            .iter()
            .enumerate()
            .map(|(i, step)| {
                let filter_key = step.filter_key();
                let index_hint = filter_key.as_deref().and_then(|key| {
                    // Check property indexes first
                    indexes
                        .iter()
                        .find(|idx| idx.property == key)
                        .map(|idx| format!("{} ({:?})", idx.name, idx.index_type))
                        // Then check text indexes
                        .or_else(|| {
                            if text_indexes.iter().any(|t| t == key) {
                                Some("full-text".to_string())
                            } else {
                                None
                            }
                        })
                });

                StepExplanation {
                    name: step.dyn_name(),
                    index: i,
                    is_barrier: step.is_barrier(),
                    category: step.category(),
                    description: step.describe(),
                    index_hint,
                    has_filter_key: filter_key.is_some(),
                }
            })
            .collect();

        let has_barriers = step_explanations.iter().any(|s| s.is_barrier);
        let step_count = step_explanations.len();

        Self {
            source: source.map(format_source),
            steps: step_explanations,
            has_barriers,
            step_count,
        }
    }
}

/// Format anonymous traversal steps as a compact pipeline string.
///
/// E.g. `__.out("knows").hasLabel("person")` becomes `out("knows").hasLabel("person")`.
/// Used by `describe()` on branch/repeat steps.
pub fn format_traversal_steps(steps: &[Box<dyn DynStep>]) -> String {
    if steps.is_empty() {
        return "identity".to_string();
    }
    steps
        .iter()
        .map(|s| {
            let name = s.dyn_name();
            match s.describe() {
                Some(desc) => format!("{name}({desc})"),
                None => format!("{name}()"),
            }
        })
        .collect::<Vec<_>>()
        .join(".")
}

/// Format a `Value` concisely for explain output.
pub fn format_value(v: &Value) -> String {
    match v {
        Value::String(s) => format!("\"{s}\""),
        Value::Int(n) => n.to_string(),
        Value::Float(f) => f.to_string(),
        Value::Bool(b) => b.to_string(),
        _ => format!("{v:?}"),
    }
}

/// Format a `TraversalSource` for display.
fn format_source(source: &TraversalSource) -> String {

    match source {
        TraversalSource::AllVertices => "V() [all vertices]".to_string(),
        TraversalSource::Vertices(ids) => {
            if ids.len() <= 5 {
                let id_strs: Vec<String> = ids.iter().map(|id| format!("{}", id.0)).collect();
                format!("V({}) [{} vertex/vertices]", id_strs.join(", "), ids.len())
            } else {
                format!("V(...) [{} vertices]", ids.len())
            }
        }
        TraversalSource::AllEdges => "E() [all edges]".to_string(),
        TraversalSource::Edges(ids) => {
            if ids.len() <= 5 {
                let id_strs: Vec<String> = ids.iter().map(|id| format!("{}", id.0)).collect();
                format!("E({}) [{} edge(s)]", id_strs.join(", "), ids.len())
            } else {
                format!("E(...) [{} edges]", ids.len())
            }
        }
        TraversalSource::Inject(values) => {
            format!("inject(...) [{} values]", values.len())
        }
        #[cfg(feature = "full-text")]
        TraversalSource::VerticesWithTextScore(pairs) => {
            format!("searchTextV(...) [{} results]", pairs.len())
        }
        #[cfg(feature = "full-text")]
        TraversalSource::EdgesWithTextScore(pairs) => {
            format!("searchTextE(...) [{} results]", pairs.len())
        }
    }
}

impl fmt::Display for TraversalExplanation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Traversal Explanation")?;
        writeln!(f, "=====================")?;

        if let Some(ref source) = self.source {
            writeln!(f, "Source: {source}")?;
        } else {
            writeln!(f, "Source: (anonymous)")?;
        }

        if self.steps.is_empty() {
            writeln!(f, "Steps:  (none)")?;
            return Ok(());
        }

        // Compute column widths
        let name_width = self
            .steps
            .iter()
            .map(|s| s.name.len())
            .max()
            .unwrap_or(4)
            .max(4);
        let cat_width = self
            .steps
            .iter()
            .map(|s| format!("{}", s.category).len())
            .max()
            .unwrap_or(8)
            .max(8);

        // Summary line: count by category
        let mut cat_counts: Vec<(StepCategory, usize)> = Vec::new();
        for step in &self.steps {
            if let Some(entry) = cat_counts.iter_mut().find(|(c, _)| *c == step.category) {
                entry.1 += 1;
            } else {
                cat_counts.push((step.category, 1));
            }
        }
        let barrier_count = self.steps.iter().filter(|s| s.is_barrier).count();
        let summary_parts: Vec<String> = cat_counts
            .iter()
            .map(|(cat, n)| format!("{n} {cat}"))
            .collect();
        let barrier_note = if barrier_count > 0 {
            format!(", {} barrier", barrier_count)
        } else {
            String::new()
        };
        writeln!(
            f,
            "Steps:  {} ({}{})",
            self.step_count,
            summary_parts.join(", "),
            barrier_note,
        )?;

        writeln!(f)?;

        // Check if any step has index information to show
        let show_index_col = self.steps.iter().any(|s| s.has_filter_key);

        // Compute index column width
        let idx_width = if show_index_col {
            self.steps
                .iter()
                .filter_map(|s| {
                    if s.index_hint.is_some() || s.has_filter_key {
                        let text = match &s.index_hint {
                            Some(hint) => hint.len(),
                            None => 8, // "no index"
                        };
                        Some(text)
                    } else {
                        None
                    }
                })
                .max()
                .unwrap_or(5)
                .max(5)
        } else {
            0
        };

        // Column prefix width: "  0  step      Category    "
        let prefix_width = 5 + name_width + 2 + cat_width + 2;

        // Table header
        if show_index_col {
            writeln!(
                f,
                "  #  {:<name_width$}  {:<cat_width$}  {:<idx_width$}  Description",
                "Step", "Category", "Index",
                name_width = name_width,
                cat_width = cat_width,
                idx_width = idx_width,
            )?;
            let rule_len = prefix_width + idx_width + 2 + 11;
            writeln!(f, "  {}", "─".repeat(rule_len))?;
        } else {
            writeln!(
                f,
                "  #  {:<name_width$}  {:<cat_width$}  Description",
                "Step", "Category",
                name_width = name_width,
                cat_width = cat_width,
            )?;
            let rule_len = prefix_width + 11;
            writeln!(f, "  {}", "─".repeat(rule_len))?;
        }

        // Steps
        for step in &self.steps {
            // Barrier separator before barrier steps
            if step.is_barrier {
                let rule_len = if show_index_col {
                    prefix_width + idx_width + 2 + 11
                } else {
                    prefix_width + 11
                };
                writeln!(
                    f,
                    "  {0}── barrier {0}──",
                    "─".repeat((rule_len.saturating_sub(13)) / 2)
                )?;
            }

            let cat_str = format!("{}", step.category);
            let desc = step.description.as_deref().unwrap_or("");

            // Split description into first line and continuation lines
            let mut desc_lines = desc.split('\n');
            let first_line = desc_lines.next().unwrap_or("");

            if show_index_col {
                let idx_str = match &step.index_hint {
                    Some(hint) => hint.as_str(),
                    None if step.has_filter_key => "no index",
                    None => "",
                };
                writeln!(
                    f,
                    "  {:<2} {:<name_width$}  {:<cat_width$}  {:<idx_width$}  {first_line}",
                    step.index,
                    step.name,
                    cat_str,
                    idx_str,
                    name_width = name_width,
                    cat_width = cat_width,
                    idx_width = idx_width,
                    first_line = first_line,
                )?;
                // Continuation lines indented to Description column
                let indent = prefix_width + idx_width + 2;
                for line in desc_lines {
                    writeln!(f, "{:indent$}{line}", "", indent = indent, line = line)?;
                }
            } else {
                writeln!(
                    f,
                    "  {:<2} {:<name_width$}  {:<cat_width$}  {first_line}",
                    step.index,
                    step.name,
                    cat_str,
                    name_width = name_width,
                    cat_width = cat_width,
                    first_line = first_line,
                )?;
                // Continuation lines indented to Description column
                for line in desc_lines {
                    writeln!(f, "{:prefix_width$}{line}", "", prefix_width = prefix_width, line = line)?;
                }
            }
        }

        Ok(())
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn step_category_display() {
        assert_eq!(format!("{}", StepCategory::Navigation), "Navigation");
        assert_eq!(format!("{}", StepCategory::Filter), "Filter");
        assert_eq!(format!("{}", StepCategory::Other), "Other");
    }

    #[test]
    fn format_source_all_vertices() {
        let s = format_source(&TraversalSource::AllVertices);
        assert_eq!(s, "V() [all vertices]");
    }

    #[test]
    fn format_source_specific_vertices() {
        let s = format_source(&TraversalSource::Vertices(vec![VertexId(1), VertexId(2)]));
        assert!(s.contains("V(1, 2)"));
        assert!(s.contains("2 vertex"));
    }

    #[test]
    fn format_source_many_vertices() {
        let ids: Vec<VertexId> = (0..10).map(VertexId).collect();
        let s = format_source(&TraversalSource::Vertices(ids));
        assert!(s.contains("V(...)"));
        assert!(s.contains("10 vertices"));
    }

    #[test]
    fn format_source_all_edges() {
        let s = format_source(&TraversalSource::AllEdges);
        assert_eq!(s, "E() [all edges]");
    }

    #[test]
    fn format_source_inject() {
        use crate::value::Value;
        let s = format_source(&TraversalSource::Inject(vec![Value::Int(1), Value::Int(2)]));
        assert!(s.contains("inject(...)"));
        assert!(s.contains("2 values"));
    }

    #[test]
    fn explanation_empty() {
        let exp = TraversalExplanation::from_steps(None, &[], &[], &[]);
        assert_eq!(exp.step_count, 0);
        assert!(!exp.has_barriers);
        assert!(exp.source.is_none());
    }

    #[test]
    fn explanation_display_empty() {
        let exp = TraversalExplanation::from_steps(None, &[], &[], &[]);
        let display = format!("{exp}");
        assert!(display.contains("(anonymous)"));
        assert!(display.contains("(none)"));
    }

    #[test]
    fn explanation_display_with_source() {
        let exp =
            TraversalExplanation::from_steps(Some(&TraversalSource::AllVertices), &[], &[], &[]);
        let display = format!("{exp}");
        assert!(display.contains("V() [all vertices]"));
    }
}