GraphService

Struct GraphService 

Source
pub struct GraphService<B: GraphBackend> {
    backend: Arc<B>,
}
Expand description

High-level service for knowledge graph operations.

Wraps a GraphBackend and provides:

  • Entity CRUD with deduplication hints
  • Relationship management
  • Graph traversal and path finding
  • Integration with memory system

§Thread Safety

The service is thread-safe when the underlying backend is thread-safe. Both SqliteGraphBackend and InMemoryGraphBackend are thread-safe.

Fields§

§backend: Arc<B>

Implementations§

Source§

impl<B: GraphBackend> GraphService<B>

Source

pub fn new(backend: B) -> Self

Creates a new graph service with the given backend.

Source

pub const fn with_shared_backend(backend: Arc<B>) -> Self

Creates a new graph service with a shared backend.

Source

pub fn backend(&self) -> &B

Returns a reference to the underlying backend.

Source

pub fn store_entity(&self, entity: &Entity) -> Result<()>

Stores an entity in the graph.

If an entity with the same ID exists, it will be updated.

§Errors

Returns an error if the storage operation fails.

Source

pub fn get_entity(&self, id: &EntityId) -> Result<Option<Entity>>

Retrieves an entity by ID.

§Returns

Some(entity) if found, None otherwise.

§Errors

Returns an error if the retrieval fails.

Source

pub fn query_entities(&self, query: &EntityQuery) -> Result<Vec<Entity>>

Queries entities matching the given criteria.

§Errors

Returns an error if the query fails.

Source

pub fn find_by_type( &self, entity_type: EntityType, limit: usize, ) -> Result<Vec<Entity>>

Finds entities by type with a limit.

Convenience method for common entity type queries.

§Errors

Returns an error if the query fails.

Source

pub fn find_by_name( &self, name: &str, entity_type: Option<EntityType>, domain: Option<&Domain>, limit: usize, ) -> Result<Vec<Entity>>

Finds entities by name with optional type and domain filtering.

Performs case-insensitive partial matching on entity names and aliases.

§Errors

Returns an error if the search fails.

Source

pub fn delete_entity(&self, id: &EntityId) -> Result<bool>

Deletes an entity and its relationships/mentions.

§Returns

true if the entity existed and was deleted.

§Errors

Returns an error if the deletion fails.

Source

pub fn merge_entities( &self, entity_ids: &[EntityId], canonical_name: &str, ) -> Result<Entity>

Merges multiple entities into one canonical entity.

The first entity ID becomes the canonical entity. All relationships and mentions from other entities are redirected to the canonical entity.

§Arguments
  • entity_ids - IDs of entities to merge (first is canonical)
  • canonical_name - The name for the merged entity
§Returns

The merged entity.

§Errors

Returns an error if:

  • No entity IDs provided
  • Canonical entity not found
  • Merge operation fails
Source

pub fn find_duplicates( &self, entity: &Entity, threshold: f32, ) -> Result<Vec<Entity>>

Finds potential duplicate entities based on name similarity.

Returns entities that may be duplicates of the given entity, useful for deduplication workflows.

§Errors

Returns an error if the search fails.

Source

pub fn store_relationship(&self, relationship: &Relationship) -> Result<()>

Stores a relationship between entities.

If a relationship with the same (from, to, type) exists, it will be updated.

§Errors

Returns an error if the storage operation fails.

Source

pub fn relate( &self, from: &EntityId, to: &EntityId, relationship_type: RelationshipType, ) -> Result<Relationship>

Creates a relationship between two entities.

Convenience method that creates and stores a relationship.

§Errors

Returns an error if:

  • Either entity doesn’t exist
  • Storage operation fails
Source

pub fn query_relationships( &self, query: &RelationshipQuery, ) -> Result<Vec<Relationship>>

Queries relationships matching the given criteria.

§Errors

Returns an error if the query fails.

Source

pub fn get_outgoing_relationships( &self, entity_id: &EntityId, ) -> Result<Vec<Relationship>>

Gets all relationships from an entity.

§Errors

Returns an error if the query fails.

Source

pub fn get_incoming_relationships( &self, entity_id: &EntityId, ) -> Result<Vec<Relationship>>

Gets all relationships to an entity.

§Errors

Returns an error if the query fails.

Source

pub fn delete_relationships(&self, query: &RelationshipQuery) -> Result<usize>

Deletes relationships matching the query.

§Returns

Number of relationships deleted.

§Errors

Returns an error if the deletion fails.

Source

pub fn get_relationship_types( &self, from: &EntityId, to: &EntityId, ) -> Result<Vec<RelationshipType>>

Gets all relationship types between two entities.

§Errors

Returns an error if the query fails.

Source

pub fn record_mention( &self, entity_id: &EntityId, memory_id: &MemoryId, ) -> Result<()>

Records an entity mention in a memory.

§Errors

Returns an error if the storage operation fails.

Source

pub fn get_mentions(&self, entity_id: &EntityId) -> Result<Vec<EntityMention>>

Gets all mentions of an entity.

§Errors

Returns an error if the query fails.

Source

pub fn get_entities_in_memory( &self, memory_id: &MemoryId, ) -> Result<Vec<Entity>>

Gets all entities mentioned in a memory.

§Errors

Returns an error if the query fails.

Source

pub fn remove_mentions_for_memory(&self, memory_id: &MemoryId) -> Result<usize>

Removes entity mentions when a memory is deleted.

§Returns

Number of mentions removed.

§Errors

Returns an error if the deletion fails.

Source

pub fn traverse( &self, start: &EntityId, max_depth: u32, relationship_types: Option<&[RelationshipType]>, min_confidence: Option<f32>, ) -> Result<TraversalResult>

Traverses the graph from a starting entity.

Performs breadth-first search up to the specified depth.

§Arguments
  • start - Starting entity ID
  • max_depth - Maximum traversal depth
  • relationship_types - Optional filter for relationship types
  • min_confidence - Optional minimum confidence threshold
§Returns

Entities and relationships discovered during traversal.

§Errors

Returns an error if the traversal fails.

Source

pub fn find_path( &self, from: &EntityId, to: &EntityId, max_depth: u32, ) -> Result<Option<TraversalResult>>

Finds the shortest path between two entities.

§Arguments
  • from - Starting entity ID
  • to - Target entity ID
  • max_depth - Maximum path length
§Returns

Some(result) with the path if found, None otherwise.

§Errors

Returns an error if the search fails.

Source

pub fn get_neighbors( &self, entity_id: &EntityId, depth: u32, ) -> Result<Vec<Entity>>

Gets neighbors of an entity within a given depth.

Convenience method for single-depth traversal.

§Errors

Returns an error if the traversal fails.

Source

pub fn query_entities_at( &self, query: &EntityQuery, point: &BitemporalPoint, ) -> Result<Vec<Entity>>

Queries entities at a specific point in time.

Uses bitemporal filtering to find entities that were:

  • Valid at the specified time (valid_at)
  • Known in the system at the specified time (as_of)
§Errors

Returns an error if the query fails.

Source

pub fn query_relationships_at( &self, query: &RelationshipQuery, point: &BitemporalPoint, ) -> Result<Vec<Relationship>>

Queries relationships at a specific point in time.

§Errors

Returns an error if the query fails.

Source

pub fn close_entity_valid_time( &self, id: &EntityId, end_time: i64, ) -> Result<()>

Closes an entity’s valid time (marks it as no longer valid).

Used when an entity is superseded or becomes invalid.

§Errors

Returns an error if the entity doesn’t exist or update fails.

Source

pub fn close_relationship_valid_time( &self, from: &EntityId, to: &EntityId, relationship_type: RelationshipType, end_time: i64, ) -> Result<()>

Closes a relationship’s valid time.

§Errors

Returns an error if the relationship doesn’t exist or update fails.

Source

pub fn get_stats(&self) -> Result<GraphStats>

Gets graph statistics.

§Errors

Returns an error if the statistics cannot be retrieved.

Source

pub fn clear(&self) -> Result<()>

Clears all graph data.

Warning: This permanently deletes all entities, relationships, and mentions.

§Errors

Returns an error if the clear operation fails.

Auto Trait Implementations§

§

impl<B> Freeze for GraphService<B>

§

impl<B> RefUnwindSafe for GraphService<B>
where B: RefUnwindSafe,

§

impl<B> Send for GraphService<B>

§

impl<B> Sync for GraphService<B>

§

impl<B> Unpin for GraphService<B>

§

impl<B> UnwindSafe for GraphService<B>
where B: RefUnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,