linkrs-core 0.1.0

Core data structures, types, and errors for Linkrs
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
//! Storage layer error type
//!
//! Errors related to the underlying storage operations of the database.
//!
//! ## Design
//!
//! `StorageError` is a struct with boxed source error to keep size small (~24 bytes).
//! This follows the same pattern as `DBError` and `QueryError` for consistency.

use std::error::Error;

use super::BoxedError;
use crate::error::codes::{ErrorCode, PublicError, ToPublicError};

/// Storage layer result type
pub type StorageResult<T> = Result<T, StorageError>;

/// Storage error kind enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StorageErrorKind {
    DbError,
    StorageError,
    SerializeError,
    DeserializeError,
    NodeNotFound,
    EdgeNotFound,
    NotSupported,
    Conflict,
    LockTimeout,
    Deadlock,
    IOError,
    NotFound,
    AlreadyExists,
    InvalidInput,
    ParseError,
    VertexNotFound,
    VertexAlreadyExists,
    EdgeAlreadyExists,
    LabelNotFound,
    LabelAlreadyExists,
    PropertyNotFound,
    ColumnNotFound,
    ColumnAlreadyExists,
    StorageNotOpen,
    CapacityExceeded,
    NullValueNotAllowed,
    TypeMismatch,
    InvalidOffset,
    InvalidOperation,
    WalError,
    CompressError,
    DecompressError,
    DataCorruption,
    HistoryBeforeFloor,
}

impl StorageErrorKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            StorageErrorKind::DbError => "db_error",
            StorageErrorKind::StorageError => "storage_error",
            StorageErrorKind::SerializeError => "serialize_error",
            StorageErrorKind::DeserializeError => "deserialize_error",
            StorageErrorKind::NodeNotFound => "node_not_found",
            StorageErrorKind::EdgeNotFound => "edge_not_found",
            StorageErrorKind::NotSupported => "not_supported",
            StorageErrorKind::Conflict => "conflict",
            StorageErrorKind::LockTimeout => "lock_timeout",
            StorageErrorKind::Deadlock => "deadlock",
            StorageErrorKind::IOError => "io_error",
            StorageErrorKind::NotFound => "not_found",
            StorageErrorKind::AlreadyExists => "already_exists",
            StorageErrorKind::InvalidInput => "invalid_input",
            StorageErrorKind::ParseError => "parse_error",
            StorageErrorKind::VertexNotFound => "vertex_not_found",
            StorageErrorKind::VertexAlreadyExists => "vertex_already_exists",
            StorageErrorKind::EdgeAlreadyExists => "edge_already_exists",
            StorageErrorKind::LabelNotFound => "label_not_found",
            StorageErrorKind::LabelAlreadyExists => "label_already_exists",
            StorageErrorKind::PropertyNotFound => "property_not_found",
            StorageErrorKind::ColumnNotFound => "column_not_found",
            StorageErrorKind::ColumnAlreadyExists => "column_already_exists",
            StorageErrorKind::StorageNotOpen => "storage_not_open",
            StorageErrorKind::CapacityExceeded => "capacity_exceeded",
            StorageErrorKind::NullValueNotAllowed => "null_value_not_allowed",
            StorageErrorKind::TypeMismatch => "type_mismatch",
            StorageErrorKind::InvalidOffset => "invalid_offset",
            StorageErrorKind::InvalidOperation => "invalid_operation",
            StorageErrorKind::WalError => "wal_error",
            StorageErrorKind::CompressError => "compress_error",
            StorageErrorKind::DecompressError => "decompress_error",
            StorageErrorKind::DataCorruption => "data_corruption",
            StorageErrorKind::HistoryBeforeFloor => "history_before_floor",
        }
    }
}

/// Storage layer error type
///
/// Design principles:
/// 1. Small size: Uses boxed errors to keep struct size minimal (~24 bytes)
/// 2. Full context: Preserves error chain
/// 3. Clone support: Can be cloned for logging/propagation
#[derive(Debug)]
pub struct StorageError {
    kind: StorageErrorKind,
    message: String,
    source: Option<BoxedError>,
}

impl StorageError {
    pub fn new(kind: StorageErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
            source: None,
        }
    }

    pub fn with_source(mut self, source: BoxedError) -> Self {
        self.source = Some(source);
        self
    }

    pub fn kind(&self) -> StorageErrorKind {
        self.kind
    }

    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn source(&self) -> &Option<BoxedError> {
        &self.source
    }

    fn from_boxed<E: Error + Send + Sync + 'static>(kind: StorageErrorKind, error: E) -> Self {
        Self {
            kind,
            message: error.to_string(),
            source: Some(Box::new(error)),
        }
    }

    pub fn is_retryable(&self) -> bool {
        matches!(
            self.kind,
            StorageErrorKind::LockTimeout | StorageErrorKind::Deadlock
        )
    }

    // Convenience constructors
    pub fn db_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::DbError, message)
    }

    #[allow(clippy::self_named_constructors)]
    pub fn storage_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::StorageError, message)
    }

    pub fn serialize_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::SerializeError, message)
    }

    pub fn deserialize_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::DeserializeError, message)
    }

    pub fn node_not_found(value: crate::types::VertexId) -> Self {
        Self::new(StorageErrorKind::NodeNotFound, format!("{}", value))
    }

    pub fn edge_not_found(value: crate::types::VertexId) -> Self {
        Self::new(StorageErrorKind::EdgeNotFound, format!("{}", value))
    }

    pub fn not_supported(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::NotSupported, message)
    }

    pub fn conflict(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::Conflict, message)
    }

    /// A write-write conflict detected at the storage layer write path.
    ///
    /// Two writes touched the same entity with incompatible timestamps (e.g.
    /// deleting an edge that another write already deleted at a different
    /// timestamp). This is the first line of defense; the transaction layer's
    /// commit-time WriteSet certification remains the fallback.
    pub fn write_write_conflict(entity: impl Into<String>) -> Self {
        Self::new(
            StorageErrorKind::Conflict,
            format!("Write-write conflict: {}", entity.into()),
        )
    }

    pub fn lock_timeout(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::LockTimeout, message)
    }

    pub fn deadlock() -> Self {
        Self::new(StorageErrorKind::Deadlock, "Deadlock detected")
    }

    pub fn io_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::IOError, message)
    }

    pub fn not_found(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::NotFound, message)
    }

    pub fn already_exists(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::AlreadyExists, message)
    }

    pub fn invalid_input(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::InvalidInput, message)
    }

    pub fn parse_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::ParseError, message)
    }

    pub fn vertex_not_found() -> Self {
        Self::new(StorageErrorKind::VertexNotFound, "Vertex not found")
    }

    pub fn vertex_already_exists(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::VertexAlreadyExists, message)
    }

    pub fn edge_already_exists(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::EdgeAlreadyExists, message)
    }

    pub fn label_not_found(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::LabelNotFound, message)
    }

    pub fn label_already_exists(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::LabelAlreadyExists, message)
    }

    pub fn property_not_found(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::PropertyNotFound, message)
    }

    pub fn column_not_found(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::ColumnNotFound, message)
    }

    pub fn column_already_exists(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::ColumnAlreadyExists, message)
    }

    pub fn storage_not_open() -> Self {
        Self::new(StorageErrorKind::StorageNotOpen, "Storage not open")
    }

    pub fn capacity_exceeded() -> Self {
        Self::new(StorageErrorKind::CapacityExceeded, "Capacity exceeded")
    }

    pub fn null_value_not_allowed(column: impl Into<String>) -> Self {
        Self::new(
            StorageErrorKind::NullValueNotAllowed,
            format!("Null value not allowed for column: {}", column.into()),
        )
    }

    pub fn type_mismatch(expected: crate::DataType, actual: crate::DataType) -> Self {
        Self::new(
            StorageErrorKind::TypeMismatch,
            format!("Type mismatch: expected {:?}, got {:?}", expected, actual),
        )
    }

    pub fn invalid_offset(offset: u32) -> Self {
        Self::new(
            StorageErrorKind::InvalidOffset,
            format!("Invalid offset: {}", offset),
        )
    }

    pub fn invalid_operation(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::InvalidOperation, message)
    }

    pub fn wal_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::WalError, message)
    }

    pub fn compress_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::CompressError, message)
    }

    pub fn decompress_error(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::DecompressError, message)
    }

    pub fn data_corruption(message: impl Into<String>) -> Self {
        Self::new(StorageErrorKind::DataCorruption, message)
    }

    pub fn history_before_floor(row: u32, ts: u64, floor: u64) -> Self {
        Self::new(
            StorageErrorKind::HistoryBeforeFloor,
            format!(
                "vertex row {} history at {} before epoch floor {} is not retained; version chains do not survive checkpoints",
                row, ts, floor
            ),
        )
    }

    pub fn history_before_floor_edge(edge: impl Into<String>, ts: u64, floor: u64) -> Self {
        Self::new(
            StorageErrorKind::HistoryBeforeFloor,
            format!(
                "edge {} history at {} before epoch floor {} is not retained; version chains do not survive checkpoints",
                edge.into(),
                ts,
                floor
            ),
        )
    }
}

impl std::fmt::Display for StorageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[{}] {}", self.kind.as_str(), self.message)?;
        if let Some(ref source) = self.source {
            write!(f, "\n  Caused by: {}", source)?;
        }
        Ok(())
    }
}

impl Clone for StorageError {
    fn clone(&self) -> Self {
        Self {
            kind: self.kind,
            message: self.message.clone(),
            source: None,
        }
    }
}

impl std::error::Error for StorageError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_ref()
            .map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
    }
}

impl From<std::io::Error> for StorageError {
    fn from(e: std::io::Error) -> Self {
        Self::from_boxed(StorageErrorKind::IOError, e)
    }
}

impl From<String> for StorageError {
    fn from(s: String) -> Self {
        Self::db_error(s)
    }
}

impl From<&str> for StorageError {
    fn from(s: &str) -> Self {
        Self::db_error(s)
    }
}

impl<T> From<std::sync::PoisonError<T>> for StorageError {
    fn from(e: std::sync::PoisonError<T>) -> Self {
        Self::db_error(e.to_string())
    }
}

impl From<postcard::Error> for StorageError {
    fn from(e: postcard::Error) -> Self {
        Self::from_boxed(StorageErrorKind::SerializeError, e)
    }
}

impl From<crate::types::UndoLogError> for StorageError {
    fn from(e: crate::types::UndoLogError) -> Self {
        Self::db_error(e.to_string())
    }
}

impl ToPublicError for StorageError {
    fn to_public_error(&self) -> PublicError {
        PublicError::new(self.to_error_code(), self.to_public_message())
    }

    fn to_error_code(&self) -> ErrorCode {
        match self.kind {
            StorageErrorKind::NodeNotFound
            | StorageErrorKind::EdgeNotFound
            | StorageErrorKind::NotFound => ErrorCode::ResourceNotFound,
            StorageErrorKind::AlreadyExists => ErrorCode::ResourceAlreadyExists,
            StorageErrorKind::InvalidInput
            | StorageErrorKind::InvalidOperation
            | StorageErrorKind::HistoryBeforeFloor => ErrorCode::InvalidInput,
            StorageErrorKind::LockTimeout => ErrorCode::Timeout,
            StorageErrorKind::Deadlock => ErrorCode::Deadlock,
            StorageErrorKind::Conflict => ErrorCode::Conflict,
            StorageErrorKind::NotSupported => ErrorCode::InvalidStatement,
            _ => ErrorCode::InternalError,
        }
    }

    fn to_public_message(&self) -> String {
        match self.kind {
            StorageErrorKind::NodeNotFound => "Node does not exist".to_string(),
            StorageErrorKind::EdgeNotFound => "Edge does not exist".to_string(),
            StorageErrorKind::NotFound => format!("Resource not found: {}", self.message),
            StorageErrorKind::AlreadyExists => format!("Resource already exists: {}", self.message),
            _ => "Storage operation failed".to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_storage_error_size() {
        assert!(
            std::mem::size_of::<StorageError>() <= 64,
            "StorageError should be small, got {} bytes",
            std::mem::size_of::<StorageError>()
        );
    }

    #[test]
    fn test_storage_error_creation() {
        let err = StorageError::not_found("test resource");
        assert_eq!(err.kind(), StorageErrorKind::NotFound);
        assert!(err.message().contains("test resource"));
    }

    #[test]
    fn test_retryable_error() {
        let err = StorageError::lock_timeout("test");
        assert!(err.is_retryable());

        let err = StorageError::deadlock();
        assert!(err.is_retryable());

        let err = StorageError::not_found("test");
        assert!(!err.is_retryable());
    }
}