# Collections

> Collections are created under a name and the modality they hold, resolved by name once and addressed by CollectionID from then on, and a database holds none until one is created.

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

Collections are created under a name and the modality they hold, resolved by name once and addressed by `CollectionID` from then on, and a database holds none until one is created. A name starting with `_` is refused with `StatusInvalidArgument`, as the engines keep their own system collections there. `CollectionFind` answers the identifier and modality of every name in a batch, and `CollectionList` every collection with its name. `CollectionDrop` empties a collection of its pairs or values with `DropPairs` or `DropValues`, or drops it whole with `DropCollection`. Every batch verb takes a `Collections`: a bare `CollectionID` serves every task, and `Each` names one per task.

```go
people, err := database.CollectionCreate("people", ustore.ModalityBlobs)
found, err := database.CollectionFind(ustore.TapeOf("people", "places"), arena)
id, modality, ok := found.Get(0)
values, err := database.BlobsRead(ustore.Each{people, places}, []ustore.Key{ada, ada}, arena, nil)
err = database.CollectionDrop(people, ustore.DropCollection)
```

## API

### Types

#### `ArenaDescription`

ArenaDescription is where an arena's outputs live, which decides whether Go code may read them, laid out as ustore_arena_description_t.

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

```go
type ArenaDescription struct {
    Residency Residency
    Device int32
    Vendor GPUVendor
    VendorOrdinal int32
    HostAccess HostAccess
}
```

#### Fields

| `Residency` | `Residency` | Residency is [ResidencyRam](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#ResidencyRam) for host pages and [ResidencyVram](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#ResidencyVram) for one device's memory. |
| - | - | - |
| `Device` | `int32` | Device is the ordinal of the GPU holding the outputs, as [GPUDevices](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#GPUDevices) lists it, or -1 for host pages. |
| `Vendor` | `GPUVendor` | Vendor is the vendor whose memory it is, as [GPUDevices](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#GPUDevices) reports it, [GPUVendorUnknown](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#GPUVendorUnknown) for host pages. |
| `VendorOrdinal` | `int32` | VendorOrdinal is the vendor's own device number, which Arrow and DLPack name, or -1 for host pages. |
| `HostAccess` | `HostAccess` | HostAccess is whether Go code may read the outputs, as it may host, pinned, managed and Apple's shared memory. |

#### `Capabilities`

Capabilities is the set of features an engine reports, one bit each.

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

```go
type Capabilities int32
```

#### Values

| `CapabilitiesTransactions` | | CapabilitiesTransactions means the engine supports transactions. |
| - | - | - |
| `CapabilitiesSnapshots` | | CapabilitiesSnapshots means the engine supports snapshots. |
| `CapabilitiesSavesSnapshots` | | CapabilitiesSavesSnapshots means the engine writes a snapshot out as a database of its own. |

#### `Collections`

Collections names the collection of every task in a batch: a [CollectionID](#CollectionID) the whole batch shares, or [Each](https://www.unum.cloud/docs/ustore/go/scans.md#Each), one per task.

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

```go
type Collections interface {
    // contains filtered or unexported methods
}
```

#### `CompactConfig`

CompactConfig carries the optional arguments of [DatabaseOn.Compact](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#Compact).

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

```go
type CompactConfig struct {
    Plan *CompactPlan
    Progress ProgressCallback
}
```

#### Fields

| `Plan` | `*CompactPlan` | Plan keeps a compaction live across calls on an executor; nil runs it to its end. |
| - | - | - |
| `Progress` | `ProgressCallback` | Progress is called at every barrier of every phase, and may cancel at that barrier. |

#### `CompactPlan`

CompactPlan is a compaction kept live across calls: the handle the library opens on the first call naming it, and where the last such call stopped.

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

```go
type CompactPlan struct {
    Phase PhaseProgress
    // contains filtered or unexported fields
}
```

It opens only on an executor, and continues or finishes on any host executor, while its database stays open until the finish.

#### Fields

| `Phase` | `PhaseProgress` | Phase is where the last call naming the plan stopped, [PhaseDone](#PhaseDone) once the compaction ended. |
| - | - | - |

#### `DatabaseDescription`

DatabaseDescription is what [DatabaseOn.Describe](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#Describe) reports, laid out as ustore_database_description_t.

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

```go
type DatabaseDescription struct {
    Capabilities Capabilities
    Engine Engine
    Isolation Isolation
    CPUClass CPUClass
    Keys uint64
    MemoryBytes uint64
    DiskBytes uint64
    UnpinnedStagingBytes uint64
    RequestsAcrossRegisteredSlots uint64
    CommitConflictsLimit uint64
}
```

#### Fields

| `Capabilities` | `Capabilities` | Capabilities are the features the engine supports. |
| - | - | - |
| `Engine` | `Engine` | Engine is the engine serving the database. |
| `Isolation` | `Isolation` | Isolation is how transactions see each other. |
| `CPUClass` | `CPUClass` | CPUClass is the class the engine's calls run as, picked at open. |
| `Keys` | `uint64` | Keys counts the keys across every collection. |
| `MemoryBytes` | `uint64` | MemoryBytes is the memory the engine holds for the data. |
| `DiskBytes` | `uint64` | DiskBytes is what the files of the database take on disk. |
| `UnpinnedStagingBytes` | `uint64` | UnpinnedStagingBytes is the NVMe engine's io_uring arena the locked-memory cap refused to pin, so each request pins its own. |
| `RequestsAcrossRegisteredSlots` | `uint64` | RequestsAcrossRegisteredSlots counts the requests the NVMe engine sent unpinned while its arena was pinned, as each crossed a gibibyte of it. |
| `CommitConflictsLimit` | `uint64` | CommitConflictsLimit is the conflicts a commit of the database's own absorbs before it fails, as its open named or derived them, on the RAM and NVMe engines, and zero elsewhere. |

#### Type `CollectionID`

CollectionID identifies a collection: a UUIDv7 its create mints once and never reuses.1

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

```go
type CollectionID [16]byte
```

The zero value names none.

##### `String`

String renders the identifier as a lowercase dashed UUID.

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

```go
func (collection CollectionID) String() string
```

#### Struct `CollectionList`

CollectionList is every collection a call listed into its arena, readable until the arena serves another call.4

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

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

##### `ID`

ID reports the identifier of collection index.

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

```go
func (list CollectionList) ID(index uint) CollectionID
```

##### `Len`

Len reports how many collections the call listed.

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

```go
func (list CollectionList) Len() uint
```

##### `Modality`

Modality reports what collection index holds.

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

```go
func (list CollectionList) Modality(index uint) Modality
```

##### `Name`

Name views the name of collection index in the arena.

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

```go
func (list CollectionList) Name(index uint) []byte
```

#### Enum `DropMode`

DropMode is what a drop or a clear removes.2

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

```go
type DropMode int32
```

#### Values

| `DropCollection` | | DropCollection removes the collection with everything it holds. |
| - | - | - |
| `DropPairs` | | DropPairs removes every key and value, keeping the collection. |
| `DropValues` | | DropValues empties every value, keeping its key. |

##### `DropModeFromName`

DropModeFromName reads a drop mode back from the spelling [DropMode.String](https://www.unum.cloud/docs/ustore/go/platform-notes.md#String) renders.

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

```go
func DropModeFromName(name string) (DropMode, error)
```

##### `String`

String renders the drop mode as the library spells it.

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

```go
func (mode DropMode) String() string
```

#### Struct `FoundCollections`

FoundCollections is what a lookup of names found, in the order of the names, readable until the arena serves another call.2

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

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

##### `Get`

Get reports the collection name index addresses and what it holds, and false when no collection carries the name.

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

```go
func (found FoundCollections) Get(
    index uint,
) (CollectionID, Modality, bool)
```

##### `Len`

Len reports how many names the call looked up.

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

```go
func (found FoundCollections) Len() uint
```

#### Type `Key`

Key identifies a value within one collection: sixteen bytes, ordered as their bytes.3

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

```go
type Key [16]byte
```

An integer spells as its 128-bit big-endian bytes, so keys made from integers order as the integers.

##### `KeyFromUint64`

KeyFromUint64 spells an integer as its 128-bit big-endian bytes.

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

```go
func KeyFromUint64(value uint64) Key
```

##### `KeysViewFromArrow`

KeysViewFromArrow views the 16-byte keys of a fixed_size_binary(16) column in place, tagged arrow\.uuid or not, viewing the column's buffers.

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

```go
func KeysViewFromArrow(
    schema *ArrowSchema,
    array *ArrowArray,
) ([]Key, error)
```

##### `String`

String renders the key as a lowercase dashed UUID.

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

```go
func (key Key) String() string
```

#### Enum `Modality`

Modality is what a collection holds, recorded beside its name so that a verb of another kind is refused.3

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

```go
type Modality int32
```

#### Values

| `ModalityBlobs` | | ModalityBlobs holds binary values of blobs.h. |
| - | - | - |
| `ModalityDocs` | | ModalityDocs holds JSON documents of docs.h. |
| `ModalityGraph` | | ModalityGraph holds vertices and their edges of graph.h. |
| `ModalityStrings` | | ModalityStrings holds values keyed by strings of strings.h. |
| `ModalityPages` | | ModalityPages holds structured pages of pages.h. |

##### `ModalityFromName`

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

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

```go
func ModalityFromName(name string) (Modality, error)
```

##### `Compiled`

Compiled reports whether this build serves collections of the modality; creating one it omits answers [StatusUnsupported](https://www.unum.cloud/docs/ustore/go/errors.md#StatusUnsupported).

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

```go
func (modality Modality) Compiled() bool
```

##### `String`

String renders the modality as the library spells it.

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

```go
func (modality Modality) String() string
```

#### Enum `Phase`

Phase is the kind of work a plan is doing, shared by every long operation.2

[types.go:193 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/types.go#L193)

```go
type Phase int32
```

#### Values

| `PhaseNone` | | PhaseNone means no plan, or one that has not started. |
| - | - | - |
| `PhaseFlush` | | PhaseFlush writes what memory holds to storage. |
| `PhaseMerge` | | PhaseMerge merges or vacuums the stored versions one unit at a time. |
| `PhaseReclaim` | | PhaseReclaim frees what no reader reaches any more. |
| `PhaseDone` | | PhaseDone means the plan ran to its end, and nothing is pending. |

##### `PhaseFromName`

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

[types.go:212 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/types.go#L212)

```go
func PhaseFromName(name string) (Phase, error)
```

##### `String`

String renders the phase as the library spells it.

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

```go
func (phase Phase) String() string
```

### Constants

#### `KeyUnknown`

KeyUnknown is the key that stands for no key, every bit of it set, the start of a descending walk over a whole collection.

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

```go
var KeyUnknown = Key{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
```
