Compute the purge plan for an array whose store is `store` and whose bitemporal horizon is `horizon_ms`. `horizon_ms` must already be the absolute system-time cutoff (i.e. `now_ms - audit_retain_ms`). This function does **not** recompute it.
(
store: &ArrayStore,
horizon_ms: i64,
schema: &ArraySchema,
)
| 55 | /// `horizon_ms` must already be the absolute system-time cutoff (i.e. |
| 56 | /// `now_ms - audit_retain_ms`). This function does **not** recompute it. |
| 57 | pub fn plan( |
| 58 | store: &ArrayStore, |
| 59 | horizon_ms: i64, |
| 60 | schema: &ArraySchema, |
| 61 | ) -> Result<PurgePlan, ArrayError> { |
| 62 | if store.manifest().segments.is_empty() { |
| 63 | return Ok(PurgePlan { |
| 64 | segment_actions: Vec::new(), |
| 65 | cells_carried_forward: 0, |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | // ── Step 1: Collect all TileEntries tagged with their owning segment id and |
| 70 | // flush_lsn (used to choose the ceiling host). ──────────────────────────── |
| 71 | struct TaggedEntry { |
| 72 | segment_id: String, |
| 73 | flush_lsn: u64, |
| 74 | tile_id: TileId, |
| 75 | } |
| 76 | |
| 77 | let mut all_entries: Vec<TaggedEntry> = Vec::new(); |
| 78 | for seg_ref in &store.manifest().segments { |
| 79 | let handle = match store.segments.get(&seg_ref.id) { |
| 80 | Some(h) => h, |
| 81 | None => continue, // segment in manifest but not in open handles — skip |
| 82 | }; |
| 83 | let reader = handle.reader(); |
| 84 | for entry in reader.tiles() { |
| 85 | all_entries.push(TaggedEntry { |
| 86 | segment_id: seg_ref.id.clone(), |
| 87 | flush_lsn: seg_ref.flush_lsn, |
| 88 | tile_id: entry.tile_id, |
| 89 | }); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if all_entries.is_empty() { |
| 94 | return Ok(PurgePlan { |
| 95 | segment_actions: Vec::new(), |
| 96 | cells_carried_forward: 0, |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | // ── Step 2: Check whether any tile is out-of-horizon. If none, no-op. ────── |
| 101 | let any_outside = all_entries |
| 102 | .iter() |
| 103 | .any(|e| e.tile_id.system_from_ms < horizon_ms); |
| 104 | if !any_outside { |
| 105 | return Ok(PurgePlan { |
| 106 | segment_actions: Vec::new(), |
| 107 | cells_carried_forward: 0, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | // ── Step 3: Group entries by hilbert_prefix. ───────────────────────────── |
| 112 | // For each prefix, build: inside list, outside list (newest→oldest), and |
| 113 | // the earliest-flushed segment id for the ceiling host. |
| 114 | struct PrefixGroup { |