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
//! # Interstellar
//!
//! A high-performance Rust graph traversal library with a Gremlin-style fluent API.
//!
//! Interstellar provides a type-safe, ergonomic interface for graph operations with
//! support for both in-memory and persistent (memory-mapped) storage backends.
//!
//! ## Features
//!
//! - **Gremlin-style Fluent API**: Chainable traversal steps with lazy evaluation
//! - **Dual Storage Backends**: In-memory (HashMap-based) and memory-mapped (persistent)
//! - **Anonymous Traversals**: Composable fragments via the [`__`] factory module
//! - **Rich Predicate System**: Filtering with [`p::eq`], [`p::gt`], [`p::within`], [`p::regex`], and more
//! - **GQL Query Language**: Declarative queries as an alternative to the programmatic API
//! - **Thread-Safe**: Snapshot-based concurrency for safe parallel reads
//!
//! ## Quick Start
//!
//! ```rust
//! use interstellar::prelude::*;
//! use std::collections::HashMap;
//!
//! // Create a new graph
//! let graph = Graph::new();
//!
//! // Add vertices with properties
//! let alice = graph.add_vertex("person", HashMap::from([
//! ("name".to_string(), Value::from("Alice")),
//! ("age".to_string(), Value::from(30i64)),
//! ]));
//!
//! let bob = graph.add_vertex("person", HashMap::from([
//! ("name".to_string(), Value::from("Bob")),
//! ("age".to_string(), Value::from(25i64)),
//! ]));
//!
//! // Add an edge
//! graph.add_edge(alice, bob, "knows", HashMap::new()).unwrap();
//!
//! // Create a snapshot for read access
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//!
//! // Traverse: find all people Alice knows
//! let friends = g.v_ids([alice])
//! .out_labels(&["knows"])
//! .values("name")
//! .to_list();
//!
//! assert_eq!(friends, vec![Value::String("Bob".to_string())]);
//! ```
//!
//! ## Simpler Quick Start
//!
//! For the simplest setup, use the convenience constructor:
//!
//! ```rust
//! use interstellar::prelude::*;
//!
//! // Create an empty in-memory graph
//! let graph = Graph::in_memory();
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//!
//! // Count vertices (0 in empty graph)
//! assert_eq!(g.v().count(), 0);
//! ```
//!
//! ## Module Overview
//!
//! | Module | Description |
//! |--------|-------------|
//! | [`graph`] | Graph container with snapshot-based concurrency ([`Graph`], [`GraphSnapshot`]) |
//! | [`storage`] | Storage backends ([`Graph`](storage::Graph), [`MmapGraph`](storage::mmap::MmapGraph)) |
//! | [`traversal`] | Fluent traversal API, steps, predicates ([`p`]), anonymous traversals ([`__`]) |
//! | [`value`] | Core value types ([`Value`], [`VertexId`], [`EdgeId`]) |
//! | [`error`] | Error types ([`StorageError`], [`TraversalError`], [`MutationError`](error::MutationError)) |
//! | [`gql`] | GQL query language parser and compiler (requires `gql` feature) |
//! | [`algorithms`] | Graph algorithms (BFS, DFS, shortest path) |
//!
//! ## Traversal API Overview
//!
//! The traversal API follows the Gremlin pattern with source, navigation, filter,
//! transform, branch, and terminal steps:
//!
//! ```rust
//! use interstellar::prelude::*;
//!
//! let graph = Graph::in_memory();
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//!
//! // Source steps - where traversals begin
//! let _ = g.v(); // All vertices
//! let _ = g.e(); // All edges
//! // g.v_ids([id1, id2]) // Specific vertices
//! // g.inject([1, 2, 3]) // Inject arbitrary values
//!
//! // Navigation steps - traverse the graph structure
//! // .out("label") // Follow outgoing edges
//! // .in_("label") // Follow incoming edges
//! // .both("label") // Both directions
//! // .out_e() / .in_e() / .both_e() // Get edge objects
//! // .out_v() / .in_v() / .other_v() // Get edge endpoints
//!
//! // Filter steps - narrow down results
//! // .has("key") // Has property
//! // .has_value("key", value) // Property equals value
//! // .has_where("key", p::gt(30)) // Property matches predicate
//! // .has_label("person") // Filter by label
//! // .dedup() // Remove duplicates
//! // .limit(10) // Take first N
//!
//! // Transform steps - modify or extract data
//! // .values("name") // Extract property value
//! // .value_map() // All properties as map
//! // .id() / .label() // Element metadata
//! // .map(|v| ...) // Custom transformation
//!
//! // Terminal steps - execute and collect results
//! let count = g.v().count(); // Count results
//! let list = g.v().to_list(); // Collect all results
//! // .first() // First result (Option)
//! // .one() // Exactly one (Result)
//! ```
//!
//! ## Predicates
//!
//! The [`p`] module provides predicates for filtering:
//!
//! ```rust
//! use interstellar::prelude::*;
//!
//! // Comparison predicates
//! let _ = p::eq(30); // Equals
//! let _ = p::gt(30); // Greater than
//! let _ = p::between(20, 40); // Range [20, 40)
//!
//! // Collection predicates
//! let _ = p::within([1i64, 2, 3]); // In set
//!
//! // String predicates
//! let _ = p::containing("sub"); // Contains substring
//! let _ = p::regex(r"^\d+$"); // Regex match
//!
//! // Logical predicates
//! let _ = p::and(p::gt(20), p::lt(40));
//! let _ = p::or(p::lt(20), p::gt(40));
//! let _ = p::not(p::eq(30));
//! ```
//!
//! ## Anonymous Traversals
//!
//! The [`__`] module provides anonymous traversal fragments for composition:
//!
//! ```rust
//! use interstellar::prelude::*;
//!
//! let graph = Graph::in_memory();
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//!
//! // Use in branch steps
//! let _ = g.v().union(vec![
//! __.out_labels(&["knows"]),
//! __.out_labels(&["created"]),
//! ]);
//!
//! // Use in repeat
//! let _ = g.v().repeat(__.out_labels(&["parent"])).times(3);
//!
//! // Use in where clauses
//! // g.v().where_(__.out("knows").count().is_(p::gt(3)))
//! ```
//!
//! ## GQL Query Language
//!
//! For declarative queries, enable the `gql` feature and use the GQL interface:
//!
//! ```toml
//! [dependencies]
//! interstellar = { version = "0.1", features = ["gql"] }
//! ```
//!
//! ```rust,ignore
//! use interstellar::prelude::*;
//! use std::collections::HashMap;
//!
//! let graph = Graph::new();
//! graph.add_vertex("Person", HashMap::from([
//! ("name".to_string(), Value::from("Alice")),
//! ]));
//!
//! let snapshot = graph.snapshot();
//!
//! // Execute a GQL query (requires `gql` feature)
//! let results = graph.gql("MATCH (n:Person) RETURN n.name").unwrap();
//! assert_eq!(results.len(), 1);
//! ```
//!
//! ## Error Handling
//!
//! Interstellar uses `Result` types throughout. See the [`error`] module for details
//! on error types and recovery patterns:
//!
//! ```rust
//! use interstellar::prelude::*;
//!
//! let graph = Graph::in_memory();
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//!
//! // Handle "exactly one" requirement
//! match g.v().one() {
//! Ok(vertex) => println!("Found: {:?}", vertex),
//! Err(TraversalError::NotOne(0)) => println!("No vertices found"),
//! Err(TraversalError::NotOne(n)) => println!("Too many: {}", n),
//! Err(e) => println!("Error: {}", e),
//! }
//! ```
//!
//! ## Storage Backends
//!
//! ### In-Memory (Default)
//!
//! The COW (Copy-on-Write) graph for development and small graphs:
//!
//! ```rust
//! use interstellar::storage::Graph;
//!
//! let graph = Graph::new();
//! // Use directly with traversal API
//! ```
//!
//! ### Memory-Mapped (Persistent)
//!
//! Persistent storage with write-ahead logging. Enable with the `mmap` feature:
//!
//! ```toml
//! [dependencies]
//! interstellar = { version = "0.1", features = ["mmap"] }
//! ```
//!
//! ```ignore
//! use interstellar::storage::MmapGraph;
//!
//! let graph = MmapGraph::open("my_graph.db").unwrap();
//! // Data persists across restarts
//! ```
//!
//! ## Feature Flags
//!
//! | Feature | Description | Default |
//! |---------|-------------|---------|
//! | `graphson` | GraphSON import/export support | Yes |
//! | `mmap` | Memory-mapped persistent storage (**not available on WASM**) | No |
//! | `gql` | GQL query language support | No |
//! | `full-text` | Full-text search with Tantivy (**not available on WASM**) | No |
//! | `full` | Enable all features | No |
//!
//! Note: In-memory graph storage is always available (core functionality).
//!
//! ## WASM Support
//!
//! Interstellar supports WebAssembly targets (`wasm32-unknown-unknown`).
//! The following features work on WASM:
//!
//! - Core in-memory `Graph`
//! - Full traversal API
//! - GQL query language (with `gql` feature)
//! - GraphSON serialization (string-based only; file I/O excluded)
//!
//! Build for WASM:
//!
//! ```bash
//! cargo build --target wasm32-unknown-unknown
//! cargo build --target wasm32-unknown-unknown --features gql
//! ```
//!
//! ## Thread Safety
//!
//! [`Graph`] uses a readers-writer lock for safe concurrent access:
//!
//! - Multiple [`GraphSnapshot`]s can exist simultaneously (shared reads)
//! - [`GraphMut`] requires exclusive access (exclusive writes)
//! - Snapshots see a consistent view of the graph
//!
//! ```rust
//! use interstellar::prelude::*;
//! use std::sync::Arc;
//! use std::thread;
//!
//! let graph = Arc::new(Graph::in_memory());
//!
//! // Multiple threads can read concurrently
//! let handles: Vec<_> = (0..4).map(|_| {
//! let g = Arc::clone(&graph);
//! thread::spawn(move || {
//! let snap = g.snapshot();
//! snap.gremlin().v().count()
//! })
//! }).collect();
//!
//! for handle in handles {
//! let _ = handle.join().unwrap();
//! }
//! ```
//!
//! ## Examples
//!
//! The `examples/` directory contains comprehensive demonstrations:
//!
//! - `basic_traversal.rs` - Getting started with traversals
//! - `navigation_steps.rs` - Graph navigation patterns
//! - `filter_steps.rs` - Filtering and predicates
//! - `branch_steps.rs` - Branching and conditional logic
//! - `repeat_steps.rs` - Iterative traversals
//! - `british_royals.rs` - Real-world family tree queries
//! - `nba.rs` - Sports analytics queries
//!
//! Run examples with:
//!
//! ```bash
//! cargo run --example basic_traversal
//! cargo run --example british_royals
//! ```
/// Creates a property map for vertices and edges.
///
/// This macro provides a convenient way to construct `HashMap<String, Value>`
/// for use with [`Graph::add_vertex`](storage::Graph::add_vertex)
/// and [`Graph::add_edge`](storage::Graph::add_edge).
///
/// Values are automatically converted using [`Into<Value>`](Value), so you can
/// use native Rust types directly.
///
/// # Example
///
/// ```rust
/// use interstellar::prelude::*;
/// use interstellar::storage::Graph;
///
/// let graph = Graph::new();
///
/// // Create a vertex with properties
/// let alice = graph.add_vertex("person", props! {
/// "name" => "Alice",
/// "age" => 30i64,
/// "active" => true,
/// });
///
/// // Empty properties
/// let bob = graph.add_vertex("person", props! {});
///
/// // Edge with properties
/// graph.add_edge(alice, bob, "knows", props! {
/// "since" => 2020i64,
/// "weight" => 0.95,
/// }).unwrap();
/// ```
///
/// # Supported Types
///
/// Any type that implements `Into<Value>` can be used:
/// - `&str` and `String` → `Value::String`
/// - `i64` → `Value::Int`
/// - `f64` → `Value::Float`
/// - `bool` → `Value::Bool`
/// - `Vec<Value>` → `Value::List`
/// - `HashMap<String, Value>` → `Value::Map`
///
/// **Note**: For integers, use `i64` explicitly (e.g., `30i64`) since Rust's
/// default integer type is `i32` which doesn't implement `Into<Value>`.
// Internal time abstraction for WASM compatibility
pub
// WASM JavaScript bindings
// Re-export graph element types for convenience
pub use ;
// Re-export GraphAccess trait for generic graph element support
pub use GraphAccess;
/// The prelude module re-exports commonly used types.
///
/// Import the prelude to get started quickly:
///
/// ```rust
/// use interstellar::prelude::*;
///
/// let graph = Graph::new();
/// let snapshot = graph.snapshot();
/// let g = snapshot.gremlin();
/// ```
///
/// This imports:
///
/// - Graph types: [`Graph`], [`GraphSnapshot`]
/// - Persistent graph types (mmap feature): [`PersistentGraph`], [`PersistentSnapshot`]
/// - Traversal: [`Traversal`], [`BoundTraversal`], [`GraphTraversalSource`]
/// - Anonymous traversals: [`__`]
/// - Predicates: [`p`]
/// - Values: [`Value`], [`VertexId`], [`EdgeId`], [`ElementId`]
/// - Paths: [`Path`], [`PathElement`], [`PathValue`], [`Traverser`]
/// - Errors: [`StorageError`], [`TraversalError`]
/// - Macros: [`props!`]
pub use *;