hawdb-core 0.34.0

An embedded Rust graph database for local data-plane workloads
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::RuntimeCapability;
use std::fmt::{Display, Formatter};

mod resource;
pub use resource::{ResourceBudgetError, ResourceBudgetKind, ResourceBudgetScope};
mod transaction;
pub use transaction::{TransactionRetryError, TransactionRetryReason};
mod storage_format;
pub use storage_format::{
    StorageFormatError, APPEND_WAL_FORMAT, LOGICAL_DUMP_FORMAT,
    RELATIONAL_CHECKPOINT_LAYOUT_FORMAT, RELATIONAL_DURABLE_CODEC_FORMAT,
    RELATIONAL_ROW_PAGE_FORMAT, RELATIONAL_SCHEMA_FORMAT, SEARCH_PROJECTION_MANIFEST_FORMAT,
    STORAGE_MANIFEST_FORMAT,
};
mod relational;
mod wal_record;
pub use relational::{find_relational_error_kind, RelationalErrorKind, RelationalErrorSurface};
pub use wal_record::{WalRecordLimit, WalRecordLimitError};

/// Descriptor admission failures are distinct from corrupt storage or a busy
/// branch. Project quota counts belong to one engine resource domain; OS-limit
/// failures can additionally report the process allowance when it is known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileDescriptorError {
    InvalidBudget {
        limit: usize,
    },
    ConfigurationConflict {
        configured: usize,
        requested: usize,
    },
    CapacityPolicyConflict {
        configured: bool,
        requested: bool,
    },
    BudgetExceeded {
        requested: usize,
        available: usize,
        limit: usize,
    },
    OsLimit {
        requested: usize,
        os_code: Option<i32>,
        soft: Option<u64>,
        hard: Option<u64>,
    },
}

impl Display for FileDescriptorError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidBudget { limit } => write!(formatter, "invalid file descriptor budget: {limit}"),
            Self::ConfigurationConflict { configured, requested } => write!(formatter,
                "project file descriptor budget conflict: configured {configured}, requested {requested}"),
            Self::CapacityPolicyConflict { configured, requested } => write!(formatter,
                "project strict_capacity_limit conflict: configured {configured}, requested {requested}"),
            Self::BudgetExceeded { requested, available, limit } => write!(formatter,
                "project file descriptor budget exceeded: requested {requested}, available {available}, limit {limit}"),
            Self::OsLimit { requested, os_code, soft, hard } => write!(formatter,
                "operating system file descriptor limit: requested {requested}, soft {soft:?}, hard {hard:?}, OS code {os_code:?}"),
        }
    }
}

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

/// The result resource whose selected read limit rejected delivery.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadBudgetResource {
    Rows,
    PayloadBytes,
}

impl ReadBudgetResource {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Rows => "result_rows",
            Self::PayloadBytes => "result_payload_bytes",
        }
    }

    /// The `DatabaseConfig` read ceiling that bounds this resource. A
    /// per-request limit or an admitted runtime result budget can be smaller;
    /// the refusal's `limit` is the effective one.
    pub const fn budget_option(self) -> &'static str {
        match self {
            Self::Rows => "max_read_result_rows",
            Self::PayloadBytes => "max_read_result_payload_bytes",
        }
    }
}

/// Retains budget identity independently of the execution diagnostic text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadBudgetError {
    pub resource: ReadBudgetResource,
    pub limit: usize,
    pub message: String,
}

impl Display for ReadBudgetError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.message)
    }
}

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

/// Why a search projection rebuild was refused before it installed anything.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchProjectionRebuildRefusal {
    /// The target name already exists; the rebuild writes only to a new directory.
    TargetExists,
    /// The target's parent directory does not exist.
    TargetParentMissing,
    /// The target path has no final component.
    TargetInvalid,
    /// Another entry appeared at the target name before the no-replace install.
    TargetAppeared,
    /// The built generation differs from the pinned graph scan.
    VerificationMismatch,
}

impl SearchProjectionRebuildRefusal {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::TargetExists => "target_exists",
            Self::TargetParentMissing => "target_parent_missing",
            Self::TargetInvalid => "target_invalid",
            Self::TargetAppeared => "target_appeared",
            Self::VerificationMismatch => "verification_mismatch",
        }
    }
}

/// Why a metadata-only search projection epoch advance was refused before it
/// published anything. The projection keeps its previous epoch; the host can
/// fall back to an ordinary apply or catch-up.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchProjectionEpochAdvanceRefusal {
    /// A graph commit in the range changed something that maps to a search
    /// projection document, so the projection content may differ at the target.
    ProjectionChanged,
    /// The retained search change log does not cover the whole range: a
    /// commit in it could not be captured, or its entries were trimmed.
    ChangeLogGap,
    /// The range cannot be checked: the projection has no source epoch, or the
    /// target is beyond the graph's current commit epoch.
    RangeUnavailable,
    /// The resident projection holds changes that are not checkpointed, so its
    /// durable files do not describe the in-memory projection.
    UncheckpointedChanges,
}

impl SearchProjectionEpochAdvanceRefusal {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::ProjectionChanged => "projection_changed",
            Self::ChangeLogGap => "change_log_gap",
            Self::RangeUnavailable => "range_unavailable",
            Self::UncheckpointedChanges => "uncheckpointed_changes",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HawDBError {
    Parse(String),
    Semantic(String),
    Storage(String),
    /// A relational constraint violation, such as a duplicate key or a NULL
    /// in a NOT NULL column. It displays as `Storage` or `Execution` would,
    /// by `surface`; `message` is the relational text after that prefix.
    Constraint {
        surface: RelationalErrorSurface,
        message: String,
    },
    /// A relational schema refusal, such as a value of the wrong type for its
    /// column. Displays like [`HawDBError::Constraint`].
    Schema {
        surface: RelationalErrorSurface,
        message: String,
    },
    FileDescriptors(FileDescriptorError),
    StorageIntegrity(String),
    /// Recovery IO may have completed an effect; reopen to resume its audit.
    WalRecoveryIo {
        operation: &'static str,
        kind: std::io::ErrorKind,
        os_code: Option<i32>,
        message: String,
    },
    UnsupportedStorageFormat(StorageFormatError),
    /// A directory publication reached the point where its result may be
    /// visible at `destination`, then failed before success was durable (for
    /// example the parent-directory sync). The destination is not a failed
    /// attempt: never remove it on the strength of this error. Reconcile it
    /// instead (docs/LOGICAL_DUMP.md, "Unknown publication outcome").
    PublishOutcomeUnknown {
        destination: std::path::PathBuf,
        message: String,
    },
    /// A registered Search target is retained by another native operation.
    ProjectionBusy,
    /// A search projection's resident snapshot disagrees with the immutable
    /// projection its selector publishes, for example after a crash inside a
    /// resident checkpoint written by HawDB 0.31.0 or earlier (#1711). The
    /// projection is refused as a whole and is not retryable as-is: rebuild it
    /// from the authoritative graph. The check reads only the projection's own
    /// directory; the graph opens first and is not affected. `projection` is
    /// the projection directory when the refusing open knows it. Displays as
    /// a `storage error`.
    SearchProjectionMismatch {
        projection: Option<std::path::PathBuf>,
        message: String,
    },
    /// A search projection rebuild was refused and installed nothing. The
    /// graph, the current projection directory and the target name are
    /// unchanged; only this attempt is void. This is not a storage failure:
    /// fix the precondition named by `reason` (or retry a verification
    /// mismatch after investigating the host source) and rebuild again.
    SearchProjectionRebuildRefused {
        reason: SearchProjectionRebuildRefusal,
        target: std::path::PathBuf,
        message: String,
    },
    /// A metadata-only search projection epoch advance was refused and
    /// published nothing: the projection keeps `source_graph_commit_epoch`.
    /// This is not a storage failure. The graph and the projection are
    /// unchanged; apply or catch up the range normally instead.
    SearchProjectionEpochAdvanceRefused {
        reason: SearchProjectionEpochAdvanceRefusal,
        source_graph_commit_epoch: Option<u64>,
        target_graph_commit_epoch: u64,
        message: String,
    },
    Execution(String),
    /// Box the diagnostic so successful Result values do not grow with its fields.
    ResourceBudgetExceeded(Box<ResourceBudgetError>),
    ReadBudgetExceeded(ReadBudgetError),
    /// A commit's WAL record would exceed a configured per-record limit. It
    /// is refused before anything is written and leaves the handle usable;
    /// see [`WalRecordLimitError`]. Not a storage failure.
    WalRecordLimitExceeded(Box<WalRecordLimitError>),
    GraphExpansionCandidateLimitExceeded {
        requested: usize,
        limit: usize,
    },
    GraphExpansionPayloadLimitExceeded {
        requested: usize,
        limit: usize,
    },
    TransactionConflict {
        read_epoch: u64,
        committed_epoch: u64,
        key: String,
    },
    /// A pessimistic conflict requires retrying the complete transaction.
    TransactionRetryRequired(Box<TransactionRetryError>),
    /// Requested read validation cannot be proved; never downgrade isolation.
    TransactionReadDependencyRefused {
        reason: String,
    },
    AppendSequenceExhausted {
        table: String,
        watermark: i64,
        requested: usize,
    },
    CapabilityUnavailable {
        capability: RuntimeCapability,
    },
    /// A write was refused because the handle is read-only (for example an
    /// embedded store opened in `GraphMode::ShadowReadOnly`). Nothing was
    /// written. This is a caller error, not a storage failure: reopen the
    /// store writable to perform `operation`.
    ReadOnly {
        operation: &'static str,
    },
    BranchCommandUnsupported {
        command: &'static str,
        context: &'static str,
    },
    BranchBusy {
        resource: &'static str,
    },
    BranchEvidenceBudgetExceeded {
        resource: &'static str,
        requested: u64,
        limit: u64,
    },
    /// An unleased pending create prevents a complete reclamation mark.
    /// This identifies retained evidence, not authorization to discard it.
    BranchReclamationBlocked {
        branch_id: crate::Uuid,
        catalog_revision: u64,
        metadata_revision: u64,
        cause: Box<HawDBError>,
    },
}

impl Display for HawDBError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            HawDBError::Parse(message) => write!(f, "parse error: {message}"),
            HawDBError::Semantic(message) => write!(f, "semantic error: {message}"),
            HawDBError::Storage(message) => write!(f, "storage error: {message}"),
            HawDBError::Constraint { surface, message }
            | HawDBError::Schema { surface, message } => {
                write!(f, "{}: {message}", surface.prefix())
            }
            HawDBError::FileDescriptors(error) => Display::fmt(error, f),
            HawDBError::WalRecoveryIo { operation, message, .. } => write!(f, "WAL recovery {operation} failed; retain evidence and retry open: {message}"),
            HawDBError::StorageIntegrity(message) => {
                write!(f, "storage integrity error: {message}")
            }
            HawDBError::UnsupportedStorageFormat(error) => Display::fmt(error, f),
            HawDBError::PublishOutcomeUnknown { destination, message } => write!(
                f,
                "publication of {} has an unknown outcome; keep the destination and reconcile it before retrying: {message}",
                destination.display()
            ),
            HawDBError::ProjectionBusy => f.write_str("search projection operation is busy"),
            HawDBError::SearchProjectionMismatch { message, .. } => {
                write!(f, "storage error: {message}")
            }
            HawDBError::SearchProjectionRebuildRefused {
                reason, message, ..
            } => write!(
                f,
                "search projection rebuild refused ({}): {message}",
                reason.as_str()
            ),
            HawDBError::SearchProjectionEpochAdvanceRefused {
                reason, message, ..
            } => write!(
                f,
                "search projection epoch advance refused ({}): {message}",
                reason.as_str()
            ),
            HawDBError::Execution(message) => write!(f, "execution error: {message}"),
            HawDBError::ResourceBudgetExceeded(error) => write!(f, "execution error: {error}"),
            HawDBError::ReadBudgetExceeded(error) => write!(f, "execution error: {error}"),
            HawDBError::GraphExpansionCandidateLimitExceeded { requested, limit } => write!(
                f,
                "graph expansion candidate_limit exceeded: requested {requested} rows, limit {limit} rows; configure ExecutionMemoryConfig.graph_expansion_budget.candidate_limit"
            ),
            HawDBError::WalRecordLimitExceeded(error) => write!(f, "execution error: {error}"),
            HawDBError::GraphExpansionPayloadLimitExceeded { requested, limit } => write!(
                f,
                "graph expansion payload_byte_limit exceeded: requested {requested} bytes, limit {limit} bytes; configure ExecutionMemoryConfig.graph_expansion_budget.payload_byte_limit"
            ),
            HawDBError::TransactionConflict {
                read_epoch,
                committed_epoch,
                key,
            } => write!(
                f,
                "transaction conflict on {key}: transaction read epoch {read_epoch}, conflicting commit epoch {committed_epoch}"
            ),
            HawDBError::TransactionRetryRequired(error) => Display::fmt(error, f),
            HawDBError::AppendSequenceExhausted {
                table,
                watermark,
                requested,
            } => write!(
                f,
                "append commit sequence exhausted for table {table}: watermark={watermark}, requested={requested}"
            ),
            HawDBError::TransactionReadDependencyRefused { reason } => {
                write!(f, "transaction read dependency capture refused: {reason}")
            }
            HawDBError::CapabilityUnavailable { capability } => {
                write!(f, "capability unavailable: {}", capability.as_str())
            }
            HawDBError::ReadOnly { operation } => {
                write!(f, "execution error: {operation} requires a writable handle; this handle is read-only")
            }
            HawDBError::BranchCommandUnsupported { command, context } => {
                write!(f, "{command} is unavailable in {context}")
            }
            HawDBError::BranchBusy { resource } => write!(f, "branch is busy: {resource}"),
            HawDBError::BranchEvidenceBudgetExceeded { resource, requested, limit } => write!(
                f,
                "branch quarantine evidence {resource} budget exceeded: requested {requested}, limit {limit}"
            ),
            HawDBError::BranchReclamationBlocked {
                branch_id,
                catalog_revision,
                metadata_revision,
                cause,
            } => write!(
                f,
                "pending branch {branch_id} blocks reclamation at catalog revision {catalog_revision}, branch revision {metadata_revision}: {cause}"
            ),
        }
    }
}

impl std::error::Error for HawDBError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::FileDescriptors(error) => Some(error),
            Self::ResourceBudgetExceeded(error) => Some(error.as_ref()),
            Self::ReadBudgetExceeded(error) => Some(error),
            Self::WalRecordLimitExceeded(error) => Some(error.as_ref()),
            Self::TransactionRetryRequired(error) => Some(error.as_ref()),
            Self::UnsupportedStorageFormat(error) => Some(error),
            Self::BranchReclamationBlocked { cause, .. } => Some(cause.as_ref()),
            _ => None,
        }
    }
}

impl HawDBError {
    pub fn read_budget_exceeded(
        resource: ReadBudgetResource,
        limit: usize,
        message: impl Into<String>,
    ) -> Self {
        Self::ReadBudgetExceeded(ReadBudgetError {
            resource,
            limit,
            message: message.into(),
        })
    }

    /// A relational refusal of `kind`, reported by `surface`.
    pub fn relational(
        kind: RelationalErrorKind,
        surface: RelationalErrorSurface,
        message: impl Into<String>,
    ) -> Self {
        let message = message.into();
        match kind {
            RelationalErrorKind::Constraint => Self::Constraint { surface, message },
            RelationalErrorKind::Schema => Self::Schema { surface, message },
        }
    }

    /// The relational refusal kind, for `Constraint` and `Schema` only.
    pub const fn relational_error_kind(&self) -> Option<RelationalErrorKind> {
        match self {
            Self::Constraint { .. } => Some(RelationalErrorKind::Constraint),
            Self::Schema { .. } => Some(RelationalErrorKind::Schema),
            _ => None,
        }
    }

    /// The layer that reported a `Constraint` or `Schema` refusal.
    pub const fn relational_error_surface(&self) -> Option<RelationalErrorSurface> {
        match self {
            Self::Constraint { surface, .. } | Self::Schema { surface, .. } => Some(*surface),
            _ => None,
        }
    }

    /// Preserve typed storage refusals through nested error wrappers.
    pub fn from_storage_error(error: impl std::error::Error + 'static) -> Self {
        if let Some(error) = (&error as &dyn std::error::Error).downcast_ref::<Self>() {
            return error.clone();
        }
        if let Some(resource) = file_descriptor_error(&error) {
            Self::FileDescriptors(resource)
        } else if let Some(resource) = resource::find_resource_budget_error(&error) {
            Self::from(resource)
        } else if let Some(error) = storage_format::find_storage_format_error(&error) {
            Self::UnsupportedStorageFormat(error)
        } else if let Some(kind) = find_relational_error_kind(&error) {
            Self::relational(kind, RelationalErrorSurface::Storage, error.to_string())
        } else {
            Self::Storage(error.to_string())
        }
    }

    /// `from_storage_error`, prefixing a generic `Storage` message with
    /// `context` and leaving a preserved typed cause (e.g. `FileDescriptors`)
    /// unprefixed and unchanged.
    pub fn from_storage_error_with_context(
        error: impl std::error::Error + 'static,
        context: &str,
    ) -> Self {
        match Self::from_storage_error(error) {
            Self::Storage(message) => Self::Storage(format!("{context}: {message}")),
            Self::Constraint {
                surface: surface @ RelationalErrorSurface::Storage,
                message,
            } => Self::Constraint {
                surface,
                message: format!("{context}: {message}"),
            },
            Self::Schema {
                surface: surface @ RelationalErrorSurface::Storage,
                message,
            } => Self::Schema {
                surface,
                message: format!("{context}: {message}"),
            },
            error => error,
        }
    }

    /// Retry the complete transaction, never only the failed statement.
    /// Lock timeouts and generic execution/storage failures are not classified here.
    pub const fn is_retryable_transaction_conflict(&self) -> bool {
        matches!(
            self,
            Self::TransactionConflict { .. } | Self::TransactionRetryRequired(_)
        )
    }
}

pub type Result<T> = std::result::Result<T, HawDBError>;

impl From<ResourceBudgetError> for HawDBError {
    fn from(error: ResourceBudgetError) -> Self {
        Self::ResourceBudgetExceeded(Box::new(error))
    }
}

impl From<std::io::Error> for HawDBError {
    fn from(error: std::io::Error) -> Self {
        Self::from_storage_error(error)
    }
}

pub fn file_descriptor_error(
    error: &(dyn std::error::Error + 'static),
) -> Option<FileDescriptorError> {
    let mut current = Some(error);
    while let Some(error) = current {
        if let Some(resource) = error.downcast_ref::<FileDescriptorError>() {
            return Some(resource.clone());
        }
        if let Some(HawDBError::FileDescriptors(resource)) = error.downcast_ref::<HawDBError>() {
            return Some(resource.clone());
        }
        if let Some(error) = error.downcast_ref::<std::io::Error>() {
            if let Some(code) = error.raw_os_error()
                && descriptor_os_limit(code)
            {
                return Some(FileDescriptorError::OsLimit {
                    requested: 1,
                    os_code: Some(code),
                    soft: None,
                    hard: None,
                });
            }
            // io::Error::source can skip its boxed concrete error. Inspect it
            // explicitly so typed capacity rejections survive each IO layer.
            if let Some(inner) = error.get_ref()
                && let Some(resource) = file_descriptor_error(inner)
            {
                return Some(resource);
            }
        }
        current = error.source();
    }
    None
}

fn descriptor_os_limit(code: i32) -> bool {
    #[cfg(unix)]
    {
        matches!(code, 23 | 24)
    }
    #[cfg(windows)]
    {
        code == 4
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = code;
        false
    }
}