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
use std::collections::HashMap;
use std::sync::Arc;

use crate::engine::params::{EdgeOperationParams, InsertEdgeParams};
use crate::index::types::EdgeIdentity;
use linkrs_core::types::{
    EdgeIdentifier, InsertEdgeInfo, InsertVertexInfo, UpdateInfo, UpdateOp, UpdateTarget, VertexId,
};
use linkrs_core::wal::redo::{DeleteEdgeRedo, InsertEdgeRedo, InsertVertexRedo};
use linkrs_core::wal::types::WalOpType;
use linkrs_core::{StorageError, StorageResult, Value};

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 super::super::reader;

/// Parse a user supplied external id without silent normalization.
///
/// Numeric strings become integer ids through the rejecting constructor so
/// negatives fail here. Negative numeric strings fall back to text and are
/// resolved by space normalization. Overlong text fails instead of
/// truncating.
fn parse_user_vertex_id(id: &str) -> StorageResult<VertexId> {
    if let Ok(parsed) = id.parse::<i64>() {
        if let Ok(vid) = VertexId::try_from_int64(parsed) {
            return Ok(vid);
        }
    }
    VertexId::try_from_string(id).map_err(StorageError::invalid_input)
}

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

    let tag = ctx
        .schema_manager()
        .get_tag(space, &info.tag_name)?
        .ok_or_else(|| StorageError::not_found(format!("Tag {} not found", info.tag_name)))?;

    if info.space_id != space_info.space_id {
        return Err(StorageError::db_error("Space ID mismatch".to_string()));
    }

    let ts = ctx.get_write_timestamp()?;

    let label_id = tag.tag_id;
    let vid = VertexId::try_from(&info.vertex_id)
        .map_err(|e| StorageError::invalid_input(e.to_string()))?;
    let vid = VertexId::normalize_for_vid_type(&space_info.vid_type, vid)?;

    let props =
        super::constraints::apply_tag_constraints(ctx, space, &info.tag_name, info.props.clone())?;
    let redo = InsertVertexRedo {
        label: label_id,
        vid,
        properties: props.clone(),
    };
    let redo_entry = ctx.append_wal_redo(WalOpType::InsertVertex, ts, &redo)?;

    // Staging scope path shared with `insert_vertex`: stage, apply, then
    // indexes and the transaction record before the timestamp publishes.
    // A duplicate key surfaces from the apply-time primary-key recheck; the
    // IF-NOT-EXISTS contract maps it to `false`, with nothing applied.
    let mut scope = crate::vertex::WriteScope::new(ts);
    let key = match route_vertex_id(&vid)? {
        RoutedVertexId::Int(id_int) => {
            ctx.insert_vertex_by_i64_with_scope(label_id, id_int, &props, ts, &mut scope)?;
            crate::vertex::IdKey::Int(id_int)
        }
        RoutedVertexId::Text(id_str) => {
            ctx.insert_vertex_with_scope(label_id, &id_str, &props, ts, &mut scope)?;
            crate::vertex::IdKey::Text(id_str)
        }
    };

    // Online: the row is held in the transaction staging buffer and applied
    // at the commit point. IF-NOT-EXISTS against a row already visible to
    // this statement is a false with nothing staged.
    if ctx.is_online_write() {
        let exists = match &key {
            crate::vertex::IdKey::Int(id_int) => {
                ctx.get_vertex_by_i64(label_id, *id_int, ts).is_some()
            }
            crate::vertex::IdKey::Text(id_str) => ctx.get_vertex(label_id, id_str, ts).is_some(),
        };
        if exists {
            let dropped = scope.rollback_label(label_id);
            ctx.release_staged_reservations(
                &dropped
                    .into_iter()
                    .map(|id| (label_id, id))
                    .collect::<Vec<_>>(),
            );
            ctx.commit_write_timestamp_ordered(ts)?;
            return Ok(false);
        }
        let (buffer, mark) = match ctx.txn_staging_mark(ts) {
            Ok(mark) => mark,
            Err(error) => {
                ctx.abort_write_timestamp(ts);
                return Err(error);
            }
        };
        let outcome = ctx
            .absorb_write_scope(&mut scope, ts)
            .and_then(|()| super::vertex::record_vertex_insert(ctx, vid, Some(redo_entry)))
            .and_then(|()| {
                ctx.stage_vertex_index_op(
                    ts,
                    StagedIndexOp::Insert {
                        space_id: space_info.space_id,
                        vid: info.vertex_id.clone(),
                        tag: info.tag_name.clone(),
                        properties: props,
                    },
                )
            });
        if let Err(error) = outcome {
            ctx.rollback_staging_to(&buffer, mark);
            ctx.abort_write_timestamp(ts);
            return Err(error);
        }
        return Ok(true);
    }

    let internal_id = match ctx.commit_write_scope(label_id, &mut scope, ts) {
        Ok(mapping) => match mapping.into_iter().find(|(k, _)| *k == key) {
            Some((_, global_id)) => global_id,
            None => {
                scope.clear();
                ctx.abort_write_timestamp(ts);
                return Err(StorageError::db_error(format!(
                    "commit apply lost staged vertex {:?}",
                    vid
                )));
            }
        },
        Err(error)
            if error.kind()
                == linkrs_core::error::storage::StorageErrorKind::VertexAlreadyExists =>
        {
            scope.clear();
            ctx.commit_write_timestamp_ordered(ts)?;
            return Ok(false);
        }
        Err(error) => {
            scope.clear();
            ctx.abort_write_timestamp(ts);
            return Err(error);
        }
    };
    debug_assert!(scope.is_empty());

    if let Err(error) = super::index_maintenance::update_vertex_indexes(
        ctx,
        ctx.index_metadata_manager(),
        space_info.space_id,
        &info.vertex_id,
        &info.tag_name,
        &props,
        ts,
    ) {
        let _ = super::index_maintenance::delete_vertex_indexes(
            ctx,
            ctx.index_metadata_manager(),
            space_info.space_id,
            &info.vertex_id,
            &info.tag_name,
            ts,
        );
        ctx.undo_applied_scope_inserts(label_id, &[internal_id]);
        ctx.abort_write_timestamp(ts);
        return Err(error);
    }
    if let Err(error) = super::vertex::record_vertex_insert(ctx, vid, Some(redo_entry)) {
        let _ = super::index_maintenance::delete_vertex_indexes(
            ctx,
            ctx.index_metadata_manager(),
            space_info.space_id,
            &info.vertex_id,
            &info.tag_name,
            ts,
        );
        ctx.undo_applied_scope_inserts(label_id, &[internal_id]);
        ctx.abort_write_timestamp(ts);
        return Err(error);
    }
    match &key {
        crate::vertex::IdKey::Int(id_int) => {
            ctx.cache_inserted_vertex_id(label_id, &id_int.to_string(), internal_id, ts);
            ctx.mark_vertex_modified(label_id);
            ctx.observe_vertex_id_i64(label_id, *id_int);
        }
        crate::vertex::IdKey::Text(id_str) => {
            ctx.cache_inserted_vertex_id(label_id, id_str, internal_id, ts);
            ctx.mark_vertex_modified(label_id);
            ctx.observe_vertex_id_string(label_id);
        }
    }
    ctx.commit_write_timestamp_ordered(ts)?;
    Ok(true)
}

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

    let edge_type = ctx
        .schema_manager()
        .get_edge_type(space, &info.edge_name)?
        .ok_or_else(|| {
            StorageError::not_found(format!("Edge type {} not found", info.edge_name))
        })?;

    if info.space_id != space_info.space_id {
        return Err(StorageError::db_error("Space ID mismatch".to_string()));
    }

    let ts = ctx.get_write_timestamp()?;

    let edge_label_id = edge_type.edge_type_id;
    let src_vid = VertexId::try_from(&info.src_vertex_id)
        .map_err(|e| StorageError::invalid_input(e.to_string()))?;
    let src_vid = VertexId::normalize_for_vid_type(&space_info.vid_type, src_vid)?;
    let dst_vid = VertexId::try_from(&info.dst_vertex_id)
        .map_err(|e| StorageError::invalid_input(e.to_string()))?;
    let dst_vid = VertexId::normalize_for_vid_type(&space_info.vid_type, dst_vid)?;
    let src_label_id =
        endpoint_label_id(ctx, space, &edge_type.src_tag_name)?.ok_or_else(|| {
            StorageError::not_found(format!("Source tag {} not found", edge_type.src_tag_name))
        })?;
    let dst_label_id =
        endpoint_label_id(ctx, space, &edge_type.dst_tag_name)?.ok_or_else(|| {
            StorageError::not_found(format!(
                "Destination tag {} not found",
                edge_type.dst_tag_name
            ))
        })?;
    let props = super::constraints::apply_edge_type_constraints(
        ctx,
        space,
        &info.edge_name,
        info.props.clone(),
    )?;
    let redo = InsertEdgeRedo {
        src_label: src_label_id,
        src_vid,
        dst_label: dst_label_id,
        dst_vid,
        edge_label: edge_label_id,
        rank: info.rank,
        properties: props.clone(),
    };
    let redo_entry = ctx.append_wal_redo(WalOpType::InsertEdge, ts, &redo)?;
    let result = ctx.insert_edge(InsertEdgeParams {
        edge_label: edge_label_id,
        src_label: src_label_id,
        src_id: src_vid,
        dst_label: dst_label_id,
        dst_id: dst_vid,
        rank: info.rank,
        properties: &props,
        ts,
    });

    let final_result = match result {
        Ok(_) => {
            let src_value = Value::from(src_vid);
            let dst_value = Value::from(dst_vid);
            let edge_identity = EdgeIdentity::new(
                space_info.space_id,
                &src_value,
                &dst_value,
                &info.edge_name,
                info.rank,
            );
            match ctx.update_all_edge_indexes_mvcc(&edge_identity, &props, ts) {
                Ok(()) => {
                    super::edge::record_edge_insert(
                        ctx,
                        EdgeIdentifier::new(
                            src_label_id,
                            src_vid,
                            dst_label_id,
                            dst_vid,
                            edge_label_id,
                            info.rank,
                        ),
                        Some(redo_entry),
                    )?;
                    Ok(true)
                }
                Err(error) => {
                    let _ = ctx.delete_edge(
                        &EdgeOperationParams {
                            edge_label: edge_label_id,
                            src_label: src_label_id,
                            src_id: src_vid,
                            dst_label: dst_label_id,
                            dst_id: dst_vid,
                            rank: info.rank,
                        },
                        ts,
                    );
                    Err(error)
                }
            }
        }
        Err(ref e)
            if e.kind() == linkrs_core::error::storage::StorageErrorKind::EdgeAlreadyExists =>
        {
            Ok(false)
        }
        Err(e) => Err(e),
    };
    // Explicit transactions settle once at their commit point.
    if ctx.is_online_write() {
        return final_result;
    }
    if final_result.is_ok() {
        ctx.commit_write_timestamp_ordered(ts)?;
    } else {
        ctx.abort_write_timestamp(ts);
    }
    final_result
}

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

    let raw = parse_user_vertex_id(vertex_id)?;
    let vid = VertexId::normalize_for_vid_type(&space_info.vid_type, raw)?;

    super::vertex_delete::delete_vertex(ctx, space, tag, &vid)?;
    Ok(true)
}

pub(crate) fn delete_edge_data(
    ctx: &GraphStorageContext,
    space: &str,
    src: &str,
    dst: &str,
    rank: i64,
) -> StorageResult<bool> {
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;
    let space_id = space_info.space_id;
    let edge_types = ctx.schema_manager().list_edge_types(space)?;
    let ts = ctx.get_write_timestamp()?;
    let mut deleted = false;

    for et in edge_types {
        let edge_label_id = et.edge_type_id;
        let src_label_id = match endpoint_label_id(ctx, space, &et.src_tag_name)? {
            Some(id) => id,
            None => continue,
        };
        let dst_label_id = match endpoint_label_id(ctx, space, &et.dst_tag_name)? {
            Some(id) => id,
            None => continue,
        };
        let src_vid = parse_user_vertex_id(src)?;
        let src_vid = VertexId::normalize_for_vid_type(&space_info.vid_type, src_vid)?;
        let dst_vid = parse_user_vertex_id(dst)?;
        let dst_vid = VertexId::normalize_for_vid_type(&space_info.vid_type, dst_vid)?;
        let previous = reader::get_edge(ctx, space, &src_vid, &dst_vid, &et.edge_type_name, rank)?;
        let redo_entry = previous
            .as_ref()
            .map(|_| {
                let redo = DeleteEdgeRedo {
                    src_label: src_label_id,
                    src_vid,
                    dst_label: dst_label_id,
                    dst_vid,
                    edge_label: edge_label_id,
                    rank,
                };
                ctx.append_wal_redo(WalOpType::DeleteEdge, ts, &redo)
            })
            .transpose()?;
        if ctx
            .delete_edge(
                &EdgeOperationParams {
                    edge_label: edge_label_id,
                    src_label: src_label_id,
                    src_id: src_vid,
                    dst_label: dst_label_id,
                    dst_id: dst_vid,
                    rank,
                },
                ts,
            )
            .is_ok_and(|deleted_edge| deleted_edge)
        {
            if let Some(previous) = previous {
                super::edge::record_edge_remove(
                    ctx,
                    EdgeIdentifier::new(
                        src_label_id,
                        src_vid,
                        dst_label_id,
                        dst_vid,
                        edge_label_id,
                        rank,
                    ),
                    previous.props.into_iter().collect(),
                    redo_entry,
                )?;
            }
            let src_value = Value::from(src_vid);
            let dst_value = Value::from(dst_vid);
            let edge_identity =
                EdgeIdentity::new(space_id, &src_value, &dst_value, &et.edge_type_name, rank);
            ctx.delete_all_edge_indexes_mvcc(&edge_identity, ts)?;
            deleted = true;
        }
    }

    // Explicit transactions settle once at their commit point.
    if ctx.is_online_write() {
        return Ok(deleted);
    }
    ctx.commit_write_timestamp_ordered(ts)?;

    Ok(deleted)
}

pub(crate) fn update_data(
    ctx: &GraphStorageContext,
    space: &str,
    space_id: u64,
    info: &UpdateInfo,
) -> StorageResult<bool> {
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;

    if space_info.space_id != space_id {
        return Err(StorageError::db_error("Space ID mismatch".to_string()));
    }

    let ts = ctx.get_write_timestamp()?;

    let UpdateTarget {
        space_name,
        label,
        id,
        prop,
    } = &info.update_target;

    if space_name != space {
        return Err(StorageError::db_error(
            "Space name mismatch in update target".to_string(),
        ));
    }

    if let Some(label_id) = tag_label_id(ctx, space, label)? {
        let vid = VertexId::try_from(id).map_err(|e| StorageError::invalid_input(e.to_string()))?;
        let vid = VertexId::normalize_for_vid_type(&space_info.vid_type, vid)?;
        let routed = super::super::ops::route_vertex_id(&vid)?;
        let current_record = match &routed {
            super::super::ops::RoutedVertexId::Int(id_int) => {
                ctx.get_vertex_by_i64(label_id, *id_int, ts)
            }
            super::super::ops::RoutedVertexId::Text(id_str) => ctx.get_vertex(label_id, id_str, ts),
        };
        let value = match &info.update_op {
            UpdateOp::Set => info.value.clone(),
            UpdateOp::Add => {
                if let Some(current) = current_record.as_ref() {
                    let current_val = current
                        .properties
                        .iter()
                        .find(|(k, _)| k == prop)
                        .map(|(_, v)| v);
                    if let (Some(linkrs_core::Value::Int(cv)), linkrs_core::Value::Int(add_val)) =
                        (current_val, &info.value)
                    {
                        linkrs_core::Value::Int(cv + add_val)
                    } else {
                        info.value.clone()
                    }
                } else {
                    info.value.clone()
                }
            }
            UpdateOp::Subtract => {
                if let Some(current) = current_record.as_ref() {
                    let current_val = current
                        .properties
                        .iter()
                        .find(|(k, _)| k == prop)
                        .map(|(_, v)| v);
                    if let (Some(linkrs_core::Value::Int(cv)), linkrs_core::Value::Int(sub_val)) =
                        (current_val, &info.value)
                    {
                        linkrs_core::Value::Int(cv - sub_val)
                    } else {
                        info.value.clone()
                    }
                } else {
                    info.value.clone()
                }
            }
            _ => info.value.clone(),
        };

        // Online: the property update buffers in the transaction staging
        // hold; a statement failure unwinds it back to the mark.
        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 update_result = match &routed {
            super::super::ops::RoutedVertexId::Int(id_int) => {
                ctx.update_vertex_property_by_i64(label_id, *id_int, prop, &value, ts)
            }
            super::super::ops::RoutedVertexId::Text(id_str) => {
                ctx.update_vertex_property(label_id, id_str, prop, &value, ts)
            }
        };
        if let Err(error) = update_result {
            return Err(unwind(error));
        }
        if let Err(error) = super::vertex::record_vertex_property_update(ctx, vid, None) {
            return Err(unwind(error));
        }

        let mut merged_props: HashMap<Arc<str>, Value> = current_record
            .as_ref()
            .map(|record| record.properties.iter().cloned().collect())
            .unwrap_or_default();
        merged_props.insert(prop.clone(), value);
        let merged: Vec<(Arc<str>, Value)> = merged_props.into_iter().collect();

        if online {
            // Index refresh replays at commit apply, after the new bytes land.
            if let Err(error) = ctx.stage_vertex_index_op(
                ts,
                StagedIndexOp::Update {
                    space_id,
                    vid: Value::from(vid),
                    tag: label.clone(),
                    properties: merged,
                },
            ) {
                return Err(unwind(error));
            }
            return Ok(true);
        }

        if let Err(error) = super::index_maintenance::refresh_vertex_indexes(
            ctx,
            ctx.index_metadata_manager(),
            space_info.space_id,
            &Value::from(vid),
            label,
            &merged,
            ts,
        ) {
            return Err(unwind(error));
        }
        ctx.commit_write_timestamp_ordered(ts)?;
        Ok(true)
    } else {
        ctx.abort_write_timestamp(ts);
        Err(StorageError::not_found(format!(
            "Label {} not found",
            label
        )))
    }
}