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
use super::*;

#[test]
fn test_trigger_background_freeze_execution() {
    let mut storage = create_test_storage();
    let _space_id = setup_space(&mut storage);
    setup_person_tag(&mut storage);
    setup_knows_edge(&mut storage);

    // Insert vertices
    let alice = VertexId::try_from_int64(1).expect("test vertex id");
    let bob = VertexId::try_from_int64(2).expect("test vertex id");

    let v1 = Vertex::new(
        alice,
        linkrs_core::vertex_edge_path::Tag::new(
            "Person".to_string(),
            [("name".into(), Value::string("Alice"))]
                .iter()
                .cloned()
                .collect(),
        ),
    );

    let v2 = Vertex::new(
        bob,
        linkrs_core::vertex_edge_path::Tag::new(
            "Person".to_string(),
            [("name".into(), Value::string("Bob"))]
                .iter()
                .cloned()
                .collect(),
        ),
    );

    storage.insert_vertex("test_space", v1).unwrap();
    storage.insert_vertex("test_space", v2).unwrap();

    // Insert edge
    let edge = Edge {
        src: alice,
        dst: bob,
        edge_type: "KNOWS".to_string(),
        ranking: 0,
        props: [("since".into(), Value::Int(2020))]
            .iter()
            .cloned()
            .collect(),
    };

    storage.insert_edge("test_space", edge).unwrap();

    // Trigger freeze - should succeed
    let result = storage.trigger_background_freeze();
    assert!(result.is_ok(), "Freeze should succeed: {:?}", result.err());
}

#[test]
fn test_cleanup_threshold_gc_integration() {
    // Test that compaction uses cleanup_threshold from SnapshotTracker
    let (_, mut storage) = create_persistent_storage();
    let _space_id = setup_space(&mut storage);
    let _person_tag = setup_person_tag(&mut storage);
    let _knows_edge = setup_knows_edge(&mut storage);

    // Create vertices
    let alice = VertexId::try_from_int64(1).expect("test vertex id");

    // Insert vertices
    let v1 = Vertex::new(
        alice,
        linkrs_core::vertex_edge_path::Tag::new(
            "Person".to_string(),
            [("name".into(), Value::string("Alice"))]
                .iter()
                .cloned()
                .collect(),
        ),
    );

    storage.insert_vertex("test_space", v1).unwrap();

    // Verify SnapshotTracker is accessible through VersionManager
    let version_manager = storage.ctx.version_manager().clone();
    let snapshot_tracker = version_manager.snapshot_tracker();

    // Before any snapshots, cleanup_threshold should be MAX
    let initial_threshold = snapshot_tracker.cleanup_threshold();
    assert_eq!(
        initial_threshold,
        Timestamp::MAX,
        "Initial cleanup_threshold should be Timestamp::MAX"
    );

    // Acquire a read timestamp (creates a snapshot)
    let read_ts = version_manager
        .acquire_read_timestamp()
        .expect("Failed to acquire read timestamp");
    assert!(read_ts > 0, "Read timestamp should be valid");

    // Now cleanup_threshold should equal the read timestamp
    let threshold_with_active = snapshot_tracker.cleanup_threshold();
    assert_eq!(
        threshold_with_active, read_ts,
        "cleanup_threshold should equal active read timestamp"
    );

    // Release the read timestamp
    version_manager.release_read_timestamp();

    // After releasing, cleanup_threshold should be MAX again
    let final_threshold = snapshot_tracker.cleanup_threshold();
    assert_eq!(
        final_threshold,
        Timestamp::MAX,
        "Final cleanup_threshold should be Timestamp::MAX after releasing"
    );

    // Verify compaction works (it uses cleanup_threshold internally)
    let result = storage.compact(&Default::default());
    assert!(
        result.is_ok(),
        "Compaction should succeed with cleanup_threshold: {:?}",
        result.err()
    );
}

#[test]
fn test_snapshot_tracker_cleanup_threshold_multiple_readers() {
    // Test cleanup_threshold with multiple concurrent read transactions
    let storage = create_test_storage();
    let version_manager = storage.ctx.version_manager().clone();
    let snapshot_tracker = version_manager.snapshot_tracker();

    // Initially, no active snapshots
    assert_eq!(snapshot_tracker.cleanup_threshold(), Timestamp::MAX);
    assert_eq!(snapshot_tracker.active_count(), 0);

    // Acquire multiple read timestamps
    let ts1 = version_manager
        .acquire_read_timestamp()
        .expect("Failed to acquire read timestamp");
    let ts2 = version_manager
        .acquire_read_timestamp()
        .expect("Failed to acquire read timestamp");
    let ts3 = version_manager
        .acquire_read_timestamp()
        .expect("Failed to acquire read timestamp");

    // All should use the same read_ts (due to MVCC design)
    assert_eq!(ts1, ts2);
    assert_eq!(ts2, ts3);

    // cleanup_threshold should be the minimum active
    assert_eq!(snapshot_tracker.cleanup_threshold(), ts1);

    // Reference count should be 3
    assert_eq!(snapshot_tracker.ref_count(ts1), Some(3));

    // Release one
    version_manager.release_read_timestamp();
    assert_eq!(snapshot_tracker.ref_count(ts1), Some(2));
    assert_eq!(snapshot_tracker.cleanup_threshold(), ts1); // Still active

    // Release another
    version_manager.release_read_timestamp();
    assert_eq!(snapshot_tracker.ref_count(ts1), Some(1));

    // Release the last one
    version_manager.release_read_timestamp();
    assert_eq!(snapshot_tracker.active_count(), 0);
    assert_eq!(snapshot_tracker.cleanup_threshold(), Timestamp::MAX);
}

#[test]
fn test_compact_maintenance_propagates_vertex_remap_to_edge_tables() {
    // Regression: vertex compaction densifies internal IDs; the old-to-new
    // mapping must be propagated into edge CSR rows/neighbors or every
    // edge referencing a surviving vertex breaks.
    let (_, mut storage) = create_persistent_storage();
    setup_space(&mut storage);
    setup_person_tag(&mut storage);
    setup_knows_edge(&mut storage);

    // 100 vertices (internal ids 0..99 in insertion order).
    for i in 1..=100i64 {
        insert_test_vertex(&mut storage, i, &format!("v{i}"));
    }

    // Edges only between vertices that will survive compaction:
    // odd pairs (1,3), (3,5), ..., (77,79) and (79,81), ..., (97,99).
    let expected_edges: Vec<(i64, i64)> = (1..=97).step_by(2).map(|src| (src, src + 2)).collect();
    for (src, dst) in &expected_edges {
        let edge = Edge::new(
            VertexId::try_from_int64(*src).expect("test vertex id"),
            VertexId::try_from_int64(*dst).expect("test vertex id"),
            "KNOWS".to_string(),
            0,
            std::collections::HashMap::new(),
        );
        storage.insert_edge("test_space", edge).unwrap();
    }

    // Delete 40 vertices (external ids 2..80 step 2); their internal ids
    // are interleaved with survivors, forcing a real ID remap.
    for i in (2..=80).step_by(2) {
        storage
            .delete_vertex(
                "test_space",
                "Person",
                &VertexId::try_from_int64(i).expect("test vertex id"),
            )
            .unwrap();
    }

    let vertex_count_before = storage
        .ctx
        .data_store()
        .with_vertex_tables(|tables| {
            Ok::<usize, linkrs_core::StorageError>(
                tables
                    .values()
                    .map(|t| t.approximate_total_count())
                    .sum::<usize>(),
            )
        })
        .unwrap();
    assert_eq!(vertex_count_before, 100);

    storage.compact(&Default::default()).unwrap();

    // 40 vertices removed by compaction.
    let vertex_count_after = storage
        .ctx
        .data_store()
        .with_vertex_tables(|tables| {
            Ok::<usize, linkrs_core::StorageError>(
                tables
                    .values()
                    .map(|t| t.approximate_total_count())
                    .sum::<usize>(),
            )
        })
        .unwrap();
    assert_eq!(
        vertex_count_after, 60,
        "compaction should remove 40 vertices"
    );

    // Deleted vertices no longer resolve.
    assert!(storage
        .get_vertex(
            "test_space",
            "Person",
            &VertexId::try_from_int64(2).expect("test vertex id")
        )
        .unwrap()
        .is_none());

    // Every surviving edge still resolves through the remapped edge CSR.
    for (src, dst) in &expected_edges {
        let retrieved = storage
            .get_edge(
                "test_space",
                &VertexId::try_from_int64(*src).expect("test vertex id"),
                &VertexId::try_from_int64(*dst).expect("test vertex id"),
                "KNOWS",
                0,
            )
            .unwrap();
        assert!(
            retrieved.is_some(),
            "edge {src}->{dst} lost after vertex compaction remap"
        );
    }

    // Node-edge scans resolve through remapped out/in CSRs.
    let out_edges = storage
        .get_node_edges(
            "test_space",
            &VertexId::try_from_int64(1).expect("test vertex id"),
            EdgeDirection::Out,
            &[],
        )
        .unwrap();
    assert_eq!(out_edges.len(), 1);
    assert_eq!(
        out_edges[0].dst,
        VertexId::try_from_int64(3).expect("test vertex id")
    );
    let in_edges = storage
        .get_node_edges(
            "test_space",
            &VertexId::try_from_int64(79).expect("test vertex id"),
            EdgeDirection::In,
            &[],
        )
        .unwrap();
    assert_eq!(in_edges.len(), 1);
    assert_eq!(
        in_edges[0].src,
        VertexId::try_from_int64(77).expect("test vertex id")
    );
}

#[test]
fn test_auto_vertex_compaction_reclaims_id_holes() {
    // Background maintenance must reclaim deleted-vertex ID holes without
    // an explicit compact transaction when thresholds are exceeded.
    let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let mut property_config = PropertyGraphConfig::test();
    property_config.auto_compact = AutoCompactConfig {
        enable_vertex_compaction: true,
        min_holes: 20,
        min_hole_ratio: 0.1,
        min_interval_secs: 0,
    };
    let persistence_config = PersistenceConfig::for_work_dir(temp_dir.path())
        .with_property_graph_config(property_config);
    let mut storage =
        GraphStorage::new_with_persistence(temp_dir.path().to_path_buf(), persistence_config)
            .expect("Failed to create persistent storage");
    setup_space(&mut storage);
    setup_person_tag(&mut storage);
    setup_knows_edge(&mut storage);

    for i in 1..=100i64 {
        insert_test_vertex(&mut storage, i, &format!("v{i}"));
    }
    let expected_edges: Vec<(i64, i64)> = (1..=97).step_by(2).map(|src| (src, src + 2)).collect();
    for (src, dst) in &expected_edges {
        let edge = Edge::new(
            VertexId::try_from_int64(*src).expect("test vertex id"),
            VertexId::try_from_int64(*dst).expect("test vertex id"),
            "KNOWS".to_string(),
            0,
            std::collections::HashMap::new(),
        );
        storage.insert_edge("test_space", edge).unwrap();
    }
    // Delete 40 vertices; holes appear but nothing is reclaimed yet.
    for i in (2..=80).step_by(2) {
        storage
            .delete_vertex(
                "test_space",
                "Person",
                &VertexId::try_from_int64(i).expect("test vertex id"),
            )
            .unwrap();
    }

    let (live, allocated) = storage
        .ctx
        .data_store()
        .with_vertex_tables(|tables| {
            Ok::<(usize, usize), linkrs_core::StorageError>(
                tables
                    .values()
                    .map(|t| t.approximate_id_hole_stats(u64::MAX))
                    .fold((0, 0), |(l, a), (x, y)| (l + x, a + y)),
            )
        })
        .unwrap();
    assert_eq!(
        (live, allocated),
        (60, 100),
        "holes must not be reclaimed yet"
    );

    storage.trigger_background_maintenance().unwrap();

    let (live, allocated) = storage
        .ctx
        .data_store()
        .with_vertex_tables(|tables| {
            Ok::<(usize, usize), linkrs_core::StorageError>(
                tables
                    .values()
                    .map(|t| t.approximate_id_hole_stats(u64::MAX))
                    .fold((0, 0), |(l, a), (x, y)| (l + x, a + y)),
            )
        })
        .unwrap();
    assert_eq!(
        (live, allocated),
        (60, 100),
        "stable compaction absorbs tombstones without re-densifying the ID space"
    );

    // Absorbed holes are reusable by new inserts through the free stack;
    // recycled slots must serve fresh data. (Allocation counts are
    // routing-dependent across shards, so exact reuse counts stay covered
    // by the table-level hole-reuse test.)
    for i in 101..=105i64 {
        insert_test_vertex(&mut storage, i, &format!("v{i}"));
    }
    let (live, allocated) = storage
        .ctx
        .data_store()
        .with_vertex_tables(|tables| {
            Ok::<(usize, usize), linkrs_core::StorageError>(
                tables
                    .values()
                    .map(|t| t.approximate_id_hole_stats(u64::MAX))
                    .fold((0, 0), |(l, a), (x, y)| (l + x, a + y)),
            )
        })
        .unwrap();
    assert!(
        allocated <= 105,
        "allocation must not exceed one slot per new insert: {live}/{allocated}"
    );
    for i in 101..=105i64 {
        let vertex = storage
            .get_vertex(
                "test_space",
                "Person",
                &VertexId::try_from_int64(i).expect("test vertex id"),
            )
            .unwrap()
            .unwrap_or_else(|| panic!("vertex v{i} lost after hole reuse"));
        assert_eq!(
            vertex.property_value("name"),
            Some(Value::string(format!("v{i}"))),
            "vertex v{i} property corrupted by hole reuse"
        );
    }

    // Surviving edges still resolve through the remapped edge CSR.
    for (src, dst) in &expected_edges {
        let retrieved = storage
            .get_edge(
                "test_space",
                &VertexId::try_from_int64(*src).expect("test vertex id"),
                &VertexId::try_from_int64(*dst).expect("test vertex id"),
                "KNOWS",
                0,
            )
            .unwrap();
        assert!(
            retrieved.is_some(),
            "edge {src}->{dst} lost after auto vertex compaction remap"
        );
    }

    // Every surviving vertex keeps its properties through the remap. This
    // must cover all survivors (not a sample): rows whose internal ids did
    // not move are absent from the partial id mapping and used to be
    // dropped by the remap.
    let survivors: Vec<i64> = (1..=100i64).filter(|id| *id % 2 == 1 || *id > 80).collect();
    assert_eq!(survivors.len(), 60);
    for id in survivors {
        let vertex = storage
            .get_vertex(
                "test_space",
                "Person",
                &VertexId::try_from_int64(id).expect("test vertex id"),
            )
            .unwrap()
            .unwrap_or_else(|| panic!("vertex v{id} lost after vertex compaction remap"));
        assert_eq!(
            vertex.property_value("name"),
            Some(Value::string(format!("v{id}"))),
            "vertex v{id} property corrupted by remap"
        );
    }
    for id in [2i64, 40, 80] {
        assert!(
            storage
                .get_vertex(
                    "test_space",
                    "Person",
                    &VertexId::try_from_int64(id).expect("test vertex id")
                )
                .unwrap()
                .is_none(),
            "deleted vertex v{id} resurrected by remap"
        );
    }
}