# Graphs

> A graph collection, created with ModalityGraph, indexes every edge from both of its vertices, and an Edge is laid out as the source, target and identifier keys the ABI puts on its tapes, so a slice of edges crosses as three strided columns.

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

A graph collection, created with `ModalityGraph`, indexes every edge from both of its vertices, and an `Edge` is laid out as the source, target and identifier keys the ABI puts on its tapes, so a slice of edges crosses as three strided columns. `GraphReadEdges` answers `Neighborhoods`, the degree of every vertex and its edges, outgoing ones before incoming ones, and `Edge.Neighbor` names the other end. A `VertexRole` gives the whole batch one role and `VertexRoles` gives every vertex its own.

```go
follows, err := database.CollectionCreate("follows", ustore.ModalityGraph)
edges := []ustore.Edge{
	{Source: first, Target: second, ID: ustore.EdgeIDDefault},
	{Source: first, Target: third, ID: ustore.EdgeIDDefault},
	{Source: second, Target: third, ID: seventh},
}
err = database.GraphWriteEdges(follows, edges, nil)

vertices := []ustore.Key{first, third}
found, err := database.GraphReadEdges(follows, vertices, ustore.VertexRoleAny, arena)
for index, vertex := range vertices {
	for _, edge := range found.Edges(uint(index)) {
		fmt.Println(vertex, "meets", edge.Neighbor(vertex))
	}
}
err = database.GraphRemoveVertices(follows, []ustore.Key{first}, nil)
```

## API

### Types

#### `GraphRemoveVerticesConfig`

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

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

```go
type GraphRemoveVerticesConfig struct {
    EndVertices []Key
    Roles Roles
    Durability Durability
}
```

#### Fields

| `EndVertices` | `[]Key` | EndVertices turns every vertex into the inclusive start of a range ending before its end vertex, one per task, or removes the vertices alone when empty. |
| - | - | - |
| `Roles` | `Roles` | Roles are the roles of the vertices, needed only by joining graphs. |
| `Durability` | `Durability` | Durability is how durable the removals are once the call finishes at the head. |

#### `GraphScanConfig`

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

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

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

#### Fields

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

#### `GraphVerticesCursorConfig`

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

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

```go
type GraphVerticesCursorConfig struct {
    EndKey *Key
    Direction ScanDirection
    Predicate Admitter
}
```

#### Fields

| `EndKey` | `*Key` | EndKey is the vertex the walk stops before, or nil for no bound. |
| - | - | - |
| `Direction` | `ScanDirection` | Direction is which way the walk goes from its start. |
| `Predicate` | `Admitter` | Predicate admits or passes over the vertices of every page. |

#### `Roles`

Roles names the role of every vertex in a batch: a [VertexRole](#VertexRole) the whole batch shares, or [VertexRoles](#VertexRoles), one per vertex; nil stands for [VertexRoleAny](#VertexRoleAny).

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

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

#### `VertexRoles`

VertexRoles names one role per vertex of a batch.

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

```go
type VertexRoles []VertexRole
```

#### Struct `Edge`

Edge is a source, a target and an edge identifier, laid out as the three keys the library puts on its tapes, so a slice of edges crosses as three strided columns.1

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

```go
type Edge struct {
    Source Key
    Target Key
    ID     Key
}
```

##### `Neighbor`

Neighbor reports the end of the edge other than vertex.

[graph.go:50 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L50)

```go
func (edge Edge) Neighbor(vertex Key) Key
```

#### Struct `Neighborhoods`

Neighborhoods is the degree and the edges of every vertex a read named, readable until the arena serves another call.3

[graph.go:135 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L135)

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

##### `Degree`

Degree reports how many edges vertex index connects to, and false for a missing vertex.

[graph.go:150 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L150)

```go
func (neighborhoods Neighborhoods) Degree(
    index uint,
) (uint32, bool)
```

##### `Edges`

Edges views the edges of vertex index in the arena: with both roles the outgoing ones first, sorted by target, then the incoming ones, sorted by source.

[graph.go:159 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L159)

```go
func (neighborhoods Neighborhoods) Edges(index uint) []Edge
```

##### `Len`

Len reports how many vertices the call read.

[graph.go:144 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L144)

```go
func (neighborhoods Neighborhoods) Len() uint
```

#### Enum `VertexRole`

VertexRole is the role a vertex plays in the edges a call names.2

[graph.go:58 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L58)

```go
type VertexRole int32
```

#### Values

| `VertexRoleUnknown` | | VertexRoleUnknown is no role. |
| - | - | - |
| `VertexRoleSource` | | VertexRoleSource is the vertex an edge leaves. |
| `VertexRoleTarget` | | VertexRoleTarget is the vertex an edge enters. |
| `VertexRoleAny` | | VertexRoleAny is either end of an edge. |

##### `VertexRoleFromName`

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

[graph.go:77 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L77)

```go
func VertexRoleFromName(name string) (VertexRole, error)
```

##### `String`

String renders the role as the library spells it.

[graph.go:72 ↗](https://github.com/unum-cloud/UStore/blob/084197c470a8c329d83051d880c71e7972b9e1b2/graph.go#L72)

```go
func (role VertexRole) String() string
```

### Constants

#### `EdgeIDDefault`

EdgeIDDefault is the identifier of an edge that was given none, every bit of it set.

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

```go
var EdgeIDDefault = KeyUnknown
```
