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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! The lazy equality/string postings engine (ADR-0063, ADR-0185): the pair
//! build-and-drain, the per-value postings, and the dirty-mark read seam.
//! A sibling of `index.rs` (the `degree_stats` family precedent), so the
//! parent keeps the index state, the apply seams, and the rebuild
//! orchestration.
use super::declared::DrainState;
use super::index::PropPairIndex;
use super::index_lookup::RecheckSpec;
use super::*;
use crate::group_key::identity_narrower_than_eq;
use crate::hash_seed::seeded_set;
/// How a keyed equality lookup is answered (ADR-0614): a declared
/// RANGE pair (ordered postings, no lazy build) always wins; else the
/// lazy equality postings when the values pass the decline predicate;
/// else the scan. The one three-way decision the id seek, the
/// cardinality, the keyed-lookup arm, and the overlay's scratch-mode
/// flag all read — precedence and the decline in one place.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::graph) enum EqualityRoute {
/// A declared RANGE pair covers the sought key(s).
Declared,
/// No declared pair; the values pass the decline predicate.
Postings,
/// Neither: the caller scans (ADR-0614's decline).
Scan,
}
/// One walked version's recency — the live/superseded distinction the
/// pair walk hands its consumer alongside the value: named, not a
/// positional `bool` (the `Extremum` precedent).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum PairVersion {
/// The newest version with no `xmax` — the current committed value.
Live,
/// A version an older statement snapshot may still see.
Superseded,
}
impl PairVersion {
/// The classification at one walk position, against
/// [`Chain::live_index`]'s position — stated once for the pair walk.
fn at(idx: usize, live_at: Option<usize>) -> Self {
if Some(idx) == live_at {
PairVersion::Live
} else {
PairVersion::Superseded
}
}
}
/// Feed `ids` to the pair ascending, so each bucket's sorted id vector
/// grows by append — the one owner of that ordering for a build and a drain.
fn feed_sorted(mut ids: Vec<i64>, st: &mut PropPairIndex, feed: &impl Fn(i64, &mut PropPairIndex)) {
ids.sort_unstable();
for id in ids {
feed(id, st);
}
}
impl Graph {
/// `(label, key)` strings → interned pair key, honouring the `*`
/// any-label bucket. `None` when either side was never interned.
pub(super) fn string_pair_id(&self, label: &str, key: &str) -> Option<(u32, u32)> {
let lid = if label == "*" {
self.indexes.star_label_id
} else {
self.interner.get_id(label)?
};
let kid = self.interner.get_id(key)?;
Some((lid, kid))
}
/// Build the pair from `candidates` on first use, drain the ids written
/// since it was last current, and hand it to `read` while its write
/// guard is held (ADR-0063, ADR-0185). The one committed-side pair
/// engine for nodes and relationships: the entity seam is the three
/// closures — `candidates` lists the pair's ids (the label index, or
/// the whole `rels` map), `feed` posts one candidate's every version
/// and, where the pair kind keeps a trust mark, reconciles it from the
/// live version, and `read` consumes the pair under its guard. The two
/// adapters are `with_prop_pair` (nodes: the label-plus-property walk,
/// the live-reconciled mark) and `with_rel_prop_pair` (relationships:
/// the type-plus-payload walk, post-only — its pairs carry no mark).
///
/// Lock order: the pair's write guard is held across the build and the
/// drain, which take `label_index`, `nodes` or `rels` read guards.
/// `index_add`/`rel_index_add` take the same guard in the reverse
/// order: they dirty-mark the pair after the installed version's
/// `nodes`/`rels` guard is released. Holding the pair guard is what
/// keeps a concurrent commit's entity out of the gap between the
/// build's candidate copy and `built = true`: the write side waits,
/// then finds the pair built and dirty-marks it. The inversion cannot
/// deadlock while both sides take `nodes`/`rels` as readers: dashmap
/// 6's shard lock blocks a reader only while a writer holds the shard,
/// not while one waits, and no `nodes`/`rels` writer waits for a pair
/// guard while it holds its shard. The `relate` tests
/// `uniqueness_probes_in_a_transaction_complete_beside_concurrent_commits`
/// and its relationship twin drive both sides at once.
pub(super) fn with_pair<R>(
&self,
pairs: &CoreMap<(u32, u32), PropPairIndex>,
pair: (u32, u32),
candidates: impl Fn() -> Vec<i64>,
// The pair kind's one feed: post one candidate's every version,
// and — where the pair keeps a trust mark — reconcile it from the
// live version. The node pairs do; the relationship pairs post
// alone.
feed: impl Fn(i64, &mut PropPairIndex),
read: impl FnOnce(&PropPairIndex) -> R,
) -> R {
let mut st = pairs.entry(pair).or_default();
if !st.built {
feed_sorted(candidates(), &mut st, &feed);
st.built = true;
st.postings.shrink_to_fit();
}
if !st.dirty.is_empty() {
let ids: Vec<i64> = std::mem::take(&mut st.dirty).into_iter().collect();
// The feed owns the remove-then-post order (the node pairs' is
// `post_pair_node`'s): a live version that carries no value for
// the pair leaves whatever a stale version marked cleared, not
// trusted.
feed_sorted(ids, &mut st, &feed);
}
read(&st)
}
/// Prune `id` from the built pairs of `pairs` (ADR-0063, ADR-0185): for
/// every (class, key, value) triple a dead version carried that no
/// remaining version carries, remove `id` from that pair's bucket. Lazy
/// postings: only built pairs; unbuilt pairs have no stale ids to
/// remove. The one prune for nodes and relationships; each side's
/// `triples` closure enumerates its versions' (class, key, value key)
/// triples.
pub(super) fn drop_unused_pairs<V>(
pairs: &CoreMap<(u32, u32), PropPairIndex>,
id: i64,
dead: &[V],
remaining: &[V],
triples: impl Fn(&V, &mut dyn FnMut(u32, u32, GroupKey)),
) {
let mut keep = seeded_set();
for v in remaining {
triples(v, &mut |class, kid, gk| {
keep.insert((class, kid, gk));
});
}
// `unpost` is idempotent, so a triple a dead version repeats is
// pruned once and a no-op after.
for v in dead {
triples(v, &mut |class, kid, gk| {
if keep.contains(&(class, kid, gk.clone())) {
return;
}
if let Some(mut st) = pairs.get_mut(&(class, kid)) {
if st.built {
st.postings.unpost(&gk, id);
}
}
});
}
}
/// Whether `(lid, kid)` has an equality pair an earlier seek already
/// built — the lazy pair's own one answer for its readiness, so a
/// reader outside this module never reaches into the pair map to ask.
pub(super) fn prop_pair_built(&self, pair: (u32, u32)) -> bool {
self.indexes
.prop_pairs
.get(&pair)
.is_some_and(|st| st.built)
}
/// Build `(label_id, key_id)` from the label index on first use, drain
/// the ids written since the pair was last current, and hand the pair to
/// `read` while its write guard is held (ADR-0063) — the node adapter of
/// [`Self::with_pair`], whose lock order applies here. `equality_postings`
/// and `equality_postings_len` both read through this, so a bucket's
/// contents and its size cannot drift.
pub(super) fn with_prop_pair<R>(
&self,
lid: u32,
kid: u32,
read: impl FnOnce(&PropPairIndex) -> R,
) -> R {
self.with_pair(
&self.indexes.prop_pairs,
(lid, kid),
|| self.pair_candidate_ids(lid),
// The node pairs' feed is the declared pairs' one protocol
// (`post_pair_node`): post every version, and on THE live
// version restate `unposted` from it alone — a superseded NaN
// must not poison the pair, the live NaN still declines, and a
// live version carrying no value clears whatever a stale one
// marked.
|id, st| self.post_pair_node(st, id, lid, kid),
read,
)
}
/// The guard preamble `equality_postings` and `equality_postings_len`
/// share: the interned `(label, key)` pair plus `value`'s
/// property-index key. `None` when the label or key was never interned
/// or `value` contains a NaN.
fn equality_pair_key(
&self,
label: &str,
key: &str,
value: &Value,
) -> Option<(u32, u32, GroupKey)> {
let gk = GroupKey::for_property_index(value)?;
let (lid, kid) = self.string_pair_id(label, key)?;
Some((lid, kid, gk))
}
/// Equality postings for `(label, key) = value` from the lazily built
/// pair (ADR-0063 superset: every version of every candidate), for the
/// constraint uniqueness probe (ADR-0172 §3). Unlike `lookup_nodes_at`
/// it does not require a range index on the pair: a node constraint's
/// backing index materializes seek postings (LABS-328), but relationship
/// constraints have no backing-index postings at all, so this lazy pair
/// is the one probe both entity kinds share. Empty when the label or key
/// was never interned or `value` contains a NaN. Callers re-verify every
/// id.
pub(crate) fn equality_postings(&self, label: &str, key: &str, value: &Value) -> Vec<NodeId> {
let Some((lid, kid, gk)) = self.equality_pair_key(label, key, value) else {
return Vec::new();
};
self.with_prop_pair(lid, kid, |st| st.postings.ids(&gk).to_vec())
}
/// The size of the `(label, key) = value` equality bucket from the same
/// lazily built pair `equality_postings` reads, counted in place
/// (ADR-0184). Empty when the label or key was never interned or
/// `value` contains a NaN.
pub(crate) fn equality_postings_len(&self, label: &str, key: &str, value: &Value) -> usize {
let Some((lid, kid, gk)) = self.equality_pair_key(label, key, value) else {
return 0;
};
self.with_prop_pair(lid, kid, |st| st.postings.ids(&gk).len())
}
/// Whether the lazy equality postings can answer a keyed equality
/// lookup's values — a pattern map's or an `IN` list's (ADR-0614):
/// the labels are non-empty (the pair engine keys on interned
/// labels; the label-less shape scans) and no value's identity is
/// narrower than its `=` class — a named-zone datetime, bare or
/// nested, whose DST fall-back halves are `=`-equal but key apart,
/// so an identity bucket would miss a partner the scan finds. The
/// route deciders ([`Self::map_equality_route`],
/// [`Self::values_equality_route`]) read this one predicate, so
/// every route declines the same values by construction.
/// Emptiness is each caller's own invariant — the map form always
/// carries a first entry, and an empty `IN` list is the serve's
/// empty answer — so the predicate states only what it decides. A
/// NaN has no property-index key at all: the serve answers it
/// empty (a NaN equals nothing), never by decline.
pub(in crate::graph) fn equality_postings_serve<'a>(
labels: &[String],
mut values: impl Iterator<Item = &'a Value>,
) -> bool {
!labels.is_empty() && !values.any(identity_narrower_than_eq)
}
/// The route for a keyed equality lookup over a property map
/// (ADR-0614). The map form's own invariant is a non-empty map —
/// an empty one has no pair to seek and never routes to the
/// postings. Read by the keyed-lookup arm
/// (`Graph::node_candidates`) and the overlay's scratch-mode
/// decision (`Graph::equality_served_by_index`).
pub(in crate::graph) fn map_equality_route(
&self,
labels: &[String],
props: &BTreeMap<String, Value>,
) -> EqualityRoute {
if self.range_serves_equality(labels, props) {
EqualityRoute::Declared
} else if !props.is_empty() && Self::equality_postings_serve(labels, props.values()) {
EqualityRoute::Postings
} else {
EqualityRoute::Scan
}
}
/// The route for a single-key `=`/`IN` seek (ADR-0614). Read by the
/// WHERE-form id seek; the cardinality's postings arm is `None`
/// (ADR-0614 §6: the lazy bucket has no count-without-build).
pub(in crate::graph) fn values_equality_route(
&self,
labels: &[String],
key: &str,
values: &[Value],
) -> EqualityRoute {
if self.range_serves_key(labels, key) {
EqualityRoute::Declared
} else if Self::equality_postings_serve(labels, values.iter()) {
EqualityRoute::Postings
} else {
EqualityRoute::Scan
}
}
/// The interned `(label, key)` pair a postings serve streams: the
/// first property (a `BTreeMap`, so the choice is deterministic),
/// on the pattern label with the smallest label-index bucket — the
/// one shared shortest-bucket choice the scan tail also reads
/// ([`Graph::smallest_interned_bucket`]), fail-closed on an
/// uninterned label or key alike: no node carries either, so the
/// serve answers empty, exactly what a scan against no bucket finds.
fn smallest_serve_pair(&self, labels: &[String], key: &str) -> Option<(u32, u32)> {
let kid = self.interner.get_id(key)?;
let lid = self.smallest_interned_bucket(labels)?;
Some((lid, kid))
}
/// The keyed lookup `labels × props` served from the lazy equality
/// postings (ADR-0614) — call on [`EqualityRoute::Postings`]; the
/// route owns the decline. The identity bucket of the smallest
/// interned `(label, key)` pair, streamed under the pair's guard
/// (the lock order the declared arm's capped serve already uses)
/// through the shared re-check walk, so the capped path never
/// materializes the id set — the ADR-0063 superset contract, a
/// stale posting costs a read, never a wrong answer. With `cap`
/// the walk stops at the first `cap + 1` matches
/// ([`Graph::lookup_nodes_upto_at`]'s contract). A value with no
/// property-index key (a NaN) or an uninterned name matches
/// nothing: the empty answer, as the scan would give.
pub(in crate::graph) fn equality_postings_upto(
&self,
labels: &[String],
props: &BTreeMap<String, Value>,
ts: u64,
cap: Option<usize>,
) -> Vec<NodeId> {
// The map form's own invariant: a keyed lookup carries a first
// entry to seek (the route never sends an empty map here).
let Some((key, value)) = props.first_key_value() else {
return Vec::new();
};
let Some(gk) = GroupKey::for_property_index(value) else {
return Vec::new();
};
let Some((lid, kid)) = self.smallest_serve_pair(labels, key) else {
return Vec::new();
};
let spec = RecheckSpec {
labels,
props,
ts,
cap,
};
let mut walk = self.recheck_walk(&spec);
self.with_prop_pair(lid, kid, |st| {
for id in st.postings.ids(&gk) {
if walk.visit(*id).is_break() {
break;
}
}
});
walk.finish()
}
/// The single-key `=`/`IN` serve from the lazy equality postings
/// (ADR-0614) — call on [`EqualityRoute::Postings`]; the route owns
/// the decline. Each value's identity bucket on the smallest
/// interned `(label, key)` pair, unioned, then re-verified through
/// the declared seek family's snapshot filter — so a WHERE-form
/// runtime seek answers exactly what the declared index's equality
/// serve would. A NaN value contributes nothing (it equals
/// nothing); an uninterned name answers empty.
pub(in crate::graph) fn equality_postings_values(
&self,
labels: &[String],
key: &str,
values: &[Value],
ts: u64,
) -> Vec<NodeId> {
// A NaN has no property-index key and equals nothing, so it
// contributes no bucket (the doc's contract). Nothing addressable
// answers empty before the pair is built.
let keys: Vec<GroupKey> = values
.iter()
.filter_map(GroupKey::for_property_index)
.collect();
if keys.is_empty() {
return Vec::new();
}
let Some((lid, kid)) = self.smallest_serve_pair(labels, key) else {
return Vec::new();
};
let mut acc: HashSet<NodeId> = HashSet::new();
self.with_prop_pair(lid, kid, |st| {
for gk in &keys {
acc.extend(st.postings.ids(gk));
}
});
self.finish_ids(acc, labels, key, ts, |v| {
values.iter().any(|b| v.eq_non_null(b))
})
}
/// Candidate ids of a `(label, key)` pair: the label index (the
/// canonical bucket copy, uncapped), or every node for the `*`
/// bucket.
pub(super) fn pair_candidate_ids(&self, lid: u32) -> Vec<NodeId> {
if lid == self.indexes.star_label_id {
return self.nodes.keys();
}
self.label_index_copy(lid, usize::MAX).0
}
/// The value of `kid` on every version of `id` that carries `lid` (any
/// label for the `*` bucket), each handed to `f` with whether it is
/// THE live version (the newest with no `xmax`, [`Chain::live`]'s
/// rule — a shadowed open-ended version is superseded).
/// Every pair build and drain walks versions here, so all declared
/// and equality postings share one superset contract (ADR-0018): the
/// seek re-checks the snapshot. The [`PairVersion`] flag is the one
/// pass's way out for per-node trust state that must reflect the
/// current committed value (the range index's `unposted`): a second
/// nodes-map read to recover it would walk what this already visited.
pub(super) fn for_each_node_pair_value(
&self,
id: NodeId,
lid: u32,
kid: u32,
mut f: impl FnMut(&Value, PairVersion),
) {
let Some(vers) = self.nodes.get(&id) else {
return;
};
let star = lid == self.indexes.star_label_id;
// THE live version is the newest open-ended one —
// [`Chain::live_index`]'s one statement of the rule, consulted
// by position so the walk keeps its oldest-to-newest order: the
// point drain's per-id posting is last-writer-wins, so a value
// posted by a superseded version must not come after the live
// one's.
let live_at = vers.live_index();
for (idx, v) in vers.iter().enumerate() {
if !star && !v.live.has_label(lid) {
continue;
}
if let Some(val) = v.live.props.get(kid) {
f(val, PairVersion::at(idx, live_at));
}
}
}
}
/// The equality pair as a [`DrainState`], so the node feed runs the one
/// post/reconcile protocol (`Graph::post_pair_node`) the declared pairs
/// run. Its posting policy is the lazy superset: `remove_node` does not
/// unpost (stale versions' buckets are pruned by `drop_unused_pairs`),
/// it only drops the trust mark the reconcile restates.
impl DrainState for PropPairIndex {
fn dirty_mut(&mut self) -> &mut HashSet<NodeId> {
&mut self.dirty
}
fn insert(&mut self, id: NodeId, value: &Value) {
prop_pair_post(self, id, GroupKey::for_property_index(value).as_ref());
}
fn remove_node(&mut self, id: NodeId) {
self.unposted.clear(id);
}
fn reconcile_live(&mut self, id: NodeId, live: Option<&Value>) {
let unpostable = live.is_some_and(|v| GroupKey::for_property_index(v).is_none());
self.unposted.restate(id, unpostable);
}
}
/// The one posting step both pair feeds take — the identity computed by
/// the caller, which the node feed also needs for the trust mark. The
/// `unposted` mark is never decided here: only the live reconciliation
/// writes it (a superseded NaN must not poison the pair).
pub(super) fn prop_pair_post(st: &mut PropPairIndex, id: NodeId, gk: Option<&GroupKey>) {
if let Some(gk) = gk {
st.postings.post(gk, id);
}
}