Skip to main content

alopex_core/
error.rs

1//! Error and Result types for AlopexDB.
2use std::path::PathBuf;
3use thiserror::Error;
4
5use crate::columnar::error::ColumnarError;
6
7/// A convenience `Result` type.
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// The error type for AlopexDB operations.
11#[derive(Debug, Error)]
12pub enum Error {
13    /// The requested key was not found.
14    #[error("key not found")]
15    NotFound,
16
17    /// The transaction has already been closed (committed or rolled back).
18    #[error("transaction is closed")]
19    TxnClosed,
20
21    /// Read-only トランザクションで書き込み操作を試みた。
22    #[error("transaction is read-only")]
23    TxnReadOnly,
24
25    /// A transaction conflict occurred (e.g., optimistic concurrency control failure).
26    #[error("transaction conflict")]
27    TxnConflict,
28
29    /// An underlying I/O error occurred.
30    #[error("io error: {0}")]
31    Io(#[from] std::io::Error),
32
33    /// The on-disk format is invalid or corrupted.
34    #[error("invalid format: {0}")]
35    InvalidFormat(String),
36
37    /// A checksum validation failed.
38    #[error("checksum mismatch")]
39    ChecksumMismatch,
40
41    /// WAL recovery stopped before completion.
42    #[error("recovery incomplete: recovered={recovered_entries}, stop_offset={stop_offset}, reason={reason}")]
43    RecoveryIncomplete {
44        /// Number of entries recovered before stopping.
45        recovered_entries: usize,
46        /// Byte offset where recovery stopped.
47        stop_offset: u64,
48        /// Reason for stopping.
49        reason: String,
50    },
51
52    /// On-disk segment is corrupted (e.g., checksum failure).
53    #[error("corrupted segment {segment_id}: {reason}")]
54    CorruptedSegment {
55        /// Segment identifier.
56        segment_id: u64,
57        /// Reason for corruption detection.
58        reason: String,
59    },
60
61    /// A vector with an unexpected dimension was provided.
62    #[error("dimension mismatch: expected {expected}, got {actual}")]
63    DimensionMismatch {
64        /// Expected dimension.
65        expected: usize,
66        /// Dimension of the provided vector.
67        actual: usize,
68    },
69
70    /// A metric that is not supported was requested.
71    #[error("unsupported metric: {metric}")]
72    UnsupportedMetric {
73        /// Name of the unsupported metric.
74        metric: String,
75    },
76
77    /// A vector value is invalid for the requested operation.
78    #[error("invalid vector at index {index}: {reason}")]
79    InvalidVector {
80        /// Zero-based index of the offending vector.
81        index: usize,
82        /// Reason for invalidation.
83        reason: String,
84    },
85
86    /// A filter expression is malformed or unsupported.
87    #[error("invalid filter: {0}")]
88    InvalidFilter(String),
89
90    /// Memory usage exceeded configured limit.
91    #[error("memory limit exceeded: limit={limit}, requested={requested}")]
92    MemoryLimitExceeded {
93        /// Maximum allowed memory (bytes).
94        limit: usize,
95        /// Requested memory (bytes) that triggered the limit.
96        requested: usize,
97    },
98
99    /// External spill operation failed.
100    #[error("spill failed: {reason}")]
101    SpillFailed {
102        /// Stable failure description.
103        reason: String,
104    },
105
106    /// A DataFrame scalar cast failed.
107    #[error("cast failed: from={from_type}, to={to_type}, reason={reason}")]
108    CastFailed {
109        /// Source type name.
110        from_type: String,
111        /// Target type name.
112        to_type: String,
113        /// Stable failure code.
114        reason: String,
115    },
116
117    /// The provided path already exists and cannot be overwritten.
118    #[error("path exists: {0}")]
119    PathExists(PathBuf),
120
121    /// The data directory is already open somewhere else (issue #181).
122    ///
123    /// Alopex's storage engine has exactly one writer per data directory: the
124    /// WAL is a fixed-length ring addressed from an in-memory offset and
125    /// SSTable ids come from a process-local counter, so a second writer
126    /// overwrites the first one's bytes. Opening therefore takes an OS-level
127    /// exclusive lock, and this is what a caller that lost the race sees.
128    ///
129    /// The message deliberately contains the stable, greppable phrase
130    /// `already open by another process`; tests and user-facing tooling match
131    /// on that substring rather than on the whole rendering, which varies with
132    /// the holder diagnostics (unavailable on Windows — see 裁定 D10).
133    #[error(
134        "data directory {path} is already open by another process ({holder}); \
135         an Alopex database can only be opened by one process at a time — \
136         share it through alopex-server instead (lock file: {lock_path})"
137    )]
138    AlreadyOpen {
139        /// The data directory that could not be opened.
140        path: PathBuf,
141        /// The lock file guarding it.
142        lock_path: PathBuf,
143        /// Best-effort description of the current holder, or `unknown`.
144        holder: String,
145    },
146
147    /// An index configuration parameter is invalid.
148    #[error("invalid parameter {param}: {reason}")]
149    InvalidParameter {
150        /// Name of the invalid parameter.
151        param: String,
152        /// Description of the expected range or constraint.
153        reason: String,
154    },
155
156    /// An index with the requested name was not found.
157    #[error("index not found: {name}")]
158    IndexNotFound {
159        /// Name of the missing index.
160        name: String,
161    },
162
163    /// An index failed integrity checks.
164    #[error("corrupted index {name}: {reason}")]
165    CorruptedIndex {
166        /// Name of the corrupted index.
167        name: String,
168        /// Description of the corruption.
169        reason: String,
170    },
171
172    /// The stored index version is unsupported by this binary.
173    #[error("unsupported index version: found {found}, supported {supported}")]
174    UnsupportedIndexVersion {
175        /// Version detected in storage.
176        found: u32,
177        /// Highest supported version.
178        supported: u32,
179    },
180
181    /// An unknown configuration or runtime option was provided.
182    #[error("unknown option: {key}")]
183    UnknownOption {
184        /// Name of the option.
185        key: String,
186    },
187
188    /// A column type does not match the expected layout.
189    #[error("invalid column type for {column}, expected {expected}")]
190    InvalidColumnType {
191        /// Column name.
192        column: String,
193        /// Expected type description.
194        expected: String,
195    },
196
197    /// The index is busy and cannot serve the requested operation.
198    #[error("index busy during {operation}")]
199    IndexBusy {
200        /// Operation that was attempted.
201        operation: String,
202    },
203
204    /// Errors originating from columnar components.
205    #[error("columnar error: {0}")]
206    Columnar(#[from] ColumnarError),
207
208    /// The unified `.alopex` container format rejected a read or write.
209    #[error("container format error: {0}")]
210    ContainerFormat(#[from] crate::storage::format::FormatError),
211
212    /// An S3 operation failed.
213    #[error("S3 error: {0}")]
214    S3(String),
215
216    /// Required credentials are missing.
217    #[error("missing credentials: {0}")]
218    MissingCredentials(String),
219}