Skip to main content

SqlxPgGraph

Struct SqlxPgGraph 

Source
pub struct SqlxPgGraph { /* private fields */ }
Expand description

Async PostgreSQL graph backend over a shared sqlx::PgPool.

Every constructor applies the schema and runs pending migrations on connect, matching PgGraph. An optional table_prefix namespaces every backing table and index so several graphs can coexist in one database (identical semantics to PgGraph’s *_with_prefix constructors). The prefix is validated by is_identifier_safe before any SQL is emitted, keeping the identifier interpolation injection-safe.

§Transaction exposure

pool plus append_edges_in_tx and remove_edges_for_node_in_tx let a caller coordinate a multi-table transaction (e.g. klr’s prune: delete chunks + vectors + edges atomically). Begin a tx on pool(), run cross-table DML, call the *_in_tx helpers with the same &mut PgConnection, then commit or rollback — mirroring PgVectorIndex::remove_in_tx.

Implementations§

Source§

impl SqlxPgGraph

Source

pub async fn from_pool(pool: PgPool) -> Result<Self>

Adopt a caller-owned PgPool, apply schema + migrations, and return a ready backend. Uses the default (empty) table prefix.

Source

pub async fn from_pool_with_prefix(pool: PgPool, prefix: &str) -> Result<Self>

Like from_pool, but every table/index is namespaced under prefix. The prefix is validated by is_identifier_safe before any SQL runs; an unsafe value returns KernelError::Store.

Source

pub async fn connect(url: &str) -> Result<Self>

Connect to url (libpq connstring / postgresql://…) with an 8-connection pool, apply schema + migrations, and return a ready backend. Uses the default (empty) table prefix.

Source

pub async fn connect_with_prefix(url: &str, prefix: &str) -> Result<Self>

Like connect, but every table/index is namespaced under prefix.

Source

pub fn pool(&self) -> &PgPool

The underlying connection pool.

Callers that need cross-table transactional consistency — e.g. pruning a law’s chunks plus their graph edges atomically — can pool().begin() and run their own DML, then call the append_edges_in_tx / remove_edges_for_node_in_tx helpers on the same &mut PgConnection.

Source

pub async fn current_version(&self) -> Result<u32>

Recorded graph schema version from {prefix}_meta, or 0 if unset.

Source

pub async fn migrate(&self) -> Result<u32>

Apply pending migrations up to GRAPH_SCHEMA_VERSION. No-op when current.

Runs in a single transaction with rollback on failure — matching pg.rs::migrate. Returns the new version.

Source

pub async fn upsert_node(&self, node: &GraphNode) -> Result<()>

Insert or update a node (upsert by id). Mirrors PgGraph::upsert_node.

Source

pub async fn read_node(&self, id: &str) -> Result<Option<GraphNode>>

Read a node by id, or None if absent.

Source

pub async fn delete_node(&self, id: &str) -> Result<bool>

Delete a node by id. Returns true if a row was removed.

Source

pub async fn append_edge(&self, edge: &GraphEdge) -> Result<()>

Append a single edge (ON CONFLICT DO NOTHING). Mirrors PgGraph::append_edge.

Source

pub async fn edges_for_node(&self, node_id: &str) -> Result<Vec<GraphEdge>>

All edges touching node_id (either endpoint). Mirrors PgGraph::edges_for_node.

Source

pub async fn delete_edge(&self, id: &str) -> Result<bool>

Delete a single edge by id. Returns true if a row was removed.

Source

pub async fn append_edges(&self, edges: &[GraphEdge]) -> Result<()>

Batch-insert edges in chunks of 5000, each chunk its own transaction (ON CONFLICT DO NOTHING per row). Mirrors PgGraph::append_edges. sqlx caches the prepared statement per connection, so the repeated INSERT reuses one parse/plan per chunk.

Source

pub async fn append_edges_in_tx( &self, tx: &mut PgConnection, edges: &[GraphEdge], ) -> Result<()>

Append edges within a caller-provided transaction (commit is the caller’s responsibility — this method does not commit). Enables atomic cross-table writes: begin a tx on pool, write chunks / vectors / other relations, call this with the same &mut PgConnection, then commit(). A failure anywhere rolls back the whole set.

Source

pub async fn edges_for_node_dir( &self, node_id: &str, dir: EdgeDirection, relation: Option<&str>, ) -> Result<Vec<GraphEdge>>

Directed, optionally relation-filtered edge lookup. Mirrors PgGraph::edges_for_node_dir. dir selects out / in / both; relation further restricts to a single relationship type when Some.

Source

pub async fn neighbors_weighted( &self, seed_ids: &[String], dir: EdgeDirection, relation: Option<&str>, ) -> Result<Vec<(String, f64)>>

Aggregate neighbor weights from seed nodes, optionally direction- and relation-filtered. Mirrors PgGraph::neighbors_weighted: walks the source/target halves, uses = ANY($1) for the seed set, GROUP BY the opposite endpoint, and SUM(weight). Seeds themselves are excluded. Capped at 100 seeds.

Source

pub async fn remove_edges_for_node(&self, node_id: &str) -> Result<()>

Remove every edge touching node_id (either endpoint). Mirrors PgGraph::remove_edges_for_node.

Source

pub async fn remove_edges_for_node_in_tx( &self, tx: &mut PgConnection, node_id: &str, ) -> Result<()>

Remove edges for node_id within a caller-provided transaction (no commit here). Same atomic-prune pattern as Self::append_edges_in_tx.

Source

pub async fn search_nodes( &self, query: &str, limit: usize, ) -> Result<Vec<GraphNode>>

ILIKE substring search over (title, body, tags) for each whitespace-separated term (AND of terms). Mirrors PgGraph::search_nodes — extension-free vanilla PostgreSQL ILIKE with escaped, wrapped patterns.

Source

pub async fn related_nodes( &self, start_id: &str, depth: usize, ) -> Result<Vec<String>>

Bidirectional BFS up to depth hops from start_id via a recursive CTE. Mirrors PgGraph::related_nodes. Returns distinct neighbor IDs (the start node is excluded), capped at 500.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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