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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//! Compaction edge-case and idempotency tests.
//!
//! These tests verify compaction behaviors that are not covered by the
//! main compaction test suite: idempotent re-runs, single-entry SSTables,
//! compaction with minimal (1-byte) values, overlapping range tombstones
//! across SSTables, and tombstone compaction threshold behavior.
//!
//! ## See also
//! - [`compaction::stcs::tests`] — core compaction correctness
//! - [`tests_crash_compaction`] — crash during compaction
//! - [`tests_file_cleanup`] — file-count verification after compaction
#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
use crate::engine::tests::helpers::*;
use crate::engine::{Engine, EngineConfig};
use tempfile::TempDir;
fn compaction_config() -> EngineConfig {
init_tracing();
EngineConfig {
write_buffer_size: 128, // Very small — each key gets its own SSTable.
compaction_strategy: crate::compaction::CompactionStrategyType::Stcs,
bucket_low: 0.5,
bucket_high: 1.5,
min_sstable_size: 64, // Very low — all SSTables qualify as "small bucket".
min_threshold: 2, // Compact with just 2 SSTables.
max_threshold: 32,
tombstone_ratio_threshold: 0.2,
tombstone_compaction_interval: 0, // No age requirement.
tombstone_bloom_fallback: true,
tombstone_range_drop: true,
thread_pool_size: 2,
}
}
// ================================================================
// 1. Idempotent compaction re-run
// ================================================================
/// # Scenario
/// Run minor compaction twice in a row. The second run should find
/// nothing to do and return `Ok(false)` (or compact remaining if any).
///
/// # Expected behavior
/// No panic, no data loss. All keys remain accessible.
#[test]
fn memtable_sstable__minor_compact_idempotent() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
for i in 0..30u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("val_{i:04}_padding").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
// First minor compaction.
let did_compact = engine.minor_compact().unwrap();
assert!(did_compact, "First minor compaction should find work");
// Second minor compaction — should be a no-op or compact remaining.
let _ = engine.minor_compact();
// Verify data integrity.
for i in 0..30u32 {
let key = format!("key_{i:04}").into_bytes();
assert!(
engine.get(key).unwrap().is_some(),
"key_{i:04} must be readable after double compaction"
);
}
}
// ================================================================
// 2. Major compaction idempotent re-run
// ================================================================
/// # Scenario
/// Run major compaction twice. The second run should see only 1
/// SSTable and return `Ok(false)`.
///
/// # Expected behavior
/// Second `major_compact()` returns `false`. Data intact.
#[test]
fn memtable_sstable__major_compact_idempotent() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = engine_with_multi_sstables(path, 200, "key");
let first = engine.major_compact().unwrap();
assert!(first, "First major compaction should do work");
let second = engine.major_compact().unwrap();
assert!(
!second,
"Second major compaction should be no-op (1 SSTable)"
);
for i in 0..200u32 {
let key = format!("key_{i:04}").into_bytes();
assert!(engine.get(key).unwrap().is_some());
}
}
// ================================================================
// 3. Compaction with minimal 1-byte values
// ================================================================
/// # Scenario
/// Write keys with 1-byte values, flush, compact. Very small SSTables
/// test the edge case where record overhead dominates payload.
///
/// # Expected behavior
/// Compaction produces a valid merged SSTable. All keys accessible.
#[test]
fn memtable_sstable__compaction_with_1byte_values() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
for i in 0..20u32 {
engine
.put(format!("k{i:02}").into_bytes(), vec![i as u8])
.unwrap();
}
engine.flush_all_frozen().unwrap();
engine.minor_compact().unwrap();
for i in 0..20u32 {
let val = engine
.get(format!("k{i:02}").into_bytes())
.unwrap()
.unwrap();
assert_eq!(val, vec![i as u8]);
}
}
// ================================================================
// 4. Compaction with overlapping range tombstones
// ================================================================
/// # Scenario
/// Two SSTables contain overlapping range tombstones covering the
/// same key space. After compaction, the merged range tombstone
/// should correctly hide keys in the overlap region.
///
/// # Expected behavior
/// Keys in the overlapping range are deleted. Keys outside are intact.
#[test]
fn memtable_sstable__compaction_overlapping_range_tombstones() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
// Write some keys.
for i in 0..20u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("val_{i:04}").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
// Range delete [key_0005, key_0012).
engine
.delete_range(b"key_0005".to_vec(), b"key_0012".to_vec())
.unwrap();
engine.flush_all_frozen().unwrap();
// Overlapping range delete [key_0008, key_0018).
engine
.delete_range(b"key_0008".to_vec(), b"key_0018".to_vec())
.unwrap();
engine.flush_all_frozen().unwrap();
// Compact.
engine.major_compact().unwrap();
// Keys 0-4: alive (outside both ranges).
for i in 0..5u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_some(),
"key_{i:04} should survive (outside range)"
);
}
// Keys 5-17: deleted (covered by one or both range tombstones).
for i in 5..18u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_none(),
"key_{i:04} should be deleted (in overlapping range)"
);
}
// Keys 18-19: alive (outside both ranges).
for i in 18..20u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_some(),
"key_{i:04} should survive (outside range)"
);
}
}
// ================================================================
// 5. Tombstone compaction with bloom fallback
// ================================================================
/// # Scenario
/// Delete keys and trigger tombstone compaction with `tombstone_bloom_fallback`
/// enabled. Tombstones for keys only in older SSTables should be dropped
/// when the bloom filter confirms no false-positive references.
///
/// # Expected behavior
/// After tombstone compaction, the tombstones are removed (or pruned),
/// but the deleted keys remain invisible.
#[test]
fn memtable_sstable__tombstone_compaction_with_bloom_fallback() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
// Write keys.
for i in 0..20u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("val_{i:04}").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
// Delete half.
for i in 0..10u32 {
engine.delete(format!("key_{i:04}").into_bytes()).unwrap();
}
engine.flush_all_frozen().unwrap();
// Run tombstone compaction.
let _ = engine.tombstone_compact();
// Verify: deleted keys stay deleted, alive keys stay alive.
for i in 0..10u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_none(),
"key_{i:04} should remain deleted"
);
}
for i in 10..20u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_some(),
"key_{i:04} should survive"
);
}
}
// ================================================================
// 6. Tombstone compaction with range tombstone drop
// ================================================================
/// # Scenario
/// Range-delete keys and trigger tombstone compaction with
/// `tombstone_range_drop` enabled. Range tombstones should be
/// dropped when older SSTables have no live keys in the range.
///
/// # Expected behavior
/// Range-deleted keys remain invisible. Alive keys unaffected.
#[test]
fn memtable_sstable__tombstone_compaction_range_drop() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
for i in 0..20u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("val_{i:04}").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
engine
.delete_range(b"key_0005".to_vec(), b"key_0015".to_vec())
.unwrap();
engine.flush_all_frozen().unwrap();
let _ = engine.tombstone_compact();
// Verify: range-deleted keys still deleted.
for i in 5..15u32 {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_none(),
"key_{i:04} should remain range-deleted"
);
}
// Alive keys.
for i in (0..5).chain(15..20) {
assert!(
engine
.get(format!("key_{i:04}").into_bytes())
.unwrap()
.is_some(),
"key_{i:04} should survive"
);
}
}
// ================================================================
// 7. Minor compaction then major — full pipeline
// ================================================================
/// # Scenario
/// Run minor compaction first, then major compaction on the result.
/// Verifies that the two compaction levels compose correctly.
///
/// # Expected behavior
/// After both compactions, exactly 1 SSTable remains.
/// All data intact.
#[test]
fn memtable_sstable__minor_then_major_compaction() {
init_tracing();
let tmp = TempDir::new().unwrap();
let path = tmp.path();
let engine = Engine::open(path, compaction_config()).unwrap();
for i in 0..40u32 {
engine
.put(
format!("key_{i:04}").into_bytes(),
format!("val_{i:04}_padding").into_bytes(),
)
.unwrap();
}
engine.flush_all_frozen().unwrap();
// Minor compact first.
let _ = engine.minor_compact();
// Major compact to merge everything.
engine.major_compact().unwrap();
let stats = engine.stats().unwrap();
assert_eq!(
stats.sstables_count, 1,
"Should have exactly 1 SSTable after minor + major"
);
for i in 0..40u32 {
let key = format!("key_{i:04}").into_bytes();
assert!(engine.get(key).unwrap().is_some());
}
}
}