# `Mead.Ledger.JournalFile`
[🔗](https://github.com/albertosca/mead/blob/main/lib/mead/ledger/journal_file.ex#L1)

The file side of the ledger journal: `ledger.journal` in the storage directory, a sequence of `Mead.Session.Record` records, each one `Mead.Ledger.Journal` event.

`read/1` has no side effect. `load/1` is what the ledger does at boot: it cuts a torn last record off the file, so that the next append does not land behind garbage, and it moves a file that is damaged anywhere else to `ledger.journal.corrupt` and starts empty, because the ledger is diagnostic and a damaged file must not stop the application. The name has no UUID shape, so `Mead.Session.Store.list/1` never mistakes the file for a conversation.

# `append`

```elixir
@spec append(String.t(), [Mead.Ledger.Journal.event()]) :: :ok | {:error, term()}
```

Appends the events with one write and one sync, creating the directory and the file on first use. A failed write is rolled back to the size the file had before it.

# `load`

```elixir
@spec load(String.t()) :: Mead.Ledger.Journal.t()
```

Reads the journal for the boot of the ledger, recovering from whatever it finds.

# `path`

```elixir
@spec path(String.t()) :: Path.t()
```

The path of the journal in `directory`.

# `read`

```elixir
@spec read(String.t()) ::
  {:ok, Mead.Ledger.Journal.t(), non_neg_integer(), :clean | :torn}
  | {:error, :corrupt | {:persistence, term()}}
```

Reads the journal into a model, without touching the file.

Returns the model, the size of the valid prefix, and whether the file ended in a torn tail. An absent file is an empty model.

# `rewrite`

```elixir
@spec rewrite(String.t(), Mead.Ledger.Journal.t()) :: :ok | {:error, term()}
```

Replaces the journal with one checkpoint record of `model`, written to `ledger.journal.tmp`, synced and renamed over the journal, so a crash leaves the old file or the new one. A failure leaves the journal as it was and removes the temporary file.

# `size`

```elixir
@spec size(String.t()) :: non_neg_integer()
```

The size of the journal in bytes, or zero when it does not exist.

---

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