Compact ops below the min-ack frontier into snapshots, then prune the log. Algorithm: 1. Determine frontier = `acks.min_ack_hlc()`. If `None`, return early with an empty report — GC must not proceed without knowing every peer's progress. 2. Collect distinct array names whose ops have `hlc < frontier`. 3. For each such array, call `snapshot_for_array(name, frontier)`. - `Some(snap)` → write via `s
(
log: &dyn OpLog,
acks: &AckVector,
sink: &dyn SnapshotSink,
snapshot_for_array: impl Fn(&str, Hlc) -> ArrayResult<Option<TileSnapshot>>,
)
| 48 | /// the count in `ops_dropped`. |
| 49 | /// 5. Return the report. |
| 50 | pub fn collapse_below( |
| 51 | log: &dyn OpLog, |
| 52 | acks: &AckVector, |
| 53 | sink: &dyn SnapshotSink, |
| 54 | snapshot_for_array: impl Fn(&str, Hlc) -> ArrayResult<Option<TileSnapshot>>, |
| 55 | ) -> ArrayResult<GcReport> { |
| 56 | let frontier = match acks.min_ack_hlc() { |
| 57 | None => { |
| 58 | return Ok(GcReport { |
| 59 | snapshots_written: 0, |
| 60 | ops_dropped: 0, |
| 61 | frontier: Hlc::ZERO, |
| 62 | }); |
| 63 | } |
| 64 | Some(h) => h, |
| 65 | }; |
| 66 | |
| 67 | // Collect distinct array names with ops below the frontier. |
| 68 | let mut arrays_to_snapshot: HashSet<String> = HashSet::new(); |
| 69 | for item in log.scan_from(Hlc::ZERO)? { |
| 70 | let op = item?; |
| 71 | if op.header.hlc < frontier { |
| 72 | arrays_to_snapshot.insert(op.header.array.clone()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Write snapshots — abort on the first error; do NOT mutate the log yet. |
| 77 | let mut snapshots_written: u64 = 0; |
| 78 | for array in &arrays_to_snapshot { |
| 79 | if let Some(snap) = snapshot_for_array(array, frontier)? { |
| 80 | sink.write_snapshot(&snap)?; |
| 81 | snapshots_written += 1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // All snapshots succeeded — now prune the log. |
| 86 | let ops_dropped = log.drop_below(frontier)?; |
| 87 | |
| 88 | Ok(GcReport { |
| 89 | snapshots_written, |
| 90 | ops_dropped, |
| 91 | frontier, |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | // ─── MockSnapshotSink ──────────────────────────────────────────────────────── |
| 96 |