Skip to main content

reifydb_core/
retention.rs

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