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
use super::codec::{decode_rkyv, encode_rkyv, ensure_section_within_cap, validate_sorted_unique};
use crate::graph::SeleneGraph;
use crate::graph_types::GraphTypeDef;

/// CORE/GTYP section format version.
///
/// Single version per the 2026-05-30 clean-break directive (greenfield, no consumers): the
/// legacy multi-magic V1/V2/V3 decoder is removed and the on-disk layout IS the live
/// [`GraphTypeDef`] rkyv archive.
///
/// Bumped `1 -> 2` by the typed-descriptor stream: the archived `PropertyTypeDef`
/// gained mid-struct `decimal_type` / `character_string_type` / `byte_string_type`
/// fields, and `PropertyElementType` / `RecordFieldType` gained descriptor
/// variants ahead of existing ones, so a version-1 archive must be rejected
/// rather than bytechecked against the wrong shape.
// Why: D14 (rkyv snapshot archive) / D19 (GG02 catalog persistence). The GTYP version byte
// is internal to the `b"GTYP"` section payload and is independent of the SLSN container's
// `SNAPSHOT_VERSION_MINOR` (which bumped 3 -> 4 for the same descriptor break).
const GTYP_VERSION: u8 = 2;

pub(in crate::core_provider) fn encode_graph_types(
    graph: &SeleneGraph,
) -> Result<Vec<u8>, crate::ProviderError> {
    let rows = graph
        .meta
        .bound_type
        .as_ref()
        .map(|type_def| vec![(0_u32, (**type_def).clone())])
        .unwrap_or_default();
    let mut payload = Vec::with_capacity(1);
    payload.push(GTYP_VERSION);
    payload.extend(encode_rkyv(&rows, "CORE/GTYP")?);
    ensure_section_within_cap("CORE/GTYP", payload.len())?;
    Ok(payload)
}

pub(in crate::core_provider) fn decode_graph_types(
    bytes: &[u8],
) -> Result<Vec<(u32, GraphTypeDef)>, crate::ProviderError> {
    // Single-version clean break (USER DIRECTIVE 2026-05-30): the on-disk layout IS the
    // live `GraphTypeDef` archive; there are no legacy V1/V2/V3 decoders. A mismatched or
    // absent version byte is a hard decode error, never a silent legacy fall-through.
    let Some((&version, rest)) = bytes.split_first() else {
        return Err(crate::ProviderError::InvalidPayload {
            reason: "CORE/GTYP section is empty".to_owned(),
        });
    };
    if version != GTYP_VERSION {
        return Err(crate::ProviderError::InvalidPayload {
            reason: format!(
                "CORE/GTYP section version {version} is unsupported (expected {GTYP_VERSION})"
            ),
        });
    }
    let rows: Vec<(u32, GraphTypeDef)> = decode_rkyv(rest, "CORE/GTYP")?;
    validate_sorted_unique(&rows, "CORE/GTYP")?;
    Ok(rows)
}

#[cfg(test)]
mod tests {
    use selene_core::{
        ByteStringType, CharacterStringType, DecimalType, GraphId, LabelSet, PropertyValueType,
        db_string,
    };

    use super::*;
    use crate::SharedGraph;
    use crate::graph_types::{
        EdgeEndpointDef, EdgeTypeDef, NodeTypeDef, PropertyTypeDef, RecordFieldType,
        RecordFieldTypeDef, RecordFieldTypes, ValidationMode,
    };

    #[test]
    fn encode_graph_types_writes_version_byte() {
        let person = db_string("VersionPerson").unwrap();
        let graph_type = GraphTypeDef {
            name: db_string("version.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(),
        };
        let graph = SharedGraph::builder(GraphId::new(211))
            .bound_to(graph_type)
            .unwrap()
            .build()
            .unwrap()
            .read()
            .as_ref()
            .clone();

        let bytes = encode_graph_types(&graph).unwrap();

        assert_eq!(bytes.first(), Some(&GTYP_VERSION));
    }

    #[test]
    fn gtyp_round_trips_oneof_endpoint() {
        // BRIEF-131e OneOf shape under the single-version (post-collapse) layout: encode +
        // decode a GraphTypeDef with an OneOf edge endpoint and assert structural equality
        // including OneOf payload sort order, so a future variant-reorder regression
        // surfaces here rather than as silent on-disk corruption.
        let person = db_string("V3OneOfPerson").unwrap();
        let company = db_string("V3OneOfCompany").unwrap();
        let school = db_string("V3OneOfSchool").unwrap();
        let affiliated = db_string("V3_AFFILIATED").unwrap();
        let graph_type = GraphTypeDef {
            name: db_string("v3.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.clone(),
                label: affiliated,
                source_node_type: EdgeEndpointDef::NodeType(0),
                target_node_type: EdgeEndpointDef::one_of([1, 2]),
                properties: Vec::new(),
                validation_mode: ValidationMode::Strict,
            }],
        };
        let graph = SharedGraph::builder(GraphId::new(212))
            .bound_to(graph_type.clone())
            .unwrap()
            .build()
            .unwrap()
            .read()
            .as_ref()
            .clone();

        let bytes = encode_graph_types(&graph).unwrap();
        assert_eq!(bytes.first(), Some(&GTYP_VERSION));

        let decoded = decode_graph_types(&bytes).unwrap();
        assert_eq!(decoded.len(), 1);
        let decoded_graph_type = &decoded[0].1;
        decoded_graph_type.validate_ref().unwrap();
        let edge_type = &decoded_graph_type.edge_types[0];
        assert_eq!(
            edge_type.target_node_type,
            EdgeEndpointDef::OneOf(vec![1, 2])
        );
        assert_eq!(edge_type.source_node_type, EdgeEndpointDef::NodeType(0));
    }

    #[test]
    fn gtyp_round_trips_record_field_types() {
        // A closed/typed RECORD property descriptor must survive the GTYP rkyv archive,
        // including nested LIST/RECORD/NOT NULL field types (exercises the
        // bytecheck recursion bounds on RecordFieldType across the archive).
        let person = db_string("RecordPerson").unwrap();
        let config = db_string("config").unwrap();
        let host = db_string("host").unwrap();
        let ports = db_string("ports").unwrap();
        let nested = db_string("nested").unwrap();
        let open_nested = db_string("open_nested").unwrap();
        let flag = db_string("flag").unwrap();
        let amount = db_string("amount").unwrap();
        let title = db_string("title").unwrap();
        let digest = db_string("digest").unwrap();
        let code = db_string("code").unwrap();
        let history = db_string("history").unwrap();
        let payloads = db_string("payloads").unwrap();
        let aliases = db_string("aliases").unwrap();
        let decimal = DecimalType::new(5, 2).unwrap();
        let history_decimal = DecimalType::new(4, 1).unwrap();
        let nested_decimal = DecimalType::new(6, 3).unwrap();
        let character_string = CharacterStringType::new(2, 4).unwrap();
        let history_character_string = CharacterStringType::new(1, 2).unwrap();
        let nested_character_string = CharacterStringType::new(4, 4).unwrap();
        let byte_string = ByteStringType::new(2, 4).unwrap();
        let history_byte_string = ByteStringType::new(1, 2).unwrap();
        let nested_byte_string = ByteStringType::new(4, 4).unwrap();
        let record_field_types = RecordFieldTypes(vec![
            RecordFieldTypeDef {
                name: host,
                field_type: RecordFieldType::Scalar(PropertyValueType::String),
                required: true,
            },
            RecordFieldTypeDef {
                name: amount.clone(),
                field_type: RecordFieldType::Decimal(nested_decimal),
                required: false,
            },
            RecordFieldTypeDef {
                name: code.clone(),
                field_type: RecordFieldType::CharacterString(nested_character_string),
                required: false,
            },
            RecordFieldTypeDef {
                name: digest.clone(),
                field_type: RecordFieldType::ByteString(nested_byte_string),
                required: false,
            },
            RecordFieldTypeDef {
                name: ports,
                field_type: RecordFieldType::List(Box::new(RecordFieldType::NotNull(Box::new(
                    RecordFieldType::Scalar(PropertyValueType::Int),
                )))),
                required: false,
            },
            RecordFieldTypeDef {
                name: open_nested,
                field_type: RecordFieldType::OpenRecord,
                required: false,
            },
            RecordFieldTypeDef {
                name: nested,
                field_type: RecordFieldType::Record(Box::new(RecordFieldTypes(vec![
                    RecordFieldTypeDef {
                        name: flag,
                        field_type: RecordFieldType::Scalar(PropertyValueType::Bool),
                        required: true,
                    },
                ]))),
                required: false,
            },
        ]);
        let graph_type = GraphTypeDef {
            name: db_string("record.graph").unwrap(),
            node_types: vec![NodeTypeDef {
                name: person.clone(),
                key_labels: LabelSet::single(person),
                properties: vec![
                    PropertyTypeDef {
                        name: config,
                        value_type: PropertyValueType::RecordTyped,
                        list_element_type: None,
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: Some(record_field_types.clone()),
                    },
                    PropertyTypeDef {
                        name: amount.clone(),
                        value_type: PropertyValueType::Decimal,
                        list_element_type: None,
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: Some(decimal),
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: None,
                    },
                    PropertyTypeDef {
                        name: title,
                        value_type: PropertyValueType::String,
                        list_element_type: None,
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: Some(character_string),
                        byte_string_type: None,
                        record_field_types: None,
                    },
                    PropertyTypeDef {
                        name: digest,
                        value_type: PropertyValueType::Bytes,
                        list_element_type: None,
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: Some(byte_string),
                        record_field_types: None,
                    },
                    PropertyTypeDef {
                        name: history,
                        value_type: PropertyValueType::List,
                        list_element_type: Some(crate::graph_types::PropertyElementType::Decimal(
                            history_decimal,
                        )),
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: None,
                    },
                    PropertyTypeDef {
                        name: payloads,
                        value_type: PropertyValueType::List,
                        list_element_type: Some(
                            crate::graph_types::PropertyElementType::ByteString(
                                history_byte_string,
                            ),
                        ),
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: None,
                    },
                    PropertyTypeDef {
                        name: aliases,
                        value_type: PropertyValueType::List,
                        list_element_type: Some(
                            crate::graph_types::PropertyElementType::CharacterString(
                                history_character_string,
                            ),
                        ),
                        required: false,
                        default: None,
                        immutable: false,
                        unique: false,
                        decimal_type: None,
                        character_string_type: None,
                        byte_string_type: None,
                        record_field_types: None,
                    },
                ],
                validation_mode: ValidationMode::Strict,
            }],
            edge_types: Vec::new(),
        };
        let graph = SharedGraph::builder(GraphId::new(213))
            .bound_to(graph_type)
            .unwrap()
            .build()
            .unwrap()
            .read()
            .as_ref()
            .clone();

        let bytes = encode_graph_types(&graph).unwrap();
        assert_eq!(bytes.first(), Some(&GTYP_VERSION));

        let decoded = decode_graph_types(&bytes).unwrap();
        assert_eq!(decoded.len(), 1);
        let decoded_graph_type = &decoded[0].1;
        decoded_graph_type.validate_ref().unwrap();
        let property = &decoded_graph_type.node_types[0].properties[0];
        assert_eq!(property.value_type, PropertyValueType::RecordTyped);
        assert_eq!(property.record_field_types, Some(record_field_types));
        let property = &decoded_graph_type.node_types[0].properties[1];
        assert_eq!(property.value_type, PropertyValueType::Decimal);
        assert_eq!(property.decimal_type, Some(decimal));
        let property = &decoded_graph_type.node_types[0].properties[2];
        assert_eq!(property.value_type, PropertyValueType::String);
        assert_eq!(property.character_string_type, Some(character_string));
        let property = &decoded_graph_type.node_types[0].properties[3];
        assert_eq!(property.value_type, PropertyValueType::Bytes);
        assert_eq!(property.byte_string_type, Some(byte_string));
        let property = &decoded_graph_type.node_types[0].properties[4];
        assert_eq!(
            property.list_element_type,
            Some(crate::graph_types::PropertyElementType::Decimal(
                history_decimal
            ))
        );
        let property = &decoded_graph_type.node_types[0].properties[5];
        assert_eq!(
            property.list_element_type,
            Some(crate::graph_types::PropertyElementType::ByteString(
                history_byte_string
            ))
        );
        let property = &decoded_graph_type.node_types[0].properties[6];
        assert_eq!(
            property.list_element_type,
            Some(crate::graph_types::PropertyElementType::CharacterString(
                history_character_string
            ))
        );
    }

    #[test]
    fn decode_rejects_unknown_or_empty_version() {
        // The clean break rejects any non-current version byte (e.g. the retired 0xB7 V3
        // magic) and an empty section, rather than silently falling through to a legacy
        // decoder or treating it as zero rows.
        let person = db_string("UnknownVersionPerson").unwrap();
        let graph_type = GraphTypeDef {
            name: db_string("unknown.version.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(),
        };
        let graph = SharedGraph::builder(GraphId::new(214))
            .bound_to(graph_type)
            .unwrap()
            .build()
            .unwrap()
            .read()
            .as_ref()
            .clone();
        let mut bytes = encode_graph_types(&graph).unwrap();
        bytes[0] = 0xB7; // retired V3 magic
        assert!(matches!(
            decode_graph_types(&bytes),
            Err(crate::ProviderError::InvalidPayload { .. })
        ));
        assert!(matches!(
            decode_graph_types(&[]),
            Err(crate::ProviderError::InvalidPayload { .. })
        ));
    }

    #[test]
    fn decode_rejects_pre_descriptor_version_one() {
        // Typed-descriptor clean break: a version-1 GTYP payload archives
        // `PropertyTypeDef` WITHOUT the decimal/character-string/byte-string
        // descriptor fields, so the version gate must reject it with a clean
        // versioned error instead of bytechecking the body against the wrong
        // archived shape.
        let person = db_string("PreDescriptorPerson").unwrap();
        let graph_type = GraphTypeDef {
            name: db_string("pre.descriptor.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(),
        };
        let graph = SharedGraph::builder(GraphId::new(215))
            .bound_to(graph_type)
            .unwrap()
            .build()
            .unwrap()
            .read()
            .as_ref()
            .clone();
        let mut bytes = encode_graph_types(&graph).unwrap();
        bytes[0] = 1; // pre-descriptor GTYP version
        assert!(matches!(
            decode_graph_types(&bytes),
            Err(crate::ProviderError::InvalidPayload { reason })
                if reason.contains("version 1 is unsupported")
        ));
    }
}