cep

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package cep 实现 MATCH_RECOGNIZE 模式识别(SQL:2016)。

架构:

  • pattern.go 组合式模式树 → NFA(Thompson 构造),支持序列/量词/选择/分组/PERMUTE。
  • nfa.go NFA 状态机 + epsilon 闭包。
  • eval.go DEFINE/MEASURES 求值:表达式在 NewEngine 期预编译(prepare), 导航/聚合/符号限定字段改写为占位符,复用手写 expr 引擎求值。
  • engine.go 分区 NFA 模拟 + LRU + WITHIN/行数上限(有界)+ MEASURES 投影 + 匹配输出。

CEP 是独立子系统,不依赖也不污染现有直连/窗口/分析路径与三套求值器。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalDefine

func EvalDefine(cond string, buffer []map[string]any, labels []string, candidate map[string]any, candLabel string, symbols map[string]bool) bool

EvalDefine 即时编译并求值符号的 DEFINE 条件(布尔)。仅供测试/外部使用; 引擎热路径用 Engine.evalDefine(预编译产物)。buffer=已匹配行,candidate=待分类行。 不携带 SUBSET 成员表——含 SUBSET 限定的引用(如 S.v)需用 EvalDefineWithSubsets。

func EvalDefineWithSubsets

func EvalDefineWithSubsets(cond string, buffer []map[string]any, labels []string, candidate map[string]any, candLabel string, symbols map[string]bool, subsets map[string][]string) bool

EvalDefineWithSubsets 同 EvalDefine,但携带 SUBSET 成员表,支持 SUBSET 限定的引用。

func EvalMeasure

func EvalMeasure(expression string, rows []map[string]any, labels []string, cur, matchNumber int, symbols map[string]bool) (any, bool)

EvalMeasure 即时编译并求值 MEASURES 表达式(值)。仅供测试/外部使用。 不携带 SUBSET 成员表——含 SUBSET 限定的引用需用 EvalMeasureWithSubsets。

func EvalMeasureWithSubsets

func EvalMeasureWithSubsets(expression string, rows []map[string]any, labels []string, cur, matchNumber int, symbols map[string]bool, subsets map[string][]string) (any, bool)

EvalMeasureWithSubsets 同 EvalMeasure,但携带 SUBSET 成员表,支持 SUBSET 限定的引用。

func Validate

func Validate(spec *types.MatchRecognizeSpec) error

Validate 校验 spec 可编译(模式树、ORDER BY、DEFINE/MEASURES 表达式)。供 Execute 期 fail-fast。

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine 是一个 MATCH_RECOGNIZE 查询的 CEP 引擎:按分区维护 NFA 运行态,逐事件推进, 匹配完成时产出 MEASURES 投影行。线程安全(Process 串行;分区 LRU)。

func NewEngine

func NewEngine(spec *types.MatchRecognizeSpec) (*Engine, error)

NewEngine 编译 spec 为 NFA 并构建引擎,预编译全部 DEFINE/MEASURES 表达式。

func (*Engine) Flush

func (e *Engine) Flush() []map[string]any

Flush 冲刷所有分区中「已可接受但未终结」的部分匹配(如流末未界的 A+ 突发)。 适配器在 Stop 时调用,避免流末未闭合的匹配丢失。返回冲刷出的输出行。

func (*Engine) Process

func (e *Engine) Process(row map[string]any, partitionKey string) []map[string]any

Process 投入一行事件到其分区,返回本事件完成的匹配经 MEASURES 投影后的输出行。

func (*Engine) SetLogger

func (e *Engine) SetLogger(l logger.Logger)

SetLogger 覆盖求值诊断日志器(per-engine,避免包级共享导致多 Stream 互相覆盖/竞争)。

func (*Engine) SetMaxPartitions

func (e *Engine) SetMaxPartitions(n int)

SetMaxPartitions 覆盖分区上限。

func (*Engine) SetMaxRunRows

func (e *Engine) SetMaxRunRows(n int)

SetMaxRunRows 覆盖单次部分匹配行数上限。

func (*Engine) SetMaxRuns

func (e *Engine) SetMaxRuns(n int)

SetMaxRuns 覆盖单分区活跃部分匹配数上限。

func (*Engine) Start

func (e *Engine) Start()

Start 启动 WITHIN 主动过期 sweeper(仅 within>0 时)。幂等。 sweeper 定期用 wall-clock 扫描,清掉超窗的部分匹配,避免空闲分区内存滞留。

func (*Engine) Stop

func (e *Engine) Stop()

Stop 停止 sweeper 并 join。幂等;未 Start 时直接返回。

type NFA

type NFA struct {
	// contains filtered or unexported fields
}

NFA 是编译后的模式自动机:start 经 epsilon/match 转移到达 accept。

func Compile

func Compile(node *types.PatternNode) (*NFA, error)

Compile 把模式树编译为 NFA(Thompson 构造)。组合式节点:序列/选择/分组/PERMUTE/量词。 PatternExclusion({- -},absence)暂不支持,返回明确错误。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL