relate-executor 1.4.0

In-memory graph and Cypher physical executor
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
//! The property-store seam at the executor's read boundary (ADR-0665): the
//! live reads a property representation answers for the executor. The
//! default implementation is the in-memory store's compact live records
//! (ADR-0033); a columnar store implements the same trait, and the engine
//! adapter maps interned key ids at the boundary (ADR-0541).
//!
//! [`Graph::node_prop_id_at`](super::Graph::node_prop_id_at) reads through
//! [`PropStore::point`], so every point read has one implementation. The
//! equality lookup shape routes through the same attached store
//! (ADR-0665 A/B): a store installed with
//! [`Graph::attach_prop_store`](super::Graph::attach_prop_store) answers
//! it, and with none attached the index routes answer as before. The
//! label scan never routes to the store — the measured ordering
//! (ADR-0665 point 7) keeps it on the canonical label route.

use std::sync::{Arc, OnceLock};

use relate_core::{NodeId, Value};

use super::*;

/// One aggregate a fold read asks of one column — the aggregate ladder's
/// `min`/`max`/`count` asks and [`PropStore::folds`]'s wire, spelled once
/// (the ladder's items and the store's answer share it; the extremum
/// direction is `Extremum`'s, not a third vocabulary).
#[derive(Clone, Copy)]
pub enum PropFold {
    Count,
    Min,
    Max,
}

/// The property-read contract the executor's read boundary dispatches to.
pub trait PropStore: Send + Sync + std::fmt::Debug {
    /// Point read: the value of `kid` on `id`, visible at `ts`. `None` is
    /// a miss, not a final answer: the compact records answer every
    /// store miss (the store is additive — its worst case is the default
    /// engine's every case).
    fn point(&self, ts: u64, id: NodeId, kid: u32) -> Option<Value>;

    /// Whether the store carries a column for interned key `kid`. A store
    /// that promotes only some types answers the keys it carries. Required,
    /// with no permissive default: a store that forgot to declare it would
    /// answer `point` for a key it has no column for, reporting null where
    /// the compact records hold the value.
    fn carries(&self, kid: u32) -> bool;

    /// Whether the store answers reads at `ts`; a store built from one
    /// snapshot declines every other timestamp, and the compact records
    /// answer. Required for the same reason as [`Self::carries`].
    fn serves_at(&self, ts: u64) -> bool;

    /// `=` / `IN` rows over the store's lookup structure, filtered to
    /// `labels` and capped at `cap + 1`, **ascending and deduplicated**
    /// — the shape the engine's candidate merge consumes (a two-pointer
    /// union with no duplicate guard and no closing re-sort, LABS-803),
    /// so an answer in row order or with repeats is not a slow answer
    /// but a corrupting one. `None` leaves the lookup to the index
    /// route.
    fn equality(
        &self,
        _labels: &[u32],
        _kid: u32,
        _values: &[Value],
        _cap: Option<usize>,
    ) -> Option<Vec<NodeId>> {
        None
    }

    /// The extremum/count folds over one column — the aggregate shapes
    /// ADR-0611's ladder recognizes. The answer aligns position-wise with
    /// `wants`, one entry per ask; `None` for the whole call — a fold
    /// that cannot be answered included — leaves the shape to the
    /// postings path and the scan.
    fn folds(&self, _labels: &[u32], _kid: u32, _wants: &[PropFold]) -> Option<Vec<Value>> {
        None
    }

    /// One `(value, count)` pair per distinct non-null value of `kid`
    /// over the rows carrying `labels` — the shape ADR-0611's grouped
    /// rung folds into its charged table. The absent-value group is the
    /// engine's own (`null_group` reads the label's live count in the
    /// exactness window) — the store never carries it. `None` leaves the
    /// shape to the postings path and the scan.
    fn grouped_counts(&self, _labels: &[u32], _kid: u32) -> Option<Vec<(Value, u64)>> {
        None
    }

    /// The publication feed (ADR-0665's versioning step 1): every row a
    /// commit changed arrives here once, inside the publication's write
    /// window and BEFORE the commit's ts becomes visible to readers
    /// (`end_write` feeds the store, then stores `published_ts`) — a
    /// reader that resolves the new ts must find the delta already
    /// holding the publication, never the pre-commit columns. A store that keeps a delta overlay appends these as its
    /// versioned entries; one that serves a single immutable snapshot
    /// (the round-1/3 shape) can decline future reads instead, with an
    /// empty arm stating that choice. Required, with no permissive
    /// default, for the same reason as [`Self::carries`]: a store that
    /// forgot to declare its feed policy would silently drop every
    /// publication. `ts` is monotonic across calls; an aborted write
    /// never reaches here.
    fn on_publication(&self, ts: u64, rows: &[PublishedRow]);
}

/// One changed row a publication hands the attached store — the
/// delta-overlay feed (ADR-0665's versioning step 1): the row's committed
/// state at the publication's `ts`. `props` is the row's FULL property
/// state by interned key (a key the store carries that is absent here is
/// absent at `ts` — the row state, not a patch); `labels` are the interned
/// label ids it carries; `deleted` is the tombstone for a row the commit
/// removed.
pub struct PublishedRow {
    pub id: NodeId,
    pub labels: Vec<u32>,
    pub props: Vec<(u32, Value)>,
    pub deleted: bool,
}

/// The seam's one point-read resolution, stated once beside the trait it
/// governs (ADR-0665 A/B): a store that carries `kid` answers first; a
/// miss — a key it does not carry, or the store's own `None` — falls to
/// the compact records. A store miss is not a final answer: the store is
/// additive, so the attached engine's worst case is the default engine's
/// every case (a broken column, a delta the base tier cannot see, or a
/// genuinely absent property all resolve through the fallback, one
/// version-chain probe). Both read boundaries — the graph's own `point`
/// and the statement-resolved overlay — resolve through this one shape,
/// so a miss can only ever mean the fallback.
impl Graph {
    pub(crate) fn point_through(
        &self,
        store: Option<&dyn PropStore>,
        ts: u64,
        id: NodeId,
        kid: u32,
    ) -> Option<Value> {
        store
            .filter(|store| store.carries(kid))
            .and_then(|store| store.point(ts, id, kid))
            .or_else(|| self.node_prop_row(id, ts, kid))
    }
}

/// The default store: the compact live records in the MVCC version chains.
impl PropStore for Graph {
    fn point(&self, ts: u64, id: NodeId, kid: u32) -> Option<Value> {
        self.point_through(
            self.serving_store(ts).map(|store| store.as_ref()),
            ts,
            id,
            kid,
        )
    }

    // The compact records carry every interned key and answer every
    // timestamp (visibility is resolved in `point`).
    fn carries(&self, _kid: u32) -> bool {
        true
    }

    fn serves_at(&self, _ts: u64) -> bool {
        true
    }

    // The compact records ARE the live store: a publication changes them
    // directly, so there is nothing to feed a second representation.
    fn on_publication(&self, _ts: u64, _rows: &[PublishedRow]) {}
}

/// The attached experimental store's whole state (ADR-0665 A/B) — the
/// shared cell, the publication feed's pending rows, and the
/// write-lifecycle hooks that keep them — one bundle beside its contract
/// in this module, so the graph's central state file carries one field.
#[derive(Debug, Default)]
pub(crate) struct AttachedStore {
    /// The installed store; empty until [`Self::attach`] publishes one.
    cell: OnceLock<Arc<dyn PropStore>>,
    /// Rows a write window marked for the store's publication feed
    /// (ADR-0665 versioning step 1) — taken and handed to the store at
    /// the commit's publication close, cleared on abort. Beside the
    /// checkpoint's own dirty sets, not part of them: the checkpoint's
    /// take-and-clear must never starve the feed.
    pending: std::sync::Mutex<std::collections::HashSet<NodeId>>,
}

impl AttachedStore {
    /// Install a store: first attach wins, a second call leaves the
    /// published store untouched, drops the argument, and returns
    /// `false`. The graph must be quiescent — the store is built from
    /// its state at attach time.
    pub(crate) fn attach(&self, store: Arc<dyn PropStore>) -> bool {
        self.cell.set(store).is_ok()
    }

    /// The installed store, if any.
    pub(crate) fn get(&self) -> Option<&Arc<dyn PropStore>> {
        self.cell.get()
    }

    /// Mark `id` for the publication feed — gated on a store being
    /// attached, so the default write path pays one acquire load, not a
    /// second mutex for a set nobody drains.
    pub(crate) fn mark_dirty(&self, id: NodeId) {
        if self.cell.get().is_some() {
            self.pending.lock().unwrap().insert(id);
        }
    }

    /// The write window's marked rows, taken: the commit's publication
    /// close materializes and hands them to the store.
    pub(crate) fn take_pending(&self) -> Vec<NodeId> {
        std::mem::take(&mut *self.pending.lock().unwrap())
            .into_iter()
            .collect()
    }

    /// An aborted write never reaches the feed.
    pub(crate) fn clear(&self) {
        self.pending.lock().unwrap().clear();
    }
}

impl Graph {
    /// Install an experimental property store (ADR-0665 A/B): point reads
    /// and the equality/aggregate lookups then route to it.
    #[doc(hidden)]
    pub fn attach_prop_store(&self, store: Arc<dyn PropStore>) -> bool {
        self.attached_store.attach(store)
    }

    /// The interned id of `name`, if a property or label of that name has
    /// been interned. The A/B harnesses map their name-keyed lookups through
    /// this boundary.
    #[doc(hidden)]
    pub fn interned_id(&self, name: &str) -> Option<u32> {
        self.interner.get_id(name)
    }

    /// Whether a store is attached — the CLI's experimental store-info note
    /// reads this without reaching the store.
    #[doc(hidden)]
    pub fn has_attached_prop_store(&self) -> bool {
        self.attached_prop_store().is_some()
    }

    /// The store attached to this graph, if any.
    pub(crate) fn attached_prop_store(&self) -> Option<&Arc<dyn PropStore>> {
        self.attached_store.get()
    }

    /// The attached store when it answers reads at `ts` — the one
    /// attached-store gate every read shape consults.
    fn serving_store(&self, ts: u64) -> Option<&Arc<dyn PropStore>> {
        self.attached_prop_store()
            .filter(|store| store.serves_at(ts))
    }

    /// [`Self::serving_store`] as an owned handle — the per-statement
    /// resolution's one-time call (ADR-0665 A/B): a statement's read guard
    /// pins the publication epoch, so this answer cannot move while the
    /// statement runs (ADR-0508's placement argument), and every read
    /// below the overlay dispatches through the one handle instead of
    /// re-resolving per read.
    pub(crate) fn serving_store_handle(&self, ts: u64) -> Option<Arc<dyn PropStore>> {
        self.serving_store(ts).cloned()
    }

    /// The compact records' own point read — the default arm of
    /// [`PropStore::point`] and the per-statement resolution's fallback
    /// when the resolved store declines a key.
    pub(crate) fn node_prop_row(&self, id: NodeId, ts: u64, kid: u32) -> Option<Value> {
        self.nodes
            .get(&id)
            .and_then(|vers| visible_version(&vers, ts).and_then(|v| v.live.prop_by_id(kid)))
    }

    /// The store's `=` / `IN` arm for the executor's equality serves. `cap`
    /// is the capped serve's stop (`cap + 1`); `None` answers in full. `None`
    /// leaves the lookup to the index route.
    pub(crate) fn prop_store_equality(
        &self,
        labels: &[String],
        key: &str,
        values: &[Value],
        ts: u64,
        cap: Option<usize>,
    ) -> Option<Vec<NodeId>> {
        let store = self.serving_store(ts)?;
        let (kid, lids) = self.resolved_serving(store, labels, key)?;
        let ids = store.equality(&lids, kid, values, cap)?;
        // The store answer's acceptance protocol (the folds alignment
        // and `null_group`'s checked subtraction are the siblings): the
        // engine's candidate merge is a two-pointer union over ascending,
        // deduplicated sides, so an out-of-crate answer that is unsorted
        // or repeats ids would silently corrupt the merge — decline to
        // the index route, the one answer to a broken store.
        let ascending_dedup = ids.windows(2).all(|pair| pair[0] < pair[1]);
        ascending_dedup.then_some(ids)
    }

    /// The store's aggregate arm for ADR-0611's ladder — the folds rung.
    /// Serves behind the declared postings (the caller checks), gated by
    /// the same attach/snapshot/carry rules as the set arms; `None`
    /// leaves the shape to the postings path and the scan.
    pub(crate) fn prop_store_folds(
        &self,
        labels: &[String],
        key: &str,
        wants: &[PropFold],
        ts: u64,
    ) -> Option<Vec<Value>> {
        let store = self.serving_store(ts)?;
        let (kid, lids) = self.resolved_serving(store, labels, key)?;
        // The store answer's ONE acceptance protocol, owned at the seam:
        // the answer aligns position-wise with `wants`, one entry per
        // ask — a store answering any other length is broken and the
        // whole arm declines to the postings path, never a partial row
        // (the ladder's rungs consume validated answers only; the
        // grouped arm's twin guard is `null_group`'s checked
        // subtraction, which declines a broken count the same way).
        let served = store.folds(&lids, kid, wants)?;
        (served.len() == wants.len()).then_some(served)
    }

    /// The store's aggregate arm for ADR-0611's ladder — the grouped
    /// rung's feed. Same gates; `None` leaves it to the postings path.
    pub(crate) fn prop_store_grouped_counts(
        &self,
        labels: &[String],
        key: &str,
        ts: u64,
    ) -> Option<Vec<(Value, u64)>> {
        let store = self.serving_store(ts)?;
        let (kid, lids) = self.resolved_serving(store, labels, key)?;
        store.grouped_counts(&lids, kid)
    }

    /// Hand the attached store the rows this commit changed — the
    /// delta-overlay feed (ADR-0665's versioning step 1). Runs inside the
    /// publication close, after `commit_ts` advanced and before the write
    /// window ends: every pending row's committed state materializes once
    /// (labels by id, properties by id from the compact record), the
    /// tombstone for a row the commit removed. A store with a delta
    /// overlay appends these as its versioned entries; the round-1/3
    /// single-snapshot stores ignore them with an empty arm. Aborted
    /// writes never reach here — `abort_write` clears the pending set
    /// without publishing.
    pub(in crate::graph) fn publish_to_prop_store(&self, ts: u64) {
        let ids = self.attached_store.take_pending();
        let Some(store) = self.attached_prop_store() else {
            // Nothing is attached: the pending set drained above, so a
            // later attach never receives a stale backlog.
            return;
        };
        if ids.is_empty() {
            return;
        }
        let mut rows = Vec::with_capacity(ids.len());
        for id in ids {
            let vers = self.nodes.get(&id);
            match vers.as_ref().and_then(|vers| visible_version(vers, ts)) {
                Some(v) => rows.push(PublishedRow {
                    id,
                    labels: v.live.labels.clone(),
                    props: v
                        .live
                        .props
                        .iter()
                        .map(|(kid, value)| (kid, value.clone()))
                        .collect(),
                    deleted: false,
                }),
                None => rows.push(PublishedRow {
                    id,
                    labels: Vec::new(),
                    props: Vec::new(),
                    deleted: true,
                }),
            }
        }
        store.on_publication(ts, &rows);
    }

    /// The one carry/label gate every key-shaped store arm consults: the
    /// interned id of `key` the store carries, and the interned label ids.
    /// `None` — the key or a label was never interned, or the store carries
    /// no column for the key — leaves the read to the index route, the
    /// postings path and the scan.
    fn resolved_serving(
        &self,
        store: &Arc<dyn PropStore>,
        labels: &[String],
        key: &str,
    ) -> Option<(u32, Vec<u32>)> {
        // A key no node carries, or one whose values the store does not
        // promote, falls through: a promoted column answers every value.
        let kid = self.interned_id(key)?;
        if !store.carries(kid) {
            return None;
        }
        let lids = self.interned_labels(labels)?;
        Some((kid, lids))
    }

    /// The interned ids of every label, or `None` when one was never
    /// interned (no node carries it, so the answer is empty).
    fn interned_labels(&self, labels: &[String]) -> Option<Vec<u32>> {
        labels.iter().map(|label| self.interned_id(label)).collect()
    }
}