Skip to main content

coven_database/
write_failure.rs

1//! Preserve the facade's callback failure when a transaction or cleanup also fails.
2//! A committed result never becomes a failure because of cleanup (ยง2.1, Appendix C.3).
3
4use crate::store_failure::StoreFailure;
5use crate::DbError;
6
7/// A caller's write error can retain both its original cause and failures that
8/// happen while rolling back or removing bytes, and says which bug it carries,
9/// for a write whose caller stopped waiting (E15). A write fails with its
10/// closure's own error type, which implements this: `DbError` for ordinary
11/// database clients, sync's error for sync's writes, and an app's own type
12/// for the app's writes through the facade (E3).
13pub trait WriteFailure:
14    From<DbError> + From<sqlive::rusqlite::Error> + coven_foundation::task::CarriesBug
15{
16    /// Retain a failed rollback alongside the original failure.
17    fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self;
18    /// Retain the failed write's error and every failed byte-removal operation.
19    fn with_cleanup(write: Self, failures: Vec<DbError>) -> Self
20    where
21        Self: Sized;
22}
23
24/// The outcome of an operation whose released bytes were then removed. A
25/// failed removal stays recorded for the next write or open (Appendix C.3): it
26/// joins a failed operation's error, and leaves a committed result as it is.
27pub(crate) fn after_cleanup<R, E: WriteFailure>(
28    result: Result<R, E>,
29    failures: Vec<DbError>,
30) -> Result<R, E> {
31    if failures.is_empty() {
32        return result;
33    }
34    match result {
35        Ok(result) => {
36            tracing::error!(
37                ?failures,
38                "removing released file bytes failed; their records stay for the next write or open"
39            );
40            Ok(result)
41        }
42        Err(error) => Err(E::with_cleanup(error, failures)),
43    }
44}
45
46impl WriteFailure for DbError {
47    fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
48        Self::Rollback { operation: Box::new(self), rollback }
49    }
50    fn with_cleanup(write: Self, failures: Vec<DbError>) -> Self {
51        Self::FileCleanup { write: Err(Box::new(write)), failures }
52    }
53}
54
55impl sqlive::RollbackJoin<StoreFailure, DbError> for DbError {
56    fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
57        WriteFailure::with_rollback(self, rollback)
58    }
59}
60
61/// A caller's error `E` inside an engine call. `E` may be an app's or sync's
62/// type, which knows only `DbError`, so the engine's errors reach it as the
63/// `DbError` each maps to.
64pub(crate) struct Hosted<E>(pub(crate) E);
65
66impl<E> Hosted<E> {
67    /// The caller's error.
68    pub(crate) fn into_inner(self) -> E {
69        self.0
70    }
71}
72
73/// Run `run`, whose error is the caller's `E`, inside an engine closure.
74pub(crate) fn hosted<T, E>(run: impl FnOnce() -> Result<T, E>) -> Result<T, Hosted<E>> {
75    run().map_err(Hosted)
76}
77
78impl<E: From<DbError>> From<sqlive::Error<StoreFailure, DbError>> for Hosted<E> {
79    fn from(error: sqlive::Error<StoreFailure, DbError>) -> Self {
80        Self(E::from(DbError::from(error)))
81    }
82}
83
84impl<E: WriteFailure> sqlive::RollbackJoin<StoreFailure, DbError> for Hosted<E> {
85    fn with_rollback(self, rollback: sqlive::rusqlite::Error) -> Self {
86        Self(self.0.with_rollback(rollback))
87    }
88}