Skip to main content

uni_fork/
host.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4//! Host traits that invert the dependency between the fork diff/promote
5//! engine and uni-db.
6//!
7//! The diff/promote logic needs to run Cypher queries against a session, read
8//! the storage manager's UID indexes, and bulk-insert promoted rows into a
9//! primary transaction. All of those are uni-db types, so the engine is made
10//! generic over these traits and uni-db supplies the implementations
11//! (`Session: ForkQueryHost`, `Transaction: ForkPromoteSink`).
12
13use std::sync::Arc;
14
15use uni_common::Properties;
16use uni_common::Result;
17use uni_common::core::id::Vid;
18use uni_store::storage::manager::StorageManager;
19
20/// Read-side host for the diff engine and promote scans.
21///
22/// Implemented by `uni_db::Session`.
23#[async_trait::async_trait]
24pub trait ForkQueryHost: Send + Sync {
25    /// Run a read-only Cypher query and return its result.
26    async fn query(&self, cypher: &str) -> Result<uni_query::QueryResult>;
27
28    /// The session's storage manager (used to read the shared UID index).
29    fn storage(&self) -> Arc<StorageManager>;
30
31    /// The session's schema manager (labels + edge types to diff over).
32    fn schema(&self) -> Arc<uni_common::core::schema::SchemaManager>;
33}
34
35/// Write-side host for `run_promote` — the primary-targeted transaction
36/// that promoted rows are bulk-inserted into.
37///
38/// Implemented by `uni_db::Transaction`. The edge sink takes
39/// `(src_vid, dst_vid, properties)` tuples (not `uni_bulk::EdgeData`), so
40/// uni-fork does not need to depend on uni-bulk.
41#[async_trait::async_trait]
42pub trait ForkPromoteSink: Send + Sync {
43    /// Bulk-insert promoted vertices; returns the allocated VIDs in order.
44    async fn bulk_insert_vertices(&self, label: &str, rows: Vec<Properties>) -> Result<Vec<Vid>>;
45
46    /// Bulk-insert promoted edges between resolved primary endpoints.
47    async fn bulk_insert_edges(
48        &self,
49        edge_type: &str,
50        edges: Vec<(Vid, Vid, Properties)>,
51    ) -> Result<()>;
52}