zero_client/ivm/
operator.rs1use super::{Change, Comparator, Constraint, Node, NodeStream, PipelineError, Row, SourceChange};
6use crate::ast::{Condition, OrderPart, System};
7use crate::map::OrderedMap;
8use crate::schema::{Column, Table};
9use std::collections::HashSet;
10use std::rc::{Rc, Weak};
11
12#[derive(Clone)]
14pub struct SourceSchema {
15 pub table_name: String,
16 pub columns: OrderedMap<Column>,
17 pub primary_key: Vec<String>,
18 pub relationships: Vec<(String, Rc<SourceSchema>)>,
20 pub is_hidden: bool,
21 pub system: System,
22 pub compare_rows: Comparator,
23 pub sort: Option<Vec<OrderPart>>,
25}
26
27impl SourceSchema {
28 pub fn relationship(&self, name: &str) -> Option<&Rc<SourceSchema>> {
29 self.relationships
30 .iter()
31 .find(|(n, _)| n == name)
32 .map(|(_, s)| s)
33 }
34
35 pub fn relationship_names(&self) -> Vec<&str> {
36 self.relationships.iter().map(|(n, _)| n.as_str()).collect()
37 }
38
39 pub fn adding(&self, name: &str, schema: Rc<SourceSchema>) -> SourceSchema {
41 let mut copy = self.clone();
42 match copy.relationships.iter_mut().find(|(n, _)| n == name) {
43 Some(entry) => entry.1 = schema,
44 None => copy.relationships.push((name.to_owned(), schema)),
45 }
46 copy
47 }
48}
49
50impl std::fmt::Debug for SourceSchema {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("SourceSchema")
53 .field("table_name", &self.table_name)
54 .field("primary_key", &self.primary_key)
55 .field("relationships", &self.relationship_names())
56 .field("is_hidden", &self.is_hidden)
57 .field("system", &self.system)
58 .field("sort", &self.sort)
59 .finish()
60 }
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
65pub enum Basis {
66 At,
67 After,
68}
69
70#[derive(Clone, Debug)]
72pub struct Start {
73 pub row: Row,
74 pub basis: Basis,
75}
76
77#[derive(Clone, Debug, Default)]
79pub struct FetchRequest {
80 pub constraint: Option<Constraint>,
81 pub multi_constraints: Option<Vec<Vec<Constraint>>>,
83 pub start: Option<Start>,
86 pub reverse: bool,
88 pub filter: Option<Condition>,
91}
92
93pub trait InputBase {
95 fn get_schema(&self) -> Rc<SourceSchema>;
96 fn destroy(&self);
97}
98
99pub trait Input: InputBase {
102 fn set_output(&self, output: Weak<dyn Output>);
103 fn fetch(&self, req: &FetchRequest) -> NodeStream;
104}
105
106pub trait Output {
112 fn push(&self, change: Change, pusher: &dyn InputBase);
113 fn reconcile(&self, _pusher: &dyn InputBase) {}
115}
116
117pub trait Operator: Input + Output {}
120
121impl<T: Input + Output> Operator for T {}
122
123pub fn is_same(a: &dyn InputBase, b: &dyn InputBase) -> bool {
125 std::ptr::addr_eq(a, b)
126}
127
128pub fn no_output() -> Weak<dyn Output> {
131 Weak::<ClosureOutput>::new()
132}
133
134pub fn no_filter_output() -> Weak<dyn FilterOutput> {
136 Weak::<NoFilterOutput>::new()
137}
138
139pub struct NoFilterOutput;
141
142impl Output for NoFilterOutput {
143 fn push(&self, _: Change, _: &dyn InputBase) {}
144}
145
146impl FilterOutput for NoFilterOutput {
147 fn begin_filter(&self) {}
148 fn filter(&self, _: &Node) -> bool {
149 false
150 }
151 fn end_filter(&self) {}
152}
153
154pub struct ClosureOutput {
157 on_push: Box<dyn Fn(Change)>,
158 on_reconcile: Box<dyn Fn()>,
159}
160
161impl ClosureOutput {
162 pub fn new(push: impl Fn(Change) + 'static, reconcile: impl Fn() + 'static) -> Rc<Self> {
163 Rc::new(ClosureOutput {
164 on_push: Box::new(push),
165 on_reconcile: Box::new(reconcile),
166 })
167 }
168}
169
170impl Output for ClosureOutput {
171 fn push(&self, change: Change, _pusher: &dyn InputBase) {
172 (self.on_push)(change)
173 }
174
175 fn reconcile(&self, _pusher: &dyn InputBase) {
176 (self.on_reconcile)()
177 }
178}
179
180pub trait FilterInput: InputBase {
182 fn set_filter_output(&self, output: Weak<dyn FilterOutput>);
183}
184
185pub trait FilterOutput: Output {
188 fn begin_filter(&self);
189 fn filter(&self, node: &Node) -> bool;
190 fn end_filter(&self);
191}
192
193pub trait FilterOperator: FilterInput + FilterOutput {}
194
195impl<T: FilterInput + FilterOutput> FilterOperator for T {}
196
197pub trait Source {
199 fn table_schema(&self) -> &Table;
200 fn connect(
202 &self,
203 sort: Option<Vec<OrderPart>>,
204 filters: Option<&Condition>,
205 split_edit_keys: Option<&HashSet<String>>,
206 ) -> Result<Rc<dyn SourceInput>, PipelineError>;
207 fn push(&self, change: SourceChange);
208}
209
210pub trait SourceInput: Input {
212 fn fully_applied_filters(&self) -> bool;
215}