Skip to main content

icydb_core/db/
write_context.rs

1//! Module: db::write_context
2//! Responsibility: canonical mutation mode and durable operation-time context.
3//! Does not own: application normalization, validation, or accepted write policy.
4//! Boundary: frontend mutation intent -> accepted after-image resolution.
5
6use crate::types::Timestamp;
7
8///
9/// MutationMode
10///
11/// Exact row-existence contract selected for one database mutation.
12///
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub enum MutationMode {
16    /// Insert only when the target row is absent.
17    Insert,
18
19    /// Replace either an absent or existing target row.
20    Replace,
21
22    /// Update only when the target row exists.
23    Update,
24}
25
26impl MutationMode {
27    /// Project the canonical mutation mode into the bounded diagnostic taxonomy.
28    #[must_use]
29    pub(crate) const fn diagnostic_operation(
30        self,
31    ) -> icydb_diagnostic_code::DiagnosticMutationOperation {
32        match self {
33            Self::Insert => icydb_diagnostic_code::DiagnosticMutationOperation::Insert,
34            Self::Replace => icydb_diagnostic_code::DiagnosticMutationOperation::Replace,
35            Self::Update => icydb_diagnostic_code::DiagnosticMutationOperation::Update,
36        }
37    }
38}
39
40///
41/// AcceptedWriteContext
42///
43/// Database-owned operation facts frozen before accepted after-image
44/// resolution. Application normalizers and validators never receive this
45/// context.
46///
47
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub(in crate::db) struct AcceptedWriteContext {
50    operation_timestamp: Timestamp,
51}
52
53impl AcceptedWriteContext {
54    /// Build one accepted write context from frontend-frozen operation facts.
55    #[must_use]
56    pub(in crate::db) const fn new(operation_timestamp: Timestamp) -> Self {
57        Self {
58            operation_timestamp,
59        }
60    }
61
62    /// Return the durable timestamp shared by the logical operation.
63    #[must_use]
64    pub(in crate::db) const fn operation_timestamp(self) -> Timestamp {
65        self.operation_timestamp
66    }
67}