Skip to main content

hawdb_core/error/
wal_record.rs

1// Copyright 2026 Nowledge
2// SPDX-License-Identifier: Apache-2.0
3
4//! A commit whose WAL record exceeds a configured per-record limit.
5
6use std::fmt::{Display, Formatter};
7
8/// The per-record WAL limit a commit exceeded.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum WalRecordLimit {
11    /// The encoded record is larger than `max_wal_record_bytes`.
12    RecordBytes,
13    /// The record holds more operations than `max_wal_batch_operations`.
14    BatchOperations,
15}
16
17impl WalRecordLimit {
18    pub const fn as_str(self) -> &'static str {
19        match self {
20            Self::RecordBytes => "record_bytes",
21            Self::BatchOperations => "batch_operations",
22        }
23    }
24
25    /// The `DatabaseConfig` option that sets this limit.
26    pub const fn config_option(self) -> &'static str {
27        match self {
28            Self::RecordBytes => "DatabaseConfig.max_wal_record_bytes",
29            Self::BatchOperations => "DatabaseConfig.max_wal_batch_operations",
30        }
31    }
32}
33
34/// A commit refused because its single WAL record would exceed `limit`.
35///
36/// The refusal happens before any byte reaches the WAL and before the commit
37/// is applied: the store, its commit epoch and earlier commits are unchanged,
38/// and the handle stays usable. It is a caller error about the size of one
39/// transaction, not a storage failure: split the transaction (for example,
40/// write a very large property value in a separate transaction, or smaller
41/// batches) or raise the option named by [`WalRecordLimit::config_option`].
42/// Retrying the same transaction unchanged fails the same way.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct WalRecordLimitError {
45    pub limit: WalRecordLimit,
46    /// The size of the refused record, in bytes or operations.
47    pub requested: u64,
48    /// The configured maximum, in the same unit.
49    pub maximum: u64,
50}
51
52impl Display for WalRecordLimitError {
53    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
54        match self.limit {
55            WalRecordLimit::RecordBytes => write!(
56                formatter,
57                "WAL record byte limit exceeded before append: the transaction's WAL record is {} bytes, max_wal_record_bytes={}; nothing was written, split the transaction or raise {}",
58                self.requested,
59                self.maximum,
60                self.limit.config_option()
61            ),
62            WalRecordLimit::BatchOperations => write!(
63                formatter,
64                "WAL batch operation limit exceeded before append: the transaction has {} operations, max_wal_batch_operations={}; nothing was written, split the transaction or raise {}",
65                self.requested,
66                self.maximum,
67                self.limit.config_option()
68            ),
69        }
70    }
71}
72
73impl std::error::Error for WalRecordLimitError {}