# `Rheo.Backend.Mnesia`
[🔗](https://github.com/thanos/rheo/blob/v1.0.0/lib/rheo/backend/mnesia.ex#L1)

Durable OTP `:mnesia` implementation of `Rheo.Backend`.

Always available (OTP `:mnesia` is an included application). Prefer when you
want ETS-shaped tables that survive process restart on a single node without
Mongo, Postgres, or Redis. Prefer Redis / Ecto Postgres / Mongo when several
BEAM nodes must fetch the same group (`distributed: false` in v0.11).

Prefer the `Rheo` facade for application code. The opaque handle is this
process's registered name (default `Rheo.Mnesia`), started via `child_spec/1`.

## When to use

  * Single-node hosts that need durability across GenServer restart
  * Local / embedded deployments that already rely on OTP disk schema
  * Bridging from ETS prototypes without introducing an external database

Do **not** use for multi-node lease arbitration in v0.11
(`distributed: false`). Multiple Rheo instances on one node share the Mnesia
schema but use unique table name prefixes.

## Capabilities

  * `durable: true` — survives process restart when the Mnesia directory is kept
  * `distributed: false` — single-node `disc_copies` only (ADR 028)
  * `batch_writes: true`, `ordered_range_scan: true`
  * `replay: true`, `partitions: true`, `contiguous_frontier: true`
  * `secondary_indexes: false` — `query/2` filters in-process
  * `atomic_compare_and_set: false` — `dirty_write` is not
    `:mnesia.sync_transaction` crash durability
  * `notifications: false`

## Tables

ETS-shaped `disc_copies` tables under a per-handle prefix:

    <prefix>.streams / .events / .groups / .deliveries
    <prefix>.event_ids / .delivery_by_seq / .open_deliveries

## Options

  * `:name` — handle / process name (default `default_handle/0`)
  * `:dir` — Mnesia directory (default under `System.tmp_dir!/0`)

## Supervision example

    children = [
      {Rheo, name: MyRheo, backend: {Rheo.Backend.Mnesia, dir: "/var/lib/rheo/mnesia"}}
    ]

Named handle:

    children = [
      {Rheo,
       name: MyRheo,
       backend: {Rheo.Backend.Mnesia, name: MyRheo.Mnesia, dir: "/var/lib/rheo/mnesia"}}
    ]

Callback semantics are documented on `Rheo.Backend`.

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Child spec for the Mnesia GenServer (backend handle).

## Arguments

  * `opts` — keyword options:
    * `:name` — handle / process name (default `default_handle/0`)
    * `:dir` — Mnesia directory (default under `System.tmp_dir!/0`)

## Examples

    iex> spec = Rheo.Backend.Mnesia.child_spec(name: :demo_mnesia, dir: "/tmp/rheo_mnesia_demo")
    iex> {spec.id, elem(spec.start, 0), spec.type}
    {{Rheo.Backend.Mnesia, :demo_mnesia}, Rheo.Backend.Mnesia, :worker}

## Returns

A supervisor child spec map. The directory is created and the schema started
when the GenServer initializes.

# `default_handle`

```elixir
@spec default_handle() :: atom()
```

Default Mnesia handle name for the `Rheo` instance.

## Examples

    iex> Rheo.Backend.Mnesia.default_handle()
    Rheo.Mnesia

## Returns

A process name atom (`Rheo.Mnesia`).

---

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