1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! Compaction strategies
use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime};
/// Compaction strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum CompactionStrategy {
/// Periodic compaction at fixed intervals
Periodic,
/// Threshold-based (compact when fragmentation exceeds threshold)
ThresholdBased,
/// Size-based (compact when wasted space exceeds threshold)
SizeBased,
/// Adaptive (automatically adjust based on workload)
#[default]
Adaptive,
/// Manual (only compact when explicitly triggered)
Manual,
}
/// Strategy evaluator
pub struct StrategyEvaluator {
strategy: CompactionStrategy,
last_compaction: Option<SystemTime>,
}
impl StrategyEvaluator {
/// Create a new strategy evaluator
pub fn new(strategy: CompactionStrategy) -> Self {
Self {
strategy,
last_compaction: None,
}
}
/// Check if compaction should be triggered
pub fn should_compact(
&self,
fragmentation: f64,
wasted_bytes: u64,
time_since_last: Option<Duration>,
interval: Duration,
fragmentation_threshold: f64,
size_threshold_bytes: u64,
) -> bool {
match self.strategy {
CompactionStrategy::Periodic => {
// Compact if interval has elapsed
time_since_last.map(|d| d >= interval).unwrap_or(true)
}
CompactionStrategy::ThresholdBased => {
// Compact if fragmentation exceeds threshold
fragmentation >= fragmentation_threshold
}
CompactionStrategy::SizeBased => {
// Compact if wasted space exceeds threshold
wasted_bytes >= size_threshold_bytes
}
CompactionStrategy::Adaptive => {
// Combine multiple factors
self.evaluate_adaptive(
fragmentation,
wasted_bytes,
time_since_last,
interval,
fragmentation_threshold,
size_threshold_bytes,
)
}
CompactionStrategy::Manual => {
// Never automatically compact
false
}
}
}
/// Adaptive evaluation combining multiple factors
fn evaluate_adaptive(
&self,
fragmentation: f64,
wasted_bytes: u64,
time_since_last: Option<Duration>,
interval: Duration,
fragmentation_threshold: f64,
size_threshold_bytes: u64,
) -> bool {
let mut score = 0.0;
// Factor 1: Fragmentation (40% weight)
if fragmentation > 0.0 {
let frag_ratio = fragmentation / fragmentation_threshold;
score += frag_ratio.min(1.0) * 0.4;
}
// Factor 2: Wasted space (30% weight)
if wasted_bytes > 0 {
let size_ratio = wasted_bytes as f64 / size_threshold_bytes as f64;
score += size_ratio.min(1.0) * 0.3;
}
// Factor 3: Time since last compaction (30% weight)
if let Some(time_elapsed) = time_since_last {
let time_ratio = time_elapsed.as_secs_f64() / interval.as_secs_f64();
score += time_ratio.min(1.0) * 0.3;
} else {
// Never compacted, give full weight
score += 0.3;
}
// Trigger if score exceeds threshold
score >= 0.7
}
/// Update last compaction time
pub fn record_compaction(&mut self) {
self.last_compaction = Some(SystemTime::now());
}
/// Get time since last compaction
pub fn time_since_last_compaction(&self) -> Option<Duration> {
self.last_compaction
.and_then(|t| SystemTime::now().duration_since(t).ok())
}
/// Calculate priority for compaction (0.0 - 1.0, higher = more urgent)
pub fn calculate_priority(
&self,
fragmentation: f64,
wasted_bytes: u64,
time_since_last: Option<Duration>,
) -> f64 {
match self.strategy {
CompactionStrategy::Periodic => {
// Priority based on time
time_since_last
.map(|d| (d.as_secs() as f64 / 3600.0).min(1.0))
.unwrap_or(1.0)
}
CompactionStrategy::ThresholdBased => {
// Priority based on fragmentation
fragmentation
}
CompactionStrategy::SizeBased => {
// Priority based on wasted space
let size_gb = wasted_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
(size_gb / 10.0).min(1.0) // Normalize to 10GB max
}
CompactionStrategy::Adaptive => {
// Weighted combination
let frag_priority = fragmentation * 0.4;
let size_priority =
(wasted_bytes as f64 / (1024.0 * 1024.0 * 1024.0) / 10.0).min(1.0) * 0.3;
let time_priority = time_since_last
.map(|d| (d.as_secs() as f64 / 3600.0).min(1.0) * 0.3)
.unwrap_or(0.3);
(frag_priority + size_priority + time_priority).min(1.0)
}
CompactionStrategy::Manual => 0.0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_periodic_strategy() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::Periodic);
// Should compact if enough time has passed
assert!(evaluator.should_compact(
0.1,
0,
Some(Duration::from_secs(7200)),
Duration::from_secs(3600),
0.3,
100_000_000,
));
// Should not compact if not enough time
assert!(!evaluator.should_compact(
0.5,
1_000_000_000,
Some(Duration::from_secs(1800)),
Duration::from_secs(3600),
0.3,
100_000_000,
));
}
#[test]
fn test_threshold_based_strategy() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::ThresholdBased);
// Should compact if fragmentation exceeds threshold
assert!(evaluator.should_compact(
0.5,
0,
None,
Duration::from_secs(3600),
0.3,
100_000_000,
));
// Should not compact if below threshold
assert!(!evaluator.should_compact(
0.2,
1_000_000_000,
None,
Duration::from_secs(3600),
0.3,
100_000_000,
));
}
#[test]
fn test_size_based_strategy() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::SizeBased);
// Should compact if wasted space exceeds threshold
assert!(evaluator.should_compact(
0.1,
200_000_000,
None,
Duration::from_secs(3600),
0.3,
100_000_000,
));
// Should not compact if below threshold
assert!(!evaluator.should_compact(
0.5,
50_000_000,
None,
Duration::from_secs(3600),
0.3,
100_000_000,
));
}
#[test]
fn test_adaptive_strategy() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::Adaptive);
// Should compact with high fragmentation and time
assert!(evaluator.should_compact(
0.4,
150_000_000,
Some(Duration::from_secs(7200)),
Duration::from_secs(3600),
0.3,
100_000_000,
));
// Should not compact with low values
assert!(!evaluator.should_compact(
0.05,
10_000_000,
Some(Duration::from_secs(300)),
Duration::from_secs(3600),
0.3,
100_000_000,
));
}
#[test]
fn test_manual_strategy() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::Manual);
// Should never auto-compact
assert!(!evaluator.should_compact(
0.9,
10_000_000_000,
Some(Duration::from_secs(100000)),
Duration::from_secs(3600),
0.3,
100_000_000,
));
}
#[test]
fn test_priority_calculation() {
let evaluator = StrategyEvaluator::new(CompactionStrategy::Adaptive);
let priority =
evaluator.calculate_priority(0.5, 500_000_000, Some(Duration::from_secs(7200)));
assert!(priority > 0.0 && priority <= 1.0);
}
}