selene-db-graph 1.3.0

In-memory property-graph storage core (ArcSwap + imbl CoW, label/typed indexes, write funnel) for selene-db.
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
use selene_core::{
    Change, GraphId, LabelSet, PropertyMap, PropertyValueType, SchemaChange, db_string,
};

use crate::{
    DropBehavior, EdgeEndpointDef, EdgeTypeDef, GraphError, GraphTypeDef, NodeTypeDef,
    PropertyTypeDef, SharedGraph, ValidationMode,
};

fn closed_empty_graph(id: u64) -> SharedGraph {
    SharedGraph::builder(GraphId::new(id))
        .bound_to(GraphTypeDef {
            name: db_string("catalog.empty").unwrap(),
            node_types: Vec::new(),
            edge_types: Vec::new(),
        })
        .unwrap()
        .build()
        .unwrap()
}

fn person_type() -> GraphTypeDef {
    let person = db_string("Person").unwrap();
    GraphTypeDef {
        name: db_string("catalog.person.graph").unwrap(),
        node_types: vec![NodeTypeDef {
            name: person.clone(),
            key_labels: LabelSet::single(person),
            properties: Vec::new(),
            validation_mode: ValidationMode::Strict,
        }],
        edge_types: Vec::new(),
    }
}

fn person_company_type() -> GraphTypeDef {
    let person = db_string("Person").unwrap();
    let company = db_string("Company").unwrap();
    let works_at = db_string("WORKS_AT").unwrap();
    GraphTypeDef {
        name: db_string("catalog.company.graph").unwrap(),
        node_types: vec![
            NodeTypeDef {
                name: person.clone(),
                key_labels: LabelSet::single(person),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            },
            NodeTypeDef {
                name: company.clone(),
                key_labels: LabelSet::single(company),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            },
        ],
        edge_types: vec![EdgeTypeDef {
            name: works_at.clone(),
            label: works_at,
            source_node_type: EdgeEndpointDef::NodeType(0),
            target_node_type: EdgeEndpointDef::NodeType(1),
            properties: Vec::new(),
            validation_mode: ValidationMode::Strict,
        }],
    }
}

#[test]
fn create_node_type_updates_bound_type_and_emits_schema_change() {
    let shared = closed_empty_graph(10);
    let person = db_string("Person").unwrap();
    let name = db_string("name").unwrap();
    let outcome = {
        let mut txn = shared.begin_write();
        {
            let mut mutator = txn.mutator();
            mutator
                .create_node_type(
                    person.clone(),
                    LabelSet::single(person.clone()),
                    vec![PropertyTypeDef {
                        name,
                        value_type: PropertyValueType::String,
                        list_element_type: None,
                        required: true,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: None,
                    }],
                    ValidationMode::Strict,
                )
                .unwrap();
            assert_eq!(
                mutator
                    .read()
                    .meta
                    .bound_type
                    .as_ref()
                    .unwrap()
                    .node_types
                    .len(),
                1
            );
        }
        txn.commit().unwrap()
    };

    let graph_type = shared.graph_type().unwrap();
    assert_eq!(graph_type.node_types[0].name, person);
    assert!(matches!(
        outcome.changes.as_slice(),
        [Change::SchemaChanged {
            change: SchemaChange::NodeTypeAddedV2 { label, .. },
            ..
        }] if *label == person
    ));
}

#[test]
fn create_edge_type_resolves_closed_type_and_emits_schema_change() {
    let shared = SharedGraph::builder(GraphId::new(11))
        .bound_to(person_type())
        .unwrap()
        .build()
        .unwrap();
    let knows = db_string("KNOWS").unwrap();
    let outcome = {
        let mut txn = shared.begin_write();
        txn.mutator()
            .create_edge_type(
                knows.clone(),
                knows.clone(),
                EdgeEndpointDef::NodeType(0),
                EdgeEndpointDef::NodeType(0),
                Vec::new(),
                ValidationMode::Strict,
            )
            .unwrap();
        txn.commit().unwrap()
    };

    let graph_type = shared.graph_type().unwrap();
    assert_eq!(graph_type.edge_types[0].name, knows);
    assert!(matches!(
        outcome.changes.as_slice(),
        [Change::SchemaChanged {
            change: SchemaChange::EdgeTypeAddedV2 { label, .. },
            ..
        }] if *label == knows
    ));
}

#[test]
fn drop_node_type_refuses_endpoint_reindexing() {
    let shared = SharedGraph::builder(GraphId::new(12))
        .bound_to(person_company_type())
        .unwrap()
        .build()
        .unwrap();
    let mut txn = shared.begin_write();
    let err = txn
        .mutator()
        .drop_node_type(db_string("Person").unwrap(), DropBehavior::Restrict)
        .unwrap_err();

    // Person is referenced directly by WORKS_AT's source endpoint, so the
    // dependency check rejects with the "still references it" message before the
    // broader reindexing guard runs.
    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("still references it")
    ));
}

#[test]
fn drop_edge_type_removes_type_and_emits_schema_change() {
    let shared = SharedGraph::builder(GraphId::new(13))
        .bound_to(person_company_type())
        .unwrap()
        .build()
        .unwrap();
    let works_at = db_string("WORKS_AT").unwrap();
    let outcome = {
        let mut txn = shared.begin_write();
        txn.mutator()
            .drop_edge_type(works_at.clone(), DropBehavior::Restrict)
            .unwrap();
        txn.commit().unwrap()
    };

    assert!(shared.graph_type().unwrap().edge_types.is_empty());
    assert!(matches!(
        outcome.changes.as_slice(),
        [Change::SchemaChanged {
            change: SchemaChange::EdgeTypeDropped { name, .. },
            ..
        }] if *name == works_at
    ));
}

#[test]
fn catalog_type_ddl_on_open_graph_is_rejected() {
    let shared = SharedGraph::new(GraphId::new(14));
    let mut txn = shared.begin_write();
    let person = db_string("Person").unwrap();
    let err = txn
        .mutator()
        .create_node_type(
            person.clone(),
            LabelSet::single(person),
            Vec::new(),
            ValidationMode::Strict,
        )
        .unwrap_err();

    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("open graph (GG01) does not support catalog type DDL")
    ));
}

#[test]
fn drop_node_type_restrict_rejects_early_with_surviving_instances() {
    // Seam-B fix (audit Item 3): RESTRICT default rejects at the drop op itself
    // — not late at commit with a mislabelled UnknownNodeLabel — and leaves both
    // the type and its instances intact (no partial state).
    let shared = SharedGraph::builder(GraphId::new(15))
        .bound_to(person_type())
        .unwrap()
        .build()
        .unwrap();
    {
        let mut txn = shared.begin_write();
        txn.mutator()
            .create_node(
                LabelSet::single(db_string("Person").unwrap()),
                PropertyMap::new(),
            )
            .unwrap();
        txn.commit().unwrap();
    }

    let mut txn = shared.begin_write();
    let err = txn
        .mutator()
        .drop_node_type(db_string("Person").unwrap(), DropBehavior::Restrict)
        .expect_err("RESTRICT rejects the drop op itself");
    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("1 instance(s) still exist") && reason.contains("CASCADE")
    ));
    drop(txn);

    // Nothing dropped, nothing removed.
    assert_eq!(shared.graph_type().unwrap().node_types.len(), 1);
    assert_eq!(shared.read().node_count(), 1);
}

#[test]
fn drop_node_type_cascade_truncates_then_drops_in_one_txn() {
    let shared = SharedGraph::builder(GraphId::new(150))
        .bound_to(person_type())
        .unwrap()
        .build()
        .unwrap();
    {
        let mut txn = shared.begin_write();
        txn.mutator()
            .create_node(
                LabelSet::single(db_string("Person").unwrap()),
                PropertyMap::new(),
            )
            .unwrap();
        txn.commit().unwrap();
    }

    let person = db_string("Person").unwrap();
    let outcome = {
        let mut txn = shared.begin_write();
        txn.mutator()
            .drop_node_type(person.clone(), DropBehavior::Cascade)
            .expect("CASCADE drops a type with surviving instances");
        txn.commit().unwrap()
    };

    // Both the truncate change and the schema drop landed in ONE committed
    // changeset, in order.
    assert!(matches!(
        outcome.changes.as_slice(),
        [
            Change::NodesOfTypeTruncated { label },
            Change::SchemaChanged {
                change: SchemaChange::NodeTypeDropped { name, .. },
                ..
            },
        ] if *label == person && *name == person
    ));
    assert!(shared.graph_type().unwrap().node_types.is_empty());
    assert_eq!(shared.read().node_count(), 0);
}

#[test]
fn drop_edge_type_restrict_rejects_early_with_surviving_instances() {
    let shared = SharedGraph::builder(GraphId::new(151))
        .bound_to(person_self_knows_type())
        .unwrap()
        .build()
        .unwrap();
    let person = db_string("Person").unwrap();
    let knows = db_string("KNOWS").unwrap();
    {
        let mut txn = shared.begin_write();
        let a = txn
            .mutator()
            .create_node(LabelSet::single(person.clone()), PropertyMap::new())
            .unwrap();
        let b = txn
            .mutator()
            .create_node(LabelSet::single(person), PropertyMap::new())
            .unwrap();
        txn.mutator()
            .create_edge(knows.clone(), a, b, PropertyMap::new())
            .unwrap();
        txn.commit().unwrap();
    }

    let mut txn = shared.begin_write();
    let err = txn
        .mutator()
        .drop_edge_type(knows, DropBehavior::Restrict)
        .expect_err("RESTRICT rejects edge-type drop with surviving edges");
    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("1 instance(s) still exist") && reason.contains("CASCADE")
    ));
    drop(txn);
    assert_eq!(shared.graph_type().unwrap().edge_types.len(), 1);
    assert_eq!(shared.read().edge_count(), 1);
}

#[test]
fn drop_edge_type_cascade_truncates_then_drops_in_one_txn() {
    let shared = SharedGraph::builder(GraphId::new(152))
        .bound_to(person_self_knows_type())
        .unwrap()
        .build()
        .unwrap();
    let person = db_string("Person").unwrap();
    let knows = db_string("KNOWS").unwrap();
    {
        let mut txn = shared.begin_write();
        let a = txn
            .mutator()
            .create_node(LabelSet::single(person.clone()), PropertyMap::new())
            .unwrap();
        let b = txn
            .mutator()
            .create_node(LabelSet::single(person), PropertyMap::new())
            .unwrap();
        txn.mutator()
            .create_edge(knows.clone(), a, b, PropertyMap::new())
            .unwrap();
        txn.commit().unwrap();
    }

    let outcome = {
        let mut txn = shared.begin_write();
        txn.mutator()
            .drop_edge_type(knows.clone(), DropBehavior::Cascade)
            .expect("CASCADE drops an edge type with surviving edges");
        txn.commit().unwrap()
    };

    assert!(matches!(
        outcome.changes.as_slice(),
        [
            Change::EdgesOfTypeTruncated { label },
            Change::SchemaChanged {
                change: SchemaChange::EdgeTypeDropped { name, .. },
                ..
            },
        ] if *label == knows && *name == knows
    ));
    assert!(shared.graph_type().unwrap().edge_types.is_empty());
    assert_eq!(shared.read().edge_count(), 0);
    // Nodes (the KNOWS endpoints) survive — only edges of the type were removed.
    assert_eq!(shared.read().node_count(), 2);
}

fn person_self_knows_type() -> GraphTypeDef {
    let person = db_string("Person").unwrap();
    let knows = db_string("KNOWS").unwrap();
    GraphTypeDef {
        name: db_string("catalog.person.knows.graph").unwrap(),
        node_types: vec![NodeTypeDef {
            name: person.clone(),
            key_labels: LabelSet::single(person),
            properties: Vec::new(),
            validation_mode: ValidationMode::Strict,
        }],
        edge_types: vec![EdgeTypeDef {
            name: knows.clone(),
            label: knows,
            source_node_type: EdgeEndpointDef::NodeType(0),
            target_node_type: EdgeEndpointDef::NodeType(0),
            properties: Vec::new(),
            validation_mode: ValidationMode::Strict,
        }],
    }
}

fn person_company_school_with_oneof_edge_type() -> GraphTypeDef {
    let person = db_string("Person").unwrap();
    let company = db_string("Company").unwrap();
    let school = db_string("School").unwrap();
    let affiliated_with = db_string("AFFILIATED_WITH").unwrap();
    GraphTypeDef {
        name: db_string("catalog.oneof.graph").unwrap(),
        node_types: vec![
            NodeTypeDef {
                name: person.clone(),
                key_labels: LabelSet::single(person),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            },
            NodeTypeDef {
                name: company.clone(),
                key_labels: LabelSet::single(company),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            },
            NodeTypeDef {
                name: school.clone(),
                key_labels: LabelSet::single(school),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            },
        ],
        edge_types: vec![EdgeTypeDef {
            name: affiliated_with.clone(),
            label: affiliated_with,
            source_node_type: EdgeEndpointDef::NodeType(0),
            target_node_type: EdgeEndpointDef::one_of([1, 2]),
            properties: Vec::new(),
            validation_mode: ValidationMode::Strict,
        }],
    }
}

#[test]
fn drop_node_type_rejects_when_oneof_endpoint_references_dropped_type() {
    // F4 fold: endpoint_depends_on_shifted_node previously used
    // node_type_index().is_some_and(...) which returns None for OneOf and
    // would have silently let the drop succeed. Explicit OneOf arm rejects
    // when any contained index >= removed_index.
    let shared = SharedGraph::builder(GraphId::new(16))
        .bound_to(person_company_school_with_oneof_edge_type())
        .unwrap()
        .build()
        .unwrap();
    let mut txn = shared.begin_write();
    let err = txn
        .mutator()
        .drop_node_type(db_string("Company").unwrap(), DropBehavior::Restrict)
        .unwrap_err();

    // Company (index 1) is directly carried by OneOf([1, 2]); the dependency
    // check rejects with the clear "still references it" message.
    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("still references it")
    ));
}

#[test]
fn drop_node_type_rejects_when_oneof_endpoint_references_tail_type() {
    // Tail-index drop (School at index 2) is rejected because OneOf([1, 2])
    // carries School directly; the dependency check rejects it as a dangling
    // endpoint rather than rewriting OneOf payloads in place.
    let shared = SharedGraph::builder(GraphId::new(17))
        .bound_to(person_company_school_with_oneof_edge_type())
        .unwrap()
        .build()
        .unwrap();
    let mut txn = shared.begin_write();
    let err = txn
        .mutator()
        .drop_node_type(db_string("School").unwrap(), DropBehavior::Restrict)
        .unwrap_err();

    // School (index 2) is also directly carried by OneOf([1, 2]); the dependency
    // check rejects with the clear "still references it" message.
    assert!(matches!(
        err,
        GraphError::Inconsistent { reason }
            if reason.contains("still references it")
    ));
}