hawdb-storage 0.14.0

An embedded Rust graph database for local data-plane workloads
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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;
use hawdb_core::{RuntimeCancellationToken, RuntimeTaskContext};
use hawdb_qos::{
    IoConcurrencyBudget, LocalQosPolicy, LocalQosScheduler, QosTelemetryEvent, QosTelemetryPhase,
    QosTelemetrySink, RuntimeGovernor, RuntimeGovernorConfig, RuntimeMaintenanceWork,
    RuntimeMemorySnapshot, RuntimeResourceBudget, RuntimeResourceSnapshot,
};
use std::sync::atomic::{AtomicUsize, Ordering};

fn admitted(
    ceiling: u64,
    task: RuntimeTaskContext,
    scheduler: LocalQosScheduler,
) -> (
    RuntimeGovernor,
    RuntimeMaintenanceWork,
    CheckpointWorkContext,
) {
    let governor = RuntimeGovernor::new(
        RuntimeGovernorConfig {
            memory_budget_bytes: Some(4 * 1024 * 1024),
            background_task_limit: Some(std::num::NonZeroUsize::MIN),
            ..RuntimeGovernorConfig::shared_host()
        },
        RuntimeResourceSnapshot::from_parts(
            RuntimeResourceBudget::from_limits(std::num::NonZeroUsize::MIN, None, None),
            RuntimeMemorySnapshot::from_limits(Some(1 << 30), Some(1 << 30), None, None, None),
        ),
        IoConcurrencyBudget::new(2, 1),
    );
    let admission = governor
        .try_admit_incremental_maintenance(4096, ceiling, 1, task)
        .unwrap();
    let work = CheckpointWorkContext::new(admission.task_context().unwrap().clone())
        .with_scheduler(scheduler);
    (governor, admission, work)
}

fn scheduler() -> LocalQosScheduler {
    LocalQosScheduler::new(LocalQosPolicy {
        max_background_operations: Some(1),
        max_total_background_operations: Some(1),
        ..Default::default()
    })
}

fn record(id: u64, body: &str) -> NodeRecord {
    NodeRecord {
        id: NodeId(id),
        labels: BTreeSet::from([LabelId(7)]),
        properties: BTreeMap::from([
            ("body".into(), Value::String(body.into())),
            ("rank".into(), Value::Int(0)),
        ]),
    }
}

#[test]
fn graph_page_copy_charge_follows_pins_after_pause_and_foreground_detachment() {
    let body = "索引".repeat(16 * 1024);
    let mut map = CowSegmentedMap::from(BTreeMap::from([(NodeId(0), record(0, &body))]));
    let old = map.clone();
    let (governor, mut admission, work) =
        admitted(2 * 1024 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_copy_for_key(&NodeId(0), &work)
        .unwrap();
    assert!(!map.shares_storage_with(&old));
    assert!(
        admission.memory_report().live_accounted_bytes >= body.len() as u64,
        "page and directory allocation owners must follow live data"
    );
    assert_eq!(map.get(&NodeId(0)), old.get(&NodeId(0)));
    // This pin owns the actual admitted page and directory, including after
    // the task/admission handle closes. A normal foreground copy has its own
    // data while the pinned original retains the original allocation leases.
    let pin = map.clone();
    map.get_mut(&NodeId(0))
        .unwrap()
        .properties
        .insert("rank".into(), Value::Int(1));
    assert_eq!(
        pin.get(&NodeId(0)).unwrap().properties["rank"],
        Value::Int(0)
    );
    assert_eq!(
        map.get(&NodeId(0)).unwrap().properties["rank"],
        Value::Int(1)
    );
    admission.pause();
    assert_eq!(governor.snapshot().active_cpu_slots, 0);
    assert_eq!(governor.snapshot().active_background_tasks, 0);
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes >= body.len() as u64);
    drop(pin);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    assert_eq!(
        old.get(&NodeId(0)).unwrap().properties["body"],
        Value::String(body)
    );
    drop(old);
    drop(map);
}

#[test]
fn missing_key_update_keeps_shared_storage_without_a_copy_charge() {
    let mut map = CowSegmentedMap::from(BTreeMap::from([(NodeId(0), record(0, "value"))]));
    let pin = map.clone();
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_copy_for_key(&NodeId(1), &work)
        .unwrap();
    assert!(map.shares_storage_with(&pin));
    assert_eq!(admission.memory_report().live_accounted_bytes, 0);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn absent_key_insertion_copies_the_target_page_with_retained_ownership() {
    let body = "copy".repeat(1024);
    let mut map = CowSegmentedMap::from(BTreeMap::from([(NodeId(0), record(0, &body))]));
    let pin = map.clone();
    let (governor, admission, work) =
        admitted(2 * 1024 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &work)
        .unwrap();
    assert!(!map.shares_storage_with(&pin));
    assert!(admission.memory_report().live_accounted_bytes >= body.len() as u64);
    map.insert(NodeId(1), record(1, "new"));
    assert_eq!(map.len(), 2);
    assert_eq!(pin.len(), 1);
    assert!(pin.get(&NodeId(1)).is_none());
    assert_eq!(map.get(&NodeId(0)), pin.get(&NodeId(0)));
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes >= body.len() as u64);
    drop(map);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn large_directory_denies_before_allocating_and_keeps_every_shared_page() {
    let len = 10_000;
    // A valid sorted directory of small one-record pages isolates directory
    // copying from payload copying without constructing oversized records.
    let mut map = CowSegmentedMap {
        segments: CowSegment::from(CowPageDirectory::new(
            (0..len)
                .map(|id| {
                    CowSegment::from(BTreeMap::from([(
                        NodeId(id),
                        NodeRecord {
                            id: NodeId(id),
                            labels: BTreeSet::new(),
                            properties: BTreeMap::new(),
                        },
                    )]))
                })
                .collect::<Vec<_>>(),
        )),
        len: len as usize,
        delta_pressure_bytes: u128::from(len) * 32,
    };
    let pin = map.clone();
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), scheduler());
    let observer = crate::test_allocator::AllocationObservation::start();
    let result = map.prepare_checkpoint_copy_for_key(&NodeId(0), &work);
    let large_allocations = observer.finish();
    assert!(
        matches!(result, Err(CheckpointWorkError::Memory(_))),
        "COW directory capacity must be admitted before allocation"
    );
    assert_eq!(large_allocations, 0);
    assert!(map.shares_storage_with(&pin));
    assert_eq!(map.shared_segment_count_with(&pin), len as usize);
    assert_eq!(admission.memory_report().live_accounted_bytes, 0);
    assert_eq!(map.len(), len as usize);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[derive(Debug)]
struct CancelAt {
    calls: AtomicUsize,
    at: usize,
    token: RuntimeCancellationToken,
}
impl QosTelemetrySink for CancelAt {
    fn record_qos(&self, event: QosTelemetryEvent) {
        if event.phase == QosTelemetryPhase::Admission
            && self.calls.fetch_add(1, Ordering::SeqCst) + 1 == self.at
        {
            self.token.cancel();
        }
    }
}

#[test]
fn cancellation_at_every_copy_unit_preserves_original_directory_and_refunds_buffers() {
    let original = CowSegmentedMap::from(
        (0..128)
            .map(|id| (NodeId(id), record(id, "value")))
            .collect::<BTreeMap<_, _>>(),
    );
    let observer = Arc::new(CancelAt {
        calls: AtomicUsize::new(0),
        at: usize::MAX,
        token: RuntimeCancellationToken::new(),
    });
    let qos = scheduler();
    qos.set_telemetry_sink(Some(observer.clone()));
    let (governor, admission, work) = admitted(2 * 1024 * 1024, RuntimeTaskContext::default(), qos);
    let mut successful = original.clone();
    successful
        .prepare_checkpoint_copy_for_key(&NodeId(0), &work)
        .unwrap();
    let units = observer.calls.load(Ordering::SeqCst);
    assert!(units > original.len());
    drop(successful);
    assert_eq!(admission.memory_report().live_accounted_bytes, 0);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    for at in 1..=units {
        let token = RuntimeCancellationToken::new();
        let observer = Arc::new(CancelAt {
            calls: AtomicUsize::new(0),
            at,
            token: token.clone(),
        });
        let qos = scheduler();
        qos.set_telemetry_sink(Some(observer));
        let (governor, admission, work) = admitted(
            2 * 1024 * 1024,
            RuntimeTaskContext::without_deadline(token),
            qos.clone(),
        );
        let mut map = original.clone();
        let error = map
            .prepare_checkpoint_copy_for_key(&NodeId(0), &work)
            .unwrap_err();
        assert!(
            matches!(error, CheckpointWorkError::Stopped(_)),
            "unit {at}: {error}"
        );
        assert!(map.shares_storage_with(&original), "unit {at}");
        assert_eq!(
            map.iter().collect::<Vec<_>>(),
            original.iter().collect::<Vec<_>>()
        );
        assert_eq!(
            admission.memory_report().live_accounted_bytes,
            0,
            "unit {at}"
        );
        assert_eq!(qos.state().running_background_operations, 0);
        drop(work);
        drop(admission);
        assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    }
}

#[test]
fn empty_map_insert_denies_initial_capacity_before_changing_shared_storage() {
    let mut map = CowSegmentedMap::<NodeId, NodeId>::default();
    let pin = map.clone();
    let (governor, admission, work) = admitted(64, RuntimeTaskContext::default(), scheduler());
    let result = map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work);
    assert!(
        matches!(result, Err(CheckpointWorkError::Memory(_))),
        "initial page and directory capacity must be admitted before allocation"
    );
    assert!(map.shares_storage_with(&pin));
    assert!(map.is_empty());
    assert_eq!(admission.memory_report().live_accounted_bytes, 0);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn empty_map_first_page_charge_survives_insertion_task_close_and_snapshot_pins() {
    let mut map = CowSegmentedMap::<NodeId, NodeId>::default();
    let original = map.clone();
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
        .unwrap();
    assert!(map.is_empty());
    assert!(map.get(&NodeId(0)).is_none());
    let first_page_bound = ownership_bytes::<BTreeMap<NodeId, NodeId>>()
        + tree_bytes::<NodeId, NodeId>(1, &work).unwrap();
    assert!(
        admission.memory_report().live_accounted_bytes >= first_page_bound as u64,
        "first-page capacity ownership must precede insertion"
    );
    assert!(!map.shares_storage_with(&original));
    map.insert(NodeId(0), NodeId(9));
    assert_eq!(map.len(), 1);
    assert_eq!(map.get(&NodeId(0)), Some(&NodeId(9)));
    assert!(original.is_empty());
    let pin = map.clone();
    drop(work);
    drop(admission);
    drop(map);
    assert!(governor.snapshot().admitted_memory_bytes > 0);
    assert_eq!(pin.get(&NodeId(0)), Some(&NodeId(9)));
    drop(pin);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn empty_map_cancellation_at_every_initial_unit_preserves_the_original_root() {
    let original = CowSegmentedMap::<NodeId, NodeId>::default();
    let observer = Arc::new(CancelAt {
        calls: AtomicUsize::new(0),
        at: usize::MAX,
        token: RuntimeCancellationToken::new(),
    });
    let qos = scheduler();
    qos.set_telemetry_sink(Some(observer.clone()));
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), qos);
    let mut map = original.clone();
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
        .unwrap();
    let units = observer.calls.load(Ordering::SeqCst);
    assert!(units > 0);
    drop(map);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    for at in 1..=units {
        let token = RuntimeCancellationToken::new();
        let observer = Arc::new(CancelAt {
            calls: AtomicUsize::new(0),
            at,
            token: token.clone(),
        });
        let qos = scheduler();
        qos.set_telemetry_sink(Some(observer));
        let (governor, admission, work) = admitted(
            4096,
            RuntimeTaskContext::without_deadline(token),
            qos.clone(),
        );
        let mut map = original.clone();
        let error = map
            .prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
            .unwrap_err();
        assert!(
            matches!(error, CheckpointWorkError::Stopped(_)),
            "unit {at}: {error}"
        );
        assert!(map.shares_storage_with(&original), "unit {at}");
        assert!(map.is_empty());
        assert_eq!(
            admission.memory_report().live_accounted_bytes,
            0,
            "unit {at}"
        );
        assert_eq!(qos.state().running_background_operations, 0);
        drop(map);
        drop(work);
        drop(admission);
        assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    }
}

#[test]
fn empty_map_copy_after_a_snapshot_keeps_its_first_insertion_capacity_admitted() {
    let mut map = CowSegmentedMap::<NodeId, NodeId>::default();
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
        .unwrap();
    let empty_pin = map.clone();
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
        .unwrap();
    assert!(map.is_empty());
    assert!(!map.shares_storage_with(&empty_pin));
    drop(empty_pin);
    let bound = ownership_bytes::<BTreeMap<NodeId, NodeId>>()
        + tree_bytes::<NodeId, NodeId>(1, &work).unwrap();
    assert!(
        admission.memory_report().live_accounted_bytes >= bound as u64,
        "a copied empty page must retain first-insertion capacity after the old pin closes"
    );
    map.insert(NodeId(0), NodeId(9));
    assert_eq!(map.get(&NodeId(0)), Some(&NodeId(9)));
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes > 0);
    drop(map);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn unique_page_insertion_denies_growth_before_mutating_or_losing_ownership() {
    let mut map = CowSegmentedMap::from(BTreeMap::from([(NodeId(0), NodeId(9))]));
    let (governor, admission, work) = admitted(64, RuntimeTaskContext::default(), scheduler());
    let result = map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &work);
    assert!(
        matches!(result, Err(CheckpointWorkError::Memory(_))),
        "a unique page still needs insertion/split capacity admission"
    );
    assert_eq!(map.len(), 1);
    assert_eq!(map.get(&NodeId(0)), Some(&NodeId(9)));
    assert!(map.get(&NodeId(1)).is_none());
    assert_eq!(admission.memory_report().live_accounted_bytes, 0);
    drop(work);
    drop(admission);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn preflighted_insertions_keep_growth_and_split_charges_with_every_live_page() {
    let count = 1025usize;
    let mut map = CowSegmentedMap::<NodeId, NodeId>::default();
    let old = map.clone();
    let (governor, admission, work) =
        admitted(4 * 1024 * 1024, RuntimeTaskContext::default(), scheduler());
    let mut first_charge = None;
    for id in 0..count {
        map.prepare_checkpoint_insertions_for_key(&NodeId(id as u64), count, &work)
            .unwrap();
        let charge = admission.memory_report().live_accounted_bytes;
        assert_eq!(
            *first_charge.get_or_insert(charge),
            charge,
            "preflight must share one growth allowance across the complete batch"
        );
    }
    let directory_capacity = map.segments.capacity();
    assert!(directory_capacity >= count);
    let charge = admission.memory_report().live_accounted_bytes;
    let page_charge = CowSegmentedMap::<NodeId, NodeId>::checkpoint_growth_bytes(
        0,
        count,
        size_of::<NodeId>(),
        &work,
    )
    .unwrap() as u64;
    assert!(
        charge >= page_charge,
        "the complete growth bound must remain admitted"
    );
    for id in 0..count {
        map.insert(NodeId(id as u64), NodeId(id as u64 + 9));
    }
    assert_eq!(map.segments.capacity(), directory_capacity);
    assert!(map.segment_count() >= 3);
    assert_eq!(map.len(), count);
    assert!(old.is_empty());
    for id in 0..count {
        assert_eq!(map.get(&NodeId(id as u64)), Some(&NodeId(id as u64 + 9)));
    }
    let pin = map.clone();
    let last_page = map.segments.last().unwrap().clone();
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes >= charge);
    drop(map);
    assert!(governor.snapshot().admitted_memory_bytes >= charge);
    drop(pin);
    assert!(
        governor.snapshot().admitted_memory_bytes >= page_charge,
        "the split right page must retain the original growth inventory"
    );
    assert_eq!(
        last_page.last_key_value(),
        Some((&NodeId(1024), &NodeId(1033)))
    );
    drop(last_page);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    drop(old);
}

#[test]
fn existing_growth_allowance_does_not_bypass_a_lower_resumed_memory_ceiling() {
    let mut map = CowSegmentedMap::from(BTreeMap::from([(NodeId(0), NodeId(9))]));
    let (governor, admission, work) =
        admitted(16 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insertions_for_key(&NodeId(1), 4, &work)
        .unwrap();
    let charge = admission.memory_report().live_accounted_bytes;
    let (lower_governor, lower_admission, lower_work) =
        admitted(64, RuntimeTaskContext::default(), scheduler());
    let result = map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &lower_work);
    assert!(matches!(result, Err(CheckpointWorkError::Memory(_))));
    assert_eq!(map.len(), 1);
    assert_eq!(map.get(&NodeId(0)), Some(&NodeId(9)));
    assert!(map.get(&NodeId(1)).is_none());
    assert_eq!(admission.memory_report().live_accounted_bytes, charge);
    assert_eq!(lower_admission.memory_report().live_accounted_bytes, 0);
    drop(lower_work);
    drop(lower_admission);
    assert_eq!(lower_governor.snapshot().admitted_memory_bytes, 0);
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes >= charge);
    drop(map);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn consumed_growth_allowance_requires_new_admission_for_the_next_batch() {
    let mut map = CowSegmentedMap::<NodeId, NodeId>::default();
    let (governor, admission, work) =
        admitted(16 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insertions_for_key(&NodeId(0), 4, &work)
        .unwrap();
    for id in 0..4 {
        map.insert(NodeId(id), NodeId(id + 9));
    }
    let before = admission.memory_report().live_accounted_bytes;
    map.prepare_checkpoint_insertions_for_key(&NodeId(4), 4, &work)
        .unwrap();
    assert!(
        admission.memory_report().live_accounted_bytes > before,
        "consumed credits cannot admit another batch for free"
    );
    for id in 4..8 {
        map.insert(NodeId(id), NodeId(id + 9));
    }
    assert_eq!(map.len(), 8);
    for id in 0..8 {
        assert_eq!(map.get(&NodeId(id)), Some(&NodeId(id + 9)));
    }
    let pin = map.clone();
    drop(work);
    drop(admission);
    drop(map);
    assert!(governor.snapshot().admitted_memory_bytes > before);
    drop(pin);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn insertion_cancellation_at_every_growth_unit_keeps_the_original_root() {
    let make_map = || CowSegmentedMap::from(BTreeMap::from([(NodeId(0), NodeId(9))]));
    let mut map = make_map();
    let original = map.clone();
    let observer = Arc::new(CancelAt {
        calls: AtomicUsize::new(0),
        at: usize::MAX,
        token: RuntimeCancellationToken::default(),
    });
    let observed_scheduler = scheduler();
    observed_scheduler.set_telemetry_sink(Some(observer.clone()));
    let (governor, admission, work) =
        admitted(4096, RuntimeTaskContext::default(), observed_scheduler);
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &work)
        .unwrap();
    let units = observer.calls.load(Ordering::SeqCst);
    assert!(units > 0);
    drop(work);
    drop(admission);
    drop(map);
    drop(original);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    for at in 1..=units {
        let mut map = make_map();
        let original = map.clone();
        let token = RuntimeCancellationToken::default();
        let observer = Arc::new(CancelAt {
            calls: AtomicUsize::new(0),
            at,
            token: token.clone(),
        });
        let observed_scheduler = scheduler();
        observed_scheduler.set_telemetry_sink(Some(observer));
        let task = RuntimeTaskContext::without_deadline(token);
        let (governor, admission, work) = admitted(4096, task, observed_scheduler.clone());
        assert!(matches!(
            map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &work),
            Err(CheckpointWorkError::Stopped(_))
        ));
        assert!(map.shares_storage_with(&original), "unit {at}");
        assert_eq!(map.len(), 1);
        assert_eq!(map.get(&NodeId(0)), Some(&NodeId(9)));
        assert_eq!(admission.memory_report().live_accounted_bytes, 0);
        assert_eq!(observed_scheduler.state().running_background_operations, 0);
        drop(work);
        drop(admission);
        assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
    }
}

#[test]
fn replacing_a_record_consumes_growth_before_another_split_can_be_admitted() {
    let mut map = CowSegmentedMap::from(
        (0..10)
            .map(|id| (NodeId(id), record(id, "")))
            .collect::<BTreeMap<_, _>>(),
    );
    assert_eq!(map.segment_count(), 1);
    let (governor, admission, work) =
        admitted(8 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_insert_copy_for_key(&NodeId(0), &work)
        .unwrap();
    let previous = map
        .insert(NodeId(0), record(0, &"p".repeat(128 * 1024)))
        .unwrap();
    assert_eq!(previous, record(0, ""));
    assert_eq!(
        map.segment_count(),
        2,
        "a replacement can split a primary page"
    );
    let charge = admission.memory_report().live_accounted_bytes;
    let result = map.prepare_checkpoint_insert_copy_for_key(&NodeId(1), &work);
    assert!(
        matches!(result, Err(CheckpointWorkError::Memory(_))),
        "the first replacement consumed its allowance; the second split needs fresh admission"
    );
    assert_eq!(map.len(), 10);
    assert_eq!(
        map.get(&NodeId(0)).unwrap().properties.get("body"),
        Some(&Value::String("p".repeat(128 * 1024)))
    );
    for id in 1..10 {
        assert_eq!(map.get(&NodeId(id)), Some(&record(id, "")));
    }
    assert_eq!(admission.memory_report().live_accounted_bytes, charge);
    drop(work);
    drop(admission);
    assert!(governor.snapshot().admitted_memory_bytes >= charge);
    drop(map);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn retained_node_row_keeps_checkpoint_copy_charge_after_directory_release() {
    let body = "retained checkpoint row".repeat(8192);
    let mut map = crate::cow::CowNodeMap::from(BTreeMap::from([(NodeId(0), record(0, &body))]));
    let old = map.clone();
    let (governor, mut admission, work) =
        admitted(2 * 1024 * 1024, RuntimeTaskContext::default(), scheduler());
    map.prepare_checkpoint_copy_for_key(&NodeId(0), &work)
        .unwrap();
    let row = map
        .retained_point_row_preflight(NodeId(0))
        .unwrap()
        .0
        .clone();
    map.get_mut(&NodeId(0))
        .unwrap()
        .properties
        .insert("rank".into(), Value::Int(1));
    assert_eq!(row.properties["rank"], Value::Int(0));
    assert_eq!(
        old.get(&NodeId(0)).unwrap().properties["rank"],
        Value::Int(0)
    );
    admission.pause();
    drop(work);
    drop(admission);
    drop(map);
    drop(old);
    assert!(governor.snapshot().admitted_memory_bytes >= body.len() as u64);
    assert_eq!(row.properties["body"], Value::String(body));
    drop(row);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn denied_retained_node_checkpoint_copy_preserves_rows_and_releases_admission() {
    let body = "large row".repeat(8192);
    let mut map = crate::cow::CowNodeMap::from(BTreeMap::from([(NodeId(0), record(0, &body))]));
    let old = map.clone();
    let (governor, admission, work) = admitted(4096, RuntimeTaskContext::default(), scheduler());
    assert!(map
        .prepare_checkpoint_copy_for_key(&NodeId(0), &work)
        .is_err());
    assert_eq!(map.get(&NodeId(0)), old.get(&NodeId(0)));
    drop(work);
    drop(admission);
    drop(map);
    drop(old);
    assert_eq!(governor.snapshot().admitted_memory_bytes, 0);
}

#[test]
fn retained_node_pressure_tracks_snapshot_mutations_and_removal() {
    let mut map = crate::cow::CowNodeMap::from(BTreeMap::from([(NodeId(0), record(0, "small"))]));
    let old = map.clone();
    let before = map.delta_pressure_bytes();
    map.get_mut(&NodeId(0))
        .unwrap()
        .properties
        .insert("body".into(), Value::String("x".repeat(8192)));
    assert_eq!(map.delta_pressure_bytes(), before + 8192 - 5);
    assert_eq!(old.delta_pressure_bytes(), before);
    map.remove(&NodeId(0));
    assert_eq!(map.delta_pressure_bytes(), 0);
    assert_eq!(
        old.get(&NodeId(0)).unwrap().properties["body"],
        Value::String("small".into())
    );
}