Module aggregation_helper

Module aggregation_helper 

Source
Expand description

节点聚合助手 - 并行的自下而上树形节点聚合计算

提供高性能的树形结构聚合计算,支持自定义聚合逻辑和并行处理。

§核心特性

  • 并行计算: 使用 rayon 实现同层节点的并行处理
  • 层级策略: 支持自定义层级计算策略,内置缓存优化
  • 类型安全: 泛型设计支持任意聚合数据类型
  • 高性能: 全局 tokio Runtime,避免重复创建开销

§示例

use mf_core::helpers::aggregation_helper::NodeAggregator;

// 定义聚合逻辑:求和
let aggregator = NodeAggregator::new(
    |node_id: NodeId, state: Arc<State>, cache: Arc<ConcurrentCache<i64>>| async move {
        let node = state.get_node(&node_id)?;
        let children: Vec<NodeId> = state.get_children(&node_id);

        let sum: i64 = children.iter()
            .filter_map(|child_id| cache.get(child_id))
            .sum();

        Ok(sum + node.get_value())
    },
    CachedLevelStrategy::new(state.clone()),
);

// 从叶子节点开始聚合
let results = aggregator.aggregate_up(&leaf_node_id, state)?;

Structs§

CachedLevelStrategy
缓存层级策略 - 缓存已计算的层级结果
ConcurrentCache
并发安全的缓存,用于存储节点聚合结果
ConcurrentCounter
并发安全的原子计数器
DefaultLevelStrategy
默认层级策略 - 每次都遍历父节点链计算层级
NodeAggregator
并行节点聚合器

Traits§

LevelStrategy
层级计算策略 Trait
NodeAggregatorTrait
节点聚合器 Trait

Type Aliases§

NodeProcessor
节点聚合处理器类型定义