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
//! Point reads of one node or relationship through the write overlay: a
//! scratch deletion hides the element, a scratch copy answers for it, and
//! otherwise the snapshot at `snapshot_ts` does.
use std::sync::Arc;
use relate_core::{Node, NodeId, RelId, Relationship, Result, Value};
use relate_ir::Direction;
use relate_names::Token;
use super::ExecGraph;
/// One candidate node as the scan-predicate check reads it: the write
/// scratch's materialized copy (string-keyed), or the snapshot's live
/// record (interned ids). The two arms carry the same facts — labels and
/// one-slot property reads — each keyed the way its storage is keyed, so
/// the predicate evaluation is spelled once over both.
pub(crate) enum ScanNode<'a> {
Scratch(&'a Node),
Live(&'a crate::store::LiveNode),
}
impl ScanNode<'_> {
/// Whether the node carries `label`, by the interned `lid` on the
/// live record and by the string on the scratch copy.
pub(crate) fn has_label(&self, label: &str, lid: Option<u32>) -> bool {
match self {
Self::Scratch(n) => n.labels.iter().any(|l| l == label),
Self::Live(live) => lid.is_some_and(|lid| live.has_label(lid)),
}
}
/// The node's `key` property, null when absent — the same read
/// `eval::prop` makes of a bound node, resolved through the interned
/// `kid` on the live record and the string on the scratch copy.
pub(crate) fn prop(&self, key: &str, kid: Option<u32>) -> Value {
match self {
Self::Scratch(n) => n.properties.get(key).cloned().unwrap_or(Value::Null),
Self::Live(live) => kid
.and_then(|kid| live.props.get(kid))
.cloned()
.unwrap_or(Value::Null),
}
}
/// The node's `key` property by reference, `None` when absent — the
/// borrowed form of [`ScanNode::prop`] for the reads that only
/// inspect the value (the temporal construction borrows its argument,
/// the padded-ISO fast path parses a `&str`), so the per-candidate
/// gate never clones the stored string. The caller's `check` closure
/// runs inside the one store read the gate opened, so the borrow
/// outlives the check.
pub(crate) fn prop_value(&self, key: &str, kid: Option<u32>) -> Option<&Value> {
match self {
Self::Scratch(n) => n.properties.get(key),
Self::Live(live) => kid.and_then(|kid| live.props.get(kid)),
}
}
}
/// What the overlay scratch says about one element (ADR-0016). Every point
/// read below starts from this prologue: a scratch deletion hides the
/// element, a scratch copy answers for it, and scratch silence falls
/// through to the committed snapshot at `snapshot_ts`.
enum ScratchAnswer<T> {
/// The scratch deleted the element: the read answers "gone".
Deleted,
/// The scratch holds the element's copy, which answered the read.
Answered(T),
/// The scratch says nothing: read the snapshot.
Snapshot,
}
impl<T> ScratchAnswer<T> {
/// Resolve the prologue into the read's answer: `deleted` when the
/// scratch hid the element, the scratch copy's answer when it spoke,
/// and `snapshot()` otherwise.
fn resolve(self, deleted: T, snapshot: impl FnOnce() -> T) -> T {
match self {
Self::Deleted => deleted,
Self::Answered(answer) => answer,
Self::Snapshot => snapshot(),
}
}
}
impl ExecGraph<'_> {
/// The node side of the visibility prologue: `read` answers from the
/// scratch's copy of `id` when it holds one.
fn node_prologue<T>(&self, id: NodeId, read: impl FnOnce(&Node) -> T) -> ScratchAnswer<T> {
if let Some(cell) = self.scratch {
let s = cell.borrow();
if s.batch.deleted_nodes.contains(&id) {
return ScratchAnswer::Deleted;
}
if let Some(n) = s.batch.nodes.get(&id) {
return ScratchAnswer::Answered(read(n));
}
}
ScratchAnswer::Snapshot
}
/// The relationship side of the visibility prologue: `read` answers
/// from the scratch's copy of `id` when it holds one.
fn rel_prologue<T>(
&self,
id: RelId,
read: impl FnOnce(&Relationship) -> T,
) -> ScratchAnswer<T> {
if let Some(cell) = self.scratch {
let s = cell.borrow();
if s.batch.deleted_rels.contains(&id) {
return ScratchAnswer::Deleted;
}
if let Some(r) = s.batch.rels.get(&id) {
return ScratchAnswer::Answered(read(r));
}
}
ScratchAnswer::Snapshot
}
pub fn node(&self, id: NodeId) -> Option<Node> {
self.node_prologue(id, |n| Some(n.clone()))
.resolve(None, || self.base.node_at(id, self.snapshot_ts()))
}
/// Existence check without materializing the node. The profiled hot path
/// (`node` clones the full `Node` — labels + property map) makes this the
/// common-case read in a write statement; callers that only need to know
/// a node is present must not pay for its properties.
pub fn node_exists(&self, id: NodeId) -> bool {
self.node_prologue(id, |_| true)
.resolve(false, || self.base.node_visible_at(id, self.snapshot_ts()))
}
/// Label ids without materializing properties. Returns owned label
/// strings; callers that only compare labels pay no property map clone.
pub fn node_labels(&self, id: NodeId) -> Option<Vec<String>> {
self.node_prologue(id, |n| Some((*n.labels).clone()))
.resolve(None, || self.base.node_labels_at(id, self.snapshot_ts()))
}
pub fn rel(&self, id: RelId) -> Option<Relationship> {
self.rel_prologue(id, |r| Some(r.clone()))
.resolve(None, || self.base.rel_at(id, self.snapshot_ts()))
}
pub fn rel_ends(&self, id: RelId) -> Option<(NodeId, NodeId)> {
self.rel_prologue(id, |r| Some((r.start, r.end)))
.resolve(None, || self.base.rel_ends_at(id, self.snapshot_ts()))
}
pub fn hop(&self, current: NodeId, rid: RelId, dir: Direction) -> Option<NodeId> {
let (start, end) = self.rel_ends(rid)?;
crate::store::hop_other(current, start, end, dir)
}
/// Plan-time label cardinality estimate (ADR-0218), forwarded for the
/// exec layer's pattern-start estimates (ADR-0351).
pub fn label_count(&self, label: &str) -> u64 {
self.base.label_count(label)
}
/// Existence check without materializing the relationship.
pub fn rel_exists(&self, id: RelId) -> bool {
self.rel_prologue(id, |_| true)
.resolve(false, || self.base.rel_visible_at(id, self.snapshot_ts()))
}
/// The type name shared (`Arc<str>` — one refcount bump off the
/// store's interned name; the batch record's copy is the only
/// allocation, and only when the rel is scratch). The selector's
/// per-walk type reads ride this, never the `String` clone.
pub fn rel_type_shared(&self, id: RelId) -> Option<Arc<str>> {
self.rel_prologue(id, |r| Some(Arc::from(r.rel_type.as_str())))
.resolve(None, || {
self.base.rel_type_name_shared(id, self.snapshot_ts())
})
}
/// Type name without materializing the relationship's property map.
pub fn rel_type(&self, id: RelId) -> Option<String> {
self.rel_prologue(id, |r| Some(r.rel_type.clone()))
.resolve(None, || {
self.base
.rel_stub_at(id, self.snapshot_ts())
.map(|r| r.rel_type)
})
}
pub fn node_stub(&self, id: NodeId) -> Option<Node> {
self.node_prologue(id, |n| {
Some(Node {
id: n.id,
labels: n.labels.clone(),
properties: crate::store::empty_properties(),
})
})
.resolve(None, || self.base.node_stub_at(id, self.snapshot_ts()))
}
pub fn rel_stub(&self, id: RelId) -> Option<Relationship> {
self.rel_prologue(id, |r| {
Some(Relationship {
id: r.id,
start: r.start,
end: r.end,
rel_type: r.rel_type.clone(),
properties: crate::store::empty_properties(),
})
})
.resolve(None, || self.base.rel_stub_at(id, self.snapshot_ts()))
}
pub fn node_prop(&self, id: NodeId, key: &str) -> Option<Value> {
self.node_prologue(id, |n| n.properties.get(key).cloned())
.resolve(None, || self.base.node_prop_at(id, self.snapshot_ts(), key))
}
/// The plan token as this view must read it — the one rebase policy
/// every token domain reads (ADR-0541 × ADR-0210): an `Absent` token
/// records a plan-time fact, the store had never interned the name,
/// and a `CALL { … } IN TRANSACTIONS` statement's own batch commits
/// intern names mid-statement, after the plan resolved them but
/// before its later parts read. A scratch rebased past this view's
/// admission pin — the state only those mid-statement commits
/// produce — therefore turns `Absent` into `Unresolved`, so the
/// caller's by-name lookup answers it; every other view keeps the
/// plan's answer (a view reading at its own pin cannot see data
/// committed after it). `Id` passes through: the interner is
/// append-only, so a plan-time id stays the name's id. The label
/// twin of this re-check is [`Self::node_has_uninterned_label`].
pub(crate) fn live_token(&self, token: Token) -> Token {
if token == Token::Absent && self.snapshot_ts() != self.pinned_ts {
Token::Unresolved
} else {
token
}
}
/// The store id a plan-resolved property-key token names (ADR-0541):
/// the token through [`Self::live_token`]'s rebase policy, then the
/// documented `Token::id_or_else` dispatch with the base's by-name
/// lookup.
pub(crate) fn prop_key_id_resolved(&self, text: &str, token: Token) -> Option<u32> {
self.live_token(token)
.id_or_else(|| self.base.prop_key_id(text))
}
/// [`Self::node_prop`] reading a plan-resolved key: no interner lookup
/// per call on the snapshot arm. An `Unresolved` key resolves by name
/// through [`Self::prop_key_id_resolved`] and an `Absent` one answers `None` without a store
/// read — unless the scratch was rebased past this view's pin
/// ([`Self::prop_key_id_resolved`]); the scratch arm is string-keyed
/// either way.
pub(crate) fn node_prop_resolved(
&self,
id: NodeId,
(name, token): &relate_names::NamedToken,
) -> Option<Value> {
name.with_str(|text| {
self.node_prologue(id, |n| n.properties.get(text).cloned())
.resolve(None, || {
self.prop_key_id_resolved(text, *token)
.and_then(|kid| self.base.node_prop_id_at(id, self.snapshot_ts(), kid))
})
})
}
/// [`Self::node_prop_resolved`] for a relationship.
pub(crate) fn rel_prop_resolved(
&self,
id: RelId,
(name, token): &relate_names::NamedToken,
) -> Option<Value> {
name.with_str(|text| {
self.rel_prologue(id, |r| r.properties.get(text).cloned())
.resolve(None, || {
self.prop_key_id_resolved(text, *token)
.and_then(|kid| self.base.rel_prop_id_at(id, self.snapshot_ts(), text, kid))
})
})
}
/// The label check for a name the store never interned (`Absent`, or an
/// interned id missing): no committed node carries it, so only a write
/// overlay can, by string — `None` for an invisible node.
pub(crate) fn node_has_uninterned_label(&self, id: NodeId, label: &str) -> Option<bool> {
if self.has_write_overlay() {
self.node_has_label(id, label)
} else {
Some(false)
}
}
/// The scan-predicate gate's one candidate read: `check` sees the
/// candidate as a [`ScanNode`] — the scratch copy when the overlay
/// holds one, else the snapshot's visible live record — and its
/// verdict settles the candidate. An invisible or scratch-deleted
/// node rejects without `check` running, the same verdict
/// `node_exists`-gated paths give.
pub(crate) fn node_scan_check(
&self,
id: NodeId,
check: impl Fn(ScanNode<'_>) -> Result<bool>,
) -> Result<bool> {
self.node_prologue(id, |n| check(ScanNode::Scratch(n)))
.resolve(Ok(false), || {
self.base
.with_visible_node(id, self.snapshot_ts(), |live| check(ScanNode::Live(live)))
.unwrap_or(Ok(false))
})
}
pub fn rel_prop(&self, id: RelId, key: &str) -> Option<Value> {
self.rel_prologue(id, |r| r.properties.get(key).cloned())
.resolve(None, || self.base.rel_prop_at(id, self.snapshot_ts(), key))
}
pub fn node_prop_keys(&self, id: NodeId) -> Vec<String> {
self.node_prologue(id, |n| n.properties.keys().cloned().collect())
.resolve(Vec::new(), || {
self.base.node_prop_keys_at(id, self.snapshot_ts())
})
}
pub fn rel_prop_keys(&self, id: RelId) -> Vec<String> {
self.rel_prologue(id, |r| r.properties.keys().cloned().collect())
.resolve(Vec::new(), || {
self.base.rel_prop_keys_at(id, self.snapshot_ts())
})
}
}