oxgraph-db 0.1.0

Standalone OxGraph-native database engine above the topology substrate.
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
//! Catalog metadata for names, projections, property keys, and indexes.

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::{
    DbError, IndexId, LabelId, ProjectionId, PropertyKeyId, RelationTypeId, RoleId,
    value::PropertyType,
};

/// Catalog entry for one structural incidence role.
///
/// # Performance
///
/// Cloning is `O(name length)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RoleDefinition {
    /// Stable role identifier.
    pub id: RoleId,
    /// Human-readable unique role name.
    pub name: String,
}

/// Catalog entry for one element or relation label.
///
/// # Performance
///
/// Cloning is `O(name length)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct LabelDefinition {
    /// Stable label identifier.
    pub id: LabelId,
    /// Human-readable unique label name.
    pub name: String,
}

/// Catalog entry for one relation type.
///
/// # Performance
///
/// Cloning is `O(name length)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RelationTypeDefinition {
    /// Stable relation type identifier.
    pub id: RelationTypeId,
    /// Human-readable unique relation type name.
    pub name: String,
}

/// Subject family accepted by a property key.
///
/// # Performance
///
/// Copying and comparing are `O(1)`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum PropertyFamily {
    /// Property applies to canonical elements.
    Element,
    /// Property applies to canonical relations.
    Relation,
    /// Property applies to canonical incidences.
    Incidence,
}

/// Catalog entry for one typed property key.
///
/// # Performance
///
/// Cloning is `O(name length)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PropertyKeyDefinition {
    /// Stable property key identifier.
    pub id: PropertyKeyId,
    /// Human-readable unique key name.
    pub name: String,
    /// Subject family this key can be attached to.
    pub family: PropertyFamily,
    /// Required scalar value type.
    pub value_type: PropertyType,
}

/// Graph projection definition.
///
/// Graph projections materialize binary relations as CSR outgoing and CSC
/// incoming arrays over canonical topology IDs.
///
/// # Performance
///
/// Cloning is `O(r)` for `r` selected relation types plus the name length.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GraphProjectionDefinition {
    /// Unique projection name.
    pub name: String,
    /// Relation types visible as binary graph edges.
    pub relation_types: BTreeSet<RelationTypeId>,
    /// Role identifying the source endpoint.
    pub source_role: RoleId,
    /// Role identifying the target endpoint.
    pub target_role: RoleId,
}

/// Hypergraph projection definition.
///
/// Hypergraph projections materialize many-participant directed relations as
/// BCSR-style relation-major and vertex-major arrays.
///
/// # Performance
///
/// Cloning is `O(r + s + t)` for relation type and role set sizes plus the
/// name length.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct HypergraphProjectionDefinition {
    /// Unique projection name.
    pub name: String,
    /// Relation types visible as hyperedges.
    pub relation_types: BTreeSet<RelationTypeId>,
    /// Roles treated as source-side participants.
    pub source_roles: BTreeSet<RoleId>,
    /// Roles treated as target-side participants.
    pub target_roles: BTreeSet<RoleId>,
}

/// Physical projection definition stored in the catalog.
///
/// # Performance
///
/// Cloning is `O(definition size)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum ProjectionDefinition {
    /// Binary graph projection.
    Graph(GraphProjectionDefinition),
    /// Directed hypergraph projection.
    Hypergraph(HypergraphProjectionDefinition),
}

impl ProjectionDefinition {
    /// Returns the unique projection name.
    ///
    /// # Performance
    ///
    /// This method is `O(1)`.
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::Graph(definition) => &definition.name,
            Self::Hypergraph(definition) => &definition.name,
        }
    }
}

/// Index definition stored in the catalog.
///
/// # Performance
///
/// Cloning is `O(key count)` for composite indexes and `O(1)` otherwise.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum IndexDefinition {
    /// Element label membership index.
    Label {
        /// Indexed label.
        label: LabelId,
    },
    /// Relation type membership index.
    RelationType {
        /// Indexed relation type.
        relation_type: RelationTypeId,
    },
    /// Equality index over one property key.
    PropertyEquality {
        /// Indexed property key.
        key: PropertyKeyId,
    },
    /// Range index over one ordered property key.
    PropertyRange {
        /// Indexed property key.
        key: PropertyKeyId,
    },
    /// Composite equality index over ordered property keys.
    CompositeEquality {
        /// Indexed property keys in tuple order.
        keys: Vec<PropertyKeyId>,
    },
    /// Projection-materialization index metadata.
    Projection {
        /// Indexed projection.
        projection: ProjectionId,
    },
}

/// Catalog entry for one index.
///
/// # Performance
///
/// Cloning is `O(name length + definition size)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct IndexEntry {
    /// Stable index identifier.
    pub id: IndexId,
    /// Human-readable unique index name.
    pub name: String,
    /// Logical index definition.
    pub definition: IndexDefinition,
}

/// Catalog entry for one projection.
///
/// # Performance
///
/// Cloning is `O(name length + definition size)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ProjectionEntry {
    /// Stable projection identifier.
    pub id: ProjectionId,
    /// Physical projection definition.
    pub definition: ProjectionDefinition,
}

/// Database catalog for names, schemas, projections, and indexes.
///
/// # Performance
///
/// Cloning is `O(catalog entries + string bytes)`.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Catalog {
    /// Roles by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    roles: BTreeMap<RoleId, RoleDefinition>,
    /// Role IDs by name.
    role_names: BTreeMap<String, RoleId>,
    /// Labels by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    labels: BTreeMap<LabelId, LabelDefinition>,
    /// Label IDs by name.
    label_names: BTreeMap<String, LabelId>,
    /// Relation types by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    relation_types: BTreeMap<RelationTypeId, RelationTypeDefinition>,
    /// Relation type IDs by name.
    relation_type_names: BTreeMap<String, RelationTypeId>,
    /// Property keys by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    property_keys: BTreeMap<PropertyKeyId, PropertyKeyDefinition>,
    /// Property key IDs by name.
    property_key_names: BTreeMap<String, PropertyKeyId>,
    /// Projections by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    projections: BTreeMap<ProjectionId, ProjectionEntry>,
    /// Projection IDs by name.
    projection_names: BTreeMap<String, ProjectionId>,
    /// Indexes by stable ID.
    #[serde(with = "serde_btree_map_vec")]
    indexes: BTreeMap<IndexId, IndexEntry>,
    /// Index IDs by name.
    index_names: BTreeMap<String, IndexId>,
}

/// Serde helper for `BTreeMap` values keyed by non-string IDs.
mod serde_btree_map_vec {
    /// Serializes a map as an ordered entry array.
    pub(super) fn serialize<S, K, V>(
        map: &std::collections::BTreeMap<K, V>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
        K: serde::Serialize,
        V: serde::Serialize,
    {
        serde::Serialize::serialize(&map.iter().collect::<Vec<_>>(), serializer)
    }

    /// Deserializes a map from an ordered entry array.
    pub(super) fn deserialize<'de, D, K, V>(
        deserializer: D,
    ) -> Result<std::collections::BTreeMap<K, V>, D::Error>
    where
        D: serde::Deserializer<'de>,
        K: Ord + serde::de::DeserializeOwned,
        V: serde::de::DeserializeOwned,
    {
        <Vec<(K, V)> as serde::Deserialize>::deserialize(deserializer)
            .map(|entries| entries.into_iter().collect())
    }
}

impl Catalog {
    /// Creates an empty catalog.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    #[must_use]
    pub(crate) const fn empty() -> Self {
        Self {
            roles: BTreeMap::new(),
            role_names: BTreeMap::new(),
            labels: BTreeMap::new(),
            label_names: BTreeMap::new(),
            relation_types: BTreeMap::new(),
            relation_type_names: BTreeMap::new(),
            property_keys: BTreeMap::new(),
            property_key_names: BTreeMap::new(),
            projections: BTreeMap::new(),
            projection_names: BTreeMap::new(),
            indexes: BTreeMap::new(),
            index_names: BTreeMap::new(),
        }
    }

    /// Returns a role definition.
    ///
    /// # Performance
    ///
    /// This method is `O(log r)`.
    #[must_use]
    pub fn role(&self, id: RoleId) -> Option<&RoleDefinition> {
        self.roles.get(&id)
    }

    /// Returns a label definition.
    ///
    /// # Performance
    ///
    /// This method is `O(log l)`.
    #[must_use]
    pub fn label(&self, id: LabelId) -> Option<&LabelDefinition> {
        self.labels.get(&id)
    }

    /// Returns a relation type definition.
    ///
    /// # Performance
    ///
    /// This method is `O(log t)`.
    #[must_use]
    pub fn relation_type(&self, id: RelationTypeId) -> Option<&RelationTypeDefinition> {
        self.relation_types.get(&id)
    }

    /// Returns a property key definition.
    ///
    /// # Performance
    ///
    /// This method is `O(log p)`.
    #[must_use]
    pub fn property_key(&self, id: PropertyKeyId) -> Option<&PropertyKeyDefinition> {
        self.property_keys.get(&id)
    }

    /// Returns a projection entry.
    ///
    /// # Performance
    ///
    /// This method is `O(log p)`.
    #[must_use]
    pub fn projection(&self, id: ProjectionId) -> Option<&ProjectionEntry> {
        self.projections.get(&id)
    }

    /// Returns an index entry.
    ///
    /// # Performance
    ///
    /// This method is `O(log i)`.
    #[must_use]
    pub fn index(&self, id: IndexId) -> Option<&IndexEntry> {
        self.indexes.get(&id)
    }

    /// Resolves a role name.
    ///
    /// # Performance
    ///
    /// This method is `O(log r + name length)`.
    #[must_use]
    pub fn role_id(&self, name: &str) -> Option<RoleId> {
        self.role_names.get(name).copied()
    }

    /// Resolves a label name.
    ///
    /// # Performance
    ///
    /// This method is `O(log l + name length)`.
    #[must_use]
    pub fn label_id(&self, name: &str) -> Option<LabelId> {
        self.label_names.get(name).copied()
    }

    /// Resolves a relation type name.
    ///
    /// # Performance
    ///
    /// This method is `O(log t + name length)`.
    #[must_use]
    pub fn relation_type_id(&self, name: &str) -> Option<RelationTypeId> {
        self.relation_type_names.get(name).copied()
    }

    /// Resolves a property key name.
    ///
    /// # Performance
    ///
    /// This method is `O(log p + name length)`.
    #[must_use]
    pub fn property_key_id(&self, name: &str) -> Option<PropertyKeyId> {
        self.property_key_names.get(name).copied()
    }

    /// Resolves a projection name.
    ///
    /// # Performance
    ///
    /// This method is `O(log p + name length)`.
    #[must_use]
    pub fn projection_id(&self, name: &str) -> Option<ProjectionId> {
        self.projection_names.get(name).copied()
    }

    /// Resolves an index name.
    ///
    /// # Performance
    ///
    /// This method is `O(log i + name length)`.
    #[must_use]
    pub fn index_id(&self, name: &str) -> Option<IndexId> {
        self.index_names.get(name).copied()
    }

    /// Iterates role definitions in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn roles(&self) -> impl Iterator<Item = &RoleDefinition> {
        self.roles.values()
    }

    /// Iterates label definitions in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn labels(&self) -> impl Iterator<Item = &LabelDefinition> {
        self.labels.values()
    }

    /// Iterates relation type definitions in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn relation_types(&self) -> impl Iterator<Item = &RelationTypeDefinition> {
        self.relation_types.values()
    }

    /// Iterates property key definitions in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn property_keys(&self) -> impl Iterator<Item = &PropertyKeyDefinition> {
        self.property_keys.values()
    }

    /// Iterates projection entries in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn projections(&self) -> impl Iterator<Item = &ProjectionEntry> {
        self.projections.values()
    }

    /// Iterates index entries in ID order.
    ///
    /// # Performance
    ///
    /// Creating the iterator is `O(1)`.
    pub fn indexes(&self) -> impl Iterator<Item = &IndexEntry> {
        self.indexes.values()
    }

    /// Registers a structural role.
    pub(crate) fn insert_role(&mut self, id: RoleId, name: String) -> Result<(), DbError> {
        insert_named(&mut self.role_names, &name, id)?;
        self.roles.insert(id, RoleDefinition { id, name });
        Ok(())
    }

    /// Registers a label.
    pub(crate) fn insert_label(&mut self, id: LabelId, name: String) -> Result<(), DbError> {
        insert_named(&mut self.label_names, &name, id)?;
        self.labels.insert(id, LabelDefinition { id, name });
        Ok(())
    }

    /// Registers a relation type.
    pub(crate) fn insert_relation_type(
        &mut self,
        id: RelationTypeId,
        name: String,
    ) -> Result<(), DbError> {
        insert_named(&mut self.relation_type_names, &name, id)?;
        self.relation_types
            .insert(id, RelationTypeDefinition { id, name });
        Ok(())
    }

    /// Registers a typed property key.
    pub(crate) fn insert_property_key(
        &mut self,
        definition: PropertyKeyDefinition,
    ) -> Result<(), DbError> {
        insert_named(
            &mut self.property_key_names,
            &definition.name,
            definition.id,
        )?;
        self.property_keys.insert(definition.id, definition);
        Ok(())
    }

    /// Registers a projection definition.
    pub(crate) fn insert_projection(
        &mut self,
        id: ProjectionId,
        definition: ProjectionDefinition,
    ) -> Result<(), DbError> {
        insert_named(&mut self.projection_names, definition.name(), id)?;
        self.projections
            .insert(id, ProjectionEntry { id, definition });
        Ok(())
    }

    /// Registers an index definition.
    pub(crate) fn insert_index(
        &mut self,
        id: IndexId,
        name: String,
        definition: IndexDefinition,
    ) -> Result<(), DbError> {
        insert_named(&mut self.index_names, &name, id)?;
        self.indexes.insert(
            id,
            IndexEntry {
                id,
                name,
                definition,
            },
        );
        Ok(())
    }
}

/// Inserts one unique name into a catalog name map.
fn insert_named<Id: Copy>(
    names: &mut BTreeMap<String, Id>,
    name: &str,
    id: Id,
) -> Result<(), DbError> {
    if names.contains_key(name) {
        return Err(DbError::DuplicateCatalogName);
    }
    names.insert(name.to_owned(), id);
    Ok(())
}