Skip to main content

valence_core/ttl/
policy.rs

1//! TTL policy types and legacy adapter trait.
2
3use crate::error::Result;
4use serde::{Deserialize, Serialize};
5
6/// Table-level time-to-live policy from the schema DSL (`ttl: { seconds, mode }`).
7///
8/// Expiry is **create-only**: set when a row is created (or creating upsert);
9/// updates and merges do not refresh the clock.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct SchemaTtlPolicy {
12    /// Lifetime in seconds from create time.
13    pub seconds: u64,
14    /// Policy mode string; default from the DSL is `"backend_capability"`.
15    pub mode: String,
16}
17
18/// Whether a storage adapter can enforce schema TTL natively.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum BackendTtlCapability {
21    /// Engine deletes expired rows (Redis `EXPIRE`, Mongo TTL index).
22    SupportedNative,
23    /// Rows are stamped with [`super::EXPIRE_AT_FIELD`]; host must wire a platform sweeper (Future).
24    Deferred,
25    /// No native TTL and no stamp path for this engine.
26    Unsupported,
27}
28
29/// Legacy dual surface — prefer [`crate::DatabaseBackend::ttl_capability`] /
30/// [`crate::DatabaseBackend::apply_ttl_policy`]. Do not add new implementors.
31#[async_trait::async_trait]
32pub trait BackendTtlAdapter: Send + Sync {
33    /// Capability for this adapter.
34    fn capability(&self) -> BackendTtlCapability;
35    /// Apply a table TTL policy when supported.
36    async fn apply_table_policy(&self, table: &str, policy: &SchemaTtlPolicy) -> Result<()>;
37}