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
use linkrs_core::types::{EdgeIdentifier, LabelId, Timestamp, VertexId};
use linkrs_core::wal::redo::{DeleteEdgeRedo, DeleteVertexRedo};
use linkrs_core::wal::types::WalOpType;
use linkrs_core::{StorageError, StorageResult, Value};

use super::super::context::helpers;
use super::super::context::txn_staging::StagedIndexOp;
use super::super::context::GraphStorageContext;
use super::super::ops::{endpoint_label_id, route_vertex_id, tag_label_id, RoutedVertexId};
use crate::engine::data_store::EdgeTableKey;
use crate::index::types::EdgeIdentity;

/// Warn when a non-cascading vertex delete leaves incident edges behind.
///
/// Plain vertex deletion keeps incident edges by design, so the orphaned
/// rows stay visible until repaired. Each matching edge type is probed with
/// a limit of one, which costs a single row visit on connected vertices and
/// nothing when the space has no edge types at all.
fn warn_if_leaves_dangling_edges(
    ctx: &GraphStorageContext,
    space: &str,
    tag_name: &str,
    label_id: LabelId,
    vid: &VertexId,
) {
    let edge_types = match ctx.schema_manager().list_edge_types(space) {
        Ok(types) if !types.is_empty() => types,
        _ => return,
    };
    let ts = ctx.get_read_timestamp();
    for edge_info in &edge_types {
        let (Some(src_label), Some(dst_label)) = (
            endpoint_label_id(ctx, space, &edge_info.src_tag_name).unwrap_or(None),
            endpoint_label_id(ctx, space, &edge_info.dst_tag_name).unwrap_or(None),
        ) else {
            continue;
        };
        // Unconstrained endpoints resolve to label 0 and match every tag.
        let out_hit = (src_label == 0 || src_label == label_id)
            && ctx
                .out_edges_projected_limit(
                    edge_info.edge_type_id,
                    src_label,
                    *vid,
                    ts,
                    Some(&[]),
                    1,
                )
                .is_some_and(|records| !records.is_empty());
        let in_hit = !out_hit
            && (dst_label == 0 || dst_label == label_id)
            && ctx
                .in_edges_projected_limit(edge_info.edge_type_id, dst_label, *vid, ts, Some(&[]), 1)
                .is_some_and(|records| !records.is_empty());
        if out_hit || in_hit {
            log::warn!(
                "delete_vertex {}.{}:{} leaves incident {} edges behind; \
                 use delete_vertex_with_edges for cascade deletes or run \
                 repair_dangling_edges afterwards",
                space,
                tag_name,
                vid,
                edge_info.edge_type_name,
            );
            return;
        }
    }
}

pub(crate) fn delete_vertex(
    ctx: &GraphStorageContext,
    space: &str,
    tag_name: &str,
    id: &VertexId,
) -> StorageResult<()> {
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;

    let label_id = tag_label_id(ctx, space, tag_name)?
        .ok_or_else(|| StorageError::not_found(format!("Tag {} not found", tag_name)))?;
    let vid = VertexId::normalize_for_vid_type(&space_info.vid_type, *id)?;
    warn_if_leaves_dangling_edges(ctx, space, tag_name, label_id, &vid);
    let routed = route_vertex_id(&vid)?;
    let ts = ctx.get_write_timestamp()?;
    delete_vertex_with_timestamp(
        ctx,
        VertexDeleteTarget {
            space_id: space_info.space_id,
            tag_name,
            label_id,
            vid: &vid,
            routed: &routed,
        },
        ts,
        true,
    )
}

/// Everything identifying one vertex deletion: its space/tag label plus the
/// identity in both logical and routed form.
struct VertexDeleteTarget<'a> {
    space_id: u64,
    tag_name: &'a str,
    label_id: LabelId,
    vid: &'a VertexId,
    routed: &'a RoutedVertexId,
}

/// Delete one vertex row with a caller-provided write timestamp.
///
/// Shared by plain, cascade and batch deletes so a cascade stamps its edges
/// and its vertices with one timestamp and settles it once: any failure
/// aborts the timestamp, hiding both the edge and the vertex writes instead
/// of leaving a half cascade. The online path still defers settling to the
/// transaction commit; `settle` only gates the offline commit, which the
/// batch caller performs once after its loop.
fn delete_vertex_with_timestamp(
    ctx: &GraphStorageContext,
    target: VertexDeleteTarget<'_>,
    ts: Timestamp,
    settle: bool,
) -> StorageResult<()> {
    let VertexDeleteTarget {
        space_id,
        tag_name,
        label_id,
        vid,
        routed,
    } = target;
    let redo = DeleteVertexRedo {
        label: label_id,
        vid: *vid,
    };
    let redo_entry = ctx.append_wal_redo(WalOpType::DeleteVertex, ts, &redo)?;

    let online = ctx.is_online_write();
    let staging = if online {
        match ctx.txn_staging_mark(ts) {
            Ok(mark) => Some(mark),
            Err(error) => {
                ctx.abort_write_timestamp(ts);
                return Err(error);
            }
        }
    } else {
        None
    };
    let unwind = |error: StorageError| -> StorageError {
        if let Some((buffer, mark)) = &staging {
            ctx.rollback_staging_to(buffer, *mark);
        }
        ctx.abort_write_timestamp(ts);
        error
    };

    let delete_result = match routed {
        RoutedVertexId::Int(vid_int) => ctx.delete_vertex_by_i64(label_id, *vid_int, ts),
        RoutedVertexId::Text(id_str) => ctx.delete_vertex(label_id, id_str, ts),
    };
    if let Err(error) = delete_result {
        return Err(unwind(error));
    }
    if let Err(error) = super::vertex::record_vertex_remove(ctx, *vid, Some(redo_entry)) {
        return Err(unwind(error));
    }

    let id_value = Value::from(*vid);
    if online {
        // Index entry removal replays at commit apply.
        if let Err(error) = ctx.stage_vertex_index_op(
            ts,
            StagedIndexOp::Delete {
                space_id,
                vid: id_value,
                tag: tag_name.to_string(),
            },
        ) {
            return Err(unwind(error));
        }
        return Ok(());
    }

    if let Err(error) = super::index_maintenance::delete_vertex_indexes(
        ctx,
        ctx.index_metadata_manager(),
        space_id,
        &id_value,
        tag_name,
        ts,
    ) {
        return Err(unwind(error));
    }

    if settle {
        ctx.commit_write_timestamp_ordered(ts)?;
    }

    Ok(())
}

/// Delete a vertex together with every incident edge in table-scoped batches.
///
/// One write timestamp covers the edges and the vertex: each edge-type table
/// removes its incident edges through the table-level batch entrance (one
/// staging precheck plus one commit per touched table), then the vertex row
/// is deleted with the same timestamp and settled once. Any failure aborts
/// the timestamp, hiding both the edge and the vertex writes instead of
/// leaving a half cascade; aborted stamps stay hidden through the pending
/// gate. The transaction layer keeps per-edge redo entries, restore records
/// and index maintenance, so explicit transactions still roll back edge by
/// edge. Commits and table log entries scale with the touched tables, not
/// the edge count.
pub(crate) fn delete_vertex_with_edges(
    ctx: &GraphStorageContext,
    space: &str,
    tag_name: &str,
    id: &VertexId,
) -> StorageResult<()> {
    let space_id = ctx.schema_manager().get_space_id(space)?;
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;
    let label_id = tag_label_id(ctx, space, tag_name)?
        .ok_or_else(|| StorageError::not_found(format!("Tag {} not found", tag_name)))?;
    let id = VertexId::normalize_for_vid_type(&space_info.vid_type, *id)?;
    let routed = route_vertex_id(&id)?;
    let edge_types = ctx.schema_manager().list_edge_types(space)?;
    let ts = ctx.get_write_timestamp()?;
    for edge_info in &edge_types {
        if let Err(error) = delete_incident_edges_of_type(ctx, space_id, &id, edge_info, ts) {
            ctx.abort_write_timestamp(ts);
            return Err(error);
        }
    }
    delete_vertex_with_timestamp(
        ctx,
        VertexDeleteTarget {
            space_id,
            tag_name,
            label_id,
            vid: &id,
            routed: &routed,
        },
        ts,
        true,
    )
}

/// Batch-delete multiple vertices together with all their incident edges.
///
/// One write timestamp covers the entire batch. For each edge type, one
/// staging batch per physical table covers all vertices, reducing commits
/// from `N * tables` to `tables`; the vertex rows are then deleted with the
/// same timestamp and settled once. Any failure aborts the timestamp, hiding
/// the whole batch instead of leaving a half cascade; aborted stamps stay
/// hidden through the pending gate. The per-edge transaction redo, restore
/// records and index maintenance keep the explicit transaction rollback
/// path intact.
pub(crate) fn batch_delete_vertices_with_edges(
    ctx: &GraphStorageContext,
    space: &str,
    tag_name: &str,
    ids: &[VertexId],
) -> StorageResult<usize> {
    if ids.is_empty() {
        return Ok(0);
    }
    let space_id = ctx.schema_manager().get_space_id(space)?;
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;
    let label_id = tag_label_id(ctx, space, tag_name)?
        .ok_or_else(|| StorageError::not_found(format!("Tag {} not found", tag_name)))?;
    let ids: Vec<VertexId> = ids
        .iter()
        .map(|id| VertexId::normalize_for_vid_type(&space_info.vid_type, *id))
        .collect::<StorageResult<_>>()?;
    let routed: Vec<RoutedVertexId> = ids
        .iter()
        .map(route_vertex_id)
        .collect::<StorageResult<_>>()?;
    let edge_types = ctx.schema_manager().list_edge_types(space)?;
    let ts = ctx.get_write_timestamp()?;

    // Cascade-delete edges for all vertices across all edge types.
    for edge_info in &edge_types {
        if let Err(error) = batch_delete_incident_edges_of_type(ctx, space_id, &ids, edge_info, ts)
        {
            ctx.abort_write_timestamp(ts);
            return Err(error);
        }
    }

    // Delete the vertices themselves with the same timestamp, settling once.
    // The online path defers settling to the transaction commit.
    let mut deleted = 0usize;
    for (id, route) in ids.iter().zip(routed.iter()) {
        delete_vertex_with_timestamp(
            ctx,
            VertexDeleteTarget {
                space_id,
                tag_name,
                label_id,
                vid: id,
                routed: route,
            },
            ts,
            false,
        )?;
        deleted += 1;
    }
    if !ctx.is_online_write() {
        if let Err(error) = ctx.commit_write_timestamp_ordered(ts) {
            ctx.abort_write_timestamp(ts);
            return Err(error);
        }
    }
    Ok(deleted)
}

/// Batch-cascade one edge type across multiple vertices through per-table
/// batch entrances.
fn batch_delete_incident_edges_of_type(
    ctx: &GraphStorageContext,
    space_id: u64,
    ids: &[VertexId],
    edge_info: &linkrs_core::types::EdgeTypeInfo,
    ts: Timestamp,
) -> StorageResult<()> {
    let keys: Vec<EdgeTableKey> = ctx.data_store().with_edge_label_index(|index| {
        index
            .get(&edge_info.edge_type_id)
            .cloned()
            .unwrap_or_default()
    });
    for key in &keys {
        // Resolve internal IDs for all vertices in one pass.
        let vertex_rows: Vec<(Option<u32>, Option<u32>)> =
            ctx.data_store().with_vertex_tables(|vertex_tables| {
                ids.iter()
                    .map(|id| {
                        (
                            helpers::resolve_internal_id(
                                ctx,
                                vertex_tables,
                                key.src_label,
                                *id,
                                ts,
                            ),
                            helpers::resolve_internal_id(
                                ctx,
                                vertex_tables,
                                key.dst_label,
                                *id,
                                ts,
                            ),
                        )
                    })
                    .collect()
            });
        // Filter out vertices that resolve to neither direction.
        let filtered: Vec<_> = vertex_rows
            .iter()
            .filter(|(s, d)| s.is_some() || d.is_some())
            .copied()
            .collect();
        if filtered.is_empty() {
            continue;
        }
        let Some(table) = ctx.data_store().try_get_edge_table_mut(key) else {
            continue;
        };
        let deleted = table
            .write()
            .delete_incident_edges_of_vertices(&filtered, ts)?;
        if deleted.is_empty() {
            continue;
        }
        for edge in &deleted {
            let (Some(src_ext), Some(dst_ext)) = (
                ctx.get_external_id_by_internal_id(key.src_label, edge.src),
                ctx.get_external_id_by_internal_id(key.dst_label, edge.dst),
            ) else {
                return Err(StorageError::not_found(format!(
                    "deleted edge {} -> {} lost its endpoint mapping",
                    edge.src, edge.dst
                )));
            };
            let redo = DeleteEdgeRedo {
                src_label: key.src_label,
                src_vid: src_ext,
                dst_label: key.dst_label,
                dst_vid: dst_ext,
                edge_label: key.edge_label,
                rank: edge.rank,
            };
            let redo_entry = ctx.append_wal_redo(WalOpType::DeleteEdge, ts, &redo)?;
            super::edge::record_edge_remove(
                ctx,
                EdgeIdentifier::new(
                    key.src_label,
                    src_ext,
                    key.dst_label,
                    dst_ext,
                    key.edge_label,
                    edge.rank,
                ),
                edge.properties.clone(),
                Some(redo_entry),
            )?;
            let src_value = Value::from(src_ext);
            let dst_value = Value::from(dst_ext);
            let edge_identity = EdgeIdentity::new(
                space_id,
                &src_value,
                &dst_value,
                &edge_info.edge_type_name,
                edge.rank,
            );
            ctx.delete_all_edge_indexes_mvcc(&edge_identity, ts)?;
        }
        ctx.mark_edge_modified(key.edge_label);
    }
    Ok(())
}

/// Remove every incident edge of one vertex from every physical table of one
/// edge type through the table-level batch entrance.
///
/// Each touched table commits once with the shared timestamp; the per-edge
/// transaction redo, restore record and index maintenance keep the explicit
/// transaction rollback path intact. Tables holding no incident edge return
/// empty without committing.
fn delete_incident_edges_of_type(
    ctx: &GraphStorageContext,
    space_id: u64,
    id: &VertexId,
    edge_info: &linkrs_core::types::EdgeTypeInfo,
    ts: Timestamp,
) -> StorageResult<()> {
    let keys: Vec<EdgeTableKey> = ctx.data_store().with_edge_label_index(|index| {
        index
            .get(&edge_info.edge_type_id)
            .cloned()
            .unwrap_or_default()
    });
    for key in &keys {
        let (src_internal, dst_internal) = ctx.data_store().with_vertex_tables(|vertex_tables| {
            (
                helpers::resolve_internal_id(ctx, vertex_tables, key.src_label, *id, ts),
                helpers::resolve_internal_id(ctx, vertex_tables, key.dst_label, *id, ts),
            )
        });
        if src_internal.is_none() && dst_internal.is_none() {
            continue;
        }
        let Some(table) = ctx.data_store().try_get_edge_table_mut(key) else {
            continue;
        };
        let deleted =
            table
                .write()
                .delete_incident_edges_of_vertex(src_internal, dst_internal, ts)?;
        if deleted.is_empty() {
            continue;
        }
        for edge in &deleted {
            let (Some(src_ext), Some(dst_ext)) = (
                ctx.get_external_id_by_internal_id(key.src_label, edge.src),
                ctx.get_external_id_by_internal_id(key.dst_label, edge.dst),
            ) else {
                return Err(StorageError::not_found(format!(
                    "deleted edge {} -> {} lost its endpoint mapping",
                    edge.src, edge.dst
                )));
            };
            let redo = DeleteEdgeRedo {
                src_label: key.src_label,
                src_vid: src_ext,
                dst_label: key.dst_label,
                dst_vid: dst_ext,
                edge_label: key.edge_label,
                rank: edge.rank,
            };
            let redo_entry = ctx.append_wal_redo(WalOpType::DeleteEdge, ts, &redo)?;
            super::edge::record_edge_remove(
                ctx,
                EdgeIdentifier::new(
                    key.src_label,
                    src_ext,
                    key.dst_label,
                    dst_ext,
                    key.edge_label,
                    edge.rank,
                ),
                edge.properties.clone(),
                Some(redo_entry),
            )?;
            let src_value = Value::from(src_ext);
            let dst_value = Value::from(dst_ext);
            let edge_identity = EdgeIdentity::new(
                space_id,
                &src_value,
                &dst_value,
                &edge_info.edge_type_name,
                edge.rank,
            );
            ctx.delete_all_edge_indexes_mvcc(&edge_identity, ts)?;
        }
        ctx.mark_edge_modified(key.edge_label);
    }
    Ok(())
}