Skip to main content

GraphVertex

Struct GraphVertex 

Source
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, typically Arc<Graph> or Arc<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 GraphVertex objects
  • 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>

Source

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 ID
  • graph - A graph reference implementing GraphAccess
§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());
Source

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);
Source

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()));
Source

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);
Source

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())));
Source

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());
Source

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)));
Source

pub fn graph(&self) -> &G

Get a reference to the graph.

This can be useful for creating new vertices/edges or spawning new traversals.

Source

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));
Source

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);
Source

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.

Source

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)));
Source

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());
Source

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())));
Source

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);
Source

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);
Source

pub fn in_all(&self) -> GraphVertexTraversal<G>

Traverse to incoming adjacent vertices (all labels).

Source

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);
Source

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>

Source§

fn clone(&self) -> GraphVertex<G>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<G: GraphAccess> Debug for GraphVertex<G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<G: GraphAccess> Hash for GraphVertex<G>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<G: GraphAccess> IntoVertexId for &GraphVertex<G>

Source§

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>

Source§

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);
Source§

impl<G: GraphAccess> PartialEq for GraphVertex<G>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<G: GraphAccess> Eq for GraphVertex<G>

Auto Trait Implementations§

§

impl<G> Freeze for GraphVertex<G>
where G: Freeze,

§

impl<G> RefUnwindSafe for GraphVertex<G>
where G: RefUnwindSafe,

§

impl<G> Send for GraphVertex<G>

§

impl<G> Sync for GraphVertex<G>

§

impl<G> Unpin for GraphVertex<G>
where G: Unpin,

§

impl<G> UnwindSafe for GraphVertex<G>
where G: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V