Skip to main content

relate_executor/graph/
label_mutation.rs

1//! LABS-425 (stellar round 1, the mod.rs 1k-creep finding): the
2//! label-mutation twins — `add_labels` / `remove_labels`, one input
3//! domain, one direction each — in their own impl-block file (the
4//! graph module's methods-in-their-own-file precedent, the sibling of
5//! the degree-stats family). The twins' shape is deliberately paired:
6//! one effect-shaped guard per twin (the request's ids resolved against
7//! the live label set, so an empty request, an all-already-carried add,
8//! an all-uninterned remove, and a missing node all no-op), one effect id
9//! vector, the same actually-mutated-ids guard on the redo/dirty pair,
10//! the same effect-derived WAL payload, and the same unguarded hand-off to
11//! the degree-stats family's own empty-set early returns (LABS-425
12//! round-1 F-1-17, rounds 3-4).
13//! `Graph` (graph/mod.rs) keeps the struct; every label-set mutation
14//! seam lives here.
15
16use super::*;
17
18impl Graph {
19    pub fn add_labels(&self, id: NodeId, labels: &[String]) {
20        let before = self.node(id);
21        if before.is_none() {
22            return;
23        }
24        let before_shape = self.node_chain_shape(id);
25        // The effect, not the request: the ids the node does not already
26        // carry. ADD mints a name it has never seen (unlike REMOVE, whose
27        // drop set is lookups only), so each request label is interned here
28        // — before the scope probe takes the map lock (interner shard read,
29        // then map lock, the seam lock order). A request of only carried
30        // labels has no effect and no-ops below.
31        let live = self.live_label_ids(id);
32        let mut added_ids: Vec<u32> = Vec::new();
33        for label in labels {
34            let lid = self.interner.intern(label);
35            if !live.contains(&lid) && !added_ids.contains(&lid) {
36                added_ids.push(lid);
37            }
38        }
39        if added_ids.is_empty() {
40            return;
41        }
42        // ADR-0439: adding a constraint label introduces the node's
43        // constrained tuple; capture the before-image now. A label no
44        // constraint carries cannot change any tuple — the map's scope is
45        // the only source of that answer.
46        let keys = self.tracked_node_edit(id, |scope| scope.touches_node_labels(&added_ids));
47        let mut n = self
48            .cow_node(id)
49            .expect("a live node has a copy-on-write version");
50        n.labels.extend(added_ids.iter().copied());
51        crate::memory::fit_reserved(&mut n.labels);
52        // New label pairs must pick up existing properties.
53        let string_kids: Vec<u32> = n.props.iter().map(|(kid, _)| kid).collect();
54        // The edited live view is in hand: hand it to the map before it
55        // moves into the version chain (the canonical completion arm).
56        self.finish_node_edit(keys, id, Some(&n));
57        self.put_node(id, n);
58        // The WAL/catalog names derive from the effect ids, through the
59        // canonical id→name lookup (remove_labels' policy): the ids are node
60        // labels, so the published-before-use invariant holds.
61        let added: Vec<String> = added_ids
62            .iter()
63            .map(|&lid| String::from(&*self.interner.get(lid)))
64            .collect();
65        self.bump_stats(|s| s.labels_added += added_ids.len() as i64);
66        self.catalog
67            .lock()
68            .unwrap()
69            .record_labels(added.iter().cloned());
70        self.push_redo(RedoOp::add_labels(id, added));
71        self.mark_node_dirty(id);
72        self.index_add(id);
73        for &lid in &added_ids {
74            for &kid in &string_kids {
75                self.declared_dirty(lid, kid, id);
76            }
77        }
78        self.charge_node_after_edit(id, before.as_ref(), before_shape);
79        // The one count-family entry: the degree rebucket (LABS-351), the
80        // incident pair counts and the exact label counts (LABS-370).
81        self.apply_label_delta_stats(id, &added_ids, CountDelta::Add);
82    }
83
84    pub fn remove_labels(&self, id: NodeId, labels: &[String]) {
85        // A name never interned cannot be on the node (its label set is
86        // interned ids): the drop set resolves through lookups only, so
87        // a stray request name neither mints an id nor charges
88        // interned-string bytes.
89        let drop: HashSet<u32> = labels
90            .iter()
91            .filter_map(|l| self.interner.get_id(l))
92            .collect();
93        // The effect guard, one early exit for every no-op shape: an
94        // empty request, one whose names are all uninterned, one whose
95        // names are interned elsewhere but not carried by this node, or
96        // a missing node — none of it intersects the live label set the
97        // COW pass would walk (`live_label_ids`, the xmax == 0 version —
98        // the in-write set included), so nothing is removed. The
99        // request's resolution alone is not the effect: without this
100        // guard, `REMOVE n:Foo` with Foo interned elsewhere passed it
101        // and paid a full version push (an identical clone per `cow`)
102        // for a no-op — a content-free version, invisible to reads at
103        // any ts and absent from the checkpoint payload (which
104        // serializes the visible version), but charged to `used_bytes`
105        // all the same.
106        let live = self.live_label_ids(id);
107        if !drop.iter().any(|l| live.contains(l)) {
108            return;
109        }
110        let before = self.node(id);
111        let before_shape = self.node_chain_shape(id);
112        // ADR-0439: a removed constraint label frees the node's constrained
113        // tuple; a label no constraint carries cannot change any tuple. The
114        // map's scope is the only source of that answer.
115        // `drop` is non-empty here (the effect guard above returned
116        // otherwise), so one scope read answers for the whole request.
117        let drop_ids: Vec<u32> = drop.iter().copied().collect();
118        let keys = self.tracked_node_edit(id, |scope| scope.touches_node_labels(&drop_ids));
119        let mut n = self
120            .cow_node(id)
121            .expect("a live node has a copy-on-write version");
122        // One pass, the overlay twin's canonical move: extract_if both
123        // collects the actually-removed ids and retains the rest.
124        let actually_removed: Vec<u32> = n.labels.extract_if(.., |l| drop.contains(l)).collect();
125        crate::memory::fit_reserved(&mut n.labels);
126        // The edited live view is in hand: hand it to the map before it
127        // moves into the version chain (the canonical completion arm).
128        self.finish_node_edit(keys, id, Some(&n));
129        self.put_node(id, n);
130        self.bump_stats(|s| s.labels_removed += actually_removed.len() as i64);
131        // The redo/dirty pair is guarded on the actually-mutated ids; the
132        // degree-stats family calls below ride the family's own
133        // empty-set early returns.
134        if !actually_removed.is_empty() {
135            // The effect, not the request: the WAL row records the names
136            // the node actually lost (the add and overlay twins' policy,
137            // ADR-0309), derived from the effect ids through the
138            // canonical id→name reverse lookup — the ids are node
139            // labels, so the published-before-use invariant holds.
140            let lost: Vec<String> = actually_removed
141                .iter()
142                .map(|&lid| String::from(&*self.interner.get(lid)))
143                .collect();
144            self.push_redo(RedoOp::remove_labels(id, lost));
145            self.mark_node_dirty(id);
146        }
147        self.index_add(id);
148        self.charge_node_after_edit(id, before.as_ref(), before_shape);
149        // The one count-family entry, in the removal direction (the ids are
150        // the labels ACTUALLY removed, never the requested list).
151        self.apply_label_delta_stats(id, &actually_removed, CountDelta::Remove);
152    }
153}
154
155#[cfg(test)]
156mod tests;