# Transactions

> A transaction groups reads and writes across collections and modalities, and commits all of them or none.

`import "github.com/unum-cloud/ustore"` 1.0.0-rc.1 · 084197c4

A transaction groups reads and writes across collections and modalities, and commits all of them or none. Its own verbs record the keys they touch, so the commit reports `StatusWriteConflict`, `StatusReadConflict` or `StatusPhantomConflict` if another writer changed them, while `Transaction.View` with `WatchNone` records nothing. A transaction conflicts rather than blocks, and keeps its contents whatever the outcome, so `Reset` reopens it on the newest state for another try. `Stage` answers a `ReadStamp`, the stamp the transaction reads at and its commit validates against, and `Commit` a `CommitStamp`, its position in commit order; the two are distinct types, so neither is compared with the other by accident. `Rollback` withdraws a stage while keeping the reads and writes, so the transaction can stage again.

```go
transaction, err := database.TransactionInit()
defer transaction.Close()
err = transaction.BlobsWrite(people, []ustore.Key{ada, grace}, ustore.TapeOf("Ada", "Grace"), nil)
peek, err := transaction.View(ustore.WatchNone).BlobsRead(people, []ustore.Key{ada}, arena, nil)
readStamp, err := transaction.Stage()
commitStamp, err := transaction.Commit(ustore.DurabilityFlushed)
```

## API

### Types

#### `CommitStamp`

CommitStamp is a commit's position in commit order, which a read at any [ReadStamp](#ReadStamp) at or above it sees.

[db.go:145 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L145)

```go
type CommitStamp uint64
```

#### `ReadStamp`

ReadStamp is the stamp a transaction or a snapshot reads at.

[db.go:141 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L141)

```go
type ReadStamp uint64
```

#### Enum `Isolation`

Isolation is how the transactions of the in-memory engine see each other's writes.2

[db.go:344 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L344)

```go
type Isolation int32
```

#### Values

| `IsolationStrictSerializable` | | IsolationStrictSerializable makes every transaction appear to run alone at one moment. |
| - | - | - |
| `IsolationSnapshot` | | IsolationSnapshot makes every transaction read the state its start saw. |

##### `IsolationFromName`

IsolationFromName reads an isolation back from the spelling [Isolation.String](https://www.unum.cloud/docs/ustore/go/platform-notes.md#String) renders.

[db.go:359 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L359)

```go
func IsolationFromName(name string) (Isolation, error)
```

##### `String`

String renders the isolation as the library spells it.

[db.go:354 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L354)

```go
func (isolation Isolation) String() string
```

#### Struct `TransactionDeferredOn`

TransactionDeferredOn is the receiver whose verbs submit their calls to an executor, offering the verbs of [TransactionOn](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#TransactionOn) with the same arguments.18

[db.go:701 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L701)

```go
type TransactionDeferredOn struct {
    // contains filtered or unexported fields
}
```

##### `*`

× 3 Commit submits [TransactionOn.Commit](#Commit).

[db.go:1692 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L1692)

```go
func (on TransactionDeferredOn) Commit(
    durability Durability,
) (*Deferred[CommitStamp], error)
```

| Symbol |
| - |
| `Commit` |
| `Rollback` |
| `Stage` |

##### `BlobsRemove`

BlobsRemove submits [DatabaseOn.BlobsRemove](#BlobsRemove).

[db.go:209 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L209)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) BlobsRemove(
    collections Collections,
    keys []Key,
    config *RemoveConfig,
) (*Deferred[struct{}], error)
```

##### `BlobsWrite`

BlobsWrite submits [DatabaseOn.BlobsWrite](#BlobsWrite).

[db.go:169 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L169)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) BlobsWrite(
    collections Collections,
    keys []Key,
    values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `DocsRemove`

DocsRemove submits [DatabaseOn.DocsRemove](#DocsRemove).

[db.go:194 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L194)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) DocsRemove(
    collections Collections,
    keys []Key,
    config *RemoveConfig,
) (*Deferred[struct{}], error)
```

##### `DocsWrite`

DocsWrite submits [DatabaseOn.DocsWrite](#DocsWrite).

[db.go:149 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L149)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) DocsWrite(
    collections Collections,
    keys []Key,
    values Tape,
    config *DocsWriteConfig,
) (*Deferred[struct{}], error)
```

##### `GraphRemoveEdges`

GraphRemoveEdges submits [DatabaseOn.GraphRemoveEdges](#GraphRemoveEdges).

[db.go:261 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L261)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) GraphRemoveEdges(
    collections Collections,
    edges []Edge,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `GraphRemoveVertices`

GraphRemoveVertices submits [DatabaseOn.GraphRemoveVertices](#GraphRemoveVertices).

[db.go:358 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L358)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) GraphRemoveVertices(
    collections Collections,
    vertices []Key,
    config *GraphRemoveVerticesConfig,
) (*Deferred[struct{}], error)
```

##### `GraphWriteEdges`

GraphWriteEdges submits [DatabaseOn.GraphWriteEdges](#GraphWriteEdges).

[db.go:219 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L219)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) GraphWriteEdges(
    collections Collections,
    edges []Edge,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `GraphWriteVertices`

GraphWriteVertices submits [DatabaseOn.GraphWriteVertices](#GraphWriteVertices).

[db.go:307 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L307)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) GraphWriteVertices(
    collections Collections,
    vertices []Key,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `PagesImport`

PagesImport submits [DatabaseOn.PagesImport](#PagesImport).

[db.go:91 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L91)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesImport(
    collections Collections,
    keys []Key,
    values Tape,
    config *PagesImportConfig,
) (*Deferred[struct{}], error)
```

##### `PagesInsert`

PagesInsert submits [DatabaseOn.PagesInsert](#PagesInsert).

[db.go:167 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L167)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesInsert(
    collections Collections,
    keys []Key,
    values Tape,
    arena *Arena,
    config *PagesInsertConfig,
) (*Deferred[Inserted], error)
```

##### `PagesMove`

PagesMove submits [DatabaseOn.PagesMove](#PagesMove).

[db.go:280 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L280)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesMove(
    collections Collections,
    keys, elements []Key,
    config *PagesMoveConfig,
) (*Deferred[struct{}], error)
```

##### `PagesRemove`

PagesRemove submits [DatabaseOn.PagesRemove](#PagesRemove).

[db.go:227 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L227)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesRemove(
    collections Collections,
    keys []Key,
    config *PagesRemoveConfig,
) (*Deferred[struct{}], error)
```

##### `PagesRemoveAttributes`

PagesRemoveAttributes submits [DatabaseOn.PagesRemoveAttributes](#PagesRemoveAttributes).

[db.go:408 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L408)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesRemoveAttributes(
    collections Collections,
    keys, elements []Key,
    fields Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `PagesSetAttributes`

PagesSetAttributes submits [DatabaseOn.PagesSetAttributes](#PagesSetAttributes).

[db.go:366 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L366)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesSetAttributes(
    collections Collections,
    keys, elements []Key,
    fields, values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `PagesSplice`

PagesSplice submits [DatabaseOn.PagesSplice](#PagesSplice).

[db.go:323 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L323)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) PagesSplice(
    collections Collections,
    keys, elements []Key,
    textStarts, textLengths []uint64,
    values Tape,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

##### `StringsRemove`

StringsRemove submits [DatabaseOn.StringsRemove](#StringsRemove).

[db.go:91 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L91)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) StringsRemove(
    collections Collections,
    strings Tape,
    config *StringsRemoveConfig,
) (*Deferred[struct{}], error)
```

##### `StringsWrite`

StringsWrite submits [DatabaseOn.StringsWrite](#StringsWrite).

[db.go:40 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L40)

Inherited from `deferredWrites[Handle]`

```go
func (on TransactionDeferredOn) StringsWrite(
    collections Collections,
    strings Tape,
    values []Key,
    config *WriteConfig,
) (*Deferred[struct{}], error)
```

#### Enum `Watch`

Watch is whether a transaction records the keys a call reads or writes, to refuse its commit when another writer changes them first.2

[db.go:244 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L244)

```go
type Watch int32
```

#### Values

| `WatchKeys` | | WatchKeys records the keys, so a concurrent change to any of them fails the commit. |
| - | - | - |
| `WatchNone` | | WatchNone records nothing, so concurrent changes to these keys never fail the commit. |

##### `WatchFromName`

WatchFromName reads a watch back from the spelling [Watch.String](https://www.unum.cloud/docs/ustore/go/platform-notes.md#String) renders.

[db.go:257 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L257)

```go
func WatchFromName(name string) (Watch, error)
```

##### `String`

String renders the watch as the library spells it.

[db.go:254 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/db.go#L254)

```go
func (watch Watch) String() string
```
