datum-persistence 0.11.3

Event sourcing, durable state, and CQRS persistence for Datum
Documentation
//! Byte-oriented durable-state storage contract.

use crate::{PersistenceId, PersistenceResult, Revision, SerializedPayload, Tag};

/// Latest serialized durable state together with its CAS revision.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VersionedState {
    /// Current state, or `None` when absent or deleted by a tombstone.
    pub state: Option<SerializedPayload>,
    /// Current revision. An id never written or deleted has revision zero.
    pub revision: Revision,
    /// Current query tag, or `None` when absent, untagged, or deleted.
    pub tag: Option<Tag>,
}

impl Default for VersionedState {
    fn default() -> Self {
        Self {
            state: None,
            revision: Revision::new(0),
            tag: None,
        }
    }
}

/// Byte-oriented durable-state storage provider.
///
/// The optional tag is part of the observable latest-value contract: upsert
/// replaces it and delete clears it. [`crate::assert_durable_state_store_conformance`]
/// is the normative executable contract for revisions, tombstones, and tags.
#[async_trait::async_trait]
pub trait DurableStateStore: Send + Sync {
    /// Gets the latest value and revision. Unknown ids return absent state at
    /// revision zero.
    async fn get(&self, persistence_id: &PersistenceId) -> PersistenceResult<VersionedState>;

    /// Stores `state` when `new_revision == stored_revision + 1`.
    ///
    /// A mismatch returns [`crate::PersistenceError::RevisionConflict`], never
    /// a generic storage failure. The first successful write uses revision one.
    /// A successful upsert replaces both the state and its optional tag.
    async fn upsert(
        &self,
        persistence_id: &PersistenceId,
        new_revision: Revision,
        state: SerializedPayload,
        tag: Option<Tag>,
    ) -> PersistenceResult<()>;

    /// Advances the revision under the same CAS rule, clears the tag, and leaves
    /// a tombstone.
    async fn delete(
        &self,
        persistence_id: &PersistenceId,
        new_revision: Revision,
    ) -> PersistenceResult<()>;
}