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
//! # Interstellar Gremlin Quickstart
//!
//! A minimal introduction to Interstellar's Gremlin-style traversal API.
//!
//! This example demonstrates:
//! - Creating an in-memory graph
//! - Adding vertices and edges with properties
//! - Basic traversals and navigation
//! - Terminal steps for collecting results
//! - Deleting elements
//!
//! Run: `cargo run --example quickstart_gremlin`
use interstellar::storage::Graph;
use interstellar::value::Value;
use std::sync::Arc;
fn main() {
println!("=== Interstellar Gremlin Quickstart ===\n");
// -------------------------------------------------------------------------
// 1. Create an in-memory graph
// -------------------------------------------------------------------------
let graph = Arc::new(Graph::new());
let g = graph.gremlin(Arc::clone(&graph));
// -------------------------------------------------------------------------
// 2. Mutations: Adding vertices with add_v() and property()
// -------------------------------------------------------------------------
println!("-- Adding Vertices --\n");
let alice = g
.add_v("Person")
.property("name", "Alice")
.property("age", 30i64)
.next()
.unwrap();
let bob = g
.add_v("Person")
.property("name", "Bob")
.property("age", 25i64)
.next()
.unwrap();
let carol = g
.add_v("Person")
.property("name", "Carol")
.property("age", 35i64)
.next()
.unwrap();
let acme = g
.add_v("Company")
.property("name", "Acme Corp")
.next()
.unwrap();
println!("Created: Alice, Bob, Carol (Person) and Acme Corp (Company)");
// Extract vertex IDs for edge creation
// With the new typed API, add_v().next() returns Option<GraphVertex>
// and we can use .id() to get the VertexId directly
let alice_id = alice.id();
let bob_id = bob.id();
let carol_id = carol.id();
let acme_id = acme.id();
// -------------------------------------------------------------------------
// 3. Mutations: Adding edges with add_e()
// -------------------------------------------------------------------------
println!("\n-- Adding Edges --\n");
g.add_e("knows")
.from_id(alice_id)
.to_id(bob_id)
.property("since", 2020i64)
.iterate();
g.add_e("knows").from_id(alice_id).to_id(carol_id).iterate();
g.add_e("knows").from_id(bob_id).to_id(carol_id).iterate();
g.add_e("works_at")
.from_id(alice_id)
.to_id(acme_id)
.iterate();
println!("Created: Alice->Bob, Alice->Carol, Bob->Carol (knows), Alice->Acme (works_at)");
// -------------------------------------------------------------------------
// 4. Basic Traversals: Counting and filtering
// -------------------------------------------------------------------------
println!("\n-- Basic Traversals --\n");
// count() - terminal step returning u64
println!("Total vertices: {}", g.v().count());
println!("Total edges: {}", g.e().count());
println!("Person vertices: {}", g.v().has_label("Person").count());
// -------------------------------------------------------------------------
// 5. Property Access: values() and to_list()
// -------------------------------------------------------------------------
println!("\n-- Property Access --\n");
// to_list() - terminal step returning Vec<Value>
let names: Vec<Value> = g.v().has_label("Person").values("name").to_list();
println!("All person names: {:?}", names);
// -------------------------------------------------------------------------
// 6. Navigation: out(), in_(), has_value()
// -------------------------------------------------------------------------
println!("\n-- Navigation --\n");
// Find Alice's connections via "knows" edges
let alice_knows: Vec<Value> = g
.v()
.has_value("name", Value::from("Alice"))
.out_label("knows")
.values("name")
.to_list();
println!("Alice knows: {:?}", alice_knows);
// Find who works at Acme (incoming "works_at" edges)
let acme_employees: Vec<Value> = g
.v()
.has_value("name", Value::from("Acme Corp"))
.in_label("works_at")
.values("name")
.to_list();
println!("Acme employees: {:?}", acme_employees);
// Multi-hop: friends of friends
let fof: Vec<Value> = g
.v()
.has_value("name", Value::from("Alice"))
.out_label("knows")
.out_label("knows")
.values("name")
.to_list();
println!("Friends of Alice's friends: {:?}", fof);
// -------------------------------------------------------------------------
// 7. Delete: drop() and iterate()
// -------------------------------------------------------------------------
println!("\n-- Delete --\n");
println!("Vertices before drop: {}", g.v().count());
// drop() marks elements for deletion, iterate() executes
g.v()
.has_value("name", Value::from("Carol"))
.drop()
.iterate();
println!("Vertices after dropping Carol: {}", g.v().count());
println!("\n=== Quickstart Complete ===");
}