oxgraph-db 0.4.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
//! Db error surface.
//!
//! [`DbError`] is the single error type on the public API. It composes four
//! subsystem enums — [`StorageError`], [`CatalogError`], [`TxnError`], and
//! [`QueryError`] — so callers can match on the failing subsystem while
//! internal code constructs the precise variant and `?`-converts via [`From`].

use std::{fmt, io};

use crate::{PropertyKeyId, catalog::PropertyFamily, value::PropertyType};

/// Canonical id family, for errors that name one.
///
/// # Performance
///
/// Copying, comparing, and formatting are `O(1)`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IdFamily {
    /// Canonical element ids.
    Element,
    /// Canonical relation ids.
    Relation,
    /// Canonical incidence ids.
    Incidence,
    /// Structural role ids.
    Role,
    /// Catalog label ids.
    Label,
    /// Catalog relation-type ids.
    RelationType,
    /// Catalog property-key ids.
    PropertyKey,
    /// Catalog projection ids.
    Projection,
    /// Catalog index ids.
    Index,
}

impl fmt::Display for IdFamily {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::Element => "element",
            Self::Relation => "relation",
            Self::Incidence => "incidence",
            Self::Role => "role",
            Self::Label => "label",
            Self::RelationType => "relation type",
            Self::PropertyKey => "property key",
            Self::Projection => "projection",
            Self::Index => "index",
        })
    }
}

/// Errors from the persistence layer: db files, the superblock, the base
/// store format, and the delta log.
///
/// # Performance
///
/// Formatting is `O(message length)`.
#[derive(Debug)]
#[non_exhaustive]
pub enum StorageError {
    /// Db files already exist.
    AlreadyExists,
    /// Db files do not exist.
    NotFound,
    /// Wraps an IO error with operation context.
    Io {
        /// Operation that failed.
        operation: &'static str,
        /// Underlying IO error.
        source: io::Error,
    },
    /// Storage bytes are invalid.
    InvalidStore {
        /// Deterministic validation message.
        message: String,
    },
    /// The store's OXGDB format version is not supported by this build. A base
    /// written under an older format (for example one lacking the persisted
    /// `SECTION_INDEX_*` postings) is rejected here rather than silently rebuilt.
    UnsupportedFormat {
        /// Format version recorded in the store.
        found: u32,
        /// Format version this build requires.
        expected: u32,
    },
    /// A delta-log record is corrupt beyond the recoverable torn tail.
    LogCorrupt {
        /// Log sequence number of the offending record.
        lsn: u64,
        /// Deterministic reason the record was rejected.
        reason: &'static str,
    },
    /// A delta-log record names a different base generation than the superblock.
    BaseGenerationMismatch {
        /// Base generation named by the superblock.
        expected: u64,
        /// Base generation found in the record.
        found: u64,
    },
}

impl StorageError {
    /// Creates an IO error with operation context.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    pub(crate) const fn io(operation: &'static str, source: io::Error) -> Self {
        Self::Io { operation, source }
    }

    /// Creates an invalid-store error.
    ///
    /// # Performance
    ///
    /// This function is `O(message.len())`.
    pub(crate) fn invalid_store(message: impl Into<String>) -> Self {
        Self::InvalidStore {
            message: message.into(),
        }
    }
}

impl fmt::Display for StorageError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AlreadyExists => formatter.write_str("database already exists"),
            Self::NotFound => formatter.write_str("database not found"),
            Self::Io { operation, source } => write!(formatter, "{operation} failed: {source}"),
            Self::InvalidStore { message } => write!(formatter, "invalid store: {message}"),
            Self::UnsupportedFormat { found, expected } => write!(
                formatter,
                "unsupported OXGDB format version: found {found}, this build requires {expected}"
            ),
            Self::LogCorrupt { lsn, reason } => {
                write!(formatter, "delta-log corrupt at lsn {lsn}: {reason}")
            }
            Self::BaseGenerationMismatch { expected, found } => write!(
                formatter,
                "base generation mismatch: superblock names {expected}, record has {found}"
            ),
        }
    }
}

impl std::error::Error for StorageError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::AlreadyExists
            | Self::NotFound
            | Self::InvalidStore { .. }
            | Self::UnsupportedFormat { .. }
            | Self::LogCorrupt { .. }
            | Self::BaseGenerationMismatch { .. } => None,
        }
    }
}

/// Errors from catalog identity: canonical ids, catalog names, and declared
/// schema shapes.
///
/// # Performance
///
/// Formatting is `O(message length)`.
#[derive(Debug)]
#[non_exhaustive]
pub enum CatalogError {
    /// A referenced canonical id is not present.
    UnknownId {
        /// Id family that was looked up.
        family: IdFamily,
        /// Raw canonical id value that was absent.
        id: u64,
    },
    /// A catalog name was not found in a bound schema.
    UnknownName {
        /// The kind of catalog entry (for example `"role"` or `"property key"`).
        kind: &'static str,
        /// The name that was not found.
        name: String,
    },
    /// Duplicate catalog name or ID.
    DuplicateName,
    /// Duplicate canonical ID.
    DuplicateId,
    /// A declared schema item conflicts with an existing catalog entry.
    SchemaConflict {
        /// The conflicting catalog name.
        name: String,
        /// Deterministic reason the declaration conflicts with the catalog.
        reason: &'static str,
    },
}

impl fmt::Display for CatalogError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownId { family, id } => write!(formatter, "unknown {family} {id}"),
            Self::UnknownName { kind, name } => write!(formatter, "unknown {kind} {name:?}"),
            Self::DuplicateName => formatter.write_str("duplicate catalog name"),
            Self::DuplicateId => formatter.write_str("duplicate ID"),
            Self::SchemaConflict { name, reason } => {
                write!(formatter, "schema conflict for {name:?}: {reason}")
            }
        }
    }
}

impl std::error::Error for CatalogError {}

/// Errors from the transaction lifecycle: the single-writer lock and id and
/// sequence allocation.
///
/// # Performance
///
/// Formatting is `O(1)`.
#[derive(Debug)]
#[non_exhaustive]
pub enum TxnError {
    /// The single-writer lock is already held by another writer.
    WriterLockHeld,
    /// Canonical ID space is exhausted.
    IdOverflow {
        /// Id family whose space is exhausted.
        family: IdFamily,
    },
    /// Transaction ID space is exhausted.
    TransactionIdOverflow,
    /// Commit sequence space is exhausted.
    CommitSeqOverflow,
}

impl fmt::Display for TxnError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::WriterLockHeld => formatter.write_str("database writer lock is held"),
            Self::IdOverflow { family } => write!(formatter, "database {family} ID overflow"),
            Self::TransactionIdOverflow => formatter.write_str("transaction ID overflow"),
            Self::CommitSeqOverflow => formatter.write_str("commit sequence overflow"),
        }
    }
}

impl std::error::Error for TxnError {}

/// Errors from query and read validation: query text, property schema checks,
/// projections, and traversals.
///
/// # Performance
///
/// Formatting is `O(message length)`.
#[derive(Debug)]
#[non_exhaustive]
pub enum QueryError {
    /// Query text is empty.
    Empty,
    /// Query text is outside the pinned profile.
    Unsupported {
        /// Deterministic explanation.
        message: String,
    },
    /// Property value type mismatched the catalog schema.
    PropertyTypeMismatch {
        /// Expected property type.
        expected: PropertyType,
        /// Actual property type.
        actual: PropertyType,
    },
    /// Property subject family mismatched the catalog schema.
    WrongPropertyFamily {
        /// Expected subject family.
        expected: PropertyFamily,
        /// Actual subject family.
        actual: PropertyFamily,
    },
    /// Projection cannot be materialized as requested.
    InvalidProjection {
        /// Deterministic validation message.
        message: String,
    },
    /// A bounded traversal failed.
    Traversal {
        /// Deterministic reason the traversal failed.
        reason: &'static str,
    },
    /// A required property was absent from a subject.
    MissingProperty {
        /// The property key that was required but absent.
        key: PropertyKeyId,
    },
    /// A numeric value was outside the representable `i64` range.
    ValueOutOfRange,
    /// A property key has no associated equality index.
    NoEqualityIndex {
        /// The property key lacking an equality index.
        key: PropertyKeyId,
    },
}

impl fmt::Display for QueryError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => formatter.write_str("empty query"),
            Self::Unsupported { message } => write!(formatter, "unsupported query: {message}"),
            Self::PropertyTypeMismatch { expected, actual } => {
                write!(
                    formatter,
                    "property type mismatch: expected {expected:?}, got {actual:?}"
                )
            }
            Self::WrongPropertyFamily { expected, actual } => {
                write!(
                    formatter,
                    "property family mismatch: expected {expected:?}, got {actual:?}"
                )
            }
            Self::InvalidProjection { message } => {
                write!(formatter, "invalid projection: {message}")
            }
            Self::Traversal { reason } => write!(formatter, "traversal error: {reason}"),
            Self::MissingProperty { key } => {
                write!(formatter, "missing property {}", key.get())
            }
            Self::ValueOutOfRange => formatter.write_str("value out of representable i64 range"),
            Self::NoEqualityIndex { key } => {
                write!(
                    formatter,
                    "no equality index for property key {}",
                    key.get()
                )
            }
        }
    }
}

impl std::error::Error for QueryError {}

/// Errors raised by the `OxGraph` database product.
///
/// # Performance
///
/// Formatting is `O(message length)`.
#[derive(Debug)]
#[non_exhaustive]
pub enum DbError {
    /// Persistence-layer failure (files, superblock, base format, delta log).
    Storage(StorageError),
    /// Catalog identity failure (canonical ids, names, schema shapes).
    Catalog(CatalogError),
    /// Transaction lifecycle failure (writer lock, id/sequence allocation).
    Txn(TxnError),
    /// Query or read validation failure.
    Query(QueryError),
}

impl DbError {
    /// Creates an IO error with operation context.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    pub(crate) const fn io(operation: &'static str, source: io::Error) -> Self {
        Self::Storage(StorageError::Io { operation, source })
    }

    /// Creates an unsupported-query error.
    ///
    /// # Performance
    ///
    /// This function is `O(message.len())`.
    pub(crate) fn unsupported(message: impl Into<String>) -> Self {
        Self::Query(QueryError::Unsupported {
            message: message.into(),
        })
    }

    /// Creates an invalid-projection error.
    ///
    /// # Performance
    ///
    /// This function is `O(message.len())`.
    pub(crate) fn invalid_projection(message: impl Into<String>) -> Self {
        Self::Query(QueryError::InvalidProjection {
            message: message.into(),
        })
    }

    /// Creates an invalid-store error.
    ///
    /// # Performance
    ///
    /// This function is `O(message.len())`.
    pub(crate) fn invalid_store(message: impl Into<String>) -> Self {
        Self::Storage(StorageError::InvalidStore {
            message: message.into(),
        })
    }

    /// Builds a traversal error from a deterministic reason.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    pub(crate) const fn traversal(reason: &'static str) -> Self {
        Self::Query(QueryError::Traversal { reason })
    }

    /// Builds an unknown-id error from any canonical id newtype.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    pub(crate) fn unknown(id: impl Into<(IdFamily, u64)>) -> Self {
        let (family, id) = id.into();
        Self::Catalog(CatalogError::UnknownId { family, id })
    }

    /// Builds the id-exhaustion error for `family`'s allocator.
    ///
    /// # Performance
    ///
    /// This function is `O(1)`.
    #[must_use]
    pub(crate) const fn id_overflow(family: IdFamily) -> Self {
        Self::Txn(TxnError::IdOverflow { family })
    }
}

impl fmt::Display for DbError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Storage(error) => fmt::Display::fmt(error, formatter),
            Self::Catalog(error) => fmt::Display::fmt(error, formatter),
            Self::Txn(error) => fmt::Display::fmt(error, formatter),
            Self::Query(error) => fmt::Display::fmt(error, formatter),
        }
    }
}

impl std::error::Error for DbError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        // Skip-level delegation: the chain stays `DbError -> io::Error` with no
        // intermediate subsystem hop, matching the pre-split surface.
        match self {
            Self::Storage(error) => std::error::Error::source(error),
            Self::Catalog(error) => std::error::Error::source(error),
            Self::Txn(error) => std::error::Error::source(error),
            Self::Query(error) => std::error::Error::source(error),
        }
    }
}

impl From<StorageError> for DbError {
    fn from(error: StorageError) -> Self {
        Self::Storage(error)
    }
}

impl From<CatalogError> for DbError {
    fn from(error: CatalogError) -> Self {
        Self::Catalog(error)
    }
}

impl From<TxnError> for DbError {
    fn from(error: TxnError) -> Self {
        Self::Txn(error)
    }
}

impl From<QueryError> for DbError {
    fn from(error: QueryError) -> Self {
        Self::Query(error)
    }
}