relate-executor 1.4.0

In-memory graph and Cypher physical executor
Documentation
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
use super::*;
use crate::adjacency::SealedArena;

/// The three fold conditions (ADR-0551), each on its own chunk: a
/// never-sealed chunk at the floor, a delta past 25% of the sealed hops,
/// and a tombstone reclaimable at the GC horizon. A chunk below its gate
/// stays delta.
#[test]
fn compaction_folds_on_floor_ratio_and_reclaimable_tombstone() {
    let type_id = 9;
    // 1. Floor: a never-sealed chunk seals at `COMPACTION_DELTA_FLOOR`.
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    for i in 0..(COMPACTION_DELTA_FLOOR - 1) as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(
        !due(&a, Some(sealed), type_id, 0),
        "one hop below the floor"
    );
    a.insert(type_id, 1000, Some(1000));
    assert!(due(&a, Some(sealed), type_id, 0), "the floor");
    assert!(fold(&mut a, sealed, 0));
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR);

    // 2. Ratio: a delta past 25% of the sealed hops folds.
    for i in 0..(COMPACTION_DELTA_FLOOR / 4) as i64 {
        a.insert(type_id, 2000 + i, Some(2000 + i));
    }
    assert!(
        !due(&a, Some(sealed), type_id, 0),
        "exactly 25% of the sealed hops is not past it"
    );
    a.insert(type_id, 3000, Some(3000));
    assert!(
        due(&a, Some(sealed), type_id, 0),
        "past 25% of the sealed hops"
    );
    assert!(fold(&mut a, sealed, 0));
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 9);

    // 3. Tombstone: a sealed hop reclaimable at the horizon folds away
    // even with an empty delta.
    sealed.tombstone_deleted(type_id, 5, 40, Supersede::Deleted);
    assert!(
        !due(&a, Some(sealed), type_id, 39),
        "the tombstone is not yet at the horizon"
    );
    assert!(
        due(&a, Some(sealed), type_id, 40),
        "the tombstone is reclaimable"
    );
    assert!(fold(&mut a, sealed, 40));
    assert!(sealed.tombstones(type_id).is_empty());
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 8);
    assert!(
        !due(&a, Some(sealed), type_id, 41),
        "a clean sealed chunk is not due"
    );
}

/// The fold is idempotent and reclaims a tombstoned hop at the horizon.
#[test]
fn compaction_reclaims_at_the_horizon_and_is_idempotent() {
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 2;
    for i in 0..COMPACTION_DELTA_FLOOR as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(fold(&mut a, sealed, 0));
    sealed.tombstone_deleted(type_id, 5, 40, Supersede::Deleted);
    // Below the horizon nothing is due again.
    assert!(!due(&a, Some(sealed), type_id, 39));
    assert!(!fold(&mut a, sealed, 39));
    // At the horizon the tombstone is reclaimable and the fold drops it.
    assert!(fold(&mut a, sealed, 40));
    assert!(sealed.tombstones(type_id).is_empty());
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR - 1);
    assert!(
        !due(&a, Some(sealed), type_id, 41),
        "a clean sealed chunk is not due"
    );
}

/// The fold drops a deleted row's delta hop once its tombstone reaches
/// the horizon, instead of sealing a hop the chain GC can no longer
/// prune. Below the horizon it seals under its tombstone, so an older
/// snapshot still sees it.
#[test]
fn the_fold_drops_a_deleted_rows_delta_hop_at_the_horizon() {
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 4;
    for i in 0..COMPACTION_DELTA_FLOOR as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(fold(&mut a, sealed, 0));
    a.insert(type_id, 100, Some(100));
    sealed.tombstone_deleted(type_id, 100, 40, Supersede::Deleted);
    for i in 0..9 {
        a.insert(type_id, 200 + i, Some(200 + i));
    }
    assert!(
        fold(&mut a, sealed, 0),
        "the delta is past 25% of the sealed hops"
    );
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 10);
    assert!(sealed.decoded(type_id).iter().any(|h| h.id == 100));

    assert!(fold(&mut a, sealed, 40), "the tombstone is at the horizon");
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 9);
    assert!(!sealed.decoded(type_id).iter().any(|h| h.id == 100));
    assert!(sealed.tombstones(type_id).is_empty());
}

/// A delete on a never-sealed chunk records its tombstone against a
/// zero-hop sealed stream created on demand, so the fold drops the dead
/// delta hop instead of sealing it as a ghost (ADR-0551). The floor still
/// gates that chunk's first seal, and the fold drops or keeps the delta
/// hop per the tombstone's two version stamps.
#[test]
fn a_never_sealed_chunk_records_its_tombstone_and_folds_the_dead_hop_away() {
    // The tombstone does not seal a small chunk early: the floor gates a
    // never-sealed chunk's first seal.
    let mut small = TypedAdj::default();
    let small_arena = SealedArena::default();
    let small_sealed = small_arena.region_or_default(0);
    for i in 0..3 {
        small.insert(11, i, Some(i));
    }
    small_sealed.tombstone_deleted(11, 1, 40, Supersede::Deleted);
    assert!(
        !due(&small, Some(small_sealed), 11, 39),
        "the floor gates the first seal while the tombstone is live"
    );
    // At the horizon the fold is the reclaim alone: the tombstone still
    // names the delta hop, so nothing is reclaimed and nothing seals.
    assert!(!fold(&mut small, small_sealed, u64::MAX));
    assert_eq!(small_sealed.chunk_len(11), 0, "no early seal");

    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 11;
    for i in 0..(COMPACTION_DELTA_FLOOR + 1) as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(!sealed.has_chunk(type_id), "the chunk is still delta");

    sealed.tombstone_deleted(type_id, 5, 40, Supersede::Deleted);
    assert!(
        sealed.has_chunk(type_id),
        "the tombstone creates the zero-hop sealed chunk"
    );
    assert_eq!(sealed.chunk_len(type_id), 0, "the zero-hop entry");
    assert_eq!(
        sealed.tombstones(type_id),
        [Tombstone {
            id: 5,
            xmax: 40,
            deleted: Some(40),
        }]
    );
    assert!(
        due(&a, Some(sealed), type_id, 39),
        "the delta reaches the floor"
    );

    // Below the horizon the fold seals the dead hop under its tombstone,
    // so an older snapshot still sees it.
    assert!(fold(&mut a, sealed, 39));
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 1);
    let mut before = Vec::new();
    a.for_each_hop_of(Some(sealed), type_id, 39, |hop| before.push(hop.id));
    assert!(before.contains(&5));

    assert!(
        due(&a, Some(sealed), type_id, 40),
        "the tombstone is reclaimable"
    );
    assert!(fold(&mut a, sealed, 40));
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR);
    assert!(sealed.tombstones(type_id).is_empty());
    assert!(!a.flatten(Some(sealed)).contains(&5), "no ghost hop");
}

/// A never-sealed chunk's reposition, then delete, keeps the relocated
/// delta hop at a horizon between the two stamps: the sealed position
/// (none here) reclaims while the live hop waits for its delete stamp.
#[test]
fn a_never_sealed_reposition_then_delete_keeps_the_relocated_hop() {
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 13;
    for i in 0..(COMPACTION_DELTA_FLOOR + 1) as i64 {
        a.insert(type_id, i, Some(i));
    }
    a.reposition(sealed, type_id, 5, Some(40), 40);
    sealed.tombstone_deleted(type_id, 5, 60, Supersede::Deleted);

    // A horizon between the two stamps changes nothing: the relocated hop
    // is the id's live position, so the fold guard keeps it in the delta.
    fold(&mut a, sealed, 40);
    assert_eq!(a.chunk_hops(type_id).len(), COMPACTION_DELTA_FLOOR + 1);
    assert_eq!(sealed.chunk_len(type_id), 0);
    assert_eq!(
        sealed.tombstones(type_id),
        [Tombstone {
            id: 5,
            xmax: 40,
            deleted: Some(60),
        }]
    );

    assert!(
        fold(&mut a, sealed, 60),
        "the delete stamp reaches the horizon"
    );
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR);
    assert!(!a.flatten(Some(sealed)).contains(&5));
}

/// The fold guard blocks only a reposition whose id still holds a sealed
/// position. A reposition (or delete) of a delta-only hop records a
/// tombstone the fold resolves, so it never wedges the chunk; a sealed
/// hop's tombstone would hide the id's live delta hop and holds the fold
/// until the horizon passes.
#[test]
fn the_fold_guard_only_blocks_a_reposition_of_a_sealed_hop() {
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 8;
    for i in 0..COMPACTION_DELTA_FLOOR as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(fold(&mut a, sealed, 0));

    // A delta-only hop repositioned inside the sealed chunk: the
    // tombstone names no sealed position and is dropped, and the hop
    // folds in as the id's live position.
    a.insert(type_id, 100, Some(1));
    a.reposition(sealed, type_id, 100, Some(2), 40);
    for i in 0..9 {
        a.insert(type_id, 200 + i, Some(200 + i));
    }
    assert!(fold(&mut a, sealed, 0));
    assert!(sealed.tombstones(type_id).is_empty());
    let mut seen = Vec::new();
    a.for_each_hop_of(Some(sealed), type_id, u64::MAX, |hop| {
        seen.push((hop.ts, hop.id))
    });
    assert!(seen.contains(&(Some(2), 100)));

    // A sealed hop's reposition holds the fold while its tombstone is
    // below the horizon: a bare-id tombstone cannot name one position.
    a.reposition(sealed, type_id, 3, Some(33), 40);
    for i in 0..10 {
        a.insert(type_id, 300 + i, Some(300 + i));
    }
    assert!(!fold(&mut a, sealed, 0), "the reposition holds the fold");
    assert!(fold(&mut a, sealed, 40), "past the horizon the fold runs");
    let mut seen = Vec::new();
    a.for_each_hop_of(Some(sealed), type_id, u64::MAX, |hop| {
        seen.push((hop.ts, hop.id))
    });
    assert!(seen.contains(&(Some(33), 3)), "the live position folds in");
    assert!(!seen.contains(&(Some(3), 3)), "the sealed position is gone");
    assert!(sealed.tombstones(type_id).is_empty());
}

/// The fold guard blocks a delete that followed a reposition while its
/// delete stamp is above the horizon: folding the relocated hop under the
/// tombstone would hide it from a reader between the two stamps. The
/// sealed position still reclaims at the reposition's stamp.
#[test]
fn the_fold_guard_blocks_a_delete_above_the_horizon_after_a_reposition() {
    let mut a = TypedAdj::default();
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(0);
    let type_id = 12;
    for i in 0..COMPACTION_DELTA_FLOOR as i64 {
        a.insert(type_id, i, Some(i));
    }
    assert!(fold(&mut a, sealed, 0));
    // The sealed position is superseded at 40; the relocated hop is
    // deleted at 60.
    a.reposition(sealed, type_id, 3, Some(40), 40);
    sealed.tombstone_deleted(type_id, 3, 60, Supersede::Deleted);
    for i in 0..10 {
        a.insert(type_id, 300 + i, Some(300 + i));
    }
    assert!(
        !fold(&mut a, sealed, 0),
        "nothing reclaims below both stamps"
    );
    assert!(
        fold(&mut a, sealed, 40),
        "the sealed position reclaims, the relocated hop stays"
    );
    assert!(
        !fold(&mut a, sealed, 40),
        "the second pass has nothing left to do"
    );
    {
        assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR - 1);
        assert_eq!(
            a.chunk_hops(type_id).len(),
            11,
            "the relocated hop stays delta"
        );
        assert_eq!(
            sealed.tombstones(type_id),
            [Tombstone {
                id: 3,
                xmax: 40,
                deleted: Some(60),
            }]
        );
    }
    assert!(
        fold(&mut a, sealed, 60),
        "the delete stamp reaches the horizon"
    );
    assert_eq!(sealed.chunk_len(type_id), COMPACTION_DELTA_FLOOR + 9);
    assert!(sealed.tombstones(type_id).is_empty());
    let mut seen = Vec::new();
    a.for_each_hop_of(Some(sealed), type_id, u64::MAX, |hop| seen.push(hop.id));
    assert!(!seen.contains(&3));
}

/// The adaptive floor's three facts (REL8-280): a never-sealed chunk
/// below the knee is due once its length holds across a pass; growth
/// The adaptive floor's facts: a never-sealed chunk below the knee is due
/// only once its length held across two fold passes — one quiet interval
/// is not evidence the chunk stopped growing; growth resets the baseline
/// (a still-growing chunk keeps the knee's floor); the
/// sub-[`STABILITY_SEAL_FLOOR`] band stays delta — the header outweighs
/// the stream it would buy.
#[test]
fn the_stability_floor_reads_the_noted_baseline() {
    let mut a = TypedAdj::default();
    // The eligible length is the floor's, not a literal: the band's lower
    // bound is the fact under test.
    for i in 0..(STABILITY_SEAL_FLOOR + 4) as i64 {
        a.insert(1, i, Some(100 - i));
    }
    assert!(
        !due(&a, None, 1, 0),
        "an unnoted chunk is never stability-sealed"
    );
    assert!(
        a.has_fold_work(None, 0),
        "a sub-knee chunk carries a baseline"
    );
    a.note_stable(None);
    assert!(
        !due(&a, None, 1, 0),
        "one held interval is not evidence of a steady state"
    );
    a.note_stable(None);
    assert!(
        due(&a, None, 1, 0),
        "the twice-held length seals below the knee"
    );

    // Growth invalidates the baseline; the new length needs its own two
    // stable passes before it can seal.
    a.insert(1, 99, Some(1));
    assert!(!due(&a, None, 1, 0), "growth resets the stability baseline");
    a.note_stable(None);
    assert!(!due(&a, None, 1, 0), "the new length must hold twice too");
    a.note_stable(None);
    assert!(due(&a, None, 1, 0), "the new length held");

    // The header-overhead band: noted and held, still delta.
    let mut small = TypedAdj::default();
    for i in 0..(STABILITY_SEAL_FLOOR - 1) as i64 {
        small.insert(1, i, Some(50 - i));
    }
    small.note_stable(None);
    small.note_stable(None);
    assert!(
        !due(&small, None, 1, 0),
        "the sub-stability band stays delta"
    );
}

/// The derived floor's residency bound: once the settle passes run — note,
/// held confirmation, seal — no chunk at or above the floor is still
/// unsealed, and the sub-floor band is still delta.
#[test]
fn the_settled_band_drains_into_the_sealed_stream() {
    let arena = SealedArena::default();
    let sealed = arena.region_or_default(7);
    let mut a = TypedAdj::default();
    let settled = STABILITY_SEAL_FLOOR + 3;
    for i in 0..settled as i64 {
        a.insert(1, i, Some(100 - i));
    }
    for i in 0..(STABILITY_SEAL_FLOOR - 1) as i64 {
        a.insert(2, i, Some(100 - i));
    }
    for _ in 0..3 {
        a.fold_at(sealed, 0);
    }
    assert_eq!(
        sealed.chunk_len(1),
        settled,
        "the settled chunk's whole stream is sealed"
    );
    assert_eq!(
        a.chunk_hops(2).len(),
        STABILITY_SEAL_FLOOR - 1,
        "the sub-floor band stays delta"
    );
    assert_eq!(sealed.chunk_len(2), 0, "the sub-floor band is never sealed");
}