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        // ADR-0595: a first label takes the node out of the empty-label
73        // bucket — that posting dies here, at the removal site, so the
74        // tail drain reclaims it.
75        if live.is_empty() {
76            self.suspect_postings(id, &[]);
77        }
78        self.index_add(id);
79        for &lid in &added_ids {
80            for &kid in &string_kids {
81                self.declared_dirty(lid, kid, id);
82            }
83        }
84        self.charge_node_after_edit(id, before.as_ref(), before_shape);
85        // The one count-family entry: the degree rebucket (LABS-351), the
86        // incident pair counts and the exact label counts (LABS-370).
87        self.apply_label_delta_stats(id, &live, &added_ids, CountDelta::Add);
88    }
89
90    pub fn remove_labels(&self, id: NodeId, labels: &[String]) {
91        // A name never interned cannot be on the node (its label set is
92        // interned ids): the drop set resolves through lookups only, so
93        // a stray request name neither mints an id nor charges
94        // interned-string bytes.
95        let drop: HashSet<u32> = labels
96            .iter()
97            .filter_map(|l| self.interner.get_id(l))
98            .collect();
99        // The effect guard, one early exit for every no-op shape: an
100        // empty request, one whose names are all uninterned, one whose
101        // names are interned elsewhere but not carried by this node, or
102        // a missing node — none of it intersects the live label set the
103        // COW pass would walk (`live_label_ids`, the xmax == 0 version —
104        // the in-write set included), so nothing is removed. The
105        // request's resolution alone is not the effect: without this
106        // guard, `REMOVE n:Foo` with Foo interned elsewhere passed it
107        // and paid a full version push (an identical clone per `cow`)
108        // for a no-op — a content-free version, invisible to reads at
109        // any ts and absent from the checkpoint payload (which
110        // serializes the visible version), but charged to `used_bytes`
111        // all the same.
112        let live = self.live_label_ids(id);
113        if !drop.iter().any(|l| live.contains(l)) {
114            return;
115        }
116        let before = self.node(id);
117        let before_shape = self.node_chain_shape(id);
118        // ADR-0439: a removed constraint label frees the node's constrained
119        // tuple; a label no constraint carries cannot change any tuple. The
120        // map's scope is the only source of that answer.
121        // `drop` is non-empty here (the effect guard above returned
122        // otherwise), so one scope read answers for the whole request.
123        let drop_ids: Vec<u32> = drop.iter().copied().collect();
124        let keys = self.tracked_node_edit(id, |scope| scope.touches_node_labels(&drop_ids));
125        let mut n = self
126            .cow_node(id)
127            .expect("a live node has a copy-on-write version");
128        // One pass, the overlay twin's canonical move: extract_if both
129        // collects the actually-removed ids and retains the rest.
130        let actually_removed: Vec<u32> = n.labels.extract_if(.., |l| drop.contains(l)).collect();
131        crate::memory::fit_reserved(&mut n.labels);
132        // The edited live view is in hand: hand it to the map before it
133        // moves into the version chain (the canonical completion arm).
134        self.finish_node_edit(keys, id, Some(&n));
135        self.put_node(id, n);
136        self.bump_stats(|s| s.labels_removed += actually_removed.len() as i64);
137        // The redo/dirty pair is guarded on the actually-mutated ids; the
138        // degree-stats family calls below ride the family's own
139        // empty-set early returns.
140        if !actually_removed.is_empty() {
141            // The effect, not the request: the WAL row records the names
142            // the node actually lost (the add and overlay twins' policy,
143            // ADR-0309), derived from the effect ids through the
144            // canonical id→name reverse lookup — the ids are node
145            // labels, so the published-before-use invariant holds.
146            let lost: Vec<String> = actually_removed
147                .iter()
148                .map(|&lid| String::from(&*self.interner.get(lid)))
149                .collect();
150            self.push_redo(RedoOp::remove_labels(id, lost));
151            self.mark_node_dirty(id);
152            // ADR-0595: the dropped labels' postings become reclamation
153            // candidates here, at the removal site — the apply's tail
154            // drain decides their fate, not a probe's walk.
155            self.suspect_postings(id, &actually_removed);
156        }
157        self.index_add(id);
158        self.charge_node_after_edit(id, before.as_ref(), before_shape);
159        // The one count-family entry, in the removal direction (the ids are
160        // the labels ACTUALLY removed, never the requested list).
161        self.apply_label_delta_stats(id, &live, &actually_removed, CountDelta::Remove);
162    }
163}
164
165#[cfg(test)]
166mod tests;