Skip to main content

EntityGraph

Struct EntityGraph 

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

实体图

描述一组关联关系的抓取计划。

§示例

use sz_orm_core::entity_graph::EntityGraph;

let mut graph = EntityGraph::new();
graph.add_edge("user", "profile");
graph.add_edge("user", "posts");

Implementations§

Source§

impl EntityGraph

Source

pub fn new() -> Self

创建空实体图

Source

pub fn add_edge( &mut self, parent_field: impl Into<String>, relation: impl Into<String>, ) -> &mut Self

添加一条边

Source

pub fn add_edge_with_graph( &mut self, parent_field: impl Into<String>, relation: impl Into<String>, sub_graph: EntityGraph, ) -> &mut Self

添加一条带子图的边(嵌套抓取)

Source

pub fn edges(&self) -> &[GraphEdge]

返回所有边

Source

pub fn edge_count(&self) -> usize

返回边的数量

Source

pub fn relations_of(&self, parent_field: &str) -> Vec<&GraphEdge>

查询某个父字段的所有关联

Source

pub fn all_relations(&self) -> Vec<String>

收集图中所有关联名(去重)

Source

pub fn all_parent_fields(&self) -> Vec<String>

收集图中所有父字段(去重)

Source

pub fn is_empty(&self) -> bool

是否为空图

Source

pub fn all_relations_recursive(&self) -> Vec<String>

递归收集图中所有关联(含子图)

Source

pub fn detect_cycles(&self) -> Result<(), Vec<String>>

P2-7:检测实体图中的循环引用

使用 DFS 遍历图(含嵌套子图),检测是否存在循环路径。 循环引用会导致递归 eager load 时栈溢出,必须在加载前检测。

§算法
  1. 展平:递归收集主图 + 所有子图的边到统一邻接表
  2. 三色标记 DFS
    • 白色(未访问):节点尚未访问
    • 灰色(访问中):节点正在当前 DFS 路径中,若再次遇到则发现回边(循环)
    • 黑色(已完成):节点及其所有子节点已访问完毕
§返回
  • Ok(()):无循环引用
  • Err(cycle_path):检测到循环,cycle_path 是循环路径上的节点列表 (如 ["user", "posts", "user"] 表示 user → posts → user 的循环)
§示例
use sz_orm_core::entity_graph::EntityGraph;

// 无循环:user → posts → comments
let mut graph = EntityGraph::new();
graph.add_edge("user", "posts");
graph.add_edge("posts", "comments");
assert!(graph.detect_cycles().is_ok());

// 有循环:user → posts → user
let mut graph = EntityGraph::new();
graph.add_edge_with_graph("user", "posts", {
    let mut sub = EntityGraph::new();
    sub.add_edge("posts", "user");
    sub
});
assert!(graph.detect_cycles().is_err());
Source

pub fn detect_duplicate_edges(&self) -> Result<(), Vec<(String, String)>>

P2-7:检测并拒绝重复边(相同 parent_field + relation)

重复边不会导致栈溢出,但会产生冗余 SQL 查询,应检测并警告。

返回 Ok(()) 表示无重复;返回 Err(duplicates) 表示有重复边。

Source

pub fn validate(&self) -> Result<(), String>

P2-7:综合校验(循环引用 + 重复边)

load_eager / load_join 前调用,确保图结构安全。

Trait Implementations§

Source§

impl Clone for EntityGraph

Source§

fn clone(&self) -> EntityGraph

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for EntityGraph

Source§

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

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

impl Default for EntityGraph

Source§

fn default() -> EntityGraph

Returns the “default value” for a type. Read more

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