Skip to main content

dm_database_sqllog2db/pipeline/
processor.rs

1use dm_database_parser_sqllog::Sqllog;
2
3/// 记录处理器接口:实现此接口即可加入处理管线
4/// 返回 true 表示保留该记录,false 表示丢弃
5pub trait LogProcessor: Send + Sync + std::fmt::Debug {
6    fn process(&self, record: &Sqllog) -> bool;
7
8    /// 使用已解析的 `Sqllog` 字段运行过滤逻辑(parser 库提供物化后的字段(栈上数据))。
9    /// 默认实现退化为 `process()`。
10    fn process_with_meta(&self, record: &Sqllog) -> bool {
11        self.process(record)
12    }
13}
14
15/// 处理管线:按顺序执行处理器,任一返回 false 则丢弃记录
16#[derive(Debug, Default)]
17pub struct Pipeline {
18    processors: Vec<Box<dyn LogProcessor>>,
19}
20
21impl Pipeline {
22    #[must_use]
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    pub fn add(&mut self, processor: Box<dyn LogProcessor>) {
28        self.processors.push(processor);
29    }
30
31    #[must_use]
32    pub fn is_empty(&self) -> bool {
33        self.processors.is_empty()
34    }
35
36    #[inline]
37    #[must_use]
38    pub fn run_with_meta(&self, record: &Sqllog) -> bool {
39        self.processors.iter().all(|p| p.process_with_meta(record))
40    }
41}