pi_db 0.19.13

Full cache based database,support transaction
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
//! ę ¹äŗ‹åŠ” `delete/dirty_delete` ēš„ēœŸå®žę‰¹ę¬”ć€å†²ēŖć€WALć€ē”®č®¤å’Œę¢å¤å„‘ēŗ¦äø“é”¹ć€‚
//!
//! 本 target ä½æē”ØēœŸå®ž 4-worker runtime态`Transaction2PcManager`态`CommitLogger`态
//! Memory/LogOrdered/Btree态redbć€č”Øę—„åæ—å’Œę–‡ä»¶ē³»ē»Ÿć€‚ę¢å¤ēŸ©é˜µå…ˆē”®č®¤ē§å­å€¼å·²ē»čæ›å…„č”Øę•°ę®ę–‡ä»¶ļ¼Œ
//! å†ē§»čµ°ē§å­ę ¹ WALļ¼Œåˆ¶é€ åˆ é™¤ WAL å·²ęäŗ¤ä½†č”Øåˆ é™¤å°šęœŖē”®č®¤ēš„ēŖ—å£ļ¼›ē”Ÿäŗ§ repair态`.bak` å’Œäø¤ę¬”
//! data-only å†·åÆåŠØå…±åŒä½œäøŗē”¬é—Øē¦ć€‚ę™®é€šäøŽ dirty éžē©ŗåŠØä½œå§‹ē»ˆä½æē”ØäøåŒę ¹ć€‚
//!
//! ę­£å¼å„‘ēŗ¦č§ `docs/ROOT_DELETE_CONTRACT.md#root-delete-contract-index`怂

mod key_version_support;

use std::{
    env,
    fs,
    path::{Path, PathBuf},
    process::{Child, Command, ExitStatus},
    thread,
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};

use pi_async_rt::rt::{
    multi_thread::MultiTaskRuntime,
    AsyncRuntime,
};
use pi_async_transaction::{
    manager_2pc::Transaction2PcStatus,
    AsyncCommitLog,
    ErrorLevel,
    Transaction2Pc,
    TransactionTree,
    UnitTransaction,
};
use pi_atom::Atom;
use pi_bon::WriteBuffer;
use pi_db::{
    db::KVDBTransaction,
    tables::{
        mem_ord_table::MemoryOrderedTable,
        KVTable,
        TableKV,
    },
    utils::CreateTableOptions,
    Binary,
    KVDBTableType,
    KVTableMeta,
    KVTableTrError,
};
use pi_sinfo::EnumType;
use pi_store::commit_logger::CommitLogger;

use key_version_support::{
    build_database,
    expect_binary,
    expect_eq,
    query_ordinary,
    read_only_transaction,
    run_on_runtime,
    writable_transaction,
    Fixture,
    RealTransaction,
    TestResult,
};

type CodecTable = MemoryOrderedTable<usize, CommitLogger>;

const TEST_NAME: &str = "test_root_delete_contract_recovery";
const PHASE_ENV: &str = "PI_DB_ROOT_DELETE_PHASE";
const ROOT_ENV: &str = "PI_DB_ROOT_DELETE_ROOT";
const SEED_WAL_DIR: &str = "confirmed-seed-root-wal";
const DELETE_WAL_DIR: &str = "confirmed-delete-root-wal";

const MEMORY_TABLE: &str = "root_delete_memory";
const VOLATILE_MEMORY_TABLE: &str = "root_delete_memory_volatile";
const LOG_ORDERED_TABLE: &str = "root_delete_log_ordered";
const BTREE_TABLE: &str = "root_delete_btree";

const ROOT_TID_BYTES: usize = 16;
const PROCESS_TIMEOUT: Duration = Duration::from_secs(180);
const SEED_TIMEOUT: Duration = Duration::from_secs(130);
const DELETE_TIMEOUT: Duration = Duration::from_secs(45);
const RECOVERY_TIMEOUT: Duration = Duration::from_secs(130);
const DATA_ONLY_TIMEOUT: Duration = Duration::from_secs(30);
const CONFLICT_TIMEOUT: Duration = Duration::from_secs(60);
const CONFIRM_TIMEOUT: Duration = Duration::from_secs(100);

#[test]
fn test_root_delete_contract_recovery() {
    if let Ok(phase) = env::var(PHASE_ENV) {
        let root = PathBuf::from(
            env::var_os(ROOT_ENV)
                .expect("root-delete child phase must receive its root path"),
        );
        run_child_phase(&phase, &root)
            .unwrap_or_else(|error| panic!("root-delete phase {phase} failed: {error}"));
        return;
    }

    let root = unique_temp_root("recovery");
    fs::create_dir_all(&root).expect("creating root-delete recovery root must succeed");
    let result = (|| -> TestResult<()> {
        run_phase_process(&root, "seed", PROCESS_TIMEOUT)?;
        archive_root_wal(&root, SEED_WAL_DIR)?;
        run_phase_process(&root, "delete", PROCESS_TIMEOUT)?;
        run_phase_process(&root, "recover", PROCESS_TIMEOUT)?;
        archive_root_wal(&root, DELETE_WAL_DIR)?;
        run_phase_process(&root, "data-only", PROCESS_TIMEOUT)?;
        run_phase_process(&root, "data-only-again", PROCESS_TIMEOUT)
    })();
    if let Err(error) = result {
        panic!(
            "root-delete recovery failed; evidence is preserved at {:?}: {error}",
            root,
        );
    }
    fs::remove_dir_all(&root).expect("cleaning root-delete recovery root must succeed");
}

#[test]
fn test_root_dirty_delete_conflict_matrix() {
    let root = TempRoot::new("dirty-conflicts")
        .expect("creating dirty-delete conflict root must succeed");
    let root_path = root.path().to_path_buf();

    run_on_runtime(CONFLICT_TIMEOUT, move |rt| async move {
        let fixture = build_database(
            &rt,
            &root_path,
            Duration::ZERO,
            Duration::ZERO,
        ).await?;
        create_tables(&fixture).await?;

        for case in [
            DirtyConflictCase::new("persistent Memory", MEMORY_TABLE, false, true),
            DirtyConflictCase::new("volatile Memory", VOLATILE_MEMORY_TABLE, true, false),
            DirtyConflictCase::new("LogOrdered", LOG_ORDERED_TABLE, false, true),
            DirtyConflictCase::new("Btree", BTREE_TABLE, true, true),
        ] {
            verify_dirty_conflict_case(&fixture, case).await?;
        }

        expect_eq(
            "dirty-delete conflict manager balance",
            &fixture.tr_manager.produced_transaction_total(),
            &fixture.tr_manager.consumed_transaction_total(),
        )?;
        expect_eq(
            "dirty-delete conflict active roots",
            &fixture.tr_manager.transaction_len(),
            &0usize,
        )?;
        let appended = fixture.logger.append_total_count();
        let confirmed = fixture.logger.confirm_total_count();
        let waiting = fixture.logger.waiting_confirm_count().await;
        expect_eq(
            "dirty-delete conflict WAL conservation",
            &appended,
            &(confirmed + waiting),
        )
    })
    .unwrap_or_else(|error| panic!("root dirty-delete conflict matrix failed: {error}"));
}

fn run_child_phase(phase: &str, root: &Path) -> TestResult<()> {
    match phase {
        "seed" => {
            let root = root.to_path_buf();
            run_on_runtime(SEED_TIMEOUT, move |rt| async move {
                phase_seed(rt, root).await
            })
        },
        "delete" => {
            let root = root.to_path_buf();
            run_on_runtime(DELETE_TIMEOUT, move |rt| async move {
                phase_delete(rt, root).await
            })
        },
        "recover" => {
            let root = root.to_path_buf();
            run_on_runtime(RECOVERY_TIMEOUT, move |rt| async move {
                phase_recover(rt, root).await
            })
        },
        "data-only" => {
            let root = root.to_path_buf();
            run_on_runtime(DATA_ONLY_TIMEOUT, move |rt| async move {
                phase_data_only(rt, root, "first data-only").await
            })
        },
        "data-only-again" => {
            let root = root.to_path_buf();
            run_on_runtime(DATA_ONLY_TIMEOUT, move |rt| async move {
                phase_data_only(rt, root, "second data-only").await
            })
        },
        other => Err(format!("unknown root-delete phase: {other}")),
    }
}

async fn phase_seed(rt: MultiTaskRuntime<()>, root: PathBuf) -> TestResult<()> {
    let fixture = build_database(
        &rt,
        &root,
        Duration::ZERO,
        Duration::ZERO,
    ).await?;
    create_tables(&fixture).await?;
    assert_table_definitions(&fixture, "seed").await?;

    let seed = writable_transaction(&fixture.db, "root-delete persisted seed")?;
    seed
        .upsert(
            baseline_entries()
                .into_iter()
                .map(ExpectedAction::into_table_kv)
                .collect(),
        )
        .await
        .map_err(|error| format!("writing root-delete seed failed: {error:?}"))?;
    commit_ordinary(&seed, "root-delete persisted seed").await?;
    assert_baseline_values(&fixture, "seed visible before confirmation").await?;

    wait_for_wal_state(
        &rt,
        &fixture,
        2,
        2,
        0,
        CONFIRM_TIMEOUT,
        "seed persistence",
    ).await?;
    assert_baseline_values(&fixture, "seed visible after confirmation").await?;
    expect_eq(
        "seed manager produced count",
        &fixture.tr_manager.produced_transaction_total(),
        &2usize,
    )?;
    expect_eq(
        "seed manager consumed count",
        &fixture.tr_manager.consumed_transaction_total(),
        &2usize,
    )?;
    expect_eq(
        "seed manager active roots",
        &fixture.tr_manager.transaction_len(),
        &0usize,
    )?;
    expect_eq(
        "seed Btree overlay drained",
        &fixture.db.table_cache_size(&Atom::from(BTREE_TABLE)).await,
        &Some(0u64),
    )?;
    if nonempty_bak_count(&root.join("root-wal"))? == 0 {
        return Err("seed confirmation produced no nonempty .bak checkpoint".to_owned());
    }
    let active = active_file_sizes(&root.join("root-wal"))?;
    if active.iter().any(|(_, len)| *len > 0) {
        return Err(format!(
            "seed confirmation left a nonempty active root WAL: {active:?}",
        ));
    }
    Ok(())
}

async fn phase_delete(rt: MultiTaskRuntime<()>, root: PathBuf) -> TestResult<()> {
    if !root.join(SEED_WAL_DIR).exists() {
        return Err("delete phase did not find archived confirmed seed WAL".to_owned());
    }
    let fixture = build_database(
        &rt,
        &root,
        Duration::ZERO,
        Duration::ZERO,
    ).await?;
    assert_table_definitions(&fixture, "delete").await?;
    assert_data_file_baseline(&fixture, "data-file baseline before delete").await?;
    expect_eq(
        "delete startup WAL append count",
        &fixture.logger.append_total_count(),
        &0usize,
    )?;
    expect_eq(
        "delete startup manager produced count",
        &fixture.tr_manager.produced_transaction_total(),
        &0usize,
    )?;

    verify_empty_and_missing_batches(&fixture).await?;
    commit_overlay_seed(&fixture).await?;
    execute_delete_plan(&fixture, &ordinary_plan()).await?;
    execute_delete_plan(&fixture, &dirty_plan()).await?;
    assert_final_state(&fixture, "delete committed view", true).await?;

    expect_eq(
        "delete phase WAL append count",
        &fixture.logger.append_total_count(),
        &3usize,
    )?;
    expect_eq(
        "delete phase WAL confirm count",
        &fixture.logger.confirm_total_count(),
        &0usize,
    )?;
    expect_eq(
        "delete phase WAL waiting count",
        &fixture.logger.waiting_confirm_count().await,
        &3usize,
    )?;
    expect_eq(
        "delete phase manager produced count",
        &fixture.tr_manager.produced_transaction_total(),
        &4usize,
    )?;
    expect_eq(
        "delete phase manager consumed count",
        &fixture.tr_manager.consumed_transaction_total(),
        &4usize,
    )?;
    expect_eq(
        "delete phase manager active roots",
        &fixture.tr_manager.transaction_len(),
        &0usize,
    )?;
    expect_eq(
        "delete phase nonempty .bak count",
        &nonempty_bak_count(&root.join("root-wal"))?,
        &0usize,
    )
}

async fn phase_recover(rt: MultiTaskRuntime<()>, root: PathBuf) -> TestResult<()> {
    let wal_path = root.join("root-wal");
    let bak_before = nonempty_bak_count(&wal_path)?;
    let fixture = build_database(
        &rt,
        &root,
        Duration::ZERO,
        Duration::ZERO,
    ).await?;
    assert_table_definitions(&fixture, "recovered").await?;
    assert_final_state(&fixture, "replayed before confirmation", true).await?;

    wait_for_wal_state(
        &rt,
        &fixture,
        3,
        3,
        0,
        CONFIRM_TIMEOUT,
        "delete recovery",
    ).await?;
    assert_final_state(&fixture, "replayed after confirmation", true).await?;
    expect_eq(
        "recovery manager produced count",
        &fixture.tr_manager.produced_transaction_total(),
        &3usize,
    )?;
    expect_eq(
        "recovery manager consumed count",
        &fixture.tr_manager.consumed_transaction_total(),
        &3usize,
    )?;
    expect_eq(
        "recovery manager active roots",
        &fixture.tr_manager.transaction_len(),
        &0usize,
    )?;
    expect_eq(
        "recovery Btree overlay drained",
        &fixture.db.table_cache_size(&Atom::from(BTREE_TABLE)).await,
        &Some(0u64),
    )?;

    let bak_after = nonempty_bak_count(&wal_path)?;
    if bak_after <= bak_before {
        return Err(format!(
            "delete recovery did not add a confirmed .bak checkpoint: before={bak_before}, after={bak_after}",
        ));
    }
    let active = active_file_sizes(&wal_path)?;
    if active.iter().any(|(_, len)| *len > 0) {
        return Err(format!(
            "nonempty active root WAL remains after delete recovery: {active:?}",
        ));
    }
    Ok(())
}

async fn phase_data_only(
    rt: MultiTaskRuntime<()>,
    root: PathBuf,
    label: &str,
) -> TestResult<()> {
    let archived = root.join(DELETE_WAL_DIR);
    if !archived.exists() || nonempty_bak_count(&archived)? == 0 {
        return Err(format!(
            "{label}: archived delete WAL has no nonempty confirmed .bak checkpoint",
        ));
    }
    if label == "second data-only" {
        let active = active_file_sizes(&root.join("root-wal"))?;
        if active.iter().any(|(_, len)| *len > 0) {
            return Err(format!(
                "{label}: first data-only start produced nonempty root WAL: {active:?}",
            ));
        }
    }

    let fixture = build_database(
        &rt,
        &root,
        Duration::ZERO,
        Duration::ZERO,
    ).await?;
    assert_table_definitions(&fixture, label).await?;
    assert_final_state(&fixture, label, false).await?;
    expect_eq(
        &format!("{label} WAL append count"),
        &fixture.logger.append_total_count(),
        &0usize,
    )?;
    expect_eq(
        &format!("{label} WAL confirm count"),
        &fixture.logger.confirm_total_count(),
        &0usize,
    )?;
    expect_eq(
        &format!("{label} WAL waiting count"),
        &fixture.logger.waiting_confirm_count().await,
        &0usize,
    )?;
    expect_eq(
        &format!("{label} manager produced count"),
        &fixture.tr_manager.produced_transaction_total(),
        &0usize,
    )?;
    expect_eq(
        &format!("{label} manager consumed count"),
        &fixture.tr_manager.consumed_transaction_total(),
        &0usize,
    )?;
    expect_eq(
        &format!("{label} Btree overlay"),
        &fixture.db.table_cache_size(&Atom::from(BTREE_TABLE)).await,
        &Some(0u64),
    )
}

async fn create_tables(fixture: &Fixture) -> TestResult<()> {
    let transaction = writable_transaction(&fixture.db, "root-delete table DDL")?;
    transaction
        .create_table(
            Atom::from(MEMORY_TABLE),
            table_meta(KVDBTableType::MemOrdTab, true),
            false,
        )
        .await
        .map_err(|error| format!("creating persistent Memory table failed: {error}"))?;
    transaction
        .create_table(
            Atom::from(VOLATILE_MEMORY_TABLE),
            table_meta(KVDBTableType::MemOrdTab, false),
            false,
        )
        .await
        .map_err(|error| format!("creating volatile Memory table failed: {error}"))?;
    transaction
        .create_table_with_options(
            Atom::from(LOG_ORDERED_TABLE),
            table_meta(KVDBTableType::LogOrdTab, true),
            CreateTableOptions::LogOrdTab(
                64 * 1024 * 1024,
                1024 * 1024,
                1024 * 1024,
            ),
            false,
        )
        .await
        .map_err(|error| format!("creating LogOrdered table failed: {error}"))?;
    transaction
        .create_table_with_options(
            Atom::from(BTREE_TABLE),
            table_meta(KVDBTableType::BtreeOrdTab, true),
            CreateTableOptions::BtreeOrdTab(4 * 1024 * 1024, false),
            false,
        )
        .await
        .map_err(|error| format!("creating Btree table failed: {error}"))?;
    commit_ordinary(&transaction, "root-delete table DDL").await
}

async fn verify_empty_and_missing_batches(fixture: &Fixture) -> TestResult<()> {
    let empty = writable_transaction(&fixture.db, "root-delete empty protocol proof")?;
    let ordinary = empty
        .delete(Vec::new())
        .await
        .map_err(|error| format!("empty ordinary delete failed: {error:?}"))?;
    let dirty = empty
        .dirty_delete(Vec::new())
        .await
        .map_err(|error| format!("empty dirty delete failed: {error:?}"))?;
    expect_eq("empty ordinary result", &ordinary.len(), &0usize)?;
    expect_eq("empty dirty result", &dirty.len(), &0usize)?;
    expect_eq("empty root child count", &empty.children_len(), &0usize)?;
    expect_eq(
        "empty root persistence",
        &empty.is_require_persistence(),
        &false,
    )?;
    drop(empty);

    let missing = writable_transaction(&fixture.db, "root-delete missing-only root")?;
    let result = missing
        .delete(vec![
            delete_item("missing-delete-first", key("missing-key-first"), None),
            delete_item(
                "missing-delete-middle",
                key("missing-key-middle"),
                Some(value(9_001)),
            ),
            delete_item("missing-delete-last", key("missing-key-last"), None),
        ])
        .await
        .map_err(|error| format!("missing-only delete failed: {error:?}"))?;
    assert_delete_results(
        "missing-only delete",
        &result,
        &[None, None, None],
    )?;
    expect_eq(
        "missing-only child count",
        &missing.children_len(),
        &0usize,
    )?;
    expect_eq(
        "missing-only persistence",
        &missing.is_require_persistence(),
        &false,
    )?;
    let prepare = missing
        .prepare_modified_conflicts()
        .await
        .map_err(|error| format!("preparing missing-only delete failed: {error:?}"))?;
    expect_eq("missing-only prepare bytes", &prepare.len(), &0usize)?;
    missing
        .commit_modified(prepare)
        .await
        .map_err(|error| format!("committing missing-only delete failed: {error:?}"))?;
    expect_eq(
        "missing-only committed status",
        &missing.get_status(),
        &Transaction2PcStatus::Commited,
    )?;
    expect_eq(
        "empty/missing WAL append count",
        &fixture.logger.append_total_count(),
        &0usize,
    )
}

async fn commit_overlay_seed(fixture: &Fixture) -> TestResult<()> {
    let transaction = writable_transaction(&fixture.db, "root-delete overlay seed")?;
    let btree = ExpectedAction::upsert(
        BTREE_TABLE,
        key("ordinary-btree-shared"),
        value(106),
    );
    let mut expected = vec![btree.clone()];
    expected.extend(
        baseline_entries()
            .into_iter()
            .filter(|action| action.table == MEMORY_TABLE),
    );
    transaction
        .upsert(
            expected
                .iter()
                .cloned()
                .map(ExpectedAction::into_table_kv)
                .collect(),
        )
        .await
        .map_err(|error| format!("writing root-delete overlay seed failed: {error:?}"))?;
    let children: Vec<RealTransaction> = transaction.to_children().collect();
    assert_child_order(
        &children,
        &[BTREE_TABLE, MEMORY_TABLE],
        "root-delete overlay seed",
    )?;
    let prepare = prepare_and_assert(
        &transaction,
        &children,
        &[BTREE_TABLE, MEMORY_TABLE],
        &expected,
        "root-delete overlay seed",
    ).await?;
    transaction
        .commit_modified(prepare)
        .await
        .map_err(|error| format!("committing root-delete overlay seed failed: {error:?}"))?;
    expect_binary(
        "shared Btree seed visible",
        query_ordinary(
            &fixture.db,
            BTREE_TABLE,
            btree.key,
            "shared Btree seed observer",
        ).await?.as_ref(),
        btree.value.as_ref(),
    )?;
    for action in expected
        .into_iter()
        .filter(|action| action.table == MEMORY_TABLE) {
        expect_binary(
            "Memory overlay seed visible",
            query_ordinary(
                &fixture.db,
                MEMORY_TABLE,
                action.key,
                "Memory overlay seed observer",
            ).await?.as_ref(),
            action.value.as_ref(),
        )?;
    }
    Ok(())
}

async fn execute_delete_plan(fixture: &Fixture, plan: &DeletePlan) -> TestResult<()> {
    let transaction = writable_transaction(
        &fixture.db,
        &format!("root-delete {} business root", plan.mode.label()),
    )?;
    expect_eq(
        &format!("{} initial status", plan.mode.label()),
        &transaction.get_status(),
        &Transaction2PcStatus::Start,
    )?;
    expect_eq(
        &format!("{} initial TID", plan.mode.label()),
        &transaction.get_transaction_uid(),
        &None,
    )?;

    plan.mode
        .upsert(&transaction, plan.pre_upserts.clone())
        .await?;
    let results = plan.mode
        .delete(&transaction, plan.delete_input.clone())
        .await?;
    assert_delete_results(
        &format!("{} delete results", plan.mode.label()),
        &results,
        &plan.expected_returns,
    )?;
    plan.mode
        .upsert(&transaction, plan.post_upserts.clone())
        .await?;

    expect_eq(
        &format!("{} root persistence", plan.mode.label()),
        &transaction.is_require_persistence(),
        &true,
    )?;
    expect_eq(
        &format!("{} child count", plan.mode.label()),
        &transaction.children_len(),
        &plan.child_order.len(),
    )?;
    let children: Vec<RealTransaction> = transaction.to_children().collect();
    assert_child_order(&children, &plan.child_order, plan.mode.label())?;

    let private = plan.mode
        .query(
            &transaction,
            plan.final_actions
                .iter()
                .cloned()
                .map(ExpectedAction::into_query)
                .collect(),
        )
        .await;
    assert_action_values(
        &format!("{} private final state", plan.mode.label()),
        &private,
        &plan.final_actions,
    )?;

    let produced_before = fixture.tr_manager.produced_transaction_total();
    let consumed_before = fixture.tr_manager.consumed_transaction_total();
    let append_before = fixture.logger.append_total_count();
    let prepare = prepare_and_assert(
        &transaction,
        &children,
        &plan.child_order,
        &plan.final_actions,
        plan.mode.label(),
    ).await?;
    transaction
        .commit_modified(prepare)
        .await
        .map_err(|error| {
            format!("committing {} business root failed: {error:?}", plan.mode.label())
        })?;
    expect_eq(
        &format!("{} committed status", plan.mode.label()),
        &transaction.get_status(),
        &Transaction2PcStatus::Commited,
    )?;
    expect_eq(
        &format!("{} manager produced increment", plan.mode.label()),
        &fixture.tr_manager.produced_transaction_total(),
        &(produced_before + 1),
    )?;
    expect_eq(
        &format!("{} manager consumed increment", plan.mode.label()),
        &fixture.tr_manager.consumed_transaction_total(),
        &(consumed_before + 1),
    )?;
    expect_eq(
        &format!("{} manager active roots", plan.mode.label()),
        &fixture.tr_manager.transaction_len(),
        &0usize,
    )?;
    expect_eq(
        &format!("{} WAL append increment", plan.mode.label()),
        &fixture.logger.append_total_count(),
        &(append_before + 1),
    )
}

async fn prepare_and_assert(
    transaction: &RealTransaction,
    children: &[RealTransaction],
    child_order: &[&str],
    expected_actions: &[ExpectedAction],
    label: &str,
) -> TestResult<Vec<u8>> {
    let prepare = transaction
        .prepare_modified_conflicts()
        .await
        .map_err(|error| format!("preparing {label} failed: {error:?}"))?;
    let transaction_uid = transaction
        .get_transaction_uid()
        .ok_or_else(|| format!("{label}: prepare did not allocate TID"))?;
    let commit_uid = transaction
        .get_commit_uid()
        .ok_or_else(|| format!("{label}: prepare did not allocate CID"))?;
    expect_eq(
        &format!("{label} prepared status"),
        &transaction.get_status(),
        &Transaction2PcStatus::Prepared,
    )?;
    assert_shared_child_identity(
        children,
        &transaction_uid,
        &commit_uid,
        label,
        Transaction2PcStatus::Prepared,
    )?;
    assert_prepare_output(
        &prepare,
        &transaction_uid,
        child_order,
        expected_actions,
        label,
    )?;
    Ok(prepare)
}

async fn verify_dirty_conflict_case(
    fixture: &Fixture,
    case: DirtyConflictCase,
) -> TestResult<()> {
    let target = key(&format!("dirty-delete-conflict-{}", case.table));
    let seed_value = value(10);
    let winner_value = value(20);

    let seed = writable_transaction(
        &fixture.db,
        &format!("{} dirty-delete seed", case.label),
    )?;
    seed
        .upsert(vec![TableKV::new(
            Atom::from(case.table),
            target.clone(),
            Some(seed_value.clone()),
        )])
        .await
        .map_err(|error| format!("{} seed upsert failed: {error:?}", case.label))?;
    commit_ordinary(&seed, &format!("{} seed", case.label)).await?;

    let produced_before = fixture.tr_manager.produced_transaction_total();
    let consumed_before = fixture.tr_manager.consumed_transaction_total();
    let append_before = fixture.logger.append_total_count();

    let stale = writable_transaction(
        &fixture.db,
        &format!("{} stale dirty deleter", case.label),
    )?;
    let stale_result = stale
        .dirty_delete(vec![TableKV::new(
            Atom::from(case.table),
            target.clone(),
            Some(value(30)),
        )])
        .await
        .map_err(|error| format!("{} stale dirty delete failed: {error:?}", case.label))?;
    let expected_stale = if case.table == BTREE_TABLE {
        vec![Some(seed_value)]
    } else {
        vec![None]
    };
    assert_delete_results(
        &format!("{} stale dirty return", case.label),
        &stale_result,
        &expected_stale,
    )?;

    let winner = writable_transaction(
        &fixture.db,
        &format!("{} concurrent winner", case.label),
    )?;
    winner
        .upsert(vec![TableKV::new(
            Atom::from(case.table),
            target.clone(),
            Some(winner_value.clone()),
        )])
        .await
        .map_err(|error| format!("{} winner upsert failed: {error:?}", case.label))?;
    commit_ordinary(&winner, &format!("{} winner", case.label)).await?;

    if case.must_conflict {
        let error = stale
            .prepare_modified_conflicts()
            .await
            .expect_err("stale dirty delete must return the frozen conflict result");
        assert_conflict(&error, case.table, &target, case.label)?;
        expect_eq(
            &format!("{} stale prepare-failed status", case.label),
            &stale.get_status(),
            &Transaction2PcStatus::PrepareFailed,
        )?;
        expect_eq(
            &format!("{} rejected root active before rollback", case.label),
            &fixture.tr_manager.transaction_len(),
            &1usize,
        )?;
        expect_eq(
            &format!("{} rejected root wrote no WAL", case.label),
            &fixture.logger.append_total_count(),
            &(append_before + usize::from(case.persistent)),
        )?;
        stale
            .rollback_modified()
            .await
            .map_err(|error| format!("{} stale rollback failed: {error:?}", case.label))?;
        expect_eq(
            &format!("{} stale rollback status", case.label),
            &stale.get_status(),
            &Transaction2PcStatus::Rollbacked,
        )?;
        expect_binary(
            &format!("{} winner after rollback", case.label),
            query_ordinary(
                &fixture.db,
                case.table,
                target.clone(),
                &format!("{} rollback observer", case.label),
            ).await?.as_ref(),
            Some(&winner_value),
        )?;

        let retry = writable_transaction(
            &fixture.db,
            &format!("{} post-rollback retry", case.label),
        )?;
        let retry_result = retry
            .delete(vec![TableKV::new(
                Atom::from(case.table),
                target.clone(),
                None,
            )])
            .await
            .map_err(|error| format!("{} retry delete failed: {error:?}", case.label))?;
        let expected_retry = if case.table == BTREE_TABLE {
            vec![Some(winner_value)]
        } else {
            vec![None]
        };
        assert_delete_results(
            &format!("{} retry delete return", case.label),
            &retry_result,
            &expected_retry,
        )?;
        commit_ordinary(&retry, &format!("{} retry", case.label)).await?;
        expect_eq(
            &format!("{} produced transaction count", case.label),
            &fixture.tr_manager.produced_transaction_total(),
            &(produced_before + 3),
        )?;
        expect_eq(
            &format!("{} consumed transaction count", case.label),
            &fixture.tr_manager.consumed_transaction_total(),
            &(consumed_before + 3),
        )?;
    } else {
        let prepare = stale
            .prepare_modified_conflicts()
            .await
            .map_err(|error| {
                format!("{} stale dirty prepare unexpectedly conflicted: {error:?}", case.label)
            })?;
        if case.persistent && prepare.len() <= ROOT_TID_BYTES {
            return Err(format!(
                "{} stale dirty delete omitted persistent WAL payload",
                case.label,
            ));
        }
        stale
            .commit_modified(prepare)
            .await
            .map_err(|error| format!("{} stale dirty commit failed: {error:?}", case.label))?;
        expect_eq(
            &format!("{} produced transaction count", case.label),
            &fixture.tr_manager.produced_transaction_total(),
            &(produced_before + 2),
        )?;
        expect_eq(
            &format!("{} consumed transaction count", case.label),
            &fixture.tr_manager.consumed_transaction_total(),
            &(consumed_before + 2),
        )?;
    }

    expect_binary(
        &format!("{} final deleted value", case.label),
        query_ordinary(
            &fixture.db,
            case.table,
            target,
            &format!("{} final observer", case.label),
        ).await?.as_ref(),
        None,
    )?;
    expect_eq(
        &format!("{} final WAL append count", case.label),
        &fixture.logger.append_total_count(),
        &(append_before + 2 * usize::from(case.persistent)),
    )?;
    expect_eq(
        &format!("{} final active roots", case.label),
        &fixture.tr_manager.transaction_len(),
        &0usize,
    )
}

async fn commit_ordinary(transaction: &RealTransaction, label: &str) -> TestResult<()> {
    let prepare = transaction
        .prepare_modified_conflicts()
        .await
        .map_err(|error| format!("preparing {label} failed: {error:?}"))?;
    transaction
        .commit_modified(prepare)
        .await
        .map_err(|error| format!("committing {label} failed: {error:?}"))
}

fn assert_conflict(
    error: &KVTableTrError,
    expected_table: &str,
    expected_key: &Binary,
    label: &str,
) -> TestResult<()> {
    if !matches!(error.level(), ErrorLevel::Normal) {
        return Err(format!(
            "{label}: conflict returned non-Normal level {:?}",
            error.level(),
        ));
    }
    if !error.is_conflicts() {
        return Err(format!("{label}: expected conflict error, observed {error:?}"));
    }
    let (table, key) = error
        .conflicts()
        .ok_or_else(|| format!("{label}: conflict error omitted table/key"))?;
    expect_eq(
        &format!("{label} conflict table"),
        &table.as_str(),
        &expected_table,
    )?;
    if key.as_ref() != expected_key.as_ref() {
        return Err(format!(
            "{label}: conflict key mismatch, expected_len={}, observed_len={}",
            expected_key.len(),
            key.len(),
        ));
    }
    Ok(())
}

fn assert_child_order(
    children: &[RealTransaction],
    expected: &[&str],
    label: &str,
) -> TestResult<()> {
    let observed = children
        .iter()
        .map(child_table_name)
        .collect::<Vec<_>>();
    expect_eq(
        &format!("{label} first-touch child order"),
        &observed,
        &expected.to_vec(),
    )?;
    for (index, child) in children.iter().enumerate() {
        expect_eq(
            &format!("{label} child {index} is unit"),
            &child.is_unit(),
            &true,
        )?;
        expect_eq(
            &format!("{label} child {index} is not tree"),
            &child.is_tree(),
            &false,
        )?;
    }
    Ok(())
}

fn child_table_name(child: &RealTransaction) -> &'static str {
    match child {
        KVDBTransaction::MemOrdTabTr(_) => MEMORY_TABLE,
        KVDBTransaction::LogOrdTabTr(_) => LOG_ORDERED_TABLE,
        KVDBTransaction::BtreeOrdTabTr(_) => BTREE_TABLE,
        KVDBTransaction::MetaTabTr(_) => ".tables_meta",
        KVDBTransaction::LogWTabTr(_) => "LogWrite",
        KVDBTransaction::RootTr(_) => "Root",
    }
}

fn assert_shared_child_identity(
    children: &[RealTransaction],
    transaction_uid: &pi_guid::Guid,
    commit_uid: &pi_guid::Guid,
    label: &str,
    status: Transaction2PcStatus,
) -> TestResult<()> {
    for (index, child) in children.iter().enumerate() {
        expect_eq(
            &format!("{label} child {index} TID"),
            &child.get_transaction_uid(),
            &Some(transaction_uid.clone()),
        )?;
        expect_eq(
            &format!("{label} child {index} CID"),
            &child.get_commit_uid(),
            &Some(commit_uid.clone()),
        )?;
        expect_eq(
            &format!("{label} child {index} status"),
            &child.get_status(),
            &status,
        )?;
    }
    Ok(())
}

fn assert_prepare_output(
    output: &Vec<u8>,
    transaction_uid: &pi_guid::Guid,
    expected_order: &[&str],
    expected_actions: &[ExpectedAction],
    label: &str,
) -> TestResult<()> {
    if output.len() <= ROOT_TID_BYTES {
        return Err(format!(
            "{label}: prepare output contains no table action, len={}",
            output.len(),
        ));
    }
    expect_eq(
        &format!("{label} WAL TID bytes"),
        &&output[..ROOT_TID_BYTES],
        &&transaction_uid.0.to_le_bytes()[..],
    )?;

    let mut offset = ROOT_TID_BYTES;
    let mut segments = Vec::new();
    while offset < output.len() {
        let (table, count, actions_offset) =
            <CodecTable as KVTable>::get_init_table_prepare_output(output, offset);
        let (actions, next_offset) =
            <CodecTable as KVTable>::get_all_key_value_from_table_prepare_output(
                output,
                &table,
                count,
                actions_offset,
            );
        if next_offset <= offset || next_offset > output.len() {
            return Err(format!(
                "{label}: invalid decoded WAL offset {next_offset} from {offset}",
            ));
        }
        segments.push((table, actions));
        offset = next_offset;
    }
    expect_eq(
        &format!("{label} final WAL offset"),
        &offset,
        &output.len(),
    )?;
    let observed_order = segments
        .iter()
        .map(|(table, _)| table.as_str())
        .collect::<Vec<_>>();
    expect_eq(
        &format!("{label} WAL table order"),
        &observed_order,
        &expected_order.to_vec(),
    )?;
    let observed_total = segments
        .iter()
        .map(|(_, actions)| actions.len())
        .sum::<usize>();
    expect_eq(
        &format!("{label} WAL final action count"),
        &observed_total,
        &expected_actions.len(),
    )?;

    for (table, actions) in segments {
        let expected = expected_actions
            .iter()
            .filter(|action| action.table == table.as_str())
            .collect::<Vec<_>>();
        expect_eq(
            &format!("{label} {} action count", table.as_str()),
            &actions.len(),
            &expected.len(),
        )?;
        for expected_action in expected {
            let matches = actions
                .iter()
                .filter(|action| {
                    action.key.as_ref() == expected_action.key.as_ref()
                        && action.value.as_ref().map(AsRef::<[u8]>::as_ref)
                            == expected_action.value.as_ref().map(AsRef::<[u8]>::as_ref)
                })
                .count();
            expect_eq(
                &format!(
                    "{label} exact final action {} key_len={}",
                    table.as_str(),
                    expected_action.key.len(),
                ),
                &matches,
                &1usize,
            )?;
        }
    }
    Ok(())
}

async fn assert_table_definitions(fixture: &Fixture, label: &str) -> TestResult<()> {
    expect_eq(
        &format!("{label} registered table count"),
        &fixture.db.table_size().await,
        &5usize,
    )?;
    let verifier = read_only_transaction(
        &fixture.db,
        &format!("{label} table definition observer"),
    )?;
    for (name, table_type, persistence) in [
        (MEMORY_TABLE, KVDBTableType::MemOrdTab, true),
        (VOLATILE_MEMORY_TABLE, KVDBTableType::MemOrdTab, false),
        (LOG_ORDERED_TABLE, KVDBTableType::LogOrdTab, true),
        (BTREE_TABLE, KVDBTableType::BtreeOrdTab, true),
    ] {
        expect_eq(
            &format!("{label} table definition for {name}"),
            &verifier.table_meta(Atom::from(name)).await,
            &Some(table_meta(table_type, persistence)),
        )?;
    }
    Ok(())
}

async fn assert_baseline_values(fixture: &Fixture, label: &str) -> TestResult<()> {
    let expected = baseline_entries();
    let transaction = read_only_transaction(
        &fixture.db,
        &format!("{label} baseline observer"),
    )?;
    let observed = transaction
        .query(
            expected
                .iter()
                .cloned()
                .map(ExpectedAction::into_query)
                .collect(),
        )
        .await;
    assert_action_values(label, &observed, &expected)
}

async fn assert_data_file_baseline(fixture: &Fixture, label: &str) -> TestResult<()> {
    for action in baseline_entries() {
        let actual = query_ordinary(
            &fixture.db,
            action.table,
            action.key,
            &format!("{label} observer for {}", action.table),
        ).await?;
        let expected = if action.table == MEMORY_TABLE {
            None
        } else {
            action.value.as_ref()
        };
        expect_binary(
            &format!("{label} value in {}", action.table),
            actual.as_ref(),
            expected,
        )?;
    }
    Ok(())
}

async fn assert_final_state(
    fixture: &Fixture,
    label: &str,
    memory_is_live: bool,
) -> TestResult<()> {
    for (table, target) in deleted_keys() {
        let actual = query_ordinary(
            &fixture.db,
            table,
            target,
            &format!("{label} deleted-key observer for {table}"),
        ).await?;
        expect_binary(
            &format!("{label} deleted key in {table}"),
            actual.as_ref(),
            None,
        )?;
    }
    for action in restored_entries() {
        let actual = query_ordinary(
            &fixture.db,
            action.table,
            action.key.clone(),
            &format!("{label} restored-key observer for {}", action.table),
        ).await?;
        expect_binary(
            &format!("{label} restored key in {}", action.table),
            actual.as_ref(),
            action.value.as_ref(),
        )?;
    }
    for action in control_entries() {
        let actual = query_ordinary(
            &fixture.db,
            action.table,
            action.key.clone(),
            &format!("{label} control observer for {}", action.table),
        ).await?;
        let expected = if action.table == MEMORY_TABLE && !memory_is_live {
            None
        } else {
            action.value.as_ref()
        };
        expect_binary(
            &format!("{label} control value in {}", action.table),
            actual.as_ref(),
            expected,
        )?;
    }
    if !memory_is_live {
        for action in baseline_entries()
            .into_iter()
            .filter(|action| action.table == MEMORY_TABLE) {
            let actual = query_ordinary(
                &fixture.db,
                MEMORY_TABLE,
                action.key,
                &format!("{label} Memory data-only observer"),
            ).await?;
            expect_binary(
                &format!("{label} Memory has no independent data file"),
                actual.as_ref(),
                None,
            )?;
        }
    }
    Ok(())
}

fn assert_action_values(
    label: &str,
    actual: &[Option<Binary>],
    expected: &[ExpectedAction],
) -> TestResult<()> {
    expect_eq(
        &format!("{label} result count"),
        &actual.len(),
        &expected.len(),
    )?;
    for (index, (actual, expected)) in actual.iter().zip(expected).enumerate() {
        if actual.as_ref().map(AsRef::<[u8]>::as_ref)
            != expected.value.as_ref().map(AsRef::<[u8]>::as_ref) {
            return Err(format!(
                "{label}: value mismatch at {index}, table={}, key_len={}, expected_value_len={:?}, observed_value_len={:?}",
                expected.table,
                expected.key.len(),
                expected.value.as_ref().map(Binary::len),
                actual.as_ref().map(Binary::len),
            ));
        }
    }
    Ok(())
}

fn assert_delete_results(
    label: &str,
    actual: &[Option<Binary>],
    expected: &[Option<Binary>],
) -> TestResult<()> {
    expect_eq(
        &format!("{label} result count"),
        &actual.len(),
        &expected.len(),
    )?;
    for (index, (actual, expected)) in actual.iter().zip(expected).enumerate() {
        if actual.as_ref().map(AsRef::<[u8]>::as_ref)
            != expected.as_ref().map(AsRef::<[u8]>::as_ref) {
            return Err(format!(
                "{label}: return mismatch at {index}, expected_len={:?}, observed_len={:?}",
                expected.as_ref().map(Binary::len),
                actual.as_ref().map(Binary::len),
            ));
        }
    }
    Ok(())
}

async fn wait_for_wal_state(
    rt: &MultiTaskRuntime<()>,
    fixture: &Fixture,
    expected_appended: usize,
    expected_confirmed: usize,
    expected_waiting: usize,
    timeout: Duration,
    label: &str,
) -> TestResult<()> {
    let deadline = Instant::now() + timeout;
    loop {
        let appended = fixture.logger.append_total_count();
        let confirmed = fixture.logger.confirm_total_count();
        let waiting = fixture.logger.waiting_confirm_count().await;
        if appended == expected_appended
            && confirmed == expected_confirmed
            && waiting == expected_waiting {
            return Ok(());
        }
        if appended > expected_appended || confirmed > expected_confirmed {
            return Err(format!(
                "{label}: WAL advanced beyond expected state, appended={appended}, confirmed={confirmed}, waiting={waiting}",
            ));
        }
        if Instant::now() >= deadline {
            return Err(format!(
                "{label}: WAL did not reach appended={expected_appended}, confirmed={expected_confirmed}, waiting={expected_waiting} before {timeout:?}; observed appended={appended}, confirmed={confirmed}, waiting={waiting}",
            ));
        }
        rt.timeout(25).await;
    }
}

fn archive_root_wal(root: &Path, archive_name: &str) -> TestResult<()> {
    let wal_path = root.join("root-wal");
    let archived = root.join(archive_name);
    if archived.exists() {
        return Err(format!("archived root WAL unexpectedly exists: {archived:?}"));
    }
    if nonempty_bak_count(&wal_path)? == 0 {
        return Err(format!(
            "root WAL cannot be archived to {archive_name} before a nonempty .bak exists",
        ));
    }
    fs::rename(&wal_path, &archived)
        .map_err(|error| format!("archiving root WAL to {archive_name} failed: {error}"))
}

fn regular_file_sizes(path: &Path) -> TestResult<Vec<(PathBuf, u64)>> {
    let mut files = Vec::new();
    for entry in fs::read_dir(path)
        .map_err(|error| format!("reading WAL directory {path:?} failed: {error}"))? {
        let entry = entry
            .map_err(|error| format!("reading WAL entry in {path:?} failed: {error}"))?;
        let metadata = entry
            .metadata()
            .map_err(|error| format!("reading metadata for {:?} failed: {error}", entry.path()))?;
        if metadata.is_file() {
            files.push((entry.path(), metadata.len()));
        }
    }
    files.sort_by(|left, right| left.0.cmp(&right.0));
    Ok(files)
}

fn nonempty_bak_count(path: &Path) -> TestResult<usize> {
    Ok(regular_file_sizes(path)?
        .into_iter()
        .filter(|(file, len)| {
            *len > 0
                && file.extension().and_then(|extension| extension.to_str()) == Some("bak")
        })
        .count())
}

fn active_file_sizes(path: &Path) -> TestResult<Vec<(PathBuf, u64)>> {
    Ok(regular_file_sizes(path)?
        .into_iter()
        .filter(|(file, _)| {
            file.extension().and_then(|extension| extension.to_str()) != Some("bak")
        })
        .collect())
}

fn run_phase_process(root: &Path, phase: &str, timeout: Duration) -> TestResult<()> {
    let executable = env::current_exe()
        .map_err(|error| format!("locating root-delete test executable failed: {error}"))?;
    let mut child = Command::new(executable)
        .arg("--exact")
        .arg(TEST_NAME)
        .arg("--nocapture")
        .arg("--test-threads=1")
        .env(PHASE_ENV, phase)
        .env(ROOT_ENV, root)
        .spawn()
        .map_err(|error| format!("spawning root-delete phase {phase} failed: {error}"))?;
    let status = wait_for_child(&mut child, timeout)?;
    if !status.success() {
        return Err(format!(
            "root-delete phase {phase} exited unsuccessfully: {status}",
        ));
    }
    Ok(())
}

fn wait_for_child(child: &mut Child, timeout: Duration) -> TestResult<ExitStatus> {
    let deadline = Instant::now() + timeout;
    loop {
        if let Some(status) = child
            .try_wait()
            .map_err(|error| format!("polling root-delete child failed: {error}"))? {
            return Ok(status);
        }
        if Instant::now() >= deadline {
            let _ = child.kill();
            let _ = child.wait();
            return Err(format!("root-delete child exceeded {timeout:?}"));
        }
        thread::sleep(Duration::from_millis(25));
    }
}

#[derive(Clone, Copy)]
enum ActionMode {
    Ordinary,
    Dirty,
}

impl ActionMode {
    const fn label(self) -> &'static str {
        match self {
            Self::Ordinary => "ordinary",
            Self::Dirty => "dirty",
        }
    }

    async fn upsert(
        self,
        transaction: &RealTransaction,
        input: Vec<TableKV>,
    ) -> TestResult<()> {
        match self {
            Self::Ordinary => transaction.upsert(input).await,
            Self::Dirty => transaction.dirty_upsert(input).await,
        }
        .map_err(|error| format!("{} root upsert failed: {error:?}", self.label()))
    }

    async fn delete(
        self,
        transaction: &RealTransaction,
        input: Vec<TableKV>,
    ) -> TestResult<Vec<Option<Binary>>> {
        match self {
            Self::Ordinary => transaction.delete(input).await,
            Self::Dirty => transaction.dirty_delete(input).await,
        }
        .map_err(|error| format!("{} root delete failed: {error:?}", self.label()))
    }

    async fn query(
        self,
        transaction: &RealTransaction,
        input: Vec<TableKV>,
    ) -> Vec<Option<Binary>> {
        match self {
            Self::Ordinary => transaction.query(input).await,
            Self::Dirty => transaction.dirty_query(input).await,
        }
    }
}

struct DeletePlan {
    mode: ActionMode,
    pre_upserts: Vec<TableKV>,
    delete_input: Vec<TableKV>,
    expected_returns: Vec<Option<Binary>>,
    post_upserts: Vec<TableKV>,
    child_order: Vec<&'static str>,
    final_actions: Vec<ExpectedAction>,
}

#[derive(Clone)]
struct ExpectedAction {
    table: &'static str,
    key: Binary,
    value: Option<Binary>,
}

impl ExpectedAction {
    fn upsert(table: &'static str, key: Binary, value: Binary) -> Self {
        Self {
            table,
            key,
            value: Some(value),
        }
    }

    fn delete(table: &'static str, key: Binary) -> Self {
        Self {
            table,
            key,
            value: None,
        }
    }

    fn into_table_kv(self) -> TableKV {
        TableKV::new(Atom::from(self.table), self.key, self.value)
    }

    fn into_query(self) -> TableKV {
        TableKV::new(Atom::from(self.table), self.key, None)
    }
}

fn ordinary_plan() -> DeletePlan {
    let btree_max = maximum_key();
    let memory_key = key("ordinary-memory");
    let memory_repeat = key("ordinary-memory-repeat");
    let log_key = key("ordinary-log");
    let log_restore = key("ordinary-log-restore");
    let btree_shared = key("ordinary-btree-shared");
    let btree_private = key("ordinary-btree-private");
    let private_value = value(107);

    DeletePlan {
        mode: ActionMode::Ordinary,
        pre_upserts: vec![TableKV::new(
            Atom::from(BTREE_TABLE),
            btree_private.clone(),
            Some(private_value.clone()),
        )],
        delete_input: vec![
            delete_item("missing-ordinary-first", key("missing-ordinary-first"), None),
            TableKV::new(Atom::from(BTREE_TABLE), btree_max.clone(), None),
            TableKV::new(Atom::from(MEMORY_TABLE), memory_key.clone(), None),
            delete_item("missing-ordinary-middle", key("missing-ordinary-middle"), None),
            TableKV::new(
                Atom::from(LOG_ORDERED_TABLE),
                log_key.clone(),
                Some(value(9_101)),
            ),
            TableKV::new(Atom::from(BTREE_TABLE), btree_shared.clone(), None),
            TableKV::new(Atom::from(BTREE_TABLE), btree_private.clone(), None),
            TableKV::new(Atom::from(BTREE_TABLE), btree_max.clone(), None),
            TableKV::new(Atom::from(MEMORY_TABLE), memory_repeat.clone(), None),
            TableKV::new(Atom::from(MEMORY_TABLE), memory_repeat.clone(), None),
            TableKV::new(Atom::from(LOG_ORDERED_TABLE), log_restore.clone(), None),
            delete_item("missing-ordinary-last", key("missing-ordinary-last"), None),
        ],
        expected_returns: vec![
            None,
            Some(value(105)),
            None,
            None,
            None,
            Some(value(106)),
            Some(private_value),
            None,
            None,
            None,
            None,
            None,
        ],
        post_upserts: vec![TableKV::new(
            Atom::from(LOG_ORDERED_TABLE),
            log_restore.clone(),
            Some(value(1_004)),
        )],
        child_order: vec![BTREE_TABLE, MEMORY_TABLE, LOG_ORDERED_TABLE],
        final_actions: vec![
            ExpectedAction::delete(BTREE_TABLE, btree_max),
            ExpectedAction::delete(BTREE_TABLE, btree_shared),
            ExpectedAction::delete(BTREE_TABLE, btree_private),
            ExpectedAction::delete(MEMORY_TABLE, memory_key),
            ExpectedAction::delete(MEMORY_TABLE, memory_repeat),
            ExpectedAction::delete(LOG_ORDERED_TABLE, log_key),
            ExpectedAction::upsert(LOG_ORDERED_TABLE, log_restore, value(1_004)),
        ],
    }
}

fn dirty_plan() -> DeletePlan {
    let memory_key = key("dirty-memory");
    let memory_private = key("dirty-memory-private");
    let log_key = key("dirty-log");
    let log_restore = key("dirty-log-restore");
    let btree_key = key("dirty-btree");

    DeletePlan {
        mode: ActionMode::Dirty,
        pre_upserts: vec![TableKV::new(
            Atom::from(MEMORY_TABLE),
            memory_private.clone(),
            Some(value(207)),
        )],
        delete_input: vec![
            delete_item("missing-dirty-first", key("missing-dirty-first"), None),
            TableKV::new(Atom::from(LOG_ORDERED_TABLE), log_key.clone(), None),
            TableKV::new(Atom::from(MEMORY_TABLE), memory_key.clone(), None),
            TableKV::new(Atom::from(BTREE_TABLE), btree_key.clone(), None),
            delete_item("missing-dirty-middle", key("missing-dirty-middle"), None),
            TableKV::new(Atom::from(MEMORY_TABLE), memory_private.clone(), None),
            TableKV::new(Atom::from(BTREE_TABLE), btree_key.clone(), None),
            TableKV::new(
                Atom::from(LOG_ORDERED_TABLE),
                log_restore.clone(),
                Some(value(9_201)),
            ),
            delete_item("missing-dirty-last", key("missing-dirty-last"), None),
        ],
        expected_returns: vec![
            None,
            None,
            None,
            Some(value(204)),
            None,
            None,
            None,
            None,
            None,
        ],
        post_upserts: vec![TableKV::new(
            Atom::from(LOG_ORDERED_TABLE),
            log_restore.clone(),
            Some(value(1_203)),
        )],
        child_order: vec![MEMORY_TABLE, LOG_ORDERED_TABLE, BTREE_TABLE],
        final_actions: vec![
            ExpectedAction::delete(MEMORY_TABLE, memory_key),
            ExpectedAction::delete(MEMORY_TABLE, memory_private),
            ExpectedAction::delete(LOG_ORDERED_TABLE, log_key),
            ExpectedAction::upsert(LOG_ORDERED_TABLE, log_restore, value(1_203)),
            ExpectedAction::delete(BTREE_TABLE, btree_key),
        ],
    }
}

fn baseline_entries() -> Vec<ExpectedAction> {
    vec![
        ExpectedAction::upsert(MEMORY_TABLE, key("ordinary-memory"), value(101)),
        ExpectedAction::upsert(MEMORY_TABLE, key("ordinary-memory-repeat"), value(102)),
        ExpectedAction::upsert(MEMORY_TABLE, key("dirty-memory"), value(201)),
        ExpectedAction::upsert(MEMORY_TABLE, key("memory-control"), value(190)),
        ExpectedAction::upsert(LOG_ORDERED_TABLE, key("ordinary-log"), value(103)),
        ExpectedAction::upsert(
            LOG_ORDERED_TABLE,
            key("ordinary-log-restore"),
            value(104),
        ),
        ExpectedAction::upsert(LOG_ORDERED_TABLE, key("dirty-log"), value(202)),
        ExpectedAction::upsert(
            LOG_ORDERED_TABLE,
            key("dirty-log-restore"),
            value(203),
        ),
        ExpectedAction::upsert(LOG_ORDERED_TABLE, key("log-control"), value(290)),
        ExpectedAction::upsert(BTREE_TABLE, maximum_key(), value(105)),
        ExpectedAction::upsert(BTREE_TABLE, key("dirty-btree"), value(204)),
        ExpectedAction::upsert(BTREE_TABLE, key("btree-control"), value(390)),
    ]
}

fn restored_entries() -> Vec<ExpectedAction> {
    vec![
        ExpectedAction::upsert(
            LOG_ORDERED_TABLE,
            key("ordinary-log-restore"),
            value(1_004),
        ),
        ExpectedAction::upsert(
            LOG_ORDERED_TABLE,
            key("dirty-log-restore"),
            value(1_203),
        ),
    ]
}

fn control_entries() -> Vec<ExpectedAction> {
    vec![
        ExpectedAction::upsert(MEMORY_TABLE, key("memory-control"), value(190)),
        ExpectedAction::upsert(LOG_ORDERED_TABLE, key("log-control"), value(290)),
        ExpectedAction::upsert(BTREE_TABLE, key("btree-control"), value(390)),
    ]
}

fn deleted_keys() -> Vec<(&'static str, Binary)> {
    vec![
        (MEMORY_TABLE, key("ordinary-memory")),
        (MEMORY_TABLE, key("ordinary-memory-repeat")),
        (MEMORY_TABLE, key("dirty-memory")),
        (MEMORY_TABLE, key("dirty-memory-private")),
        (LOG_ORDERED_TABLE, key("ordinary-log")),
        (LOG_ORDERED_TABLE, key("dirty-log")),
        (BTREE_TABLE, maximum_key()),
        (BTREE_TABLE, key("ordinary-btree-shared")),
        (BTREE_TABLE, key("ordinary-btree-private")),
        (BTREE_TABLE, key("dirty-btree")),
    ]
}

#[derive(Clone, Copy)]
struct DirtyConflictCase {
    label: &'static str,
    table: &'static str,
    must_conflict: bool,
    persistent: bool,
}

impl DirtyConflictCase {
    const fn new(
        label: &'static str,
        table: &'static str,
        must_conflict: bool,
        persistent: bool,
    ) -> Self {
        Self {
            label,
            table,
            must_conflict,
            persistent,
        }
    }
}

fn delete_item(table: &str, key: Binary, value: Option<Binary>) -> TableKV {
    TableKV::new(Atom::from(table), key, value)
}

fn table_meta(table_type: KVDBTableType, persistence: bool) -> KVTableMeta {
    KVTableMeta::new(table_type, persistence, EnumType::Bin, EnumType::Bin)
}

fn maximum_key() -> Binary {
    let key = encode_bin(&vec![0xA5; u16::MAX as usize - 3]);
    assert_eq!(key.len(), u16::MAX as usize);
    key
}

fn key(name: &str) -> Binary {
    encode_bin(name.as_bytes())
}

fn value(number: u64) -> Binary {
    encode_bin(&number.to_le_bytes())
}

fn encode_bin(bytes: &[u8]) -> Binary {
    let mut buffer = WriteBuffer::new();
    buffer.write_bin(bytes, 0..bytes.len());
    Binary::new(buffer.bytes)
}

fn unique_temp_root(label: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time must not precede UNIX_EPOCH")
        .as_nanos();
    env::temp_dir().join(format!(
        "pi_db_root_delete_{label}_{}_{}",
        std::process::id(),
        nanos,
    ))
}

struct TempRoot {
    path: PathBuf,
}

impl TempRoot {
    fn new(label: &str) -> TestResult<Self> {
        let path = unique_temp_root(label);
        fs::create_dir_all(&path)
            .map_err(|error| format!("creating temporary root {path:?} failed: {error}"))?;
        Ok(Self { path })
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TempRoot {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}