zero-client 0.3.1

A native Rust client for Zero.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Helpers shared by `Join` and `FlippedJoin` (`join-utils.ts`), and the
//! join index (`join-index.ts`).

use super::*;
use crate::json::{self, Value};
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet, VecDeque};
use std::rc::Rc;

/// `generateWithOverlay`: applies the in-flight child change `overlay` to a
/// sorted child stream, so a parent that has yet to receive the change sees
/// the children as they were.
pub fn generate_with_overlay(
    stream: NodeStream,
    overlay: Change,
    schema: Rc<SourceSchema>,
) -> NodeStream {
    Box::new(OrderedOverlay {
        stream,
        overlay,
        schema,
        applied: false,
        edit_old_applied: false,
        edit_new_applied: false,
        queue: VecDeque::new(),
        finished: false,
    })
}

struct OrderedOverlay {
    stream: NodeStream,
    overlay: Change,
    schema: Rc<SourceSchema>,
    applied: bool,
    edit_old_applied: bool,
    edit_new_applied: bool,
    queue: VecDeque<Node>,
    finished: bool,
}

impl Iterator for OrderedOverlay {
    type Item = Node;

    fn next(&mut self) -> Option<Node> {
        let compare = self.schema.compare_rows.clone();
        loop {
            if let Some(n) = self.queue.pop_front() {
                return Some(n);
            }
            if self.finished {
                return None;
            }
            let Some(node) = self.stream.next() else {
                self.finished = true;
                if !self.applied {
                    match &self.overlay {
                        Change::Remove(n) => {
                            self.applied = true;
                            return Some(n.clone());
                        }
                        Change::Edit { old_node, .. } => {
                            assert!(
                                self.edit_new_applied,
                                "edit overlay: new node must be applied before old node"
                            );
                            self.edit_old_applied = true;
                            self.applied = true;
                            return Some(old_node.clone());
                        }
                        _ => {}
                    }
                }
                assert!(
                    self.applied,
                    "overlayGenerator: overlay was never applied to any fetched node"
                );
                return None;
            };
            let mut yield_node = true;
            if !self.applied {
                match &self.overlay {
                    Change::Add(n) => {
                        if compare(&n.row, &node.row) == Ordering::Equal {
                            self.applied = true;
                            yield_node = false;
                        }
                    }
                    Change::Remove(n) => {
                        if compare(&n.row, &node.row) == Ordering::Less {
                            self.applied = true;
                            self.queue.push_back(n.clone());
                        }
                    }
                    Change::Edit { node: n, old_node } => {
                        if !self.edit_old_applied
                            && compare(&old_node.row, &node.row) == Ordering::Less
                        {
                            self.edit_old_applied = true;
                            if self.edit_new_applied {
                                self.applied = true;
                            }
                            self.queue.push_back(old_node.clone());
                        }
                        if !self.edit_new_applied && compare(&n.row, &node.row) == Ordering::Equal {
                            self.edit_new_applied = true;
                            if self.edit_old_applied {
                                self.applied = true;
                            }
                            yield_node = false;
                        }
                    }
                    Change::Child(n, child) => {
                        if compare(&n.row, &node.row) == Ordering::Equal {
                            self.applied = true;
                            self.queue
                                .push_back(overlay_child(&node, child, &self.schema));
                            yield_node = false;
                        }
                    }
                }
            }
            if yield_node {
                self.queue.push_back(node);
            }
        }
    }
}

/// `node` with the child change applied to the relationship it is on.
fn overlay_child(node: &Node, child: &ChildChange, schema: &SourceSchema) -> Node {
    let name = child.relationship_name.as_str();
    let original = node
        .relationships
        .get(name)
        .expect("overlay: relationship on node")
        .clone();
    let child_schema = schema
        .relationship(name)
        .expect("overlay: relationship in schema")
        .clone();
    let change = child.change.clone();
    node.clone().with(
        name,
        Rc::new(move || generate_with_overlay(original(), change.clone(), child_schema.clone())),
    )
}

/// `generateWithOverlayUnordered`: the overlay for an unordered child
/// stream. Removed rows go first; added rows are suppressed by primary key.
pub fn generate_with_overlay_unordered(
    stream: NodeStream,
    overlay: Change,
    schema: Rc<SourceSchema>,
) -> NodeStream {
    Box::new(UnorderedOverlay {
        stream,
        overlay,
        schema,
        injected: false,
        suppressed: false,
    })
}

struct UnorderedOverlay {
    stream: NodeStream,
    overlay: Change,
    schema: Rc<SourceSchema>,
    injected: bool,
    suppressed: bool,
}

impl Iterator for UnorderedOverlay {
    type Item = Node;

    fn next(&mut self) -> Option<Node> {
        if !self.injected {
            self.injected = true;
            match &self.overlay {
                Change::Remove(n) => return Some(n.clone()),
                Change::Edit { old_node, .. } => return Some(old_node.clone()),
                _ => {}
            }
        }
        let pk = &self.schema.primary_key;
        for node in self.stream.by_ref() {
            if !self.suppressed {
                match &self.overlay {
                    Change::Add(n) | Change::Edit { node: n, .. } => {
                        if row_equals_for_compound_key(&n.row, &node.row, pk) {
                            self.suppressed = true;
                            continue;
                        }
                    }
                    Change::Child(n, child) => {
                        if row_equals_for_compound_key(&n.row, &node.row, pk) {
                            self.suppressed = true;
                            return Some(overlay_child(&node, child, &self.schema));
                        }
                    }
                    Change::Remove(_) => {}
                }
            }
            return Some(node);
        }
        assert!(
            self.suppressed || self.overlay.change_type() == ChangeType::Remove,
            "overlayGenerator: overlay was never applied to any fetched node"
        );
        None
    }
}

pub fn row_equals_for_compound_key(a: &Row, b: &Row, key: &[String]) -> bool {
    key.iter()
        .all(|k| compare_values(a.get(k), b.get(k)) == Ordering::Equal)
}

pub fn is_join_match(
    parent: &Row,
    parent_key: &[String],
    child: &Row,
    child_key: &[String],
) -> bool {
    parent_key
        .iter()
        .zip(child_key)
        .all(|(p, c)| values_equal(parent.get(p), child.get(c)))
}

/// `buildJoinConstraint`: `target_key` mapped to `source_row`'s `source_key`
/// values, or `None` if one is null (a null key matches nothing).
pub fn build_join_constraint(
    source_row: &Row,
    source_key: &[String],
    target_key: &[String],
) -> Option<Constraint> {
    let mut c = Constraint::default();
    for (s, t) in source_key.iter().zip(target_key) {
        let value = source_row.get(s);
        if matches!(value, Some(Value::Null)) {
            return None;
        }
        c.set(t, value.cloned());
    }
    Some(c)
}

/// `canonicalKey`: a string key over `keys`, tagged by type so `1` and `"1"`
/// differ.
pub fn canonical_key(row: &Row, keys: &[String]) -> String {
    canonical_key_with(keys, |k| row.get(k))
}

/// [`canonical_key`] of a constraint's values.
pub fn canonical_key_of_constraint(constraint: &Constraint, keys: &[String]) -> String {
    canonical_key_with(keys, |k| constraint.get(k))
}

fn canonical_key_with<'a>(keys: &[String], get: impl Fn(&str) -> Option<&'a Value>) -> String {
    let mut s = String::new();
    for (i, k) in keys.iter().enumerate() {
        if i > 0 {
            s.push('\0');
        }
        match get(k) {
            None | Some(Value::Null) => s.push('n'),
            Some(Value::String(v)) => {
                s.push('s');
                s.push_str(v);
            }
            Some(Value::Number(n)) => {
                s.push('d');
                s.push_str(&json::number::format(*n));
            }
            Some(Value::Bool(b)) => s.push(if *b { 't' } else { 'f' }),
            Some(v) => {
                s.push('j');
                s.push_str(&json::stringify(v));
            }
        }
    }
    s
}

/// The partitions of indexed parents that share a join key, as fetch
/// constraints; `[None]` for an unpartitioned index (`join-index.ts`).
pub type ParentPartitions = Vec<Option<Constraint>>;

struct Partition {
    constraint: Constraint,
    pks: HashSet<String>,
}

#[derive(Default)]
struct JoinIndexState {
    unpartitioned: HashMap<String, HashSet<String>>,
    /// Join key, then partition key. Partitions keep insertion order.
    partitioned: HashMap<String, (Vec<String>, HashMap<String, Partition>)>,
    size: usize,
}

/// An index from join key to the parent rows that have it. `Join` and
/// `FlippedJoin` skip the parent fetch of a child change when no indexed
/// parent can see it.
pub struct JoinIndex {
    parent_key: Vec<String>,
    primary_key: Vec<String>,
    join_key_is_pk: bool,
    partition_key: Option<Vec<String>>,
    state: RefCell<JoinIndexState>,
}

impl JoinIndex {
    pub fn new(
        parent_key: Vec<String>,
        primary_key: Vec<String>,
        partition_key: Option<Vec<String>>,
    ) -> Rc<Self> {
        Rc::new(JoinIndex {
            join_key_is_pk: parent_key == primary_key,
            parent_key,
            primary_key,
            partition_key,
            state: RefCell::default(),
        })
    }

    /// The number of parent rows indexed.
    pub fn size(&self) -> usize {
        self.state.borrow().size
    }

    fn pk_of(&self, row: &Row, join_key: &str) -> String {
        if self.join_key_is_pk {
            join_key.to_owned()
        } else {
            canonical_key(row, &self.primary_key)
        }
    }

    pub fn add(&self, row: &Row) {
        let Some(join_key) = join_key_of(row, &self.parent_key) else {
            return;
        };
        let pk = self.pk_of(row, &join_key);
        let mut state = self.state.borrow_mut();
        let state = &mut *state;
        let Some(partition_key) = &self.partition_key else {
            if state.unpartitioned.entry(join_key).or_default().insert(pk) {
                state.size += 1;
            }
            return;
        };
        let part_key = canonical_key(row, partition_key);
        let (order, partitions) = state.partitioned.entry(join_key).or_default();
        let partition = partitions.entry(part_key.clone()).or_insert_with(|| {
            order.push(part_key);
            Partition {
                constraint: Constraint::from_row(row, partition_key),
                pks: HashSet::new(),
            }
        });
        if partition.pks.insert(pk) {
            state.size += 1;
        }
    }

    pub fn remove(&self, row: &Row) {
        let Some(join_key) = join_key_of(row, &self.parent_key) else {
            return;
        };
        let pk = self.pk_of(row, &join_key);
        let mut state = self.state.borrow_mut();
        let state = &mut *state;
        let Some(partition_key) = &self.partition_key else {
            if let Some(pks) = state.unpartitioned.get_mut(&join_key)
                && pks.remove(&pk)
            {
                state.size -= 1;
                if pks.is_empty() {
                    state.unpartitioned.remove(&join_key);
                }
            }
            return;
        };
        let Some((order, partitions)) = state.partitioned.get_mut(&join_key) else {
            return;
        };
        let part_key = canonical_key(row, partition_key);
        let Some(partition) = partitions.get_mut(&part_key) else {
            return;
        };
        if partition.pks.remove(&pk) {
            state.size -= 1;
            if partition.pks.is_empty() {
                partitions.remove(&part_key);
                order.retain(|k| *k != part_key);
                if partitions.is_empty() {
                    state.partitioned.remove(&join_key);
                }
            }
        }
    }

    /// The partitions of the indexed parents whose join key matches
    /// `child_row` over `child_key`, or `None` if there are none.
    pub fn lookup(&self, child_row: &Row, child_key: &[String]) -> Option<ParentPartitions> {
        let join_key = join_key_of(child_row, child_key)?;
        let state = self.state.borrow();
        if self.partition_key.is_none() {
            return state
                .unpartitioned
                .contains_key(&join_key)
                .then(|| vec![None]);
        }
        let (order, partitions) = state.partitioned.get(&join_key)?;
        Some(
            order
                .iter()
                .map(|k| Some(partitions[k].constraint.clone()))
                .collect(),
        )
    }
}

/// The canonical join key of `row`, or `None` if part of it is null.
fn join_key_of(row: &Row, key: &[String]) -> Option<String> {
    if key.iter().any(|k| matches!(row.get(k), Some(Value::Null))) {
        return None;
    }
    Some(canonical_key(row, key))
}