reifydb_core/
retention.rs

1use serde::{Deserialize, Serialize};
2
3use crate::common::CommitVersion;
4
5/// Retention policy for managing MVCC version cleanup
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub enum RetentionPolicy {
8	/// Keep all versions forever (default)
9	KeepForever,
10
11	/// Keep only the last N versions
12	KeepVersions {
13		count: u64,
14		cleanup_mode: CleanupMode,
15	},
16}
17
18/// Cleanup mode determines how old versions are removed
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20pub enum CleanupMode {
21	/// Create tombstones and CDC entries (only for non-deleted keys)
22	/// Simulates user deletion - maintains audit trail
23	Delete,
24
25	/// Silent removal from storage (works on both live and tombstoned keys)
26	/// No CDC entries, no tombstones - direct storage cleanup
27	Drop,
28}
29
30/// Action to take during cleanup
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum CleanupAction {
33	/// Create tombstone (mark as deleted)
34	Delete,
35
36	/// Remove from storage
37	Drop,
38
39	/// Do nothing
40	Keep,
41}
42
43impl Default for RetentionPolicy {
44	fn default() -> Self {
45		RetentionPolicy::KeepForever
46	}
47}
48
49impl RetentionPolicy {
50	/// Check if a version should be retained based on the policy
51	pub fn should_retain(
52		&self,
53		_version: CommitVersion,
54		_current_version: CommitVersion,
55		version_count: u64,
56	) -> bool {
57		match self {
58			RetentionPolicy::KeepForever => true,
59
60			RetentionPolicy::KeepVersions {
61				count,
62				..
63			} => {
64				// Keep if within the last N versions
65				version_count <= *count
66			}
67		}
68	}
69
70	/// Get the cleanup mode for this policy
71	pub fn cleanup_mode(&self) -> Option<CleanupMode> {
72		match self {
73			RetentionPolicy::KeepForever => None,
74			RetentionPolicy::KeepVersions {
75				cleanup_mode,
76				..
77			} => Some(*cleanup_mode),
78		}
79	}
80}