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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Trait for graph access operations needed by GraphVertex/GraphEdge.
//!
//! This module defines the [`GraphAccess`] trait which abstracts read and write
//! access to the graph. Both `Arc<Graph>` (in-memory) and `Arc<CowMmapGraph>`
//! (persistent) implement this trait, allowing `GraphVertex` and `GraphEdge`
//! to work with any storage backend.
//!
//! # Overview
//!
//! The `GraphAccess` trait provides the minimal interface that `GraphVertex` and
//! `GraphEdge` need to access vertex/edge data and perform mutations. This enables
//! a unified API across storage backends.
//!
//! # Thread Safety
//!
//! Implementations must be `Send + Sync` to allow concurrent access. The methods
//! use interior mutability patterns (like `RwLock`) internally, so they take `&self`
//! even for mutation operations.
//!
//! # Example
//!
//! ```rust
//! use interstellar::prelude::*;
//! use interstellar::graph_access::GraphAccess;
//! use std::sync::Arc;
//!
//! // Arc<Graph> implements GraphAccess
//! let graph: Arc<Graph> = Arc::new(Graph::new());
//!
//! // Use GraphAccess methods
//! let snapshot = graph.snapshot();
//! let g = snapshot.gremlin();
//! ```

use std::collections::HashMap;

use crate::error::StorageError;
use crate::storage::{Edge, Vertex};
use crate::value::{EdgeId, Value, VertexId};

/// Trait for graph access operations needed by GraphVertex/GraphEdge.
///
/// This trait provides the minimal interface that `GraphVertex` and `GraphEdge`
/// need to access vertex/edge data and perform mutations. Both `Graph` (in-memory)
/// and `CowMmapGraph` (persistent) implement this trait.
///
/// # Thread Safety
///
/// Implementations must be `Send + Sync` to allow concurrent access.
///
/// # Implementation Notes
///
/// The methods use interior mutability patterns (like `RwLock`) internally,
/// so they take `&self` even for mutation operations. This matches the
/// design of `Graph` which uses `RwLock<GraphState>` internally.
///
/// # Type Parameters
///
/// Implementations are typically for `Arc<Graph>` or `Arc<CowMmapGraph>`, not
/// the raw graph types, because `GraphVertex` and `GraphEdge` need to clone
/// the reference.
pub trait GraphAccess: Send + Sync + Clone + 'static {
    // =========================================================================
    // Read Operations
    // =========================================================================

    /// Get a vertex by ID.
    ///
    /// Returns `None` if the vertex doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let id = graph.add_vertex("person", HashMap::new());
    ///
    /// let vertex = graph.get_vertex(id);
    /// assert!(vertex.is_some());
    /// assert_eq!(vertex.unwrap().label, "person");
    /// ```
    fn get_vertex(&self, id: VertexId) -> Option<Vertex>;

    /// Get an edge by ID.
    ///
    /// Returns `None` if the edge doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    /// let edge_id = graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    ///
    /// let edge = graph.get_edge(edge_id);
    /// assert!(edge.is_some());
    /// assert_eq!(edge.unwrap().label, "knows");
    /// ```
    fn get_edge(&self, id: EdgeId) -> Option<Edge>;

    /// Get outgoing edge IDs from a vertex.
    ///
    /// Returns edge IDs for edges where the vertex is the source.
    /// Returns an empty Vec if the vertex doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    /// graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    ///
    /// let out_edges = graph.out_edge_ids(a);
    /// assert_eq!(out_edges.len(), 1);
    /// ```
    fn out_edge_ids(&self, vertex: VertexId) -> Vec<EdgeId>;

    /// Get incoming edge IDs to a vertex.
    ///
    /// Returns edge IDs for edges where the vertex is the destination.
    /// Returns an empty Vec if the vertex doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    /// graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    ///
    /// let in_edges = graph.in_edge_ids(b);
    /// assert_eq!(in_edges.len(), 1);
    /// ```
    fn in_edge_ids(&self, vertex: VertexId) -> Vec<EdgeId>;

    // =========================================================================
    // Write Operations
    // =========================================================================

    /// Set a property on a vertex.
    ///
    /// # Errors
    ///
    /// Returns `StorageError::VertexNotFound` if the vertex doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let id = graph.add_vertex("person", HashMap::new());
    ///
    /// graph.set_vertex_property(id, "name", Value::from("Alice")).unwrap();
    ///
    /// let vertex = graph.get_vertex(id).unwrap();
    /// assert_eq!(vertex.properties.get("name"), Some(&Value::String("Alice".to_string())));
    /// ```
    fn set_vertex_property(
        &self,
        id: VertexId,
        key: &str,
        value: Value,
    ) -> Result<(), StorageError>;

    /// Set a property on an edge.
    ///
    /// # Errors
    ///
    /// Returns `StorageError::EdgeNotFound` if the edge doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    /// let edge_id = graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    ///
    /// graph.set_edge_property(edge_id, "since", Value::from(2020i64)).unwrap();
    ///
    /// let edge = graph.get_edge(edge_id).unwrap();
    /// assert_eq!(edge.properties.get("since"), Some(&Value::Int(2020)));
    /// ```
    fn set_edge_property(&self, id: EdgeId, key: &str, value: Value) -> Result<(), StorageError>;

    /// Add a new edge between vertices.
    ///
    /// # Errors
    ///
    /// Returns `StorageError::VertexNotFound` if either `src` or `dst` doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    ///
    /// let edge_id = graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    /// assert!(graph.get_edge(edge_id).is_some());
    /// ```
    fn add_edge(
        &self,
        src: VertexId,
        dst: VertexId,
        label: &str,
        properties: HashMap<String, Value>,
    ) -> Result<EdgeId, StorageError>;

    /// Remove a vertex and all incident edges.
    ///
    /// # Errors
    ///
    /// Returns `StorageError::VertexNotFound` if the vertex doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let id = graph.add_vertex("person", HashMap::new());
    ///
    /// assert!(graph.get_vertex(id).is_some());
    /// graph.remove_vertex(id).unwrap();
    /// assert!(graph.get_vertex(id).is_none());
    /// ```
    fn remove_vertex(&self, id: VertexId) -> Result<(), StorageError>;

    /// Remove an edge.
    ///
    /// # Errors
    ///
    /// Returns `StorageError::EdgeNotFound` if the edge doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust
    /// use interstellar::prelude::*;
    /// use interstellar::graph_access::GraphAccess;
    /// use std::sync::Arc;
    /// use std::collections::HashMap;
    ///
    /// let graph = Arc::new(Graph::new());
    /// let a = graph.add_vertex("person", HashMap::new());
    /// let b = graph.add_vertex("person", HashMap::new());
    /// let edge_id = graph.add_edge(a, b, "knows", HashMap::new()).unwrap();
    ///
    /// assert!(graph.get_edge(edge_id).is_some());
    /// graph.remove_edge(edge_id).unwrap();
    /// assert!(graph.get_edge(edge_id).is_none());
    /// ```
    fn remove_edge(&self, id: EdgeId) -> Result<(), StorageError>;
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::Graph;
    use std::sync::Arc;

    #[test]
    fn arc_graph_implements_graph_access() {
        let graph = Arc::new(Graph::new());
        let id = graph.add_vertex(
            "person",
            HashMap::from([("name".to_string(), "Alice".into())]),
        );

        // Test get_vertex
        let v = graph.get_vertex(id).unwrap();
        assert_eq!(v.label, "person");
        assert_eq!(
            v.properties.get("name"),
            Some(&Value::String("Alice".to_string()))
        );

        // Test set_vertex_property
        graph
            .set_vertex_property(id, "age", Value::Int(30))
            .unwrap();
        let v = graph.get_vertex(id).unwrap();
        assert_eq!(v.properties.get("age"), Some(&Value::Int(30)));
    }

    #[test]
    fn arc_graph_edge_operations() {
        let graph = Arc::new(Graph::new());
        let a = graph.add_vertex("person", HashMap::new());
        let b = graph.add_vertex("person", HashMap::new());

        // Test add_edge via GraphAccess
        let edge_id = GraphAccess::add_edge(&graph, a, b, "knows", HashMap::new()).unwrap();
        assert!(graph.get_edge(edge_id).is_some());

        // Test out_edge_ids
        let out_edges = graph.out_edge_ids(a);
        assert_eq!(out_edges.len(), 1);
        assert_eq!(out_edges[0], edge_id);

        // Test in_edge_ids
        let in_edges = graph.in_edge_ids(b);
        assert_eq!(in_edges.len(), 1);
        assert_eq!(in_edges[0], edge_id);

        // Test set_edge_property
        graph
            .set_edge_property(edge_id, "since", Value::Int(2020))
            .unwrap();
        let e = graph.get_edge(edge_id).unwrap();
        assert_eq!(e.properties.get("since"), Some(&Value::Int(2020)));

        // Test remove_edge
        graph.remove_edge(edge_id).unwrap();
        assert!(graph.get_edge(edge_id).is_none());
    }

    #[test]
    fn arc_graph_vertex_removal() {
        let graph = Arc::new(Graph::new());
        let a = graph.add_vertex("person", HashMap::new());
        let b = graph.add_vertex("person", HashMap::new());
        let _edge_id = GraphAccess::add_edge(&graph, a, b, "knows", HashMap::new()).unwrap();

        // Remove vertex removes incident edges too
        graph.remove_vertex(a).unwrap();
        assert!(graph.get_vertex(a).is_none());
        // Edge should also be removed
        assert_eq!(graph.out_edge_ids(a).len(), 0);
    }

    #[test]
    fn arc_graph_error_on_nonexistent_vertex() {
        let graph = Arc::new(Graph::new());

        // set_vertex_property on nonexistent vertex
        let result = graph.set_vertex_property(VertexId(999), "name", Value::from("Bob"));
        assert!(matches!(result, Err(StorageError::VertexNotFound(_))));

        // remove_vertex on nonexistent vertex
        let result = graph.remove_vertex(VertexId(999));
        assert!(matches!(result, Err(StorageError::VertexNotFound(_))));
    }

    #[test]
    fn arc_graph_error_on_nonexistent_edge() {
        let graph = Arc::new(Graph::new());

        // set_edge_property on nonexistent edge
        let result = graph.set_edge_property(EdgeId(999), "since", Value::from(2020i64));
        assert!(matches!(result, Err(StorageError::EdgeNotFound(_))));

        // remove_edge on nonexistent edge
        let result = graph.remove_edge(EdgeId(999));
        assert!(matches!(result, Err(StorageError::EdgeNotFound(_))));
    }

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

        // add_edge with nonexistent dst
        let result = GraphAccess::add_edge(&graph, a, VertexId(999), "knows", HashMap::new());
        assert!(matches!(result, Err(StorageError::VertexNotFound(_))));

        // add_edge with nonexistent src
        let result = GraphAccess::add_edge(&graph, VertexId(999), a, "knows", HashMap::new());
        assert!(matches!(result, Err(StorageError::VertexNotFound(_))));
    }

    #[test]
    fn arc_graph_clone_and_thread_safety() {
        use std::thread;

        let graph = Arc::new(Graph::new());
        let id = graph.add_vertex("person", HashMap::new());

        // Clone the Arc - both references should work
        let graph2 = Arc::clone(&graph);

        let handle = thread::spawn(move || {
            let v = graph2.get_vertex(id);
            assert!(v.is_some());
        });

        handle.join().unwrap();

        // Original should still work
        assert!(graph.get_vertex(id).is_some());
    }

    // =========================================================================
    // Arc<CowMmapGraph> Tests (mmap feature)
    // =========================================================================

    #[cfg(feature = "mmap")]
    mod mmap_tests {
        use super::*;
        use crate::storage::CowMmapGraph;
        use tempfile::tempdir;

        fn temp_db_path() -> (tempfile::TempDir, std::path::PathBuf) {
            let dir = tempdir().unwrap();
            let path = dir.path().join("test.db");
            (dir, path)
        }

        #[test]
        fn arc_mmap_graph_implements_graph_access() {
            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());
            let id = graph
                .add_vertex(
                    "person",
                    HashMap::from([("name".to_string(), "Alice".into())]),
                )
                .unwrap();

            // Test get_vertex
            let v = graph.get_vertex(id).unwrap();
            assert_eq!(v.label, "person");
            assert_eq!(
                v.properties.get("name"),
                Some(&Value::String("Alice".to_string()))
            );

            // Test set_vertex_property
            graph
                .set_vertex_property(id, "age", Value::Int(30))
                .unwrap();
            let v = graph.get_vertex(id).unwrap();
            assert_eq!(v.properties.get("age"), Some(&Value::Int(30)));
        }

        #[test]
        fn arc_mmap_graph_edge_operations() {
            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());
            let a = graph.add_vertex("person", HashMap::new()).unwrap();
            let b = graph.add_vertex("person", HashMap::new()).unwrap();

            // Test add_edge via GraphAccess
            let edge_id = GraphAccess::add_edge(&graph, a, b, "knows", HashMap::new()).unwrap();
            assert!(graph.get_edge(edge_id).is_some());

            // Test out_edge_ids
            let out_edges = graph.out_edge_ids(a);
            assert_eq!(out_edges.len(), 1);
            assert_eq!(out_edges[0], edge_id);

            // Test in_edge_ids
            let in_edges = graph.in_edge_ids(b);
            assert_eq!(in_edges.len(), 1);
            assert_eq!(in_edges[0], edge_id);

            // Test set_edge_property
            graph
                .set_edge_property(edge_id, "since", Value::Int(2020))
                .unwrap();
            let e = graph.get_edge(edge_id).unwrap();
            assert_eq!(e.properties.get("since"), Some(&Value::Int(2020)));

            // Test remove_edge
            graph.remove_edge(edge_id).unwrap();
            assert!(graph.get_edge(edge_id).is_none());
        }

        #[test]
        fn arc_mmap_graph_vertex_removal() {
            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());
            let a = graph.add_vertex("person", HashMap::new()).unwrap();
            let b = graph.add_vertex("person", HashMap::new()).unwrap();
            let _edge_id = GraphAccess::add_edge(&graph, a, b, "knows", HashMap::new()).unwrap();

            // Remove vertex removes incident edges too
            graph.remove_vertex(a).unwrap();
            assert!(graph.get_vertex(a).is_none());
            // Edge should also be removed
            assert_eq!(graph.out_edge_ids(a).len(), 0);
        }

        #[test]
        fn arc_mmap_graph_error_on_nonexistent_vertex() {
            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());

            // set_vertex_property on nonexistent vertex
            let result = graph.set_vertex_property(VertexId(999), "name", Value::from("Bob"));
            assert!(matches!(result, Err(StorageError::VertexNotFound(_))));

            // remove_vertex on nonexistent vertex
            let result = graph.remove_vertex(VertexId(999));
            assert!(matches!(result, Err(StorageError::VertexNotFound(_))));
        }

        #[test]
        fn arc_mmap_graph_error_on_nonexistent_edge() {
            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());

            // set_edge_property on nonexistent edge
            let result = graph.set_edge_property(EdgeId(999), "since", Value::from(2020i64));
            assert!(matches!(result, Err(StorageError::EdgeNotFound(_))));

            // remove_edge on nonexistent edge
            let result = graph.remove_edge(EdgeId(999));
            assert!(matches!(result, Err(StorageError::EdgeNotFound(_))));
        }

        #[test]
        fn arc_mmap_graph_clone_and_thread_safety() {
            use std::thread;

            let (_dir, path) = temp_db_path();
            let graph = Arc::new(CowMmapGraph::open(&path).unwrap());
            let id = graph.add_vertex("person", HashMap::new()).unwrap();

            // Clone the Arc - both references should work
            let graph2 = Arc::clone(&graph);

            let handle = thread::spawn(move || {
                let v = graph2.get_vertex(id);
                assert!(v.is_some());
            });

            handle.join().unwrap();

            // Original should still work
            assert!(graph.get_vertex(id).is_some());
        }
    }
}