# Transactions

> UStore for Mojo

`from ustore import Stamp, Watch, Isolation, …`

## Stamp

A position in the order commits apply in, `ustore_stamp_t`: where a commit landed, or the newest commit a transaction or a snapshot reads.

```mojo
struct Stamp
```

- `Written` = `Stamp`
- `value` `UInt64`: The position, growing with every commit.

Conforms to: `Answer`, `Copyable`, `Defaultable`, `Deinitable`, `Equatable`, `ImplicitlyCopyable`, `Movable`

## __init__

Stamp zero, which a failed call leaves.

```mojo
def __init__(out self)
```

```mojo
def __init__(out self, arena: Int, generation: Int, written: Self, count: Int)
```

Adopts the stamp a call wrote.

## Watch

Whether a transaction records the keys a call touches for conflicts, `ustore_watch_t`.

```mojo
struct Watch
```

- `KEYS` = `Watch(Int32(0))`: Records the keys, so a concurrent change to any of them fails the commit.
- `NONE` = `Watch(Int32(1))`: Records nothing.
- `code` `c_int`: The raw `ustore_watch_t` value the ABI exchanges.

Conforms to: `Copyable`, `Deinitable`, `Equatable`, `ImplicitlyCopyable`, `Movable`, `RegisterPassable`, `TrivialRegisterPassable`

## Isolation

How the transactions of the in-memory engine see each other's writes, `ustore_isolation_t`.

```mojo
struct Isolation
```

- `STRICT_SERIALIZABLE` = `Isolation(Int32(0))`: Every transaction appears to run alone at one moment.
- `SNAPSHOT` = `Isolation(Int32(1))`: Every transaction reads the state its start saw and conflicts only over what it writes.
- `code` `c_int`: The raw `ustore_isolation_t` value the ABI exchanges.

Conforms to: `Copyable`, `Deinitable`, `Equatable`, `ImplicitlyCopyable`, `Movable`, `RegisterPassable`, `TrivialRegisterPassable`

## Transaction

An optimistic transaction grouping operations across collections and modalities, freed when it goes out of scope. It holds its database open, and serves one call at a time.

```mojo
struct Transaction
```

Verbs called on it run on the calling thread, read through it and write into it, recording the keys
they touch; `txn.unwatched()` records nothing, and `txn.on(executor)` and `txn.deferred(executor)`
route the same verbs to an executor.

- `library` `Library`: The loaded engine.
- `database` `Address`: The `ustore_database_t` it belongs to.
- `address` `Address`: The opaque `ustore_transaction_t`.

Conforms to: `BlobsVerbs`, `DatabaseVerbs`, `Deinitable`, `DocsVerbs`, `GraphVerbs`, `Movable`, `PagesVerbs`, `StringsVerbs`

## __init__

Adopts a transaction the engine just began.

```mojo
def __init__(out self, *, library: Library, database: Int, address: Int)
```

## __deinit__

Resets and frees the transaction. Calls `ustore_transaction_free`.

```mojo
def __deinit__(deinit self)
```

## unwatched

The same verbs on the calling thread, recording nothing for conflicts. Binding-only.

```mojo
def unwatched(ref self) -> On[origin_of(self)]
```

## on

The same verbs, blocking on `executor`. Binding-only.

```mojo
def on(ref self, ref executor: Executor, watch: Watch = Watch.KEYS) -> On[origin_of(executor, self)]
```

## deferred

The same verbs, each submitted to `executor` and answering a [`Deferred`](https://www.unum.cloud/docs/ustore/mojo/batches-and-arenas.md#Deferred) at once. Binding-only.

```mojo
def deferred(
    ref self,
    ref executor: Executor,
    watch: Watch = Watch.KEYS
) -> DeferredOn[origin_of(executor, self)]
```

