pub struct GraphVertex<G: GraphAccess> { /* private fields */ }Expand description
A vertex reference with access to the graph.
GraphVertex<G> provides TinkerPop-style vertex semantics where a vertex
object can access its properties and spawn traversals directly.
Unlike VertexId, which is a lightweight identifier, GraphVertex
carries a reference to the graph enabling:
- Direct property access without separate graph lookups
- Mutation through the vertex object
- Spawning traversals from the vertex
§Type Parameters
G: The graph type, typicallyArc<Graph>orArc<CowMmapGraph>
§Thread Safety
GraphVertex<G> is Clone, Send, and Sync when G is.
Multiple vertices can reference the same graph concurrently.
§Current State vs Snapshot
GraphVertex accesses the current graph state, not a snapshot.
This means:
- Property reads see the latest committed values
- Mutations are immediately visible to other
GraphVertexobjects - Concurrent modifications are possible (thread-safe)
If you need snapshot isolation, use GraphSnapshot directly.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::from([
("name".to_string(), "Alice".into()),
]));
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.label(), Some("person".to_string()));
assert_eq!(v.property("name"), Some(Value::String("Alice".to_string())));Implementations§
Source§impl<G: GraphAccess> GraphVertex<G>
impl<G: GraphAccess> GraphVertex<G>
Sourcepub fn new(id: VertexId, graph: G) -> Self
pub fn new(id: VertexId, graph: G) -> Self
Create a new GraphVertex.
This is typically called internally by terminal methods, but can
be used directly when you have a VertexId and graph reference.
§Arguments
id- The vertex IDgraph- A graph reference implementingGraphAccess
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());Sourcepub fn id(&self) -> VertexId
pub fn id(&self) -> VertexId
Get the vertex ID.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.id(), id);Sourcepub fn label(&self) -> Option<String>
pub fn label(&self) -> Option<String>
Get the vertex label.
Returns None if the vertex no longer exists in the graph.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.label(), Some("person".to_string()));Sourcepub fn property(&self, key: &str) -> Option<Value>
pub fn property(&self, key: &str) -> Option<Value>
Get a property value by key.
Returns None if:
- The vertex no longer exists
- The property key doesn’t exist
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::from([
("name".to_string(), "Alice".into()),
]));
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.property("name"), Some(Value::String("Alice".to_string())));
assert_eq!(v.property("nonexistent"), None);Sourcepub fn properties(&self) -> HashMap<String, Value>
pub fn properties(&self) -> HashMap<String, Value>
Get all properties as a map.
Returns an empty map if the vertex no longer exists.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::from([
("name".to_string(), "Alice".into()),
("age".to_string(), 30i64.into()),
]));
let v = GraphVertex::new(id, graph.clone());
let props = v.properties();
assert_eq!(props.len(), 2);
assert_eq!(props.get("name"), Some(&Value::String("Alice".to_string())));Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Check if the vertex still exists in the graph.
A vertex may no longer exist if it was deleted after the GraphVertex
was created.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert!(v.exists());
// After removal, exists() returns false
graph.remove_vertex(id).unwrap();
assert!(!v.exists());Sourcepub fn property_set(
&self,
key: &str,
value: impl Into<Value>,
) -> Result<(), StorageError>
pub fn property_set( &self, key: &str, value: impl Into<Value>, ) -> Result<(), StorageError>
Set a property value.
This mutates the graph directly. The change is immediately visible
to other GraphVertex objects and new snapshots.
§Errors
Returns StorageError::VertexNotFound if the vertex no longer exists.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::from([
("age".to_string(), 30i64.into()),
]));
let v = GraphVertex::new(id, graph.clone());
// Update the property
v.property_set("age", 31i64).unwrap();
// Change is immediately visible
assert_eq!(v.property("age"), Some(Value::Int(31)));Sourcepub fn graph(&self) -> &G
pub fn graph(&self) -> &G
Get a reference to the graph.
This can be useful for creating new vertices/edges or spawning new traversals.
Sourcepub fn to_value(&self) -> Value
pub fn to_value(&self) -> Value
Convert to a lightweight Value for serialization or storage.
This returns Value::Vertex(id), which is just the ID without
the graph reference.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.to_value(), Value::Vertex(id));Sourcepub fn add_edge(
&self,
label: &str,
to: &GraphVertex<G>,
) -> Result<GraphEdge<G>, StorageError>
pub fn add_edge( &self, label: &str, to: &GraphVertex<G>, ) -> Result<GraphEdge<G>, StorageError>
Add an outgoing edge to another vertex.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let alice = graph.add_vertex("person", HashMap::new());
let bob = graph.add_vertex("person", HashMap::new());
let alice_v = GraphVertex::new(alice, graph.clone());
let bob_v = GraphVertex::new(bob, graph.clone());
let edge = alice_v.add_edge("knows", &bob_v).unwrap();
assert_eq!(edge.label(), Some("knows".to_string()));
// Verify traversal works
let friends = alice_v.out("knows").to_list();
assert_eq!(friends.len(), 1);Sourcepub fn add_edge_to_id(
&self,
label: &str,
to: VertexId,
) -> Result<GraphEdge<G>, StorageError>
pub fn add_edge_to_id( &self, label: &str, to: VertexId, ) -> Result<GraphEdge<G>, StorageError>
Add an outgoing edge to a vertex by ID.
§Errors
Returns StorageError::VertexNotFound if either vertex doesn’t exist.
Sourcepub fn add_edge_with_props(
&self,
label: &str,
to: &GraphVertex<G>,
properties: HashMap<String, Value>,
) -> Result<GraphEdge<G>, StorageError>
pub fn add_edge_with_props( &self, label: &str, to: &GraphVertex<G>, properties: HashMap<String, Value>, ) -> Result<GraphEdge<G>, StorageError>
Add an outgoing edge with properties to another vertex.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let alice = graph.add_vertex("person", HashMap::new());
let bob = graph.add_vertex("person", HashMap::new());
let alice_v = GraphVertex::new(alice, graph.clone());
let bob_v = GraphVertex::new(bob, graph.clone());
let edge = alice_v.add_edge_with_props(
"knows",
&bob_v,
HashMap::from([("since".to_string(), 2020i64.into())])
).unwrap();
assert_eq!(edge.property("since"), Some(Value::Int(2020)));Sourcepub fn remove(&self) -> Result<(), StorageError>
pub fn remove(&self) -> Result<(), StorageError>
Remove this vertex from the graph.
This also removes all incident edges (both incoming and outgoing).
§Errors
Returns StorageError::VertexNotFound if the vertex doesn’t exist.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert!(v.exists());
v.remove().unwrap();
assert!(!v.exists());Sourcepub fn out(&self, label: &str) -> GraphVertexTraversal<G>
pub fn out(&self, label: &str) -> GraphVertexTraversal<G>
Traverse to outgoing adjacent vertices with a specific edge label.
This is the TinkerPop-style v.out(label) pattern.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let alice = graph.add_vertex("person", HashMap::from([
("name".to_string(), "Alice".into()),
]));
let bob = graph.add_vertex("person", HashMap::from([
("name".to_string(), "Bob".into()),
]));
graph.add_edge(alice, bob, "knows", HashMap::new()).unwrap();
let alice_v = GraphVertex::new(alice, graph.clone());
let friends = alice_v.out("knows").to_list();
assert_eq!(friends.len(), 1);
assert_eq!(friends[0].property("name"), Some(Value::String("Bob".to_string())));Sourcepub fn out_all(&self) -> GraphVertexTraversal<G>
pub fn out_all(&self) -> GraphVertexTraversal<G>
Traverse to outgoing adjacent vertices (all labels).
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
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 v = GraphVertex::new(a, graph.clone());
let neighbors = v.out_all().to_list();
assert_eq!(neighbors.len(), 1);Sourcepub fn in_(&self, label: &str) -> GraphVertexTraversal<G>
pub fn in_(&self, label: &str) -> GraphVertexTraversal<G>
Traverse to incoming adjacent vertices with a specific edge label.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let alice = graph.add_vertex("person", HashMap::new());
let bob = graph.add_vertex("person", HashMap::new());
graph.add_edge(alice, bob, "knows", HashMap::new()).unwrap();
let bob_v = GraphVertex::new(bob, graph.clone());
let knowers = bob_v.in_("knows").to_list();
assert_eq!(knowers.len(), 1);Sourcepub fn in_all(&self) -> GraphVertexTraversal<G>
pub fn in_all(&self) -> GraphVertexTraversal<G>
Traverse to incoming adjacent vertices (all labels).
Sourcepub fn both(&self, label: &str) -> GraphVertexTraversal<G>
pub fn both(&self, label: &str) -> GraphVertexTraversal<G>
Traverse to adjacent vertices in both directions with a specific edge label.
§Example
use interstellar::prelude::*;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let alice = graph.add_vertex("person", HashMap::new());
let bob = graph.add_vertex("person", HashMap::new());
graph.add_edge(alice, bob, "knows", HashMap::new()).unwrap();
let alice_v = GraphVertex::new(alice, graph.clone());
let both_neighbors = alice_v.both("knows").to_list();
assert_eq!(both_neighbors.len(), 1);Sourcepub fn both_all(&self) -> GraphVertexTraversal<G>
pub fn both_all(&self) -> GraphVertexTraversal<G>
Traverse to adjacent vertices in both directions (all labels).
Trait Implementations§
Source§impl<G: Clone + GraphAccess> Clone for GraphVertex<G>
impl<G: Clone + GraphAccess> Clone for GraphVertex<G>
Source§fn clone(&self) -> GraphVertex<G>
fn clone(&self) -> GraphVertex<G>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<G: GraphAccess> Debug for GraphVertex<G>
impl<G: GraphAccess> Debug for GraphVertex<G>
Source§impl<G: GraphAccess> Hash for GraphVertex<G>
impl<G: GraphAccess> Hash for GraphVertex<G>
Source§impl<G: GraphAccess> IntoVertexId for &GraphVertex<G>
impl<G: GraphAccess> IntoVertexId for &GraphVertex<G>
Source§fn into_vertex_id(self) -> VertexId
fn into_vertex_id(self) -> VertexId
Convert a borrowed GraphVertex reference to its VertexId.
This is the most common use case, allowing patterns like:
use interstellar::prelude::*;
use interstellar::value::IntoVertexId;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
// Borrow the vertex (common pattern)
assert_eq!((&v).into_vertex_id(), id);Source§impl<G: GraphAccess> IntoVertexId for GraphVertex<G>
impl<G: GraphAccess> IntoVertexId for GraphVertex<G>
Source§fn into_vertex_id(self) -> VertexId
fn into_vertex_id(self) -> VertexId
Convert an owned GraphVertex to its VertexId.
§Example
use interstellar::prelude::*;
use interstellar::value::IntoVertexId;
use interstellar::graph_elements::GraphVertex;
use std::sync::Arc;
use std::collections::HashMap;
let graph = Arc::new(Graph::new());
let id = graph.add_vertex("person", HashMap::new());
let v = GraphVertex::new(id, graph.clone());
assert_eq!(v.into_vertex_id(), id);