ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed system
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//! Storage Compactor
//!
//! Background compaction manager with fragmentation detection. Tracks storage
//! regions (offset, size, used/free), analyses fragmentation ratios, and merges
//! adjacent free regions to reclaim wasted space.

/// The current lifecycle state of the compactor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompactionState {
    /// No compaction activity.
    Idle,
    /// Fragmentation analysis in progress.
    Analyzing,
    /// Active compaction running.
    Compacting,
    /// Last compaction finished successfully.
    Completed,
    /// Last compaction failed.
    Failed,
}

/// Configuration knobs for [`StorageCompactor`].
#[derive(Debug, Clone)]
pub struct CompactorConfig {
    /// Fragmentation ratio above which compaction is warranted (0.0–1.0).
    /// Default: 0.3 (30%).
    pub fragmentation_threshold: f64,
    /// Maximum bytes the compactor will process in a single run.
    /// Default: 100 MB.
    pub max_budget_bytes: u64,
    /// Minimum number of ticks between compaction runs.
    /// Default: 100.
    pub min_interval_ticks: u64,
}

impl Default for CompactorConfig {
    fn default() -> Self {
        Self {
            fragmentation_threshold: 0.3,
            max_budget_bytes: 100 * 1024 * 1024, // 100 MB
            min_interval_ticks: 100,
        }
    }
}

/// Result of a fragmentation analysis pass.
#[derive(Debug, Clone)]
pub struct FragmentationReport {
    /// Total bytes spanned by all regions.
    pub total_bytes: u64,
    /// Bytes occupied by used regions.
    pub used_bytes: u64,
    /// Ratio of free space to total space: `(total - used) / total`.
    pub fragmentation_ratio: f64,
    /// Number of distinct free (unused) regions.
    pub fragmented_regions: usize,
}

/// Result of a single compaction run.
#[derive(Debug, Clone)]
pub struct CompactionResult {
    /// Number of region-merge operations performed.
    pub regions_merged: usize,
    /// Total bytes freed by merging adjacent free regions.
    pub bytes_reclaimed: u64,
    /// Ticks elapsed during this compaction (always 1 for synchronous runs).
    pub duration_ticks: u64,
}

/// Cumulative statistics for the compactor.
#[derive(Debug, Clone)]
pub struct CompactorStats {
    /// Current lifecycle state.
    pub state: CompactionState,
    /// Number of compaction runs completed.
    pub runs_completed: u64,
    /// Lifetime bytes reclaimed.
    pub bytes_reclaimed_total: u64,
    /// Fragmentation ratio from the most recent analysis.
    pub current_fragmentation: f64,
}

/// Background compaction manager.
///
/// Maintains a set of storage regions described by `(offset, size, is_used)`
/// tuples and can analyse/compact them on demand.
pub struct StorageCompactor {
    config: CompactorConfig,
    state: CompactionState,
    last_run_tick: u64,
    current_tick: u64,
    runs_completed: u64,
    bytes_reclaimed_total: u64,
    /// Storage regions: (offset, size, is_used).
    regions: Vec<(u64, u64, bool)>,
    /// Cached fragmentation ratio from the most recent `analyze()`.
    last_fragmentation: f64,
}

impl StorageCompactor {
    /// Create a new compactor with the given configuration.
    pub fn new(config: CompactorConfig) -> Self {
        Self {
            config,
            state: CompactionState::Idle,
            last_run_tick: 0,
            current_tick: 0,
            runs_completed: 0,
            bytes_reclaimed_total: 0,
            regions: Vec::new(),
            last_fragmentation: 0.0,
        }
    }

    /// Register a storage region.
    pub fn add_region(&mut self, offset: u64, size: u64, is_used: bool) {
        self.regions.push((offset, size, is_used));
    }

    /// Remove the region at the given offset.
    /// Returns `true` if a region was found and removed.
    pub fn remove_region(&mut self, offset: u64) -> bool {
        if let Some(pos) = self.regions.iter().position(|r| r.0 == offset) {
            self.regions.remove(pos);
            true
        } else {
            false
        }
    }

    /// Analyse the current region set and return a fragmentation report.
    ///
    /// Sets the compactor state to [`CompactionState::Analyzing`] during the
    /// call and back to [`CompactionState::Idle`] afterwards (unless the
    /// compactor was already in a terminal state).
    pub fn analyze(&mut self) -> FragmentationReport {
        self.state = CompactionState::Analyzing;

        let total_bytes: u64 = self.regions.iter().map(|r| r.1).sum();
        let used_bytes: u64 = self.regions.iter().filter(|r| r.2).map(|r| r.1).sum();
        let fragmented_regions = self.regions.iter().filter(|r| !r.2).count();

        let fragmentation_ratio = if total_bytes == 0 {
            0.0
        } else {
            (total_bytes - used_bytes) as f64 / total_bytes as f64
        };

        self.last_fragmentation = fragmentation_ratio;

        // Return to idle after analysis
        self.state = CompactionState::Idle;

        FragmentationReport {
            total_bytes,
            used_bytes,
            fragmentation_ratio,
            fragmented_regions,
        }
    }

    /// Returns `true` if compaction should be triggered based on the current
    /// fragmentation ratio and the configured interval.
    pub fn should_compact(&self) -> bool {
        self.last_fragmentation > self.config.fragmentation_threshold
            && (self.current_tick.saturating_sub(self.last_run_tick)
                >= self.config.min_interval_ticks)
    }

    /// Run compaction: sort regions by offset, merge adjacent free regions.
    ///
    /// Returns an error if the compactor is already in the `Compacting` state.
    pub fn compact(&mut self) -> Result<CompactionResult, String> {
        if self.state == CompactionState::Compacting {
            return Err("compaction already in progress".to_string());
        }

        self.state = CompactionState::Compacting;

        // Sort by offset
        self.regions.sort_by_key(|r| r.0);

        let mut merged: Vec<(u64, u64, bool)> = Vec::with_capacity(self.regions.len());
        let mut regions_merged: usize = 0;
        let mut bytes_reclaimed: u64 = 0;
        let mut budget_remaining = self.config.max_budget_bytes;

        for &region in &self.regions {
            if let Some(last) = merged.last_mut() {
                // Two adjacent free regions can be merged if: both free AND
                // the second starts right where the first ends.
                let adjacent = last.0 + last.1 == region.0;
                let both_free = !last.2 && !region.2;

                if adjacent && both_free && budget_remaining >= region.1 {
                    // Merge: grow the last region, count the merge.
                    bytes_reclaimed += region.1;
                    budget_remaining = budget_remaining.saturating_sub(region.1);
                    last.1 += region.1;
                    regions_merged += 1;
                    continue;
                }
            }
            merged.push(region);
        }

        self.regions = merged;
        self.bytes_reclaimed_total += bytes_reclaimed;
        self.runs_completed += 1;
        self.last_run_tick = self.current_tick;
        self.state = CompactionState::Completed;

        Ok(CompactionResult {
            regions_merged,
            bytes_reclaimed,
            duration_ticks: 1,
        })
    }

    /// Advance the logical clock by one tick.
    pub fn tick(&mut self) {
        self.current_tick += 1;
    }

    /// Total bytes reclaimed across all compaction runs.
    pub fn reclaimed_bytes(&self) -> u64 {
        self.bytes_reclaimed_total
    }

    /// Return a snapshot of the compactor's cumulative statistics.
    pub fn stats(&self) -> CompactorStats {
        CompactorStats {
            state: self.state,
            runs_completed: self.runs_completed,
            bytes_reclaimed_total: self.bytes_reclaimed_total,
            current_fragmentation: self.last_fragmentation,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_compactor() -> StorageCompactor {
        StorageCompactor::new(CompactorConfig::default())
    }

    // --- Empty compactor ---

    #[test]
    fn empty_compactor_zero_fragmentation() {
        let mut c = default_compactor();
        let report = c.analyze();
        assert_eq!(report.total_bytes, 0);
        assert_eq!(report.used_bytes, 0);
        assert!((report.fragmentation_ratio - 0.0).abs() < f64::EPSILON);
        assert_eq!(report.fragmented_regions, 0);
    }

    #[test]
    fn empty_compactor_state_idle() {
        let c = default_compactor();
        assert_eq!(c.state, CompactionState::Idle);
    }

    #[test]
    fn empty_compactor_stats() {
        let c = default_compactor();
        let s = c.stats();
        assert_eq!(s.runs_completed, 0);
        assert_eq!(s.bytes_reclaimed_total, 0);
        assert!((s.current_fragmentation - 0.0).abs() < f64::EPSILON);
    }

    // --- Add regions / analyse ---

    #[test]
    fn add_regions_and_analyze_all_used() {
        let mut c = default_compactor();
        c.add_region(0, 1000, true);
        c.add_region(1000, 2000, true);
        let report = c.analyze();
        assert_eq!(report.total_bytes, 3000);
        assert_eq!(report.used_bytes, 3000);
        assert!((report.fragmentation_ratio - 0.0).abs() < f64::EPSILON);
        assert_eq!(report.fragmented_regions, 0);
    }

    #[test]
    fn add_regions_and_analyze_half_free() {
        let mut c = default_compactor();
        c.add_region(0, 500, true);
        c.add_region(500, 500, false);
        let report = c.analyze();
        assert_eq!(report.total_bytes, 1000);
        assert_eq!(report.used_bytes, 500);
        assert!((report.fragmentation_ratio - 0.5).abs() < f64::EPSILON);
        assert_eq!(report.fragmented_regions, 1);
    }

    #[test]
    fn analyze_multiple_free_regions() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        c.add_region(100, 200, true);
        c.add_region(300, 100, false);
        c.add_region(400, 100, false);
        let report = c.analyze();
        assert_eq!(report.total_bytes, 500);
        assert_eq!(report.used_bytes, 200);
        assert_eq!(report.fragmented_regions, 3);
        let expected = 300.0 / 500.0;
        assert!((report.fragmentation_ratio - expected).abs() < f64::EPSILON);
    }

    // --- should_compact ---

    #[test]
    fn should_compact_below_threshold() {
        let mut c = StorageCompactor::new(CompactorConfig {
            fragmentation_threshold: 0.5,
            min_interval_ticks: 0,
            ..CompactorConfig::default()
        });
        c.add_region(0, 800, true);
        c.add_region(800, 200, false);
        c.analyze(); // ratio = 0.2 < 0.5
        assert!(!c.should_compact());
    }

    #[test]
    fn should_compact_above_threshold() {
        let mut c = StorageCompactor::new(CompactorConfig {
            fragmentation_threshold: 0.1,
            min_interval_ticks: 0,
            ..CompactorConfig::default()
        });
        c.add_region(0, 200, true);
        c.add_region(200, 800, false);
        c.analyze(); // ratio = 0.8 > 0.1
        assert!(c.should_compact());
    }

    #[test]
    fn should_compact_respects_interval() {
        let mut c = StorageCompactor::new(CompactorConfig {
            fragmentation_threshold: 0.1,
            min_interval_ticks: 50,
            ..CompactorConfig::default()
        });
        c.add_region(0, 100, true);
        c.add_region(100, 900, false);
        c.analyze();
        // Haven't advanced enough ticks since last_run_tick=0 and current_tick=0
        // Actually current_tick=0, last_run_tick=0, diff=0 < 50
        assert!(!c.should_compact());

        for _ in 0..50 {
            c.tick();
        }
        assert!(c.should_compact());
    }

    #[test]
    fn should_compact_not_after_recent_run() {
        let mut c = StorageCompactor::new(CompactorConfig {
            fragmentation_threshold: 0.1,
            min_interval_ticks: 10,
            ..CompactorConfig::default()
        });
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        for _ in 0..20 {
            c.tick();
        }
        c.analyze();
        assert!(c.should_compact());
        let _ = c.compact();
        // Re-analyse with new regions
        c.add_region(200, 100, false);
        c.analyze();
        // Only 0 ticks since last run
        assert!(!c.should_compact());
    }

    // --- compact ---

    #[test]
    fn compact_merges_adjacent_free_regions() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        c.add_region(100, 200, false);
        c.add_region(300, 300, false);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 2);
        assert_eq!(result.bytes_reclaimed, 500); // 200 + 300
                                                 // All three merged into one region starting at 0 with size 600
        assert_eq!(c.regions.len(), 1);
        assert_eq!(c.regions[0], (0, 600, false));
    }

    #[test]
    fn compact_does_not_merge_used_regions() {
        let mut c = default_compactor();
        c.add_region(0, 100, true);
        c.add_region(100, 200, true);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(result.bytes_reclaimed, 0);
        assert_eq!(c.regions.len(), 2);
    }

    #[test]
    fn compact_non_adjacent_free_regions_stay_separate() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        c.add_region(200, 100, false); // gap at offset 100..200
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(result.bytes_reclaimed, 0);
        assert_eq!(c.regions.len(), 2);
    }

    #[test]
    fn compact_mixed_regions() {
        let mut c = default_compactor();
        // free, free, used, free, free
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        c.add_region(200, 100, true);
        c.add_region(300, 100, false);
        c.add_region(400, 100, false);
        let result = c.compact().expect("compaction should succeed");
        // First two free merge, last two free merge
        assert_eq!(result.regions_merged, 2);
        assert_eq!(result.bytes_reclaimed, 200); // 100 + 100
        assert_eq!(c.regions.len(), 3);
    }

    #[test]
    fn compact_free_used_alternating() {
        let mut c = default_compactor();
        c.add_region(0, 50, false);
        c.add_region(50, 50, true);
        c.add_region(100, 50, false);
        c.add_region(150, 50, true);
        c.add_region(200, 50, false);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(c.regions.len(), 5);
    }

    // --- State transitions ---

    #[test]
    fn state_idle_to_analyzing_to_idle() {
        let mut c = default_compactor();
        assert_eq!(c.state, CompactionState::Idle);
        // analyze sets Analyzing then returns to Idle
        c.add_region(0, 100, true);
        let _ = c.analyze();
        assert_eq!(c.state, CompactionState::Idle);
    }

    #[test]
    fn state_compacting_to_completed() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        let _ = c.compact();
        assert_eq!(c.state, CompactionState::Completed);
    }

    #[test]
    fn cannot_compact_while_compacting() {
        // We need to test this by manipulating state directly since compact()
        // always finishes synchronously. We'll verify the guard works.
        let mut c = default_compactor();
        c.state = CompactionState::Compacting;
        let result = c.compact();
        assert!(result.is_err());
        assert_eq!(
            result.expect_err("should be error"),
            "compaction already in progress"
        );
    }

    // --- remove_region ---

    #[test]
    fn remove_region_existing() {
        let mut c = default_compactor();
        c.add_region(0, 100, true);
        c.add_region(100, 200, false);
        assert!(c.remove_region(0));
        assert_eq!(c.regions.len(), 1);
        assert_eq!(c.regions[0].0, 100);
    }

    #[test]
    fn remove_region_nonexistent() {
        let mut c = default_compactor();
        c.add_region(0, 100, true);
        assert!(!c.remove_region(999));
        assert_eq!(c.regions.len(), 1);
    }

    // --- Bytes reclaimed tracking ---

    #[test]
    fn reclaimed_bytes_zero_initially() {
        let c = default_compactor();
        assert_eq!(c.reclaimed_bytes(), 0);
    }

    #[test]
    fn reclaimed_bytes_after_compact() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        c.add_region(100, 200, false);
        let _ = c.compact();
        assert_eq!(c.reclaimed_bytes(), 200);
    }

    // --- Multiple compaction runs ---

    #[test]
    fn multiple_runs_accumulate_stats() {
        let mut c = default_compactor();
        // First run: two adjacent free
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        let r1 = c.compact().expect("run 1");
        assert_eq!(r1.bytes_reclaimed, 100);
        assert_eq!(c.runs_completed, 1);

        // Add more free regions — these are adjacent to the merged (0,200) region
        c.add_region(200, 50, false);
        c.add_region(250, 50, false);
        let r2 = c.compact().expect("run 2");
        // All three free regions merge: (0,200)+(200,50)+(250,50) → reclaims 100
        assert_eq!(r2.bytes_reclaimed, 100);
        assert_eq!(c.runs_completed, 2);
        assert_eq!(c.reclaimed_bytes(), 200);
    }

    #[test]
    fn stats_reflect_multiple_runs() {
        let mut c = default_compactor();
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        let _ = c.compact(); // merges → (0,200), reclaimed=100
                             // Add non-adjacent free regions (with a used gap)
        c.add_region(500, 100, false);
        c.add_region(600, 100, false);
        let _ = c.compact(); // merges (500,100)+(600,100), reclaimed=100

        let s = c.stats();
        assert_eq!(s.runs_completed, 2);
        assert_eq!(s.bytes_reclaimed_total, 200);
        assert_eq!(s.state, CompactionState::Completed);
    }

    // --- Tick ---

    #[test]
    fn tick_increments() {
        let mut c = default_compactor();
        assert_eq!(c.current_tick, 0);
        c.tick();
        c.tick();
        c.tick();
        assert_eq!(c.current_tick, 3);
    }

    // --- Budget limit ---

    #[test]
    fn compact_respects_budget() {
        let mut c = StorageCompactor::new(CompactorConfig {
            max_budget_bytes: 150,
            ..CompactorConfig::default()
        });
        // Three adjacent free regions of 100 each
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        c.add_region(200, 100, false);
        let result = c.compact().expect("compaction should succeed");
        // Budget = 150, first merge costs 100, second costs 100 => second exceeds 50 remaining
        assert_eq!(result.regions_merged, 1);
        assert_eq!(result.bytes_reclaimed, 100);
    }

    // --- Edge cases ---

    #[test]
    fn compact_single_free_region() {
        let mut c = default_compactor();
        c.add_region(0, 500, false);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(result.bytes_reclaimed, 0);
        assert_eq!(c.regions.len(), 1);
    }

    #[test]
    fn compact_single_used_region() {
        let mut c = default_compactor();
        c.add_region(0, 500, true);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(c.regions.len(), 1);
    }

    #[test]
    fn analyze_only_free_regions() {
        let mut c = default_compactor();
        c.add_region(0, 1000, false);
        let report = c.analyze();
        assert_eq!(report.fragmentation_ratio, 1.0);
        assert_eq!(report.fragmented_regions, 1);
    }

    #[test]
    fn compact_many_adjacent_free() {
        let mut c = default_compactor();
        for i in 0..10 {
            c.add_region(i * 100, 100, false);
        }
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 9);
        assert_eq!(result.bytes_reclaimed, 900);
        assert_eq!(c.regions.len(), 1);
        assert_eq!(c.regions[0], (0, 1000, false));
    }

    #[test]
    fn compact_unsorted_regions() {
        let mut c = default_compactor();
        // Add in reverse order — compact should sort first
        c.add_region(200, 100, false);
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 2);
        assert_eq!(c.regions.len(), 1);
        assert_eq!(c.regions[0], (0, 300, false));
    }

    #[test]
    fn default_config_values() {
        let cfg = CompactorConfig::default();
        assert!((cfg.fragmentation_threshold - 0.3).abs() < f64::EPSILON);
        assert_eq!(cfg.max_budget_bytes, 100 * 1024 * 1024);
        assert_eq!(cfg.min_interval_ticks, 100);
    }

    #[test]
    fn compaction_result_fields() {
        let mut c = default_compactor();
        c.add_region(0, 64, false);
        c.add_region(64, 64, false);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.duration_ticks, 1);
        assert_eq!(result.regions_merged, 1);
        assert_eq!(result.bytes_reclaimed, 64);
    }

    #[test]
    fn state_transitions_full_cycle() {
        let mut c = default_compactor();
        assert_eq!(c.state, CompactionState::Idle);
        c.add_region(0, 100, false);
        c.add_region(100, 100, false);
        let _ = c.analyze();
        assert_eq!(c.state, CompactionState::Idle);
        let _ = c.compact();
        assert_eq!(c.state, CompactionState::Completed);
    }

    #[test]
    fn remove_region_then_analyze() {
        let mut c = default_compactor();
        c.add_region(0, 100, true);
        c.add_region(100, 100, false);
        c.remove_region(100);
        let report = c.analyze();
        assert_eq!(report.total_bytes, 100);
        assert_eq!(report.used_bytes, 100);
        assert_eq!(report.fragmented_regions, 0);
    }

    #[test]
    fn compact_free_between_used() {
        let mut c = default_compactor();
        c.add_region(0, 100, true);
        c.add_region(100, 100, false);
        c.add_region(200, 100, true);
        let result = c.compact().expect("compaction should succeed");
        assert_eq!(result.regions_merged, 0);
        assert_eq!(c.regions.len(), 3);
    }
}