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>
impl<B: GraphBackend> GraphService<B>
Creates a new graph service with a shared backend.
Sourcepub fn store_entity(&self, entity: &Entity) -> Result<()>
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.
Sourcepub fn query_entities(&self, query: &EntityQuery) -> Result<Vec<Entity>>
pub fn query_entities(&self, query: &EntityQuery) -> Result<Vec<Entity>>
Sourcepub fn find_by_type(
&self,
entity_type: EntityType,
limit: usize,
) -> Result<Vec<Entity>>
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.
Sourcepub fn find_by_name(
&self,
name: &str,
entity_type: Option<EntityType>,
domain: Option<&Domain>,
limit: usize,
) -> Result<Vec<Entity>>
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.
Sourcepub fn delete_entity(&self, id: &EntityId) -> Result<bool>
pub fn delete_entity(&self, id: &EntityId) -> Result<bool>
Sourcepub fn merge_entities(
&self,
entity_ids: &[EntityId],
canonical_name: &str,
) -> Result<Entity>
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
Sourcepub fn find_duplicates(
&self,
entity: &Entity,
threshold: f32,
) -> Result<Vec<Entity>>
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.
Sourcepub fn store_relationship(&self, relationship: &Relationship) -> Result<()>
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.
Sourcepub fn relate(
&self,
from: &EntityId,
to: &EntityId,
relationship_type: RelationshipType,
) -> Result<Relationship>
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
Sourcepub fn query_relationships(
&self,
query: &RelationshipQuery,
) -> Result<Vec<Relationship>>
pub fn query_relationships( &self, query: &RelationshipQuery, ) -> Result<Vec<Relationship>>
Sourcepub fn get_outgoing_relationships(
&self,
entity_id: &EntityId,
) -> Result<Vec<Relationship>>
pub fn get_outgoing_relationships( &self, entity_id: &EntityId, ) -> Result<Vec<Relationship>>
Sourcepub fn get_incoming_relationships(
&self,
entity_id: &EntityId,
) -> Result<Vec<Relationship>>
pub fn get_incoming_relationships( &self, entity_id: &EntityId, ) -> Result<Vec<Relationship>>
Sourcepub fn delete_relationships(&self, query: &RelationshipQuery) -> Result<usize>
pub fn delete_relationships(&self, query: &RelationshipQuery) -> Result<usize>
Sourcepub fn get_relationship_types(
&self,
from: &EntityId,
to: &EntityId,
) -> Result<Vec<RelationshipType>>
pub fn get_relationship_types( &self, from: &EntityId, to: &EntityId, ) -> Result<Vec<RelationshipType>>
Sourcepub fn get_mentions(&self, entity_id: &EntityId) -> Result<Vec<EntityMention>>
pub fn get_mentions(&self, entity_id: &EntityId) -> Result<Vec<EntityMention>>
Sourcepub fn remove_mentions_for_memory(&self, memory_id: &MemoryId) -> Result<usize>
pub fn remove_mentions_for_memory(&self, memory_id: &MemoryId) -> Result<usize>
Sourcepub fn traverse(
&self,
start: &EntityId,
max_depth: u32,
relationship_types: Option<&[RelationshipType]>,
min_confidence: Option<f32>,
) -> Result<TraversalResult>
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 IDmax_depth- Maximum traversal depthrelationship_types- Optional filter for relationship typesmin_confidence- Optional minimum confidence threshold
§Returns
Entities and relationships discovered during traversal.
§Errors
Returns an error if the traversal fails.
Sourcepub fn find_path(
&self,
from: &EntityId,
to: &EntityId,
max_depth: u32,
) -> Result<Option<TraversalResult>>
pub fn find_path( &self, from: &EntityId, to: &EntityId, max_depth: u32, ) -> Result<Option<TraversalResult>>
Sourcepub fn get_neighbors(
&self,
entity_id: &EntityId,
depth: u32,
) -> Result<Vec<Entity>>
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.
Sourcepub fn query_entities_at(
&self,
query: &EntityQuery,
point: &BitemporalPoint,
) -> Result<Vec<Entity>>
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.
Sourcepub fn query_relationships_at(
&self,
query: &RelationshipQuery,
point: &BitemporalPoint,
) -> Result<Vec<Relationship>>
pub fn query_relationships_at( &self, query: &RelationshipQuery, point: &BitemporalPoint, ) -> Result<Vec<Relationship>>
Sourcepub fn close_entity_valid_time(
&self,
id: &EntityId,
end_time: i64,
) -> Result<()>
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.
Sourcepub fn close_relationship_valid_time(
&self,
from: &EntityId,
to: &EntityId,
relationship_type: RelationshipType,
end_time: i64,
) -> Result<()>
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.
Sourcepub fn get_stats(&self) -> Result<GraphStats>
pub fn get_stats(&self) -> Result<GraphStats>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].