# `Slap.SlateDB.Transaction`
[🔗](https://github.com/mjrusso/slap/blob/slap_slatedb-v0.2.0/slap_slatedb/lib/slap/slatedb/transaction.ex#L1)

A read-write transaction.

Start one with `Slap.SlateDB.begin/2` or, more simply, `Slap.SlateDB.transaction/3`.
Reads see the database as of the start of the transaction plus the
transaction's own writes. Writes stay in memory until `commit/2`.

A transaction belongs to the process that uses it. Do not use it from more
than one process at the same time.

# `t`

```elixir
@opaque t()
```

# `commit`

```elixir
@spec commit(t(), keyword()) ::
  {:ok, non_neg_integer() | nil} | {:error, Slap.SlateDB.Error.t()}
```

Commits the transaction.

Returns `{:ok, seq}` with the commit's sequence number, or `{:ok, nil}` if
the transaction made no writes. Returns
`{:error, %Slap.SlateDB.Error{kind: :conflict}}` if the transaction conflicts
with another one that committed first.

## Options

  * `:await_durable` - when `true`, returns only after the writes are durable
    in object storage. Defaults to `false`.
  * `:timeout` - see `Slap.SlateDB.get/3`.

# `delete`

```elixir
@spec delete(t(), binary()) :: :ok | {:error, Slap.SlateDB.Error.t()}
```

Adds a delete to the transaction.

# `get`

```elixir
@spec get(t(), binary(), keyword()) ::
  {:ok, binary() | nil} | {:error, Slap.SlateDB.Error.t()}
```

Reads `key`, including this transaction's uncommitted writes. Takes the
options of `Slap.SlateDB.get/3`.

# `get_key_value`

```elixir
@spec get_key_value(t(), binary(), keyword()) ::
  {:ok, Slap.SlateDB.key_value() | nil} | {:error, Slap.SlateDB.Error.t()}
```

Reads `key` with its metadata, like `Slap.SlateDB.get_key_value/3`,
including this transaction's uncommitted writes. Their `:seq` is nil until
commit.

# `iterator`

```elixir
@spec iterator(t(), keyword()) ::
  {:ok, Slap.SlateDB.Iterator.t()} | {:error, Slap.SlateDB.Error.t()}
```

Opens an iterator over the transaction's rows. See `Slap.SlateDB.iterator/2`.

# `merge`

```elixir
@spec merge(t(), binary(), binary(), keyword()) ::
  :ok | {:error, Slap.SlateDB.Error.t()}
```

Adds a merge operand to the transaction. The database must be opened with a
`:merge_operator`; see `Slap.SlateDB.merge/4`. Takes the `:ttl` option of `put/4`.

# `put`

```elixir
@spec put(t(), binary(), binary(), keyword()) ::
  :ok | {:error, Slap.SlateDB.Error.t()}
```

Adds a put to the transaction.

## Options

  * `:ttl` - time to live in milliseconds. By default, the database's
    `default_ttl_millis` setting applies.

# `rollback`

```elixir
@spec rollback(t()) :: :ok
```

Throws away the transaction's writes. Rolling back twice is allowed.

# `scan`

```elixir
@spec scan(t(), keyword()) :: Enumerable.t()
```

Scans rows, including this transaction's uncommitted writes. With
`with_versions: true`, those writes have a nil version. See
`Slap.SlateDB.scan/2`.

---

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