# Reads and Writes

> Keys are []Key, viewed in place, and values are a Tape: bytes end to end around one more offset than values, as Apache Arrow lays them out.

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

Keys are `[]Key`, viewed in place, and values are a `Tape`: bytes end to end around one more offset than values, as Apache Arrow lays them out. `TapeOf` packs strings or byte slices into a fresh tape, while `U32Tape` and `U64Tape` view bytes and offsets the caller already holds, without a copy. A read answers `Values`, which addresses the tape in the arena rather than copying out of it, reports a missing key as absent, and passes the whole batch back through `Values.Tape`, so a tape a read wrote is written again as it is. Since every result views the arena as Go slices, a verb into an arena whose `Describe` reports `HostAccessRefused` fails with `StatusDevice` before it runs, while managed memory and Apple’s shared memory read in place. A write carries values and nothing else, so an empty value is still a value, and `BlobsRemove` removes keys, or ranges given end keys.

```go
ada, grace, kent := ustore.KeyFromUint64(1), ustore.KeyFromUint64(2), ustore.KeyFromUint64(3)
err := database.BlobsWrite(people, []ustore.Key{ada, grace}, ustore.TapeOf("Ada", "Grace"), nil)

values, err := database.BlobsRead(people, []ustore.Key{ada, grace, kent}, arena, nil)
name, ok := values.Get(0)    // "Ada", true
_, ok = values.Get(2)        // false
headers, err := database.BlobsRead(people, []ustore.Key{ada}, arena, &ustore.BlobsReadConfig{Limits: []uint64{2}})
err = database.BlobsRemove(people, []ustore.Key{grace}, nil)
```

## API

### Types

#### `ArrowArray`

ArrowArray is the Arrow C data interface's array of one column, laid out as struct ArrowArray.

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

```go
type ArrowArray C.struct_ArrowArray
```

#### `BlobsReadConfig`

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

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

```go
type BlobsReadConfig struct {
    Offsets []uint64
    Limits []uint64
}
```

#### Fields

| `Offsets` | `[]uint64` | Offsets are the byte offsets into every value the read starts at, one per key, or whole values when empty. |
| - | - | - |
| `Limits` | `[]uint64` | Limits cap the bytes read of every value from its offset, one per key, or the rest when empty. |

#### `BlobsScanConfig`

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

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

```go
type BlobsScanConfig struct {
    StartKeys []Key
    EndKeys []Key
    Direction ScanDirection
    Patterns Patterns
    Predicate Admitter
    Mode ScanMode
    Progress ProgressCallback
}
```

#### Fields

| `StartKeys` | `[]Key` | StartKeys are the inclusive starts of the scans, one per task, or the first key in Direction when empty. |
| - | - | - |
| `EndKeys` | `[]Key` | EndKeys are the keys every scan stops before, one per task, or no bound when empty. |
| `Direction` | `ScanDirection` | Direction is which way every scan goes from its start. |
| `Patterns` | `Patterns` | Patterns keep the keys whose values match, shared by every scan. |
| `Predicate` | `Admitter` | Predicate admits or passes over the listed keys. |
| `Mode` | `ScanMode` | Mode is in what order the keys come. |
| `Progress` | `ProgressCallback` | Progress reports the scans finished, and may cancel the call. |

#### `IngestRows`

IngestRows is one batch of rows: keys and values for blobs, documents and pages, values as the strings and keys as what they map onto for strings, and sources, targets and optional edge identifiers for graphs.

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

```go
type IngestRows struct {
    Keys []Key
    Values Tape
    Sources []Key
    Targets []Key
    Edges []Key
}
```

#### Fields

| `Keys` | `[]Key` | Keys are the keys of blobs, documents and pages, or the keys strings map onto. |
| - | - | - |
| `Values` | `Tape` | Values are the values of blobs, JSON documents and XML pages, or the strings of a strings collection. |
| `Sources` | `[]Key` | Sources are the sources of edges. |
| `Targets` | `[]Key` | Targets are the targets of edges. |
| `Edges` | `[]Key` | Edges are the identifiers of edges, or nothing for the default identifier of each. |

#### `RemoveConfig`

RemoveConfig carries the optional arguments of a removal by key or by range.

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

```go
type RemoveConfig struct {
    EndKeys []Key
    Durability Durability
}
```

#### Fields

| `EndKeys` | `[]Key` | EndKeys turns every key into the inclusive start of a range ending before its end key, one per task, or removes the keys alone when empty. |
| - | - | - |
| `Durability` | `Durability` | Durability is how durable the removals are once the call finishes at the head. |

#### `StringsCursorConfig`

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

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

```go
type StringsCursorConfig struct {
    Prefix string
    StartString string
    EndString string
    Direction ScanDirection
    Patterns Patterns
    Predicate Admitter
}
```

#### Fields

| `Prefix` | `string` | Prefix is what every string the walk returns starts with, or nothing to match every string. |
| - | - | - |
| `StartString` | `string` | StartString is where the walk starts, inclusive, or the first string of the prefix in Direction when empty. |
| `EndString` | `string` | EndString is the string the walk stops before, or no bound when empty. |
| `Direction` | `ScanDirection` | Direction is which way the walk goes from its start. |
| `Patterns` | `Patterns` | Patterns keep the strings matching, compiled once. |
| `Predicate` | `Admitter` | Predicate admits or passes over the keys of every page. |

#### `StringsLayout`

StringsLayout is the layout a tape of strings exports as.

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

```go
type StringsLayout int32
```

#### Values

| `StringsU32Tape` | | StringsU32Tape is Arrow's binary layout, with 32-bit offsets. |
| - | - | - |
| `StringsU64Tape` | | StringsU64Tape is Arrow's large binary layout, with 64-bit offsets. |

#### `StringsMeasureConfig`

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

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

```go
type StringsMeasureConfig struct {
    StartStrings Tape
    EndStrings Tape
    Direction ScanDirection
}
```

#### Fields

| `StartStrings` | `Tape` | StartStrings are the inclusive starts of the ranges, one per task, or the first string in Direction when empty. |
| - | - | - |
| `EndStrings` | `Tape` | EndStrings are the strings every range stops before, one per task, or no bound when empty. |
| `Direction` | `ScanDirection` | Direction is which way every range goes from its start. |

#### `StringsRemoveConfig`

StringsRemoveConfig carries the optional arguments of [DatabaseOn.StringsRemove](https://www.unum.cloud/docs/ustore/go/transactions.md#StringsRemove).

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

```go
type StringsRemoveConfig struct {
    EndStrings Tape
    Durability Durability
}
```

#### Fields

| `EndStrings` | `Tape` | EndStrings turns every string into the inclusive start of a range ending before its end string, one per task, or removes the strings alone when empty. |
| - | - | - |
| `Durability` | `Durability` | Durability is how durable the removals are once the call finishes at the head. |

#### `StringsScanConfig`

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

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

```go
type StringsScanConfig struct {
    Prefixes Tape
    StartStrings Tape
    EndStrings Tape
    Direction ScanDirection
    Patterns Patterns
    Predicate Admitter
    Mode ScanMode
    Progress ProgressCallback
}
```

#### Fields

| `Prefixes` | `Tape` | Prefixes every scan matches, one per task, or every string when empty. |
| - | - | - |
| `StartStrings` | `Tape` | StartStrings are the inclusive starts of the scans, one per task, or the first string in Direction when empty. |
| `EndStrings` | `Tape` | EndStrings are the strings every scan stops before, one per task, or no bound when empty. |
| `Direction` | `ScanDirection` | Direction is which way every scan goes from its start. |
| `Patterns` | `Patterns` | Patterns keep the strings matching, shared by every scan. |
| `Predicate` | `Admitter` | Predicate admits or passes over the keys the listed strings map onto. |
| `Mode` | `ScanMode` | Mode is in what order the strings come. |
| `Progress` | `ProgressCallback` | Progress reports the scans finished, and may cancel the call. |

#### `WriteConfig`

WriteConfig carries the optional arguments of a write that takes nothing else.

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

```go
type WriteConfig struct {
    Durability Durability
}
```

#### Fields

| `Durability` | `Durability` | Durability is how durable the writes are once the call finishes at the head. |
| - | - | - |

#### Type `ArrowDeviceArray`

ArrowDeviceArray is an [ArrowArray](#ArrowArray) beside the device its buffers live on, laid out as struct ArrowDeviceArray.2

[arrow.go:36 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/arrow.go#L36)

```go
type ArrowDeviceArray C.struct_ArrowDeviceArray
```

##### `Array`

Array reports the column itself, which the converters read as they read a host array.

[arrow.go:47 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/arrow.go#L47)

```go
func (array *ArrowDeviceArray) Array() *ArrowArray
```

##### `Release`

Release hands the array back to its producer, once; a released array is safe to release again.

[arrow.go:42 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/arrow.go#L42)

```go
func (array *ArrowDeviceArray) Release()
```

#### Type `ArrowSchema`

ArrowSchema is the Arrow C data interface's schema of one column, laid out as struct ArrowSchema, so any Arrow library reads it through its address.1

[arrow.go:29 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/arrow.go#L29)

```go
type ArrowSchema C.struct_ArrowSchema
```

##### `Release`

Release hands the schema back to its producer, once; a released schema is safe to release again.

[arrow.go:39 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/arrow.go#L39)

```go
func (schema *ArrowSchema) Release()
```

#### Enum `Durability`

Durability is how durable a write at the head is once the call finishes; a transaction's commit decides its own.2

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

```go
type Durability int32
```

#### Values

| `DurabilityBuffered` | | DurabilityBuffered is visible on return and durable at the next flush. |
| - | - | - |
| `DurabilityFlushed` | | DurabilityFlushed is durable on return. |

##### `DurabilityFromName`

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

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

```go
func DurabilityFromName(name string) (Durability, error)
```

##### `String`

String renders the durability as the library spells it.

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

```go
func (durability Durability) String() string
```

#### Enum `IngestOrder`

IngestOrder is in what order an ingest's rows arrive.2

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

```go
type IngestOrder int32
```

#### Values

| `IngestSorted` | | IngestSorted takes keys strictly ascending across every append. |
| - | - | - |
| `IngestUnsorted` | | IngestUnsorted takes keys in any order, the last row of a key winning. |

##### `IngestOrderFromName`

IngestOrderFromName reads an ingest order back from the spelling [IngestOrder.String](https://www.unum.cloud/docs/ustore/go/platform-notes.md#String) renders.

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

```go
func IngestOrderFromName(name string) (IngestOrder, error)
```

##### `String`

String renders the order as the library spells it.

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

```go
func (order IngestOrder) String() string
```

#### Struct `Listing`

Listing is what a batch of string scans or samples wrote into its arena, strings scan after scan, readable until the arena serves another call.4

[strings.go:197 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L197)

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

##### `Count`

Count reports how many strings scan task found.

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

```go
func (listing Listing) Count(task uint) uint
```

##### `Get`

Get views string index of scan task in the arena; index must be below [Listing.Count](#Count).

[strings.go:216 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L216)

```go
func (listing Listing) Get(task, index uint) []byte
```

##### `Len`

Len reports how many scans or samples the call ran.

[strings.go:206 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L206)

```go
func (listing Listing) Len() uint
```

##### `Scores`

Scores views the BM25 score of every string scan task found, and nothing unless the patterns carried weights.

[strings.go:224 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L224)

```go
func (listing Listing) Scores(task uint) []float32
```

#### Struct `Mapped`

Mapped is the key every string a read looked up maps onto, in the order of the strings, readable until the arena serves another call.2

[strings.go:130 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L130)

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

##### `Get`

Get reports the key string index maps onto, and false for a missing string.

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

```go
func (mapped Mapped) Get(index uint) (Key, bool)
```

##### `Len`

Len reports how many strings the call looked up.

[strings.go:138 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L138)

```go
func (mapped Mapped) Len() uint
```

#### Struct `Strings`

Strings is a ragged batch a call wrote into its arena as a tape, readable until the arena serves another call.3

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

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

##### `Get`

Get views string index in the arena; index must be below [Strings.Len](https://www.unum.cloud/docs/ustore/go/graphs.md#Len).

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

```go
func (strings Strings) Get(index uint) []byte
```

##### `Len`

Len reports how many strings the call wrote.

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

```go
func (strings Strings) Len() uint
```

##### `Tape`

Tape passes the strings back as an input, so a tape one call wrote is written again as it is, valid while the arena holds it.

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

```go
func (strings Strings) Tape() Tape
```

#### Struct `StringsPage`

StringsPage is one pull of a strings walk, written into its arena and readable until the arena serves another call.3

[strings.go:396 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L396)

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

##### `Len`

Len reports how many strings the pull handed out; fewer than wanted exhausts the walk for good.

[strings.go:404 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L404)

```go
func (page StringsPage) Len() uint
```

##### `Strings`

Strings views the strings of the page in the arena.

[strings.go:407 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L407)

```go
func (page StringsPage) Strings() Strings
```

##### `Values`

Values views the key every string maps onto, and is empty after a [StringsCursor.NextStrings](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#NextStrings) pull.

[strings.go:415 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/strings.go#L415)

```go
func (page StringsPage) Values() []Key
```

#### Struct `Tape`

Tape passes a ragged batch of byte strings as Apache Arrow lays one out: the bytes end to end, and one more offset than strings around them, string i spanning offsets\[i] up to offsets\[i+1].5

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

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

A Tape views and never owns, and the binding pins both slices until each call it crosses is final. The zero Tape is the empty batch, which a verb reads as its input's default.

##### `StringsViewFromArrow`

StringsViewFromArrow views the byte strings of a binary, utf8, large binary or large utf8 column in place as a [Tape](#Tape), viewing the column's buffers.

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

```go
func StringsViewFromArrow(
    schema *ArrowSchema,
    array *ArrowArray,
) (Tape, error)
```

##### `TapeOf`

TapeOf packs strings end to end into a fresh Tape, one entry apiece, with 32-bit offsets while the bytes fit in 4 GiB and 64-bit beyond.

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

```go
func TapeOf[Text ~string | ~[]byte](texts ...Text) Tape
```

##### `U32Tape`

U32Tape views bytes end to end around len(offsets) - 1 strings, in Arrow's binary layout.

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

```go
func U32Tape(data []byte, offsets []uint32) Tape
```

Panics when the offsets overrun the bytes, which names a caller's arithmetic mistake rather than a condition the library could report.

##### `U64Tape`

U64Tape views bytes end to end around len(offsets) - 1 strings, in Arrow's large binary layout.

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

```go
func U64Tape(data []byte, offsets []uint64) Tape
```

Panics when the offsets overrun the bytes.

##### `Len`

Len reports how many strings the tape spans.

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

```go
func (tape Tape) Len() uint
```

#### Struct `Values`

Values is a batch of values a read wrote into its arena, in the order of its keys, readable until the arena serves another call.3

[blobs.go:49 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/blobs.go#L49)

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

##### `Get`

Get views value index in the arena, and false for a missing key.

[blobs.go:60 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/blobs.go#L60)

```go
func (values Values) Get(index uint) ([]byte, bool)
```

##### `Len`

Len reports how many values the call read.

[blobs.go:57 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/blobs.go#L57)

```go
func (values Values) Len() uint
```

##### `Tape`

Tape passes every value back as an input, a missing key as an empty value, so a read's tape is written again as it is.

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

```go
func (read Values) Tape() Tape
```

### Functions

#### `KeysViewToArrow`

KeysViewToArrow exports keys a call wrote into arena as one fixed_size_binary(16) column tagged arrow\.uuid, in place; until both outputs are released, the arena's next call and Close answer [StatusContended](https://www.unum.cloud/docs/ustore/go/errors.md#StatusContended), so release them after reading.

[ustore.go:95 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/ustore.go#L95)

```go
func KeysViewToArrow(
    keys []Key,
    arena *Arena,
    schema *ArrowSchema,
    array *ArrowDeviceArray,
) error
```

#### `StringsViewToArrow`

StringsViewToArrow exports strings a call wrote into arena as one binary or large binary column, in place when the tape already has layout; until both outputs are released, the arena's next call and Close answer [StatusContended](https://www.unum.cloud/docs/ustore/go/errors.md#StatusContended).

[ustore.go:107 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/ustore.go#L107)

```go
func StringsViewToArrow(
    strings Strings,
    layout StringsLayout,
    arena *Arena,
    schema *ArrowSchema,
    array *ArrowDeviceArray,
) error
```
