# Documents

> DocsWrite writes JSON text by default, whole documents or, with Fields, top-level keys and RFC 6901 pointers inside them, and DocsModification chooses between an upsert, an update, an insert, an RFC 6902 patch and an RFC 7386 merge.

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

`DocsWrite` writes JSON text by default, whole documents or, with `Fields`, top-level keys and RFC 6901 pointers inside them, and `DocsModification` chooses between an upsert, an update, an insert, an RFC 6902 patch and an RFC 7386 merge. `DocsRead` reads whole documents or fields as JSON or as strings, and `DocsGist` lists the paths to every leaf. `DocsGather` converts fields into typed columns, a row per key, whose scalars `DocsColumnScalars` views straight in the arena beside the validity, conversion and collision bitmaps.

```go
documents := ustore.TapeOf(`{"name":"Ada","born":1815}`)
err := database.DocsWrite(people, []ustore.Key{ada}, documents, nil)
died := &ustore.DocsWriteConfig{Fields: ustore.TapeOf("died")}
err = database.DocsWrite(people, []ustore.Key{ada}, ustore.TapeOf("1852"), died)

names, err := database.DocsRead(people, []ustore.Key{ada}, arena, &ustore.DocsReadConfig{
	Fields: ustore.TapeOf("name"), OutputsType: ustore.DocsFieldTypeString,
})
paths, err := database.DocsGist(people, []ustore.Key{ada}, otherArena)
columns, err := database.DocsGather(people, []ustore.Key{ada}, ustore.TapeOf("born"),
	[]ustore.DocsFieldType{ustore.DocsFieldTypeU64}, arena)
born := ustore.DocsColumnScalars[uint64](columns.Column(0))
```

## API

### Types

#### `DocsReadConfig`

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

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

```go
type DocsReadConfig struct {
    Fields Tape
    OutputsType DocsFieldType
}
```

#### Fields

| `Fields` | `Tape` | Fields to read inside the documents, one per key, top-level keys or RFC 6901 pointers, or whole documents when empty. |
| - | - | - |
| `OutputsType` | `DocsFieldType` | OutputsType is what the values are converted into, JSON text by default or a string. |

#### `DocsScalar`

DocsScalar constrains the Go types a scalar column reads as, one per fixed-width [DocsFieldType](#DocsFieldType): a 16-bit float as its bits, and a UUID as a [Key](https://www.unum.cloud/docs/ustore/go/scans.md#Key).

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

```go
type DocsScalar interface {
    ~bool | ~int8 | ~int16 | ~int32 | ~int64 | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~[16]byte
}
```

#### `DocsWriteConfig`

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

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

```go
type DocsWriteConfig struct {
    IDField string
    Fields Tape
    Type DocsFieldType
    Modification DocsModification
    Durability Durability
}
```

#### Fields

| `IDField` | `string` | IDField is the field holding the key of every document when no keys are given, such as "\_id". |
| - | - | - |
| `Fields` | `Tape` | Fields to modify inside the documents, one per task, or whole documents when empty. |
| `Type` | `DocsFieldType` | Type is the format of the written values, JSON text by default. |
| `Modification` | `DocsModification` | Modification is applied to every document, an upsert by default. |
| `Durability` | `Durability` | Durability is how durable the writes are once the call finishes at the head. |

#### Struct `Columns`

Columns is one typed column per field a gather converted, one row per document, readable until the arena serves another call.2

[docs.go:345 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L345)

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

##### `Column`

Column reports the column of field index.

[docs.go:356 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L356)

```go
func (columns Columns) Column(index uint) DocsColumn
```

##### `Len`

Len reports how many columns the call gathered, one per field.

[docs.go:353 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L353)

```go
func (columns Columns) Len() uint
```

#### Struct `DocsBatches`

DocsBatches is what one pull of a [DocsCursor](https://www.unum.cloud/docs/ustore/go/batches-and-arenas.md#DocsCursor) wrote into its arena in walk order, one batch per executor task that found rows, readable until the arena serves another call.3

[docs.go:628 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L628)

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

##### `Columns`

Columns views the columns of batch index in the arena, one per field the cursor gathers.

[docs.go:647 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L647)

```go
func (batches DocsBatches) Columns(index uint) Columns
```

##### `Keys`

Keys views the keys of batch index in the arena, one per row.

[docs.go:639 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L639)

```go
func (batches DocsBatches) Keys(index uint) []Key
```

##### `Len`

Len reports how many batches the pull handed out, none once the walk is exhausted.

[docs.go:636 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L636)

```go
func (batches DocsBatches) Len() uint
```

#### Struct `DocsColumn`

DocsColumn is the rows of one field as an Apache Arrow array of its type, beside the bitmaps of the rows found and converted, converted with a change of semantics, and colliding with a value of an inconvertible type.5

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

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

##### `Colliding`

Colliding reports whether a value of an inconvertible type sits under row's field.

[docs.go:391 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L391)

```go
func (column DocsColumn) Colliding(row uint) bool
```

##### `Converted`

Converted reports whether row's conversion changed its semantics, such as a string parsed into an integer.

[docs.go:384 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L384)

```go
func (column DocsColumn) Converted(row uint) bool
```

##### `Len`

Len reports how many rows the column holds.

[docs.go:373 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L373)

```go
func (column DocsColumn) Len() uint
```

##### `Strings`

Strings views the rows of a string, binary or JSON column in the arena, and nothing for a scalar column.

[docs.go:399 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L399)

```go
func (column DocsColumn) Strings() Strings
```

##### `Valid`

Valid reports whether row's value was found and converted.

[docs.go:376 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L376)

```go
func (column DocsColumn) Valid(row uint) bool
```

#### Enum `DocsFieldType`

DocsFieldType is the type of a document, or of a value extracted from one; every type but JSON text, strings and binary text is a fixed-width scalar.2

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

```go
type DocsFieldType int32
```

#### Values

| `DocsFieldTypeJSON` | | DocsFieldTypeJSON is JSON text. |
| - | - | - |
| `DocsFieldTypeNull` | | DocsFieldTypeNull is the JSON null. |
| `DocsFieldTypeBool` | | DocsFieldTypeBool is a boolean. |
| `DocsFieldTypeUUID` | | DocsFieldTypeUUID is a 128-bit UUID. |
| `DocsFieldTypeF64` | | DocsFieldTypeF64 is a 64-bit float. |
| `DocsFieldTypeF32` | | DocsFieldTypeF32 is a 32-bit float. |
| `DocsFieldTypeF16` | | DocsFieldTypeF16 is a 16-bit float. |
| `DocsFieldTypeI64` | | DocsFieldTypeI64 is a signed 64-bit integer. |
| `DocsFieldTypeI32` | | DocsFieldTypeI32 is a signed 32-bit integer. |
| `DocsFieldTypeI16` | | DocsFieldTypeI16 is a signed 16-bit integer. |
| `DocsFieldTypeI8` | | DocsFieldTypeI8 is a signed 8-bit integer. |
| `DocsFieldTypeU64` | | DocsFieldTypeU64 is an unsigned 64-bit integer. |
| `DocsFieldTypeU32` | | DocsFieldTypeU32 is an unsigned 32-bit integer. |
| `DocsFieldTypeU16` | | DocsFieldTypeU16 is an unsigned 16-bit integer. |
| `DocsFieldTypeU8` | | DocsFieldTypeU8 is an unsigned 8-bit integer. |
| `DocsFieldTypeBinary` | | DocsFieldTypeBinary is the text of a value without a null terminator. |
| `DocsFieldTypeString` | | DocsFieldTypeString is a value printed as text. |

##### `DocsFieldTypeFromName`

DocsFieldTypeFromName reads a field type back from the spelling [DocsFieldType.String](https://www.unum.cloud/docs/ustore/go/platform-notes.md#String) renders.

[docs.go:87 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L87)

```go
func DocsFieldTypeFromName(name string) (DocsFieldType, error)
```

##### `String`

String renders the field type as the library spells it, such as "f64".

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

```go
func (fieldType DocsFieldType) String() string
```

#### Enum `DocsModification`

DocsModification is the modification a document write applies.2

[docs.go:94 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L94)

```go
type DocsModification int32
```

#### Values

| `DocsModifyUpsert` | | DocsModifyUpsert writes whether or not the document exists. |
| - | - | - |
| `DocsModifyUpdate` | | DocsModifyUpdate writes only documents that exist. |
| `DocsModifyInsert` | | DocsModifyInsert writes only documents that do not exist. |
| `DocsModifyPatch` | | DocsModifyPatch applies an RFC 6902 patch. |
| `DocsModifyMerge` | | DocsModifyMerge applies an RFC 7386 merge. |

##### `DocsModificationFromName`

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

[docs.go:116 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L116)

```go
func DocsModificationFromName(
    name string,
) (DocsModification, error)
```

##### `String`

String renders the modification as the library spells it.

[docs.go:110 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L110)

```go
func (modification DocsModification) String() string
```

#### Struct `Paths`

Paths is every unique JSON Pointer to a leaf that a gist found, readable until the arena serves another call.2

[docs.go:284 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L284)

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

##### `Get`

Get views path index in the arena.

[docs.go:294 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L294)

```go
func (paths Paths) Get(index uint) []byte
```

##### `Len`

Len reports how many unique paths the call found.

[docs.go:291 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/docs.go#L291)

```go
func (paths Paths) Len() uint
```

### Functions

#### `DocsColumnScalars`

DocsColumnScalars views the scalars of a column gathered as the type Value spells, in place; a Value of another width than the column's type reads garbage, as the column does not record it.

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

```go
func DocsColumnScalars[Value DocsScalar](
    column DocsColumn,
) []Value
```

#### `DocsColumnViewToArrow`

DocsColumnViewToArrow exports one column a gather of fieldType wrote into arena as an Arrow column whose validity buffer is the column's validities; 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:118 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/ustore.go#L118)

```go
func DocsColumnViewToArrow(
    column DocsColumn,
    fieldType DocsFieldType,
    arena *Arena,
    schema *ArrowSchema,
    array *ArrowDeviceArray,
) error
```
