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
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Metadata step tests.
//!
//! Tests for metadata transform steps:
//! - `key()` - extracts "key" from property objects
//! - `value()` - extracts "value" from property objects
//! - `loops()` - returns loop depth in repeat traversals
//! - `index()` - wraps values with their index
//! - `property_map()` - returns properties as map of property objects

#![allow(unused_variables)]
use interstellar::traversal::__;
use interstellar::value::Value;

use crate::common::graphs::create_small_graph;

// -------------------------------------------------------------------------
// key() and value() Steps
// -------------------------------------------------------------------------

#[test]
fn key_step_extracts_property_keys() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get all property keys from Alice vertex
    let keys: Vec<Value> = g.v_ids([tg.alice]).properties().key().to_list();

    // Alice has "name" and "age" properties
    assert_eq!(keys.len(), 2);
    let key_strings: Vec<String> = keys
        .iter()
        .filter_map(|v| {
            if let Value::String(s) = v {
                Some(s.clone())
            } else {
                None
            }
        })
        .collect();
    assert!(key_strings.contains(&"name".to_string()));
    assert!(key_strings.contains(&"age".to_string()));
}

#[test]
fn value_step_extracts_property_values() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get all property values from Alice vertex
    let values: Vec<Value> = g.v_ids([tg.alice]).properties().value().to_list();

    // Alice has name="Alice" and age=30
    assert_eq!(values.len(), 2);
    assert!(values.contains(&Value::String("Alice".to_string())));
    assert!(values.contains(&Value::Int(30)));
}

#[test]
fn key_and_value_work_with_filtered_properties() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get only "name" property keys
    let keys: Vec<Value> = g
        .v_ids([tg.alice])
        .properties_keys(["name"])
        .key()
        .to_list();
    assert_eq!(keys, vec![Value::String("name".to_string())]);

    // Get only "name" property values
    let values: Vec<Value> = g
        .v_ids([tg.alice])
        .properties_keys(["name"])
        .value()
        .to_list();
    assert_eq!(values, vec![Value::String("Alice".to_string())]);
}

#[test]
fn key_value_chain_on_all_vertices() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get all "name" property values from all vertices
    let names: Vec<Value> = g.v().properties_keys(["name"]).value().to_list();

    // 4 vertices, all have "name" property
    assert_eq!(names.len(), 4);
    assert!(names.contains(&Value::String("Alice".to_string())));
    assert!(names.contains(&Value::String("Bob".to_string())));
    assert!(names.contains(&Value::String("Charlie".to_string())));
    assert!(names.contains(&Value::String("GraphDB".to_string())));
}

// -------------------------------------------------------------------------
// loops() Step
// -------------------------------------------------------------------------

#[test]
fn loops_returns_zero_outside_repeat() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Outside of repeat, loops() returns 0
    let loop_counts: Vec<Value> = g.v_ids([tg.alice]).loops().to_list();
    assert_eq!(loop_counts, vec![Value::Int(0)]);
}

#[test]
fn loops_increments_within_repeat() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Traverse out from Alice up to 2 times, emit at each step with loop count
    // Alice -> Bob (loops=0 after first out)
    // Bob -> Charlie (loops=1 after second out)
    let results: Vec<Value> = g
        .v_ids([tg.alice])
        .repeat(__.out_labels(&["knows"]))
        .times(2)
        .emit()
        .loops()
        .to_list();

    // Should have loop counts from repeat iterations
    // After 1st iteration: loops=1, after 2nd: loops=2
    assert!(!results.is_empty());
    for result in &results {
        if let Value::Int(n) = result {
            assert!(*n >= 0 && *n <= 2);
        }
    }
}

#[test]
fn loops_with_until_condition() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Repeat until we find Charlie, then get loop count
    let results: Vec<Value> = g
        .v_ids([tg.alice])
        .repeat(__.out_labels(&["knows"]))
        .until(__.has_value("name", "Charlie"))
        .loops()
        .to_list();

    // Alice -> Bob -> Charlie, so loops should be 2 when we reach Charlie
    assert!(!results.is_empty());
}

// -------------------------------------------------------------------------
// index() Step
// -------------------------------------------------------------------------

#[test]
fn index_wraps_values_with_position() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get indexed names
    let indexed: Vec<Value> = g.v().has_label("person").values("name").index().to_list();

    // Should have 3 persons, each wrapped as [value, index]
    assert_eq!(indexed.len(), 3);

    // Check structure
    for (expected_idx, result) in indexed.iter().enumerate() {
        if let Value::List(pair) = result {
            assert_eq!(pair.len(), 2);
            // Second element should be the index
            assert_eq!(pair[1], Value::Int(expected_idx as i64));
        } else {
            panic!("Expected [value, index] pair");
        }
    }
}

#[test]
fn index_starts_at_zero() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Get first indexed value
    let first: Option<Value> = g.inject([100i64, 200i64, 300i64]).index().next();

    if let Some(Value::List(pair)) = first {
        assert_eq!(pair[0], Value::Int(100));
        assert_eq!(pair[1], Value::Int(0)); // 0-based index
    } else {
        panic!("Expected [value, index] pair");
    }
}

#[test]
fn index_preserves_traverser_metadata() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Use index in a pipeline with path tracking
    let results: Vec<Value> = g
        .v_ids([tg.alice])
        .as_("start")
        .out_labels(&["knows"])
        .values("name")
        .index()
        .to_list();

    // Should still work, producing indexed values
    assert!(!results.is_empty());
    for result in &results {
        if let Value::List(pair) = result {
            assert_eq!(pair.len(), 2);
        } else {
            panic!("Expected [value, index] pair");
        }
    }
}

// -------------------------------------------------------------------------
// property_map() Step
// -------------------------------------------------------------------------

#[test]
fn property_map_extracts_all_properties() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    let maps: Vec<Value> = g.v_ids([tg.alice]).property_map().to_list();

    assert_eq!(maps.len(), 1);
    if let Value::Map(map) = &maps[0] {
        // Should have "name" and "age" keys
        assert!(map.contains_key("name"));
        assert!(map.contains_key("age"));

        // Each value should be a list of property objects
        if let Some(Value::List(name_list)) = map.get("name") {
            assert_eq!(name_list.len(), 1);
            if let Value::Map(prop_obj) = &name_list[0] {
                assert_eq!(
                    prop_obj.get("key"),
                    Some(&Value::String("name".to_string()))
                );
                assert_eq!(
                    prop_obj.get("value"),
                    Some(&Value::String("Alice".to_string()))
                );
            }
        } else {
            panic!("Expected name to be a list");
        }
    } else {
        panic!("Expected Value::Map");
    }
}

#[test]
fn property_map_with_specific_keys() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    let maps: Vec<Value> = g.v_ids([tg.alice]).property_map_keys(["name"]).to_list();

    assert_eq!(maps.len(), 1);
    if let Value::Map(map) = &maps[0] {
        // Should only have "name" key
        assert!(map.contains_key("name"));
        assert!(!map.contains_key("age"));
    } else {
        panic!("Expected Value::Map");
    }
}

#[test]
fn property_map_on_edges() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    let maps: Vec<Value> = g.e_ids([tg.alice_knows_bob]).property_map().to_list();

    assert_eq!(maps.len(), 1);
    if let Value::Map(map) = &maps[0] {
        // Edge has "since" property
        assert!(map.contains_key("since"));
        if let Some(Value::List(since_list)) = map.get("since") {
            assert_eq!(since_list.len(), 1);
            if let Value::Map(prop_obj) = &since_list[0] {
                assert_eq!(
                    prop_obj.get("key"),
                    Some(&Value::String("since".to_string()))
                );
                assert_eq!(prop_obj.get("value"), Some(&Value::Int(2020)));
            }
        }
    } else {
        panic!("Expected Value::Map");
    }
}

#[test]
fn property_map_returns_empty_for_non_elements() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Inject a non-element value and call property_map
    let maps: Vec<Value> = g.inject([42i64]).property_map().to_list();

    assert_eq!(maps.len(), 1);
    if let Value::Map(map) = &maps[0] {
        assert!(map.is_empty());
    } else {
        panic!("Expected Value::Map");
    }
}

// -------------------------------------------------------------------------
// Combined Metadata Steps
// -------------------------------------------------------------------------

#[test]
fn key_value_difference_from_values() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // values("name") directly extracts the value
    let direct_values: Vec<Value> = g.v_ids([tg.alice]).values("name").to_list();
    assert_eq!(direct_values, vec![Value::String("Alice".to_string())]);

    // properties().value() goes through property objects
    let prop_values: Vec<Value> = g
        .v_ids([tg.alice])
        .properties_keys(["name"])
        .value()
        .to_list();
    assert_eq!(prop_values, vec![Value::String("Alice".to_string())]);

    // Both should produce the same result
    assert_eq!(direct_values, prop_values);
}

#[test]
fn value_map_vs_property_map() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // value_map returns {key: [value], ...}
    let value_maps: Vec<Value> = g.v_ids([tg.alice]).value_map().to_list();

    // property_map returns {key: [{key: k, value: v}], ...}
    let property_maps: Vec<Value> = g.v_ids([tg.alice]).property_map().to_list();

    assert_eq!(value_maps.len(), 1);
    assert_eq!(property_maps.len(), 1);

    // Check value_map structure
    if let Value::Map(vmap) = &value_maps[0] {
        if let Some(Value::List(name_list)) = vmap.get("name") {
            // value_map wraps values directly in lists
            assert_eq!(name_list[0], Value::String("Alice".to_string()));
        }
    }

    // Check property_map structure
    if let Value::Map(pmap) = &property_maps[0] {
        if let Some(Value::List(name_list)) = pmap.get("name") {
            // property_map wraps property objects in lists
            if let Value::Map(prop_obj) = &name_list[0] {
                assert_eq!(
                    prop_obj.get("value"),
                    Some(&Value::String("Alice".to_string()))
                );
            }
        }
    }
}

#[test]
fn index_with_dedup() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Index after dedup
    let indexed: Vec<Value> = g
        .v()
        .has_label("person")
        .out_labels(&["knows"])
        .dedup()
        .values("name")
        .index()
        .to_list();

    // After dedup, indices should be continuous from 0
    for (i, result) in indexed.iter().enumerate() {
        if let Value::List(pair) = result {
            assert_eq!(pair[1], Value::Int(i as i64));
        }
    }
}

#[test]
fn anonymous_traversal_key_value() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Use __.key() and __.value() in anonymous traversals
    let keys: Vec<Value> = g.v_ids([tg.alice]).properties().local(__.key()).to_list();

    assert_eq!(keys.len(), 2);

    let values: Vec<Value> = g.v_ids([tg.alice]).properties().local(__.value()).to_list();

    assert_eq!(values.len(), 2);
}

#[test]
fn anonymous_traversal_index() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Use __.index() in anonymous traversal
    let indexed: Vec<Value> = g.inject([1i64, 2i64, 3i64]).local(__.index()).to_list();

    // Each element should be indexed
    assert_eq!(indexed.len(), 3);
}

#[test]
fn anonymous_traversal_property_map() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Use __.property_map() in anonymous traversal
    let maps: Vec<Value> = g.v_ids([tg.alice]).local(__.property_map()).to_list();

    assert_eq!(maps.len(), 1);
    if let Value::Map(map) = &maps[0] {
        assert!(map.contains_key("name"));
        assert!(map.contains_key("age"));
    }
}

#[test]
fn complex_metadata_pipeline() {
    let tg = create_small_graph();
    let snapshot = tg.graph.snapshot();
    let g = snapshot.gremlin();

    // Complex pipeline using multiple metadata steps:
    // 1. Get all person vertices
    // 2. Get their properties
    // 3. Extract keys
    // 4. Dedup to unique keys
    // 5. Index the unique keys
    let indexed_keys: Vec<Value> = g
        .v()
        .has_label("person")
        .properties()
        .key()
        .dedup()
        .index()
        .to_list();

    // Persons have "name" and "age" properties
    assert_eq!(indexed_keys.len(), 2);

    // Each should be [key, index] pair
    for (i, result) in indexed_keys.iter().enumerate() {
        if let Value::List(pair) = result {
            assert_eq!(pair.len(), 2);
            assert_eq!(pair[1], Value::Int(i as i64));
        }
    }
}