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
//! GraphSON 3.0 import/export support.
//!
//! This module provides functions to serialize and deserialize graphs
//! in the GraphSON 3.0 format used by Apache TinkerPop.
//!
//! GraphSON is the JSON-based format used by TinkerPop-compatible graph databases
//! like Neo4j, JanusGraph, and Amazon Neptune. Supporting GraphSON enables:
//!
//! - **Interoperability** - Import/export with other graph databases
//! - **Human readability** - JSON format is inspectable and debuggable
//! - **Tooling ecosystem** - Leverage existing visualization and analysis tools
//! - **Migration paths** - Easy data migration from/to other graph databases
//!
//! # Quick Start
//!
//! ## Export a graph
//!
//! ```rust
//! use interstellar::graphson;
//! use interstellar::storage::Graph;
//! use interstellar::value::Value;
//! use std::collections::HashMap;
//!
//! let graph = Graph::new();
//! let alice = graph.add_vertex("person", HashMap::from([
//!     ("name".to_string(), Value::String("Alice".to_string())),
//!     ("age".to_string(), Value::Int(30)),
//! ]));
//!
//! let snapshot = graph.snapshot();
//! let json = graphson::to_string(&snapshot).unwrap();
//! println!("{}", json);
//! ```
//!
//! ## Import a graph
//!
//! ```rust
//! use interstellar::graphson;
//!
//! let json = r#"{"@type": "tinker:graph", "@value": {"vertices": [], "edges": []}}"#;
//! let graph = graphson::from_str(json).unwrap();
//! ```
//!
//! ## Export with schema
//!
//! ```rust
//! use interstellar::graphson;
//! use interstellar::storage::Graph;
//! use interstellar::schema::{SchemaBuilder, PropertyType, ValidationMode};
//!
//! let graph = Graph::new();
//! let snapshot = graph.snapshot();
//!
//! let schema = SchemaBuilder::new()
//!     .mode(ValidationMode::Strict)
//!     .vertex("person")
//!         .property("name", PropertyType::String)
//!         .done()
//!     .build();
//!
//! let json = graphson::to_string_with_schema(&snapshot, &schema).unwrap();
//! ```
//!
//! # GraphSON 3.0 Format
//!
//! GraphSON 3.0 uses explicit type wrappers for all non-string values:
//!
//! ```json
//! {"@type": "g:Int64", "@value": 42}
//! {"@type": "g:Double", "@value": 3.14}
//! {"@type": "g:List", "@value": [1, 2, 3]}
//! ```
//!
//! Strings, booleans, and null are untyped:
//!
//! ```json
//! "hello"
//! true
//! null
//! ```
//!
//! # Type Mappings
//!
//! | Interstellar Type | GraphSON Type |
//! |-------------------|---------------|
//! | `Value::Null` | JSON `null` |
//! | `Value::Bool` | JSON boolean |
//! | `Value::Int` | `g:Int64` |
//! | `Value::Float` | `g:Double` |
//! | `Value::String` | JSON string |
//! | `Value::List` | `g:List` |
//! | `Value::Map` | `g:Map` |

mod deserialize;
mod error;
mod schema_ext;
mod serialize;
mod types;

// Re-export error types
pub use error::{GraphSONError, Result};

// Re-export serialization functions
pub use serialize::{
    to_json_string, to_json_string_pretty, to_writer, to_writer_pretty, SerializeOptions,
};

// Re-export deserialization functions
pub use deserialize::{from_json_str, from_reader, DeserializeOptions, IdMode};

// Re-export schema extension
pub use schema_ext::{
    graphson_to_schema, schema_to_graphson, string_to_property_type, GraphSONSchema,
};

// Re-export types for advanced usage
pub use types::{
    GraphSONEdge, GraphSONGraph, GraphSONProperty, GraphSONValue, GraphSONVertex,
    GraphSONVertexProperty, TypedEdge, TypedGraph, TypedProperty, TypedVertex, TypedVertexProperty,
};

use crate::schema::GraphSchema;
use crate::storage::GraphStorage;
#[cfg(not(target_arch = "wasm32"))]
use std::fs::File;
#[cfg(not(target_arch = "wasm32"))]
use std::io::{BufReader, BufWriter};
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;

/// Serialize a graph to a JSON string.
///
/// This is the simplest way to export a graph. For pretty-printed output,
/// use [`to_string_pretty`].
///
/// # Example
///
/// ```rust
/// use interstellar::graphson;
/// use interstellar::storage::Graph;
///
/// let graph = Graph::new();
/// let snapshot = graph.snapshot();
/// let json = graphson::to_string(&snapshot).unwrap();
/// ```
pub fn to_string<S: GraphStorage>(storage: &S) -> std::result::Result<String, serde_json::Error> {
    to_json_string(storage)
}

/// Serialize a graph to a pretty-printed JSON string.
///
/// # Example
///
/// ```rust
/// use interstellar::graphson;
/// use interstellar::storage::Graph;
///
/// let graph = Graph::new();
/// let snapshot = graph.snapshot();
/// let json = graphson::to_string_pretty(&snapshot).unwrap();
/// assert!(json.contains('\n')); // Pretty-printed
/// ```
pub fn to_string_pretty<S: GraphStorage>(
    storage: &S,
) -> std::result::Result<String, serde_json::Error> {
    to_json_string_pretty(storage)
}

/// Deserialize a graph from a JSON string.
///
/// # Example
///
/// ```rust
/// use interstellar::graphson;
///
/// let json = r#"{"@type": "tinker:graph", "@value": {"vertices": [], "edges": []}}"#;
/// let graph = graphson::from_str(json).unwrap();
/// ```
pub fn from_str(json: &str) -> Result<crate::storage::Graph> {
    from_json_str(json)
}

/// Export a graph with schema metadata.
///
/// The output includes both the graph data and schema in the
/// `interstellar:GraphWithSchema` format.
///
/// # Example
///
/// ```rust
/// use interstellar::graphson;
/// use interstellar::storage::Graph;
/// use interstellar::schema::{SchemaBuilder, PropertyType, ValidationMode};
///
/// let graph = Graph::new();
/// let snapshot = graph.snapshot();
///
/// let schema = SchemaBuilder::new()
///     .mode(ValidationMode::Strict)
///     .vertex("person")
///         .property("name", PropertyType::String)
///         .done()
///     .build();
///
/// let json = graphson::to_string_with_schema(&snapshot, &schema).unwrap();
/// assert!(json.contains("interstellar:Schema"));
/// ```
pub fn to_string_with_schema<S: GraphStorage>(
    storage: &S,
    schema: &GraphSchema,
) -> std::result::Result<String, serde_json::Error> {
    let graph = serialize::serialize_graph(storage);
    let schema_json = schema_to_graphson(schema);

    let output = serde_json::json!({
        "@type": "interstellar:GraphWithSchema",
        "@value": {
            "schema": schema_json,
            "graph": graph
        }
    });

    serde_json::to_string_pretty(&output)
}

/// Export a graph to a file.
///
/// This function is not available on WASM targets.
/// Use [`to_string`] or [`to_string_pretty`] instead.
///
/// # Example
///
/// ```no_run
/// use interstellar::graphson;
/// use interstellar::storage::Graph;
///
/// let graph = Graph::new();
/// let snapshot = graph.snapshot();
/// graphson::export_to_file(&snapshot, "graph.json").unwrap();
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn export_to_file<S: GraphStorage, P: AsRef<Path>>(storage: &S, path: P) -> Result<()> {
    let file = File::create(path)?;
    let writer = BufWriter::new(file);
    to_writer_pretty(storage, writer)?;
    Ok(())
}

/// Import a graph from a file.
///
/// This function is not available on WASM targets.
/// Use [`from_str`] instead.
///
/// # Example
///
/// ```no_run
/// use interstellar::graphson;
///
/// let graph = graphson::import_from_file("graph.json").unwrap();
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn import_from_file<P: AsRef<Path>>(path: P) -> Result<crate::storage::Graph> {
    let file = File::open(path)?;
    let reader = BufReader::new(file);
    from_reader(reader)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{PropertyType, SchemaBuilder, ValidationMode};
    use crate::storage::Graph;
    use crate::value::Value;
    use std::collections::HashMap;

    #[test]
    fn test_empty_graph_roundtrip() {
        let graph = Graph::new();
        let snapshot = graph.snapshot();
        let json = to_string(&snapshot).unwrap();
        let imported = from_str(&json).unwrap();
        assert_eq!(imported.snapshot().vertex_count(), 0);
        assert_eq!(imported.snapshot().edge_count(), 0);
    }

    #[test]
    fn test_simple_graph_roundtrip() {
        let graph = Graph::new();
        let v1 = graph.add_vertex(
            "person",
            HashMap::from([("name".into(), Value::String("Alice".into()))]),
        );
        let v2 = graph.add_vertex(
            "person",
            HashMap::from([("name".into(), Value::String("Bob".into()))]),
        );
        graph.add_edge(v1, v2, "knows", HashMap::new()).unwrap();

        let snapshot = graph.snapshot();
        let json = to_string(&snapshot).unwrap();
        let imported = from_str(&json).unwrap();

        let imported_snapshot = imported.snapshot();
        assert_eq!(imported_snapshot.vertex_count(), 2);
        assert_eq!(imported_snapshot.edge_count(), 1);
    }

    #[test]
    fn test_value_roundtrip() {
        let values = vec![
            Value::Null,
            Value::Bool(true),
            Value::Bool(false),
            Value::Int(-42),
            Value::Int(0),
            Value::Int(i64::MAX),
            Value::Float(3.14159),
            Value::String("test".into()),
            Value::String("".into()),
            Value::List(vec![Value::Int(1), Value::Int(2)]),
        ];

        for original in values {
            let graph = Graph::new();
            graph.add_vertex("test", HashMap::from([("value".into(), original.clone())]));

            let snapshot = graph.snapshot();
            let json = to_string(&snapshot).unwrap();
            let imported = from_str(&json).unwrap();
            let imported_snapshot = imported.snapshot();

            let vertices: Vec<_> = imported_snapshot.all_vertices().collect();
            assert_eq!(vertices.len(), 1);
            assert_eq!(vertices[0].properties.get("value"), Some(&original));
        }
    }

    #[test]
    fn test_to_string_with_schema() {
        let graph = Graph::new();
        graph.add_vertex(
            "person",
            HashMap::from([("name".into(), Value::String("Alice".into()))]),
        );

        let snapshot = graph.snapshot();
        let schema = SchemaBuilder::new()
            .mode(ValidationMode::Strict)
            .vertex("person")
            .property("name", PropertyType::String)
            .done()
            .build();

        let json = to_string_with_schema(&snapshot, &schema).unwrap();

        assert!(json.contains("interstellar:GraphWithSchema"));
        assert!(json.contains("interstellar:Schema"));
        assert!(json.contains("person"));
        assert!(json.contains("strict"));
    }

    #[test]
    fn test_pretty_print_has_newlines() {
        let graph = Graph::new();
        graph.add_vertex("test", HashMap::new());

        let snapshot = graph.snapshot();
        let json = to_string_pretty(&snapshot).unwrap();

        assert!(json.contains('\n'));
    }

    #[test]
    fn test_invalid_json_error() {
        let result = from_str("not valid json");
        assert!(matches!(result, Err(GraphSONError::JsonParse(_))));
    }

    #[test]
    fn test_missing_vertex_error() {
        let json = r#"{
            "@type": "tinker:graph",
            "@value": {
                "vertices": [],
                "edges": [{
                    "@type": "g:Edge",
                    "@value": {
                        "id": {"@type": "g:Int64", "@value": 1},
                        "label": "knows",
                        "outV": {"@type": "g:Int64", "@value": 999},
                        "outVLabel": "person",
                        "inV": {"@type": "g:Int64", "@value": 1000},
                        "inVLabel": "person",
                        "properties": {}
                    }
                }]
            }
        }"#;
        let result = from_str(json);
        assert!(matches!(result, Err(GraphSONError::VertexNotFound(999))));
    }

    #[test]
    fn test_edge_properties_roundtrip() {
        let graph = Graph::new();
        let v1 = graph.add_vertex("person", HashMap::new());
        let v2 = graph.add_vertex("person", HashMap::new());
        graph
            .add_edge(
                v1,
                v2,
                "knows",
                HashMap::from([
                    ("since".into(), Value::Int(2020)),
                    ("weight".into(), Value::Float(0.95)),
                ]),
            )
            .unwrap();

        let snapshot = graph.snapshot();
        let json = to_string(&snapshot).unwrap();
        let imported = from_str(&json).unwrap();
        let imported_snapshot = imported.snapshot();

        let edges: Vec<_> = imported_snapshot.all_edges().collect();
        assert_eq!(edges.len(), 1);
        assert_eq!(edges[0].properties.get("since"), Some(&Value::Int(2020)));
        assert!(edges[0].properties.contains_key("weight"));
    }

    #[test]
    fn test_multiple_edges_roundtrip() {
        let graph = Graph::new();
        let v1 = graph.add_vertex("person", HashMap::new());
        let v2 = graph.add_vertex("person", HashMap::new());
        let v3 = graph.add_vertex("person", HashMap::new());

        graph.add_edge(v1, v2, "knows", HashMap::new()).unwrap();
        graph.add_edge(v2, v3, "knows", HashMap::new()).unwrap();
        graph.add_edge(v1, v3, "likes", HashMap::new()).unwrap();

        let snapshot = graph.snapshot();
        let json = to_string(&snapshot).unwrap();
        let imported = from_str(&json).unwrap();
        let imported_snapshot = imported.snapshot();

        assert_eq!(imported_snapshot.vertex_count(), 3);
        assert_eq!(imported_snapshot.edge_count(), 3);
    }

    #[test]
    fn test_complex_properties_roundtrip() {
        let mut map = crate::value::ValueMap::new();
        map.insert("nested".to_string(), Value::Int(42));

        let graph = Graph::new();
        graph.add_vertex(
            "test",
            HashMap::from([
                (
                    "list".into(),
                    Value::List(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
                ),
                ("map".into(), Value::Map(map)),
            ]),
        );

        let snapshot = graph.snapshot();
        let json = to_string(&snapshot).unwrap();
        let imported = from_str(&json).unwrap();
        let imported_snapshot = imported.snapshot();

        let vertices: Vec<_> = imported_snapshot.all_vertices().collect();
        assert_eq!(vertices.len(), 1);

        match vertices[0].properties.get("list") {
            Some(Value::List(items)) => {
                assert_eq!(items.len(), 3);
            }
            _ => panic!("Expected list property"),
        }

        match vertices[0].properties.get("map") {
            Some(Value::Map(m)) => {
                assert_eq!(m.get("nested"), Some(&Value::Int(42)));
            }
            _ => panic!("Expected map property"),
        }
    }
}