|
| 1 | +//! Cross-engine bitemporal retention configuration. |
| 2 | +//! |
| 3 | +//! Bitemporal collections split "retention" along two independent axes: |
| 4 | +//! |
| 5 | +//! - `data_retain_ms` — how long the *current* logical row stays queryable |
| 6 | +//! in live reads (the "as of now" view). Superseded by normal |
| 7 | +//! engine-specific retention when the collection is not bitemporal. |
| 8 | +//! - `audit_retain_ms` — how long *superseded versions* of a row are |
| 9 | +//! preserved for historical / audit-time queries (the "as of then" view). |
| 10 | +//! |
| 11 | +//! The minimum audit retention is a floor enforced by policy — some |
| 12 | +//! deployments (regulated, GDPR-on-paper, SOC2) require audit history to |
| 13 | +//! survive beyond the default so operators can't silently configure it |
| 14 | +//! below the compliance floor. |
| 15 | +//! |
| 16 | +//! Lives in `nodedb-types` because multiple engines (Columnar, Array, |
| 17 | +//! EdgeStore, DocumentStrict) compose it into their engine-specific |
| 18 | +//! config. Engine code consults the two axes separately: data purge |
| 19 | +//! deletes the live row when `data_retain_ms` elapses; audit purge |
| 20 | +//! deletes superseded versions when `audit_retain_ms` elapses. |
| 21 | +
|
| 22 | +use serde::{Deserialize, Serialize}; |
| 23 | + |
| 24 | +/// Error validating a [`BitemporalRetention`]. |
| 25 | +#[derive(Debug, thiserror::Error)] |
| 26 | +#[error("bitemporal retention: {field} — {reason}")] |
| 27 | +pub struct RetentionValidationError { |
| 28 | + pub field: String, |
| 29 | + pub reason: String, |
| 30 | +} |
| 31 | + |
| 32 | +/// Per-collection bitemporal retention policy. |
| 33 | +/// |
| 34 | +/// Two independent axes: data (live rows) and audit (superseded |
| 35 | +/// versions). Zero on either axis means "retain forever". |
| 36 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 37 | +pub struct BitemporalRetention { |
| 38 | + /// Retention for currently-live rows (versions where |
| 39 | + /// `_ts_valid_until == MAX`). `0` = retain forever. |
| 40 | + pub data_retain_ms: u64, |
| 41 | + |
| 42 | + /// Retention for superseded versions (versions where |
| 43 | + /// `_ts_valid_until < MAX`). `0` = retain forever. |
| 44 | + pub audit_retain_ms: u64, |
| 45 | + |
| 46 | + /// Policy floor for `audit_retain_ms`. Operators cannot configure |
| 47 | + /// `audit_retain_ms` below this value. `0` = no floor. |
| 48 | + pub minimum_audit_retain_ms: u64, |
| 49 | +} |
| 50 | + |
| 51 | +impl BitemporalRetention { |
| 52 | + /// Retain everything forever. Sensible conservative default for |
| 53 | + /// bitemporal collections where operator intent is unstated. |
| 54 | + pub const fn retain_forever() -> Self { |
| 55 | + Self { |
| 56 | + data_retain_ms: 0, |
| 57 | + audit_retain_ms: 0, |
| 58 | + minimum_audit_retain_ms: 0, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Validate axis consistency and policy floor. |
| 63 | + pub fn validate(&self) -> Result<(), RetentionValidationError> { |
| 64 | + if self.minimum_audit_retain_ms > 0 |
| 65 | + && self.audit_retain_ms > 0 |
| 66 | + && self.audit_retain_ms < self.minimum_audit_retain_ms |
| 67 | + { |
| 68 | + return Err(RetentionValidationError { |
| 69 | + field: "audit_retain_ms".into(), |
| 70 | + reason: format!( |
| 71 | + "{} is below minimum_audit_retain_ms ({})", |
| 72 | + self.audit_retain_ms, self.minimum_audit_retain_ms |
| 73 | + ), |
| 74 | + }); |
| 75 | + } |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl Default for BitemporalRetention { |
| 81 | + fn default() -> Self { |
| 82 | + Self::retain_forever() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn retain_forever_validates() { |
| 92 | + assert!(BitemporalRetention::retain_forever().validate().is_ok()); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn audit_below_floor_rejected() { |
| 97 | + let r = BitemporalRetention { |
| 98 | + data_retain_ms: 0, |
| 99 | + audit_retain_ms: 60_000, |
| 100 | + minimum_audit_retain_ms: 120_000, |
| 101 | + }; |
| 102 | + let err = r.validate().expect_err("must reject"); |
| 103 | + assert_eq!(err.field, "audit_retain_ms"); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn audit_at_or_above_floor_ok() { |
| 108 | + let r = BitemporalRetention { |
| 109 | + data_retain_ms: 0, |
| 110 | + audit_retain_ms: 120_000, |
| 111 | + minimum_audit_retain_ms: 120_000, |
| 112 | + }; |
| 113 | + assert!(r.validate().is_ok()); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn zero_audit_ignores_floor() { |
| 118 | + // audit_retain_ms == 0 means "retain forever" — floor does not apply. |
| 119 | + let r = BitemporalRetention { |
| 120 | + data_retain_ms: 0, |
| 121 | + audit_retain_ms: 0, |
| 122 | + minimum_audit_retain_ms: 120_000, |
| 123 | + }; |
| 124 | + assert!(r.validate().is_ok()); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn data_and_audit_are_independent() { |
| 129 | + let r = BitemporalRetention { |
| 130 | + data_retain_ms: 30_000, |
| 131 | + audit_retain_ms: 300_000, |
| 132 | + minimum_audit_retain_ms: 0, |
| 133 | + }; |
| 134 | + assert!(r.validate().is_ok()); |
| 135 | + } |
| 136 | +} |
0 commit comments