Mead.Ledger.JournalFile (mead v0.1.1)

Copy Markdown View Source

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.

Summary

Functions

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.

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

The path of the journal in directory.

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

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.

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

Functions

append(directory, events)

@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(directory)

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

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

path(directory)

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

The path of the journal in directory.

read(directory)

@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(directory, model)

@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(directory)

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

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