# BLK-013: Flush and Compact

| Link | Path |
|------|------|
| **Normative** | [NORMATIVE.md](../NORMATIVE.md) |
| **Verification** | [VERIFICATION.md](../VERIFICATION.md) |
| **Tracking** | [TRACKING.yaml](../TRACKING.yaml) |
| **Spec** | [SPEC.md](../../../../resources/SPEC.md) Section 15.7 |

---

## Summary

`flush(&self) -> Result<()>` forces a RocksDB WAL flush. `compact(&self) -> Result<()>` triggers manual compaction across all column families. Both are maintenance operations for durability and space reclamation.

---

## Specification

### flush

```rust
pub fn flush(&self) -> Result<()> {
    self.db.flush()?;
    Ok(())
}
```

Forces all buffered WAL writes to disk, ensuring durability. This is useful after bulk operations (e.g., snapshot import) to ensure all data is persisted before proceeding.

### compact

```rust
pub fn compact(&self) -> Result<()> {
    for cf_name in &[CF_BLOCKS, CF_HEADERS, CF_ATTESTED, CF_CANONICAL, CF_CHECKPOINTS, CF_METADATA] {
        let cf = self.db.cf_handle(cf_name)
            .ok_or(BlockStoreError::NotInitialized)?;
        self.db.compact_range_cf(&cf, None::<&[u8]>, None::<&[u8]>);
    }
    Ok(())
}
```

Triggers manual compaction across all column families. This reclaims space from deleted entries, optimizes read performance, and runs the compaction filter (if enabled).

---

## Acceptance Criteria

- [ ] `flush()` forces WAL flush; data is durable after return
- [ ] `compact()` triggers compaction on all 6 column families
- [ ] Both methods propagate RocksDB errors via `BlockStoreError::RocksDb`
- [ ] Neither method modifies logical state (blocks, canonical chain, tip)

---

## Test Plan

| Test Name | Type | Description |
|-----------|------|-------------|
| `test_flush_succeeds` | integration | Write blocks, call flush(), verify no error |
| `test_compact_succeeds` | integration | Write blocks, call compact(), verify no error |
| `test_flush_data_durable` | integration | Write blocks, flush, kill process, reopen, verify data present |

---

## Expected Test Files

- `tests/blk_013_tests.rs`
