linkrs-storage 0.1.0

Storage engine with CSR and memory-mapped containers
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
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
use crate::cursor::{IndexCursor, IndexPredicate, IndexRow, IndexScanPlan, PartitionSelector};
use crate::index::key_codec::KeyBuilder;
use crate::index::manifest::{GenerationBuildState, GenerationState, IndexManifest, IndexShard};
use crate::index::types::{EdgeIdentity, IndexIdentity};
use crate::index::*;
use crate::persistence::write_versioned_payload;
use linkrs_core::types::{
    CommitLsn, Index, IndexConfig, IndexField, IndexGeneration, IndexType, SnapshotTimestamp,
    MAX_TIMESTAMP,
};
use linkrs_core::Value;
use std::collections::BTreeMap;

fn write_crashed_build_state(index_root: &std::path::Path, state: &GenerationBuildState) {
    let serialized = postcard::to_allocvec(state).expect("serialize");
    let mut wrapped = Vec::new();
    write_versioned_payload(&mut wrapped, &serialized);
    std::fs::create_dir_all(index_root).unwrap();
    std::fs::write(index_root.join("generation_build.bin"), &wrapped).unwrap();
}

fn create_tag_index(name: &str, schema_name: &str) -> Index {
    Index::new(IndexConfig {
        id: 1,
        name: name.to_string(),
        space_id: 1,
        schema_name: schema_name.to_string(),
        fields: vec![IndexField::new("name".into(), Value::string(""), false)],
        properties: vec![],
        index_type: IndexType::TagIndex,
        is_unique: false,
        covering: false,
        partial_condition: None,
    })
}

fn create_edge_index_with_included_properties() -> Index {
    Index::new(IndexConfig {
        id: 1,
        name: "knows_weight_idx".into(),
        space_id: 1,
        schema_name: "Person".into(),
        fields: vec![IndexField::new("weight".into(), Value::Int(0), false)],
        properties: vec!["since".into()],
        index_type: IndexType::EdgeIndex,
        is_unique: false,
        covering: true,
        partial_condition: None,
    })
}

/// Register + update + lookup vertex index.
#[test]
fn test_update_and_lookup_vertex_index() {
    let manager = IndexDataManagerImpl::new();

    let space_id = 1u64;
    let vertex_id = Value::Int(1);
    let index_name = "idx_person_name";
    let props = vec![("name".into(), Value::string("Alice"))];
    let index = create_tag_index(index_name, "person");

    manager
        .register_native_index(space_id, &index)
        .expect("register native index");
    manager
        .update_vertex_indexes_mvcc(space_id, &vertex_id, index_name, &props, MAX_TIMESTAMP)
        .expect("Failed to update vertex indexes");
    let results = manager
        .lookup_tag_index(space_id, &index, &Value::string("Alice"))
        .expect("Failed to lookup tag index");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0], vertex_id);
}

/// Same index id in different spaces uses isolated runtimes.
#[test]
fn same_index_id_in_different_spaces_uses_isolated_runtimes() {
    let manager = IndexDataManagerImpl::new();
    let first = create_tag_index("person_name", "person");
    let mut second = create_tag_index("person_name", "person");
    second.space_id = 2;

    manager
        .register_native_index(1, &first)
        .expect("register first space index");
    manager
        .register_native_index(2, &second)
        .expect("register second space index");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "person_name",
            &[("name".into(), Value::string("Alice"))],
            MAX_TIMESTAMP,
        )
        .expect("write first space index");
    manager
        .update_vertex_indexes_mvcc(
            2,
            &Value::Int(2),
            "person_name",
            &[("name".into(), Value::string("Bob"))],
            MAX_TIMESTAMP,
        )
        .expect("write second space index");

    assert_eq!(
        manager
            .lookup_tag_index(1, &first, &Value::string("Alice"))
            .expect("lookup first space"),
        vec![Value::Int(1)]
    );
    assert_eq!(
        manager
            .lookup_tag_index(2, &second, &Value::string("Bob"))
            .expect("lookup second space"),
        vec![Value::Int(2)]
    );
    assert!(manager.manifest_catalog(1, 1).is_some());
    assert!(manager.manifest_catalog(2, 1).is_some());
}

/// Split places index entries into the correct shards.
#[test]
fn split_writes_only_the_selected_index_to_each_shard() {
    let directory = tempfile::tempdir().expect("create temporary index directory");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let first_index = create_tag_index("first", "person");
    let mut second_index = create_tag_index("second", "person");
    second_index.id = 2;
    manager
        .register_native_index(1, &first_index)
        .expect("register first index");
    manager
        .register_native_index(1, &second_index)
        .expect("register second index");

    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "first",
            &[("name".into(), Value::string("Alice"))],
            1,
        )
        .expect("write first lower key");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(2),
            "first",
            &[("name".into(), Value::string("Zoe"))],
            1,
        )
        .expect("write first upper key");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(3),
            "second",
            &[("name".into(), Value::string("Other"))],
            1,
        )
        .expect("write unrelated index key");

    let boundary = KeyBuilder::build_vertex_index_value_prefix(1, "first", &Value::string("M"))
        .expect("build split boundary")
        .0;
    manager
        .split_native_index(
            IndexIdentity {
                space_id: 1,
                index_id: 1,
            },
            boundary,
            SnapshotTimestamp::new(1),
            CommitLsn::new(1),
            || Ok(CommitLsn::new(100)),
            |_, _| Ok(Vec::new()),
        )
        .expect("split first index");

    let catalog = manager.manifest_catalog(1, 1).expect("catalog exists");
    let manifest = catalog.acquire();
    assert_eq!(manifest.manifest().shards.len(), 2);
    let first_prefix = KeyBuilder::build_vertex_index_prefix(1, "first").0;
    let second_prefix = KeyBuilder::build_vertex_index_prefix(1, "second").0;
    let mut shard_entries = 0;
    for shard in &manifest.manifest().shards {
        let shard_runtime =
            crate::index::shard_runtime::shard::ShardRuntime::load_with_pool_capacity(
                shard.checkpoint_file.clone(),
                64 * 1024 * 1024,
            )
            .expect("load split shard");
        let forward = shard_runtime.read_forward().snapshot();
        shard_entries += forward.len();
        assert!(forward.keys().all(|key| key.starts_with(&first_prefix)));
        assert!(forward.keys().all(|key| !key.starts_with(&second_prefix)));
    }
    assert_eq!(shard_entries, 2);
}

// --- Split crash recovery tests ---

#[test]
fn resolve_split_crash_recovery_discards_building_state() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let index = create_tag_index("first", "person");
    manager
        .register_native_index(1, &index)
        .expect("register index");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "first",
            &[("name".into(), Value::string("Alice"))],
            1,
        )
        .expect("write");

    let index_root = directory.path().join("indexes").join("1").join("1");
    let build_state_path = index_root.join("generation_build.bin");

    let crashed = GenerationBuildState {
        generation: IndexGeneration::new(2),
        snapshot_timestamp: SnapshotTimestamp::new(1),
        start_lsn: CommitLsn::new(10),
        barrier_lsn: None,
        state: GenerationState::Building,
    };
    write_crashed_build_state(&index_root, &crashed);

    manager
        .resolve_split_crash_recovery(&index_root)
        .expect("recovery should succeed");

    assert!(
        !build_state_path.exists(),
        "Building state must be discarded on recovery"
    );

    let results = manager
        .lookup_tag_index(1, &index, &Value::string("Alice"))
        .expect("lookup after recovery");
    assert_eq!(results, vec![Value::Int(1)]);
}

#[test]
fn resolve_split_crash_recovery_discards_catching_up_state() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let index = create_tag_index("first", "person");
    manager
        .register_native_index(1, &index)
        .expect("register index");

    let index_root = directory.path().join("indexes").join("1").join("1");
    let build_state_path = index_root.join("generation_build.bin");

    let crashed = GenerationBuildState {
        generation: IndexGeneration::new(2),
        snapshot_timestamp: SnapshotTimestamp::new(1),
        start_lsn: CommitLsn::new(10),
        barrier_lsn: None,
        state: GenerationState::CatchingUp,
    };
    write_crashed_build_state(&index_root, &crashed);

    manager
        .resolve_split_crash_recovery(&index_root)
        .expect("recovery should succeed");

    assert!(
        !build_state_path.exists(),
        "CatchingUp state must be discarded on recovery"
    );
}

#[test]
fn resolve_split_crash_recovery_completes_publishing_state_with_manifest() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let index = create_tag_index("first", "person");
    manager
        .register_native_index(1, &index)
        .expect("register index");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "first",
            &[("name".into(), Value::string("Alice"))],
            1,
        )
        .expect("write");

    let index_root = directory.path().join("indexes").join("1").join("1");
    let build_state_path = index_root.join("generation_build.bin");

    let manifest = IndexManifest::new(
        1,
        1,
        IndexGeneration::new(2),
        vec![IndexShard {
            shard_id: 0,
            lower: None,
            upper: None,
            checkpoint_file: index_root.join("generation-2").join("shard-0"),
            checksum: None,
        }],
    )
    .expect("new manifest");
    manifest
        .store(&index_root.join("manifest.bin"))
        .expect("store manifest");

    let publishing = GenerationBuildState {
        generation: IndexGeneration::new(2),
        snapshot_timestamp: SnapshotTimestamp::new(1),
        start_lsn: CommitLsn::new(10),
        barrier_lsn: Some(CommitLsn::new(50)),
        state: GenerationState::Publishing,
    };
    write_crashed_build_state(&index_root, &publishing);

    manager
        .resolve_split_crash_recovery(&index_root)
        .expect("recovery should succeed");

    assert!(
        !build_state_path.exists(),
        "Publishing build state removed after completion"
    );
    assert!(
        index_root.join("manifest.bin").exists(),
        "manifest preserved after Publishing completion"
    );

    let results = manager
        .lookup_tag_index(1, &index, &Value::string("Alice"))
        .expect("lookup after publishing recovery");
    assert_eq!(results, vec![Value::Int(1)]);
}

#[test]
fn resolve_split_crash_recovery_discards_publishing_without_manifest() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let index = create_tag_index("first", "person");
    manager
        .register_native_index(1, &index)
        .expect("register index");

    let index_root = directory.path().join("indexes").join("1").join("1");
    let build_state_path = index_root.join("generation_build.bin");

    let publishing = GenerationBuildState {
        generation: IndexGeneration::new(2),
        snapshot_timestamp: SnapshotTimestamp::new(1),
        start_lsn: CommitLsn::new(10),
        barrier_lsn: Some(CommitLsn::new(50)),
        state: GenerationState::Publishing,
    };
    write_crashed_build_state(&index_root, &publishing);

    manager
        .resolve_split_crash_recovery(&index_root)
        .expect("recovery should succeed");

    assert!(
        !build_state_path.exists(),
        "Publishing state without manifest must be discarded"
    );
}

// --- Included columns MVCC tests ---

#[test]
fn included_columns_visible_in_covering_query_after_update() {
    let manager = IndexDataManagerImpl::new();
    let index = create_edge_index_with_included_properties();
    manager
        .register_native_index(1, &index)
        .expect("register edge index");
    let src = Value::Int(1);
    let dst = Value::Int(2);
    let edge = EdgeIdentity::new(1, &src, &dst, "KNOWS", 0);

    manager
        .update_edge_indexes_mvcc(
            &edge,
            "knows_weight_idx",
            &[
                ("weight".into(), Value::Int(10)),
                ("since".into(), Value::Int(2020)),
            ],
            10,
        )
        .expect("initial write");

    let covering_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 10,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &covering_plan)
        .expect("cursor");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert_eq!(rows.len(), 1);
    match &rows[0] {
        IndexRow::Covering { columns, .. } => {
            assert_eq!(columns.len(), 1);
            assert_eq!(columns[0], ("since".into(), Value::Int(2020)));
        }
        _ => panic!("expected covering row"),
    }

    manager
        .update_edge_indexes_mvcc(
            &edge,
            "knows_weight_idx",
            &[("since".into(), Value::Int(2024))],
            20,
        )
        .expect("update");

    let after_update_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 20,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &after_update_plan)
        .expect("cursor after update");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert_eq!(rows.len(), 1);
    match &rows[0] {
        IndexRow::Covering { columns, .. } => {
            assert_eq!(columns[0], ("since".into(), Value::Int(2024)));
        }
        _ => panic!("expected covering row after update"),
    }

    let snapshot_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 10,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &snapshot_plan)
        .expect("snapshot cursor");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert_eq!(rows.len(), 1);
    match &rows[0] {
        IndexRow::Covering { columns, .. } => {
            assert_eq!(columns[0], ("since".into(), Value::Int(2020)));
        }
        _ => panic!("expected covering row at snapshot"),
    }
}

#[test]
fn included_columns_not_visible_after_delete() {
    let manager = IndexDataManagerImpl::new();
    let index = create_edge_index_with_included_properties();
    manager
        .register_native_index(1, &index)
        .expect("register edge index");
    let src = Value::Int(1);
    let dst = Value::Int(2);
    let edge = EdgeIdentity::new(1, &src, &dst, "KNOWS", 0);

    manager
        .update_edge_indexes_mvcc(
            &edge,
            "knows_weight_idx",
            &[
                ("weight".into(), Value::Int(10)),
                ("since".into(), Value::Int(2020)),
            ],
            10,
        )
        .expect("write");

    let covering_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 10,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &covering_plan)
        .expect("cursor");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert_eq!(rows.len(), 1, "one edge before delete");

    manager
        .delete_edge_indexes_mvcc(&edge, &["knows_weight_idx".into()], 20)
        .expect("delete");

    let after_delete_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 20,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &after_delete_plan)
        .expect("cursor after delete");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert!(
        rows.is_empty(),
        "covering query must not return deleted edge"
    );
}

#[test]
fn included_columns_survive_rebuild_from_snapshot() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    let index = create_edge_index_with_included_properties();
    manager
        .register_native_index(1, &index)
        .expect("register index");
    let src = Value::Int(1);
    let dst = Value::Int(2);
    let edge = EdgeIdentity::new(1, &src, &dst, "KNOWS", 0);

    manager
        .update_edge_indexes_mvcc(
            &edge,
            "knows_weight_idx",
            &[
                ("weight".into(), Value::Int(10)),
                ("since".into(), Value::Int(2020)),
            ],
            10,
        )
        .expect("write");

    let runtime = manager.runtime(1, 1).expect("runtime");
    let catalog = manager.manifest_catalog(1, 1).expect("catalog");
    let manifest = catalog.acquire();
    let generation = runtime
        .generation(manifest.manifest().generation)
        .expect("active generation");
    let mut forward = BTreeMap::new();
    let mut reverse = BTreeMap::new();
    for shard in generation.shards() {
        let (f, r) = shard.snapshot();
        forward.extend(f);
        reverse.extend(r);
    }
    drop(manifest);

    let next_gen = IndexGeneration::new(3);
    let checkpoint_dir = directory
        .path()
        .join("indexes")
        .join("1")
        .join("1")
        .join(format!("generation-{}", next_gen.get()))
        .join("shard-0");
    let fwd_idx = crate::index::chunk::chunked_index::ChunkedIndex::from_btree(
        vec![],
        &forward,
        64 * 1024 * 1024,
    );
    let rev_idx = crate::index::chunk::chunked_index::ChunkedIndex::from_btree(
        vec![],
        &reverse,
        64 * 1024 * 1024,
    );
    crate::index::chunk::serialize::write_chunked_index_checkpoint(
        checkpoint_dir.join("forward_chunks"),
        &fwd_idx,
    )
    .expect("flush forward checkpoint");
    crate::index::chunk::serialize::write_chunked_index_checkpoint(
        checkpoint_dir.join("reverse_chunks"),
        &rev_idx,
    )
    .expect("flush reverse checkpoint");

    let next_manifest = IndexManifest::new(
        1,
        1,
        next_gen,
        vec![IndexShard {
            shard_id: 0,
            lower: None,
            upper: None,
            checkpoint_file: checkpoint_dir,
            checksum: None,
        }],
    )
    .expect("new manifest");
    manager
        .publish_native_index(next_manifest, forward, reverse, CommitLsn::ZERO)
        .expect("publish");

    let covering_plan = IndexScanPlan {
        space: "space".into(),
        index_id: 1,
        predicate: IndexPredicate::All,
        partition: PartitionSelector::All,
        partition_id_range: None,
        projection: Some(vec!["since".into()]),
        limit: None,
        offset: 0,
        read_timestamp: 10,
    };
    let mut cursor = manager
        .open_edge_index_cursor(1, &index, &covering_plan)
        .expect("cursor after rebuild");
    let rows: Vec<IndexRow> =
        std::iter::from_fn(|| cursor.next_batch(64).ok().filter(|b| !b.is_empty()))
            .flatten()
            .collect();
    assert_eq!(rows.len(), 1, "rebuilt index should have one entry");
    match &rows[0] {
        IndexRow::Covering { columns, .. } => {
            assert_eq!(columns[0], ("since".into(), Value::Int(2020)));
        }
        _ => panic!("expected covering row after rebuild"),
    }
}

#[test]
fn wal_recovers_data_after_checkpoint() {
    use crate::index::shard_runtime::shard::ShardRuntime;
    use crate::index::types::IndexRecord;
    use linkrs_core::types::storage_ids::VertexId;
    use linkrs_core::wal::EntityRef;

    let temp_dir = std::env::temp_dir().join("linkrs_wal_test");
    let _ = std::fs::remove_dir_all(&temp_dir);
    std::fs::create_dir_all(&temp_dir).unwrap();

    let checkpoint_file = temp_dir.join("checkpoint");
    std::fs::create_dir_all(&checkpoint_file).unwrap();

    // Create a shard and insert data
    let shard = ShardRuntime::empty_with_capacity(checkpoint_file.clone(), 64 * 1024 * 1024);
    let mut forward = BTreeMap::new();
    let mut reverse = BTreeMap::new();

    forward.insert(
        vec![1, 2, 3],
        IndexRecord::new(100).with_entity_ref(EntityRef::Vertex(
            VertexId::try_from_int64(42).expect("test vertex id"),
        )),
    );
    reverse.insert(
        vec![4, 5, 6],
        IndexRecord::new(100).with_entity_ref(EntityRef::Vertex(
            VertexId::try_from_int64(42).expect("test vertex id"),
        )),
    );

    shard.replace(forward, reverse);

    // Flush WAL to disk
    shard.flush_wal().unwrap();

    // Verify WAL file exists
    let wal_path = checkpoint_file.join("index.wal");
    assert!(wal_path.exists(), "WAL file should exist after flush_wal");

    // Load a new shard from the same checkpoint - should replay WAL
    let loaded_shard =
        ShardRuntime::load_with_pool_capacity(checkpoint_file.clone(), 128 * 1024 * 1024).unwrap();

    let fwd = loaded_shard.read_forward();
    let rev = loaded_shard.read_reverse();
    assert_eq!(
        fwd.snapshot().len(),
        1,
        "Should have 1 forward entry after WAL replay"
    );
    assert_eq!(
        rev.snapshot().len(),
        1,
        "Should have 1 reverse entry after WAL replay"
    );

    // Cleanup
    let _ = std::fs::remove_dir_all(&temp_dir);
}

#[test]
fn checkpoint_clears_wal() {
    use crate::index::shard_runtime::shard::ShardRuntime;
    use crate::index::types::IndexRecord;

    let temp_dir = std::env::temp_dir().join("linkrs_checkpoint_test");
    let _ = std::fs::remove_dir_all(&temp_dir);
    std::fs::create_dir_all(&temp_dir).unwrap();

    let checkpoint_file = temp_dir.join("checkpoint");
    std::fs::create_dir_all(&checkpoint_file).unwrap();

    let shard = ShardRuntime::empty_with_capacity(checkpoint_file.clone(), 64 * 1024 * 1024);
    let mut forward = BTreeMap::new();
    forward.insert(vec![1, 2, 3], IndexRecord::new(100));
    shard.replace(forward, BTreeMap::new());

    // Flush WAL
    shard.flush_wal().unwrap();
    let wal_path = checkpoint_file.join("index.wal");
    assert!(wal_path.exists());

    // Checkpoint - should clear WAL
    shard.checkpoint().unwrap();
    assert!(
        !wal_path.exists(),
        "WAL file should be removed after checkpoint"
    );

    // Cleanup
    let _ = std::fs::remove_dir_all(&temp_dir);
}

#[test]
fn memory_limit_triggers_compaction() {
    let manager = IndexDataManagerImpl::new();
    let index = create_tag_index("name_idx", "Person");

    // Set a very low memory limit to trigger compaction
    manager.set_memory_limit_bytes(1);

    manager
        .register_native_index(1, &index)
        .expect("register index");

    // First update
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "name_idx",
            &[("name".into(), Value::string("Alice"))],
            100,
        )
        .expect("first update");

    // Second update - should trigger compaction due to memory limit
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(2),
            "name_idx",
            &[("name".into(), Value::string("Bob"))],
            200,
        )
        .expect("second update with memory limit");

    // Verify data is still accessible
    let results = manager
        .lookup_tag_index_mvcc(1, &index, &Value::string("Alice"), 300)
        .expect("lookup Alice");
    assert!(!results.is_empty(), "Alice should be found");

    // Cleanup - reset limit
    manager.set_memory_limit_bytes(0);
}

#[test]
fn retire_generations_reclaims_retired_checkpoint_dirs() {
    let directory = tempfile::tempdir().expect("tempdir");
    let manager = IndexDataManagerImpl::new_with_root(directory.path().join("indexes"));
    // force per-statement publication so each update below creates a
    // retired generation (the accumulation path is covered elsewhere).
    manager.set_delta_publish_threshold(1);
    let index = create_tag_index("name_idx", "Person");
    manager
        .register_native_index(1, &index)
        .expect("register index");

    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(1),
            "name_idx",
            &[("name".into(), Value::string("Alice"))],
            100,
        )
        .expect("first update");
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(2),
            "name_idx",
            &[("name".into(), Value::string("Bob"))],
            200,
        )
        .expect("second update");

    // Simulate durable checkpoints for the two retired generations by
    // materializing their shard directories at the manifest-declared paths.
    let catalog = manager
        .manifest_catalog(1, 1)
        .expect("manifest catalog should exist");
    let retired = catalog.retired_reclaimable(|_| true);
    assert_eq!(retired.len(), 2);
    for manifest in &retired {
        for shard in &manifest.shards {
            std::fs::create_dir_all(&shard.checkpoint_file)
                .expect("fixture checkpoint dir should be created");
        }
    }
    let index_root = directory.path().join("indexes").join("1").join("1");
    let gen1 = index_root.join("generation-1");
    let gen2 = index_root.join("generation-2");
    assert!(gen1.is_dir(), "generation 1 checkpoint should exist");
    assert!(gen2.is_dir(), "generation 2 checkpoint should exist");

    // Advancing safe_ts past both retired generations' max_ts removes them
    // from the runtime and reclaims their checkpoint files.
    let retired_count = manager.retire_generations(300);
    assert_eq!(retired_count, 2);

    assert!(
        !gen1.exists(),
        "generation 1 files should be reclaimed after retirement"
    );
    assert!(
        !gen2.exists(),
        "generation 2 files should be reclaimed after retirement"
    );
}

// --- delta accumulation batches generation publication ---

#[test]
fn delta_accumulation_batches_generation_publication() {
    use crate::index::traits::VertexIndexOps;

    let manager = IndexDataManagerImpl::new();
    let index = create_tag_index("name_idx", "Person");
    manager.register_native_index(1, &index).unwrap();

    // Default threshold (64): 200 entries → a handful of generations instead
    // of one per statement.
    for i in 0..200u64 {
        manager
            .update_vertex_indexes_mvcc(
                1,
                &Value::Int(i as i32),
                "name_idx",
                &[("name".into(), Value::string(format!("person_{i}")))],
                i + 1,
            )
            .unwrap();
    }

    let runtime = manager.runtime(1, 1).unwrap();
    let published = runtime.generations().len();
    assert!(
        published <= 2,
        "200 writes (400 entries) below the 512-entry threshold should stay pending, got {published}"
    );

    // A read flushes any remaining pending delta and sees all writes.
    let results = manager
        .lookup_tag_index(1, &index, &Value::string("person_150"))
        .unwrap();
    assert_eq!(results, vec![Value::Int(150)]);

    // The pending buffer is drained after the read.
    let identity = crate::index::types::IndexIdentity {
        space_id: 1,
        index_id: 1,
    };
    assert_eq!(manager.pending_delta_entries(identity), 0);
}

#[test]
fn delta_accumulation_rollback_path_publishes_per_statement() {
    use crate::index::traits::VertexIndexOps;

    let manager = IndexDataManagerImpl::new();
    manager.set_delta_publish_threshold(1);
    let index = create_tag_index("name_idx", "Person");
    manager.register_native_index(1, &index).unwrap();

    for i in 0..10u64 {
        manager
            .update_vertex_indexes_mvcc(
                1,
                &Value::Int(i as i32),
                "name_idx",
                &[("name".into(), Value::string(format!("person_{i}")))],
                i + 1,
            )
            .unwrap();
    }

    let runtime = manager.runtime(1, 1).unwrap();
    // 1 base generation from registration + 10 statement publications.
    assert_eq!(
        runtime.generations().len(),
        11,
        "threshold 1 must publish one generation per statement"
    );
}

/// The pending-aware lookup (no generation publish) must agree with the
/// publish-first lookup on live entries, tombstones, and overwrites while the
/// delta is still buffered.
#[test]
fn pending_aware_lookup_matches_published_lookup() {
    use crate::index::traits::VertexIndexOps;

    let manager = IndexDataManagerImpl::new();
    let index = create_tag_index("name_idx", "Person");
    manager.register_native_index(1, &index).unwrap();

    // Writes accumulate in the pending buffer (default threshold 512).
    for i in 0..10u64 {
        manager
            .update_vertex_indexes_mvcc(
                1,
                &Value::Int(i as i32),
                "name_idx",
                &[("name".into(), Value::string(format!("name_{i}")))],
                i + 1,
            )
            .unwrap();
    }

    // Pending-aware read (no publish) sees the live entries.
    assert_eq!(
        manager
            .lookup_tag_index_pending_aware_mvcc(
                1,
                &index,
                &Value::string("name_3"),
                MAX_TIMESTAMP,
            )
            .unwrap(),
        vec![Value::Int(3)]
    );
    assert_eq!(
        manager
            .lookup_tag_index_pending_aware_mvcc(
                1,
                &index,
                &Value::string("name_9"),
                MAX_TIMESTAMP,
            )
            .unwrap(),
        vec![Value::Int(9)]
    );

    // Publishing and re-reading yields the identical result.
    let published = manager
        .lookup_tag_index_mvcc(1, &index, &Value::string("name_3"), MAX_TIMESTAMP)
        .unwrap();
    assert_eq!(published, vec![Value::Int(3)]);

    // Overwrite: change vid 5 from name_5 to name_50, both deltas stay
    // pending. The old value must be tombstoned and the new value visible.
    manager
        .update_vertex_indexes_mvcc(
            1,
            &Value::Int(5),
            "name_idx",
            &[("name".into(), Value::string("name_50"))],
            20,
        )
        .unwrap();
    assert!(
        manager
            .lookup_tag_index_pending_aware_mvcc(
                1,
                &index,
                &Value::string("name_5"),
                MAX_TIMESTAMP,
            )
            .unwrap()
            .is_empty(),
        "old value must be tombstoned while pending"
    );
    assert_eq!(
        manager
            .lookup_tag_index_pending_aware_mvcc(
                1,
                &index,
                &Value::string("name_50"),
                MAX_TIMESTAMP,
            )
            .unwrap(),
        vec![Value::Int(5)]
    );
    // The publish-first read agrees after flushing.
    assert!(manager
        .lookup_tag_index_mvcc(1, &index, &Value::string("name_5"), MAX_TIMESTAMP)
        .unwrap()
        .is_empty());
    assert_eq!(
        manager
            .lookup_tag_index_mvcc(1, &index, &Value::string("name_50"), MAX_TIMESTAMP)
            .unwrap(),
        vec![Value::Int(5)]
    );

    // Tombstone accounting reconciles pending overwrites: name_5's tombstone
    // (added at ts 20) was replaced by a live entry at ts 30, so only the
    // name_50 tombstone remains counted (forward + reverse → 2). Without
    // reconciliation the counter would read 4.
    assert_eq!(
        manager.cached_tombstone_count(),
        2,
        "tombstone counter must reconcile pending overwrites"
    );
}