tabnas-transduce 0.2.7

Streaming transducers over any tabnas parser: source events, selectors, bounded captures and the table protocol.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Shared-prefix matching of many selectors in one pass.
//!
//! The router has to recognize every capture's selector over one event
//! stream, and a document can be hundreds of megabytes, so the matcher
//! does no per-event allocation and never spells out a path: the
//! selectors are compiled into one trie of [`Step`]s, and the matcher
//! walks the document with a stack of open containers (a [`Level`] per
//! container: its kind, how many values it has started, the current
//! member's key) and, per level, the set of trie nodes that named that
//! container. A value's position names at most one child per node
//! (`Property(k)` or `Index(i)`) plus the wildcard child (`EachMember`
//! or `EachIndex`), so the sets stay tiny: one or two nodes for the
//! usual plan, and they live in one flat vector indexed by level rather
//! than in a set per level.
//!
//! The matcher also validates the protocol as it goes, because it holds
//! the state that makes a malformed stream visible (a key outside an
//! object, an array ending an object, a value after the root). Every
//! stage downstream of it can therefore trust the sequence. A concrete
//! [`Path`] is built only on request, for a delivered match or a
//! failure, from the levels' current keys and indexes.

use crate::error::Fail;
use crate::event::JsonEvent;
use crate::selector::{Path, Segment, Selector, Step};

/// Which selector matched: its position in the slice given to
/// [`Matcher::new`]. One of alchemy's shared types.
pub use tabnas_alchemy::shared::matcher::CaptureId;

type NodeId = usize;

const ROOT: NodeId = 0;

/// One trie node: the selectors' next steps below one position.
#[derive(Debug, Default)]
struct Node {
    properties: Vec<(Box<str>, NodeId)>,
    indexes: Vec<(usize, NodeId)>,
    each_index: Option<NodeId>,
    each_member: Option<NodeId>,
    /// The selectors that end here, in id order.
    terminals: Vec<CaptureId>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
    Array,
    Object,
}

/// One open container. Levels are reused across the run, so the key
/// buffer keeps its capacity and a member costs no allocation.
#[derive(Debug)]
struct Level {
    kind: Kind,
    /// Values started in this container so far; the current or last one
    /// is at index `started - 1`.
    started: usize,
    /// The current member's key, for objects.
    key: String,
    /// Between a key and its value.
    in_value: bool,
    /// The trie nodes that named this container: `node_stack[start..end]`.
    start: usize,
    end: usize,
}

impl Level {
    fn reset(&mut self, kind: Kind, start: usize, end: usize) {
        self.kind = kind;
        self.started = 0;
        self.key.clear();
        self.in_value = false;
        self.start = start;
        self.end = end;
    }
}

/// What one event did to the document's structure.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HitKind {
    /// A member name; the value follows.
    Key,
    /// A container began.
    Start,
    /// A whole scalar value: it began and completed here.
    Scalar,
    /// A container completed.
    Close,
    /// The document completed.
    End,
}

/// The matcher's answer for one event.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Hit {
    pub kind: HitKind,
    /// How many captures begin with this event: their ids are
    /// [`Matcher::begins`], valid until the next event. Zero unless `kind`
    /// is `Start` or `Scalar`. A count rather than a borrowed slice, so a
    /// stage can hold the hit while it asks the matcher for a path.
    pub begins: usize,
    /// For `Start` and `Scalar`, the number of containers enclosing the
    /// value; for `Close`, the number enclosing the container that
    /// closed. The value that began at a depth completes at the same
    /// depth, which is how a stage pairs the two without keeping a path.
    pub depth: usize,
}

/// Recognizes every selector of a set over one event stream.
#[derive(Debug)]
pub struct Matcher {
    nodes: Vec<Node>,
    levels: Vec<Level>,
    depth: usize,
    /// The active nodes of every open level, then the nodes of the value
    /// being started, as one stack.
    node_stack: Vec<NodeId>,
    /// Scratch for [`Hit::begins`].
    begins: Vec<CaptureId>,
    root_done: bool,
    ended: bool,
}

impl Matcher {
    /// Compile the selectors; their positions are the capture ids.
    pub fn new(selectors: &[Selector]) -> Matcher {
        let mut nodes = vec![Node::default()];
        for (id, selector) in selectors.iter().enumerate() {
            let mut at = ROOT;
            for step in selector.steps() {
                at = child(&mut nodes, at, step);
            }
            nodes[at].terminals.push(id);
        }
        Matcher {
            nodes,
            levels: Vec::new(),
            depth: 0,
            node_stack: Vec::with_capacity(8),
            begins: Vec::new(),
            root_done: false,
            ended: false,
        }
    }

    /// Open containers right now.
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Whether `End` has been seen.
    pub fn ended(&self) -> bool {
        self.ended
    }

    /// The concrete path of the value at `depth` enclosing containers: the
    /// current position of each of those containers. Asked at a `Start`
    /// or `Scalar` hit it names the value; asked at a `Close` hit it names
    /// the container that closed. Allocates, so it is for deliveries and
    /// failures, not for every event.
    pub fn path(&self, depth: usize) -> Path {
        let mut path = Path::root();
        for level in self.levels.iter().take(depth.min(self.depth)) {
            path.push(match level.kind {
                Kind::Array => Segment::Index(level.started.saturating_sub(1)),
                Kind::Object => Segment::Key(level.key.as_str().into()),
            });
        }
        path
    }

    /// The captures that began with the last event, in id order.
    pub fn begins(&self) -> &[CaptureId] {
        &self.begins
    }

    /// One event. A malformed sequence is a `PROTOCOL_ORDER_ERROR`.
    pub fn event(&mut self, ev: JsonEvent<'_>) -> Result<Hit, Fail> {
        if self.ended {
            return Err(Fail::protocol("an event arrived after the document ended"));
        }
        match ev {
            JsonEvent::Key(k) => {
                let level = self
                    .depth
                    .checked_sub(1)
                    .map(|d| &mut self.levels[d])
                    .filter(|l| l.kind == Kind::Object)
                    .ok_or_else(|| Fail::protocol("a key arrived outside an object"))?;
                if level.in_value {
                    return Err(Fail::protocol("a key arrived where a value was expected"));
                }
                level.key.clear();
                level.key.push_str(k);
                level.in_value = true;
                self.begins.clear();
                Ok(Hit {
                    kind: HitKind::Key,
                    begins: 0,
                    depth: self.depth,
                })
            }
            JsonEvent::ObjectStart | JsonEvent::ArrayStart => {
                let depth = self.depth;
                let start = self.begin_value()?;
                let end = self.node_stack.len();
                let kind = if ev == JsonEvent::ObjectStart {
                    Kind::Object
                } else {
                    Kind::Array
                };
                if depth == self.levels.len() {
                    self.levels.push(Level {
                        kind,
                        started: 0,
                        key: String::new(),
                        in_value: false,
                        start,
                        end,
                    });
                } else {
                    self.levels[depth].reset(kind, start, end);
                }
                self.depth += 1;
                Ok(Hit {
                    kind: HitKind::Start,
                    begins: self.begins.len(),
                    depth,
                })
            }
            JsonEvent::ObjectEnd | JsonEvent::ArrayEnd => {
                let want = if ev == JsonEvent::ObjectEnd {
                    Kind::Object
                } else {
                    Kind::Array
                };
                let level = self
                    .depth
                    .checked_sub(1)
                    .map(|d| &self.levels[d])
                    .ok_or_else(|| Fail::protocol("a container ended that had not started"))?;
                if level.kind != want {
                    return Err(Fail::protocol(match want {
                        Kind::Object => "an object ended inside an array",
                        Kind::Array => "an array ended inside an object",
                    }));
                }
                if level.in_value {
                    return Err(Fail::protocol(
                        "an object ended after a key without its value",
                    ));
                }
                let start = level.start;
                self.depth -= 1;
                self.node_stack.truncate(start);
                self.complete_value();
                self.begins.clear();
                Ok(Hit {
                    kind: HitKind::Close,
                    begins: 0,
                    depth: self.depth,
                })
            }
            JsonEvent::End => {
                if self.depth > 0 {
                    return Err(Fail::protocol("the document ended inside a container"));
                }
                if !self.root_done {
                    return Err(Fail::protocol("the document ended before its root value"));
                }
                self.ended = true;
                self.begins.clear();
                Ok(Hit {
                    kind: HitKind::End,
                    begins: 0,
                    depth: 0,
                })
            }
            JsonEvent::Null | JsonEvent::Bool(_) | JsonEvent::Number(_) | JsonEvent::String(_) => {
                let depth = self.depth;
                let start = self.begin_value()?;
                self.node_stack.truncate(start);
                self.complete_value();
                Ok(Hit {
                    kind: HitKind::Scalar,
                    begins: self.begins.len(),
                    depth,
                })
            }
        }
    }

    /// A value begins at the current position: check that one is allowed
    /// here, push the trie nodes naming it onto the node stack, and fill
    /// `begins`. Returns where on the node stack the value's nodes start.
    fn begin_value(&mut self) -> Result<usize, Fail> {
        let base = match self.depth.checked_sub(1) {
            None => {
                if self.root_done {
                    return Err(Fail::protocol("a second root value arrived"));
                }
                self.node_stack.clear();
                self.node_stack.push(ROOT);
                0
            }
            Some(d) => {
                let level = &mut self.levels[d];
                if level.kind == Kind::Object && !level.in_value {
                    return Err(Fail::protocol(
                        "a value arrived inside an object without a key",
                    ));
                }
                let (start, end, kind, index) = (level.start, level.end, level.kind, level.started);
                level.started += 1;
                self.node_stack.truncate(end);
                for at in start..end {
                    let node = &self.nodes[self.node_stack[at]];
                    match kind {
                        Kind::Array => {
                            if let Some(c) = node.each_index {
                                self.node_stack.push(c);
                            }
                            if let Some((_, c)) = node.indexes.iter().find(|(i, _)| *i == index) {
                                self.node_stack.push(*c);
                            }
                        }
                        Kind::Object => {
                            if let Some(c) = node.each_member {
                                self.node_stack.push(c);
                            }
                            let key = self.levels[d].key.as_str();
                            if let Some((_, c)) = node.properties.iter().find(|(k, _)| &**k == key)
                            {
                                self.node_stack.push(*c);
                            }
                        }
                    }
                }
                end
            }
        };
        self.begins.clear();
        for &node in &self.node_stack[base..] {
            self.begins.extend_from_slice(&self.nodes[node].terminals);
        }
        if self.begins.len() > 1 {
            self.begins.sort_unstable();
        }
        Ok(base)
    }

    /// The value at the current position is complete.
    fn complete_value(&mut self) {
        match self.depth.checked_sub(1) {
            None => self.root_done = true,
            Some(d) => self.levels[d].in_value = false,
        }
    }
}

/// The child of `at` for `step`, created on first use.
fn child(nodes: &mut Vec<Node>, at: NodeId, step: &Step) -> NodeId {
    let existing = match step {
        Step::Property(name) => nodes[at]
            .properties
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, c)| *c),
        Step::Index(i) => nodes[at]
            .indexes
            .iter()
            .find(|(j, _)| j == i)
            .map(|(_, c)| *c),
        Step::EachIndex => nodes[at].each_index,
        Step::EachMember => nodes[at].each_member,
    };
    if let Some(c) = existing {
        return c;
    }
    let c = nodes.len();
    nodes.push(Node::default());
    match step {
        Step::Property(name) => nodes[at].properties.push((name.clone(), c)),
        Step::Index(i) => nodes[at].indexes.push((*i, c)),
        Step::EachIndex => nodes[at].each_index = Some(c),
        Step::EachMember => nodes[at].each_member = Some(c),
    }
    c
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::Code;
    use crate::event::{Number, OwnedJsonEvent};

    /// Feed a document; collect `(path, ids)` for every value that begins
    /// a capture, and every path the matcher reports on `Close`.
    fn run(selectors: &[Selector], events: &[OwnedJsonEvent]) -> Vec<(String, Vec<CaptureId>)> {
        let mut m = Matcher::new(selectors);
        let mut out = Vec::new();
        for ev in events {
            let hit = m.event(ev.as_event()).unwrap();
            if hit.begins > 0 {
                let path = m.path(hit.depth).to_string();
                out.push((path, m.begins().to_vec()));
            }
        }
        assert!(m.ended());
        out
    }

    fn doc(json: &str) -> Vec<OwnedJsonEvent> {
        let d = crate::Datum::from_json(&serde_json::from_str(json).unwrap());
        let mut rec: Vec<OwnedJsonEvent> = Vec::new();
        crate::walk_datum(&d, &mut rec).unwrap();
        rec.push(OwnedJsonEvent::End);
        rec
    }

    fn sel() -> Selector {
        Selector::root()
    }

    #[test]
    fn a_property_chain_names_one_value() {
        let hits = run(
            &[sel().property("a").property("b")],
            &doc(r#"{"a":{"b":[1,2],"c":3},"b":{"b":4}}"#),
        );
        assert_eq!(hits, vec![(".a.b".to_string(), vec![0])]);
    }

    #[test]
    fn each_index_names_every_element_with_its_index() {
        let hits = run(
            &[sel().property("xs").each_index()],
            &doc(r#"{"xs":[10,{"y":1},[2]],"ys":[9]}"#),
        );
        assert_eq!(
            hits,
            vec![
                (".xs[0]".to_string(), vec![0]),
                (".xs[1]".to_string(), vec![0]),
                (".xs[2]".to_string(), vec![0]),
            ]
        );
    }

    #[test]
    fn each_member_names_every_member_value() {
        let hits = run(
            &[sel().each_member()],
            &doc(r#"{"a":1,"odd key":{"z":2},"c":[3]}"#),
        );
        assert_eq!(
            hits,
            vec![
                (".a".to_string(), vec![0]),
                (".\"odd key\"".to_string(), vec![0]),
                (".c".to_string(), vec![0]),
            ]
        );
    }

    #[test]
    fn an_index_names_one_element() {
        let hits = run(&[sel().index(1)], &doc(r#"[[0,1],[2,3],[4,5]]"#));
        assert_eq!(hits, vec![("[1]".to_string(), vec![0])]);
    }

    #[test]
    fn the_root_selector_names_the_root_value() {
        assert_eq!(
            run(&[sel()], &doc(r#"{"a":[1]}"#)),
            vec![(".".to_string(), vec![0])]
        );
        assert_eq!(run(&[sel()], &doc("42")), vec![(".".to_string(), vec![0])]);
    }

    #[test]
    fn two_selectors_sharing_a_prefix_match_independently() {
        let selectors = [
            sel().property("response").property("metadata"),
            sel().property("response").property("records").each_index(),
        ];
        let hits = run(
            &selectors,
            &doc(r#"{"response":{"metadata":[1],"records":[{"id":1},{"id":2}]},"records":[0]}"#),
        );
        assert_eq!(
            hits,
            vec![
                (".response.metadata".to_string(), vec![0]),
                (".response.records[0]".to_string(), vec![1]),
                (".response.records[1]".to_string(), vec![1]),
            ]
        );
    }

    #[test]
    fn two_selectors_naming_the_same_value_both_begin_in_id_order() {
        let selectors = [sel().property("a"), sel().each_member()];
        let hits = run(&selectors, &doc(r#"{"a":1,"b":2}"#));
        assert_eq!(
            hits,
            vec![(".a".to_string(), vec![0, 1]), (".b".to_string(), vec![1])]
        );
    }

    #[test]
    fn a_selector_that_matches_nothing_is_silent() {
        let hits = run(
            &[sel().property("missing").each_index(), sel().index(5)],
            &doc(r#"{"a":[1,2,3]}"#),
        );
        assert!(hits.is_empty());
    }

    #[test]
    fn nested_arrays_track_each_level_independently() {
        let hits = run(
            &[sel().each_index().each_index()],
            &doc(r#"[[1,2],[],[[3]]]"#),
        );
        assert_eq!(
            hits,
            vec![
                ("[0][0]".to_string(), vec![0]),
                ("[0][1]".to_string(), vec![0]),
                ("[2][0]".to_string(), vec![0]),
            ]
        );
    }

    #[test]
    fn indexes_and_properties_do_not_cross_kinds() {
        let hits = run(&[sel().index(0), sel().property("0")], &doc(r#"{"0":[7]}"#));
        assert_eq!(hits, vec![(".\"0\"".to_string(), vec![1])]);
    }

    #[test]
    fn a_close_reports_the_path_of_the_container_that_closed() {
        let mut m = Matcher::new(&[]);
        let mut closes = Vec::new();
        for ev in doc(r#"{"a":[{"b":1},{"c":[]}]}"#) {
            let hit = m.event(ev.as_event()).unwrap();
            if hit.kind == HitKind::Close {
                closes.push(m.path(hit.depth).to_string());
            }
        }
        assert_eq!(closes, vec![".a[0]", ".a[1].c", ".a[1]", ".a", "."]);
    }

    #[test]
    fn depth_is_the_number_of_enclosing_containers() {
        let mut m = Matcher::new(&[]);
        let mut seen = Vec::new();
        for ev in doc(r#"[1,[2]]"#) {
            let hit = m.event(ev.as_event()).unwrap();
            seen.push((hit.kind, hit.depth));
        }
        assert_eq!(
            seen,
            vec![
                (HitKind::Start, 0),
                (HitKind::Scalar, 1),
                (HitKind::Start, 1),
                (HitKind::Scalar, 2),
                (HitKind::Close, 1),
                (HitKind::Close, 0),
                (HitKind::End, 0),
            ]
        );
    }

    fn protocol_error(events: &[JsonEvent<'_>]) -> Code {
        let mut m = Matcher::new(&[sel()]);
        let mut last = None;
        for ev in events {
            match m.event(*ev) {
                Ok(_) => {}
                Err(e) => {
                    last = Some(e.code);
                    break;
                }
            }
        }
        last.expect("the stream should have been rejected")
    }

    #[test]
    fn malformed_streams_are_protocol_errors() {
        use JsonEvent::{ArrayEnd, ArrayStart, End, Key, ObjectEnd, ObjectStart};
        let one = JsonEvent::Number(Number::new(1.0));
        let cases: &[&[JsonEvent<'_>]] = &[
            &[Key("a")],
            &[ObjectStart, one],
            &[ObjectStart, Key("a"), Key("b")],
            &[ObjectStart, Key("a"), ObjectEnd],
            &[ArrayStart, ObjectEnd],
            &[ObjectStart, ArrayEnd],
            &[ArrayEnd],
            &[one, one],
            &[End],
            &[ArrayStart, End],
            &[one, End, End],
            &[ArrayStart, Key("a")],
        ];
        for case in cases {
            assert_eq!(protocol_error(case), Code::ProtocolOrderError, "{case:?}");
        }
    }

    #[test]
    fn levels_are_reused_without_stale_state() {
        let hits = run(
            &[sel().each_index().property("k")],
            &doc(r#"[{"k":1,"other":2},{"other":3},{"k":4}]"#),
        );
        assert_eq!(
            hits,
            vec![
                ("[0].k".to_string(), vec![0]),
                ("[2].k".to_string(), vec![0])
            ]
        );
    }
}