Skip to main content

reifydb_core/
retention.rs

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