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

use crate::engine::{
    BatchEdgeDelete, BatchEdgeInsert, DeleteEdgesBatchParams, InsertEdgesBatchParams,
};
use crate::index::types::EdgeIdentity;
use linkrs_core::types::{EdgeIdentifier, Index, LabelId, TagInfo, Timestamp, VertexId};
use linkrs_core::wal::redo::{DeleteEdgeRedo, InsertEdgeRedo};
use linkrs_core::wal::types::WalOpType;
use linkrs_core::{DataType, Edge, EdgeDeleteKey, StorageError, StorageResult, Value};

use super::super::context::GraphStorageContext;
use super::super::ops::endpoint_label_id;

/// Recommended caller-side chunk size for very large edge batches.
///
/// One batch is one atomic unit holding the table lock for the whole apply,
/// so commit latency grows with batch size. Batches up to this size are the
/// measured regime (see `edge_group_commit_bench`); callers with larger
/// fanouts split into chunks of at most this many edges and treat each chunk
/// as its own atomic unit instead of one giant commit.
pub(crate) const RECOMMENDED_BATCH_CHUNK_EDGES: usize = 10_000;

/// Split an edge batch into caller-side chunks.
///
/// Pure slicing helper behind the chunking contract above: each returned
/// chunk commits atomically on its own, so callers keep the per-commit
/// lock-hold bounded without changing the single-writer discipline. Returns
/// one chunk per `RECOMMENDED_BATCH_CHUNK_EDGES` edges (the tail may be
/// shorter); an empty input yields no chunks.
pub(crate) fn chunk_edges_for_commit(edges: &[Edge]) -> Vec<&[Edge]> {
    if edges.is_empty() {
        return Vec::new();
    }
    edges.chunks(RECOMMENDED_BATCH_CHUNK_EDGES).collect()
}

/// Pre-resolved, per-batch schema context shared by every row of a batch
/// vertex insert: the tag table, the tag indexes, and the SERIAL state all
/// come from a single pass over the batch instead of per-row lookups.
pub(super) struct PrecheckedBatchContext<'a> {
    pub(super) tag_map: &'a HashMap<&'a str, &'a TagInfo>,
    pub(super) tag_indexes: &'a [Index],
    pub(super) serial_state: &'a mut SerialBatchState,
    pub(super) vid_type: &'a DataType,
}

#[derive(Debug)]
pub(super) struct InsertedEdgeRecord {
    pub(super) edge_label_id: LabelId,
    pub(super) src_label_id: LabelId,
    pub(super) dst_label_id: LabelId,
    pub(super) src: VertexId,
    pub(super) dst: VertexId,
    pub(super) edge_type: String,
    pub(super) rank: i64,
    pub(super) redo_entry: linkrs_transaction::wal::TransactionWalEntry,
}

/// Batch-local SERIAL validation state: one committed-column snapshot per
/// serial column plus the explicit values already accepted in this batch.
/// Later rows observe earlier batch rows exactly as the per-row rescan did
/// (same-timestamp rows are snapshot-visible), without re-scanning.
pub(super) struct SerialBatchState {
    present: HashMap<(LabelId, String), HashSet<i64>>,
    seen: HashMap<(LabelId, String), HashSet<i64>>,
}

impl SerialBatchState {
    pub(super) fn new() -> Self {
        Self {
            present: HashMap::new(),
            seen: HashMap::new(),
        }
    }

    pub(super) fn add_present(
        &mut self,
        label: LabelId,
        prop_name: &str,
        scan: super::super::serial::SerialColumnScan,
    ) {
        let present: HashSet<i64> = scan.into_present().into_iter().collect();
        self.present.insert((label, prop_name.to_string()), present);
    }

    /// Validate an explicit SERIAL value against committed data and earlier
    /// batch rows, recording it for later rows on success.
    pub(super) fn check_explicit(
        &mut self,
        ctx: &GraphStorageContext,
        space_id: u64,
        table_name: &str,
        label: LabelId,
        prop_name: &str,
        value: &Value,
    ) -> StorageResult<()> {
        let Some(integer) = super::constraints::serial_value_as_i64(value) else {
            return Ok(());
        };
        if integer < 0 {
            return Ok(());
        }
        let key = (label, prop_name.to_string());
        let duplicate = self
            .present
            .get(&key)
            .is_some_and(|present| present.contains(&integer))
            || self
                .seen
                .get(&key)
                .is_some_and(|seen| seen.contains(&integer));
        if duplicate {
            return Err(StorageError::invalid_operation(format!(
                "Duplicate value {} for SERIAL column '{}': the value is already allocated",
                integer, prop_name
            )));
        }
        self.seen.entry(key).or_default().insert(integer);
        ctx.serial_allocator().advance_to(
            &super::super::serial::SerialKey::new(space_id, table_name),
            integer as u64,
        );
        Ok(())
    }
}

pub(crate) fn batch_insert_edges(
    ctx: &GraphStorageContext,
    space: &str,
    edges: Vec<Edge>,
) -> StorageResult<()> {
    if edges.len() > RECOMMENDED_BATCH_CHUNK_EDGES {
        log::debug!(
            "batch_insert_edges holds one atomic commit for {} edges (recommended chunk <= {}); very large fanouts should chunk explicitly via GraphStorage::chunk_edges_for_commit with one chunk per atomic commit",
            edges.len(),
            RECOMMENDED_BATCH_CHUNK_EDGES,
        );
    }
    let space_info = ctx
        .schema_manager()
        .get_space(space)?
        .ok_or_else(|| StorageError::not_found(format!("Space {} not found", space)))?;

    validate_edge_batch(ctx, space, &edges)?;

    let ts = ctx.get_write_timestamp()?;
    let mut rollback = Vec::new();
    if let Err(e) = batch_insert_grouped(ctx, space, space_info.space_id, &edges, ts, &mut rollback)
    {
        super::edge::rollback_edges(ctx, space_info.space_id, &rollback, ts);
        ctx.abort_write_timestamp(ts);
        return Err(e);
    }

    for item in &rollback {
        if let Err(e) = super::edge::record_edge_insert(
            ctx,
            EdgeIdentifier::new(
                item.src_label_id,
                item.src,
                item.dst_label_id,
                item.dst,
                item.edge_label_id,
                item.rank,
            ),
            Some(item.redo_entry.clone()),
        ) {
            super::edge::rollback_edges(ctx, space_info.space_id, &rollback, ts);
            ctx.abort_write_timestamp(ts);
            return Err(e);
        }
    }

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

    Ok(())
}

/// Insert a batch grouped by edge type in first-seen order.
///
/// Each type resolves its schema once, appends one redo per edge in group
/// order, commits the whole type through a single staging batch, then runs
/// the per-edge index updates in the same order. A failure rolls back every
/// previously applied edge through the shared rollback path, matching the
/// visible-state effects of the sequential loop this replaces.
fn batch_insert_grouped(
    ctx: &GraphStorageContext,
    space: &str,
    space_id: u64,
    edges: &[Edge],
    ts: Timestamp,
    rollback: &mut Vec<InsertedEdgeRecord>,
) -> StorageResult<()> {
    let mut order: Vec<String> = Vec::new();
    let mut by_type: std::collections::HashMap<String, Vec<usize>> =
        std::collections::HashMap::new();
    for (index, edge) in edges.iter().enumerate() {
        by_type
            .entry(edge.edge_type.clone())
            .or_insert_with(|| {
                order.push(edge.edge_type.clone());
                Vec::new()
            })
            .push(index);
    }
    for edge_type_name in &order {
        let positions = by_type
            .get(edge_type_name)
            .ok_or_else(|| StorageError::db_error("batch type group missing".to_string()))?;
        insert_one_type_batch(ctx, space, space_id, edges, positions, ts, rollback)?;
    }
    Ok(())
}

fn insert_one_type_batch(
    ctx: &GraphStorageContext,
    space: &str,
    space_id: u64,
    edges: &[Edge],
    positions: &[usize],
    ts: Timestamp,
    rollback: &mut Vec<InsertedEdgeRecord>,
) -> StorageResult<()> {
    let first = &edges[positions[0]];
    let edge_type = super::edge::resolve_edge_type(ctx, space, &first.edge_type)?;
    let edge_label_id = edge_type.edge_type_id;
    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 mut prepared: Vec<(Vec<(Arc<str>, Value)>, usize)> = Vec::with_capacity(positions.len());
    for &index in positions {
        let edge = &edges[index];
        let props: Vec<(Arc<str>, Value)> = edge
            .props
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let props =
            super::constraints::apply_edge_type_constraints(ctx, space, &edge.edge_type, props)?;
        prepared.push((props, index));
    }

    let mut redo_entries = Vec::with_capacity(prepared.len());
    for (props, index) in &prepared {
        let edge = &edges[*index];
        let redo = InsertEdgeRedo {
            src_label: src_label_id,
            src_vid: edge.src,
            dst_label: dst_label_id,
            dst_vid: edge.dst,
            edge_label: edge_label_id,
            rank: edge.ranking,
            properties: props.clone(),
        };
        redo_entries.push(ctx.append_wal_redo(WalOpType::InsertEdge, ts, &redo)?);
    }

    let batch_params: Vec<BatchEdgeInsert> = prepared
        .iter()
        .map(|(props, index)| {
            let edge = &edges[*index];
            BatchEdgeInsert {
                src_id: edge.src,
                dst_id: edge.dst,
                rank: edge.ranking,
                properties: props,
            }
        })
        .collect();
    ctx.insert_edges_batch(InsertEdgesBatchParams {
        edge_label: edge_label_id,
        src_label: src_label_id,
        dst_label: dst_label_id,
        edges: &batch_params,
        ts,
    })?;

    for (position, index) in prepared.iter().map(|(_, index)| index).enumerate() {
        let edge = &edges[*index];
        rollback.push(InsertedEdgeRecord {
            edge_label_id,
            src_label_id,
            dst_label_id,
            src: edge.src,
            dst: edge.dst,
            edge_type: edge.edge_type.clone(),
            rank: edge.ranking,
            redo_entry: redo_entries[position].clone(),
        });
    }

    for (props, index) in &prepared {
        let edge = &edges[*index];
        let src_value = Value::from(edge.src);
        let dst_value = Value::from(edge.dst);
        let edge_identity = EdgeIdentity::new(
            space_id,
            &src_value,
            &dst_value,
            &edge.edge_type,
            edge.ranking,
        );
        ctx.update_all_edge_indexes_mvcc(&edge_identity, props, ts)?;
    }

    Ok(())
}

fn validate_edge_batch(
    ctx: &GraphStorageContext,
    space: &str,
    edges: &[Edge],
) -> StorageResult<()> {
    for edge in edges {
        let edge_type = super::edge::resolve_edge_type(ctx, space, &edge.edge_type)?;
        if endpoint_label_id(ctx, space, &edge_type.src_tag_name)?.is_none() {
            return Err(StorageError::not_found(format!(
                "Source tag {} not found",
                edge_type.src_tag_name
            )));
        }
        if endpoint_label_id(ctx, space, &edge_type.dst_tag_name)?.is_none() {
            return Err(StorageError::not_found(format!(
                "Destination tag {} not found",
                edge_type.dst_tag_name
            )));
        }
    }
    Ok(())
}

/// Delete a batch of edges by key under one write timestamp.
///
/// Mirrors `batch_insert_edges`: one timestamp covers the whole batch, each
/// edge type resolves its schema once, one redo per key precedes one staging
/// commit per owner partition, then per-edge index cleanup and undo records
/// run for the keys that prefetched live state. Keys never created delete
/// nothing and consume no tombstone; re-deleting an already-deleted edge
/// fails the batch exactly like the single delete. A failure aborts
/// the timestamp with prior groups already committed; aborted stamps stay
/// hidden through the pending gate, matching the vertex-batch contract.
/// Returns the number of net applied deletes.
pub(crate) fn batch_delete_edges(
    ctx: &GraphStorageContext,
    space: &str,
    deletes: &[EdgeDeleteKey],
) -> StorageResult<usize> {
    if deletes.is_empty() {
        return Ok(0);
    }
    let space_id = ctx.schema_manager().get_space_id(space)?;
    for key in deletes {
        let edge_type = super::edge::resolve_edge_type(ctx, space, &key.edge_type)?;
        if endpoint_label_id(ctx, space, &edge_type.src_tag_name)?.is_none() {
            return Err(StorageError::not_found(format!(
                "Source tag {} not found",
                edge_type.src_tag_name
            )));
        }
        if endpoint_label_id(ctx, space, &edge_type.dst_tag_name)?.is_none() {
            return Err(StorageError::not_found(format!(
                "Destination tag {} not found",
                edge_type.dst_tag_name
            )));
        }
    }
    // Prefetch previous properties for the undo records, mirroring the
    // single-delete pre-delete read. Missing edges prefetch to `None` and
    // are skipped below with no further effects.
    let mut previous = Vec::with_capacity(deletes.len());
    for key in deletes {
        previous.push(super::super::reader::get_edge(
            ctx,
            space,
            &key.src,
            &key.dst,
            &key.edge_type,
            key.ranking,
        )?);
    }

    let ts = ctx.get_write_timestamp()?;
    let mut deleted = 0usize;
    if let Err(e) = batch_delete_grouped(ctx, space, space_id, deletes, &previous, ts, &mut deleted)
    {
        ctx.abort_write_timestamp(ts);
        return Err(e);
    }

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

    Ok(deleted)
}

/// Delete a batch grouped by edge type in first-seen order.
///
/// Each type resolves its schema once, appends one redo per existing edge in
/// group order, commits the whole type through one staging commit per owner
/// partition, then runs the per-edge index cleanup and undo recording in the
/// same order.
fn batch_delete_grouped(
    ctx: &GraphStorageContext,
    space: &str,
    space_id: u64,
    deletes: &[EdgeDeleteKey],
    previous: &[Option<Edge>],
    ts: Timestamp,
    deleted: &mut usize,
) -> StorageResult<()> {
    let mut order: Vec<String> = Vec::new();
    let mut by_type: std::collections::HashMap<String, Vec<usize>> =
        std::collections::HashMap::new();
    for (index, key) in deletes.iter().enumerate() {
        by_type
            .entry(key.edge_type.clone())
            .or_insert_with(|| {
                order.push(key.edge_type.clone());
                Vec::new()
            })
            .push(index);
    }
    for edge_type_name in &order {
        let positions = by_type
            .get(edge_type_name)
            .ok_or_else(|| StorageError::db_error("batch type group missing".to_string()))?;
        *deleted += delete_one_type_batch(ctx, space, space_id, deletes, positions, previous, ts)?;
    }
    Ok(())
}

fn delete_one_type_batch(
    ctx: &GraphStorageContext,
    space: &str,
    space_id: u64,
    deletes: &[EdgeDeleteKey],
    positions: &[usize],
    previous: &[Option<Edge>],
    ts: Timestamp,
) -> StorageResult<usize> {
    let first = &deletes[positions[0]];
    let edge_type = super::edge::resolve_edge_type(ctx, space, &first.edge_type)?;
    let edge_label_id = edge_type.edge_type_id;
    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 mut redo_entries = Vec::with_capacity(positions.len());
    // Redo precedes the table commit for every key, mirroring the single
    // delete: replay of a delete matching nothing is a no-op, while
    // re-deleting an already-deleted edge fails in the table batch below
    // exactly like the single path instead of reporting silent success.
    for &index in positions {
        let key = &deletes[index];
        let redo = DeleteEdgeRedo {
            src_label: src_label_id,
            src_vid: key.src,
            dst_label: dst_label_id,
            dst_vid: key.dst,
            edge_label: edge_label_id,
            rank: key.ranking,
        };
        redo_entries.push(ctx.append_wal_redo(WalOpType::DeleteEdge, ts, &redo)?);
    }

    let batch_params: Vec<BatchEdgeDelete> = positions
        .iter()
        .map(|&index| {
            let key = &deletes[index];
            BatchEdgeDelete {
                src_id: key.src,
                dst_id: key.dst,
                rank: key.ranking,
            }
        })
        .collect();
    let applied = ctx.delete_edges_batch(DeleteEdgesBatchParams {
        edge_label: edge_label_id,
        src_label: src_label_id,
        dst_label: dst_label_id,
        edges: &batch_params,
        ts,
    })?;

    for (position, &index) in positions.iter().enumerate() {
        if previous[index].is_none() {
            continue;
        }
        let key = &deletes[index];
        let src_value = Value::from(key.src);
        let dst_value = Value::from(key.dst);
        let edge_identity = EdgeIdentity::new(
            space_id,
            &src_value,
            &dst_value,
            &key.edge_type,
            key.ranking,
        );
        ctx.delete_all_edge_indexes_mvcc(&edge_identity, ts)?;
        let props = previous[index]
            .as_ref()
            .map(|edge| {
                edge.props
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect()
            })
            .unwrap_or_default();
        super::edge::record_edge_remove(
            ctx,
            EdgeIdentifier::new(
                src_label_id,
                key.src,
                dst_label_id,
                key.dst,
                edge_label_id,
                key.ranking,
            ),
            props,
            Some(redo_entries[position].clone()),
        )?;
    }

    Ok(applied)
}