# `Ankusa.Storage.Compactor`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/storage/compactor.ex#L1)

Packs committed hooks into immutable segments and clears their archive
obligations.

A hook committed while the `:storage` role runs carries an *archive
obligation* (`Ankusa.Queue`). Each tick takes obligations in `seq` order until
their stored sizes reach `config.storage.roll_bytes`, packs those hooks into
one segment, and goes on: a backlog becomes *several* segments per tick
instead of one unbounded one, so peak memory is one segment whatever the
backlog and however long a storage node was unavailable.

A segment is encoded via `config.storage.codec`, wrapped in the segment header
and sha256 trailer (`Ankusa.Storage.Segment`), and `PUT` through the blob store
under the dated layout (`Ankusa.Storage.Layout`):
`archive/v1/dt=<yyyy-mm-dd>/<node>/seg-<first>-<last>.seg`, followed by an
index object (`….idx`: event id to offset, length and seq,
`Ankusa.Storage.Index`), the node-day's `_manifest.json` summary, and the
segment's catalogue row in the store (which records the format, codec, node and
day). Only then do its obligations clear. A hook is deleted when its *last*
obligation — archive or delivery — clears, so an archive that is behind or
switched off never blocks delivery, and delivery never blocks the archive.

A backlog of `roll_bytes` or more rolls at once; a smaller one — a partial
segment — is written once its first obligation has waited `storage.roll_ms`
(`tick/1` writes it at once). One tick writes at most
`storage.max_segments_per_tick` segments.

A failed write ends the tick, nothing crashes, and the same hooks are written
again — after `storage.interval_ms` doubled per consecutive failure (jittered,
capped at 60 s) — under the same keys.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `prune_catalogue`

```elixir
@spec prune_catalogue(atom()) ::
  {:ok,
   %{
     segments: non_neg_integer(),
     objects: non_neg_integer(),
     legacy: non_neg_integer(),
     more: boolean()
   }}
  | {:error, term()}
```

Delete the catalogue rows past retention: a segment's row (`?S`) once its newest
hook is older than `storage.catalogue_retention_days` (or, with
`archive.retention_days` set, that many days, deleting the segment object and
its index through the blob store as well), and a 0.3 import's location row
(`?L`) once its hook is. Runs hourly in a process of its own; callable on its
own.

What a replay or a delivery still needs stays: a segment an archive replay job
(`running` or `paused`) can still read (its newest hook is at or after the
job's `from`), and a segment holding the hook of a delivery row that is still
owed (pending or dead): the DLQ reaper evicts that row only while the catalogue
can find its hook. The sweep deletes nothing when the replayer cannot say what
its jobs need. Emits `[:ankusa, :archive, :pruned]` (`segments`, `objects`,
`legacy`); `more` says the pass's budget, not the catalogue, ran out.

# `start_link`

# `tick`

```elixir
@spec tick(atom()) :: {:ok, non_neg_integer()}
```

Run one compaction synchronously; returns the number of segments written.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
