# Transactions

> UStore for Rust

`use ustore::CommitStamp;`

`use ustore::Isolation;`

`use ustore::ReadStamp;`

`use ustore::Transaction;`

`use ustore::Watch;`

`use ustore::db::Stage;`

`use ustore::db::Commit;`

`use ustore::db::Rollback;`

`use ustore::db::Reset;`

## CommitStamp

The position of a commit in commit order, visible to every snapshot whose read stamp is at or above it.

```rust
pub struct CommitStamp(pub u64);
```

- `0` `u64`

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Debug`, `Default`, `From<[CommitStamp](#CommitStamp)>`

## Isolation

The isolation the in-memory engine delivers to the transactions of a database.

```rust
pub enum Isolation
```

- `StrictSerializable` = `0`: Reads and writes both take part in one serial order.
- `Snapshot` = `1`: A transaction reads the snapshot it began at and conflicts only on what it wrote.

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `Debug`, `Default`, `Hash`, `Display`, `FromStr`

## ReadStamp

The stamp a transaction or a snapshot reads at, which a stage reports and the commit after it validates against.

```rust
pub struct ReadStamp(pub u64);
```

- `0` `u64`

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Debug`, `Default`

## Transaction

An optimistic transaction grouping reads and writes across collections and modalities.

```rust
pub struct Transaction<'db>
```

It serves one call at a time, so a call on a transaction that serves an unfinished deferred one finishes with
[`Status::Contended`](https://www.unum.cloud/docs/ustore/rust/errors.md#Contended). It is freed before its database closes, which the borrow of the database carries, and
dropping it waits for every deferred call naming it to finish.

Implements: `Debug`, `Drop`, `Send`

## collection

The collection `name` addresses inside this transaction, which watches what it touches.

```rust
pub fn collection(&self, name: &str) -> Result<Collection<'_, Transaction<'db>>>
```

The name resolves at the head, where a first write creates a missing collection at once, since collections are
created nowhere else. The collection borrows the transaction, so no call goes through it after
[`Transaction::commit`](#commit).

## view

The view every call inside this transaction addresses, watching the keys it touches or not.

```rust
pub fn view(&self, watch: Watch) -> On<'_, Transaction<'db>>
```

## on

The view of this transaction watching the keys it touches, with every call through it running on `executor`.

```rust
pub fn on<'with>(&'with self, executor: &'with Executor) -> On<'with, Transaction<'db>>
```

## deferred

The view of this transaction watching the keys it touches, with every verb deferred on `executor`.

```rust
pub fn deferred<'with>(
    &'with self,
    executor: &'with Executor,
) -> DeferredOn<'with, Transaction<'db>>
```

## stage

Stages the writes as the first phase of a two-phase commit, reporting the stamp the transaction reads at.

```rust
pub fn stage(&mut self) -> Result<ReadStamp>
```

## commit

Commits the writes, reporting the commit's position in commit order.

```rust
pub fn commit(&mut self, durability: Durability) -> Result<CommitStamp>
```

A conflict is reported rather than retried, and the transaction keeps its contents either way, so a caller
may stage them again after [`Transaction::rollback`](#rollback) or start over after [`Transaction::reset`](#reset).

## rollback

Withdraws a stage, releasing whatever it claimed, while keeping the reads and writes to stage again.

```rust
pub fn rollback(&mut self) -> Result<()>
```

## reset

Discards the reads and writes and reopens the transaction on the newest state, ready for another try.

```rust
pub fn reset(&mut self) -> Result<()>
```

## Watch

Whether the calls of a transaction watch the keys they touch for conflicts, as `ustore_watch_t` spells it.

```rust
pub enum Watch
```

Only a transaction watches anything, so the head of a database and a snapshot never carry this, and
it changes what a call does only at [[`Isolation::Snapshot`](https://www.unum.cloud/docs/ustore/rust/snapshots.md#Snapshot)]: a strictly serializable transaction puts
every read it makes into the one serial order whatever this says.

- `Keys` = `0`: Every key the view reads or writes is recorded, so a concurrent writer conflicts at the commit.
- `None` = `1`: Nothing the view touches is watched, so a snapshot-isolated transaction commits over a concurrent
writer instead of conflicting with it.

Implements: `Clone`, `Copy`, `StructuralPartialEq`, `PartialEq`, `Eq`, `Debug`, `Default`, `Hash`, `Display`, `FromStr`

## Stage

The first phase of a two-phase commit; see [`On::stage`](#stage).

```rust
pub struct Stage
```

Implements: `Debug`

## Commit

A commit of a transaction; see [`On::commit`](#commit).

```rust
pub struct Commit
```

Implements: `Debug`

## Rollback

A withdrawal of a stage; see [`On::rollback`](#rollback).

```rust
pub struct Rollback;
```

Implements: `Debug`

## Reset

A reset of a transaction for another try; see [`On::reset`](#reset).

```rust
pub struct Reset
```

Implements: `Debug`

