Skip to main content

relate_executor/graph/
token_changes.rs

1//! ADR-0687: the changed-token set the live mutation sites report.
2//!
3//! A commit's changed tokens cannot be reconstructed from its write records
4//! afterwards: a delete's redo op carries only the entity id, and the dying
5//! labels, relationship type and property keys live in the version the
6//! removal tombstones. Each mutation seam therefore reports its tokens while
7//! it still holds the version, [`Graph::take_applied_token_changes`] drains
8//! the accumulated set, and the facade's publication folds it into the
9//! per-token last-changed table (`relate`'s `token_changes`).
10//!
11//! ## What a seam reports
12//!
13//! One rule, stated here and pointed at by every seam:
14//!
15//! - **A create reports its classify token** — a node's label ids, a
16//!   relationship's type id — and no property keys. A footprint holds the
17//!   classify token's row count, and the (classify, key) cardinality a seek
18//!   consult reads moves with that count, so the classify stamp covers the
19//!   keys a create installs.
20//! - **A property edit reports the keys it touched** beside the entity's
21//!   classify tokens: a footprint's key ids are tokens of their own and
22//!   nothing else would stamp them.
23//! - **A delete reports the dying version's classify tokens and its
24//!   property-key ids**: the tombstone is the last place the version's keys
25//!   exist, so no later walk could report them (ADR-0687 decision 7).
26//!
27//! The asymmetry is the point: a create's keys live on in the installed
28//! version and the classify stamp covers them, while a delete's keys do not
29//! survive it.
30
31use super::*;
32
33/// The interned tokens the writes applied since the last drain changed —
34/// label ids, relationship-type ids and property-key ids, one dense
35/// append-only id space (`crate::store::interner`) — plus whether a schema
36/// op landed among them.
37#[derive(Debug, Default)]
38pub struct AppliedTokenChanges {
39    tokens: HashSet<u32>,
40    schema: bool,
41}
42
43impl AppliedTokenChanges {
44    /// The changed token ids, in no particular order, each once.
45    pub fn tokens(&self) -> impl Iterator<Item = u32> + '_ {
46        self.tokens.iter().copied()
47    }
48
49    /// Whether a schema op (an index, constraint or graph-type create/drop)
50    /// landed: no token footprint describes one, so the publication moves
51    /// the full-invalidation epoch instead of stamping a footprint.
52    pub fn schema(&self) -> bool {
53        self.schema
54    }
55}
56
57impl Graph {
58    /// Report `tokens` as changed by the writes applied since the last
59    /// drain — the mutation seams' one call, at the point each still holds
60    /// the entity version whose tokens it reports.
61    pub(in crate::graph) fn note_changed_tokens(&self, tokens: impl IntoIterator<Item = u32>) {
62        self.changed_tokens.lock().unwrap().tokens.extend(tokens);
63    }
64
65    /// Report a schema op among the applied writes.
66    pub(in crate::graph) fn note_schema_change(&self) {
67        self.changed_tokens.lock().unwrap().schema = true;
68    }
69
70    /// Drain the set the next publication folds in (ADR-0687): the commit
71    /// tail, the follower's batch apply, and the reload each drain once,
72    /// and an open's replay is the baseline, so it drains and discards.
73    pub fn take_applied_token_changes(&self) -> AppliedTokenChanges {
74        std::mem::take(&mut *self.changed_tokens.lock().unwrap())
75    }
76}