-
Notifications
You must be signed in to change notification settings - Fork 267
/
graph.go
99 lines (81 loc) · 3.04 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package graph
import (
"context"
"testing"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/datastore"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
)
// Ellipsis relation is used to signify a semantic-free relationship.
const Ellipsis = "..."
// maxDispatchChunkSize is the maximum size for a dispatch chunk. Must be less than or equal
// to the maximum ID count for filters in the datastore.
var maxDispatchChunkSize uint16 = datastore.FilterMaximumIDCount
// progressiveDispatchChunkSizes are chunk sizes growing over time for dispatching. All entries
// must be less than or equal to the maximum ID count for filters in the datastore.
var progressiveDispatchChunkSizes = []uint16{5, 10, 25, 50, maxDispatchChunkSize}
// SetDispatchChunkSizesForTesting sets the dispatch chunk sizes for testing.
func SetDispatchChunkSizesForTesting(t *testing.T, sizes []uint16) {
originalSizes := progressiveDispatchChunkSizes
maxDispatchChunkSize = sizes[len(sizes)-1]
progressiveDispatchChunkSizes = sizes
t.Cleanup(func() {
progressiveDispatchChunkSizes = originalSizes
maxDispatchChunkSize = originalSizes[len(sizes)-1]
})
}
// CheckResult is the data that is returned by a single check or sub-check.
type CheckResult struct {
Resp *v1.DispatchCheckResponse
Err error
}
// ExpandResult is the data that is returned by a single expand or sub-expand.
type ExpandResult struct {
Resp *v1.DispatchExpandResponse
Err error
}
// ReduceableExpandFunc is a function that can be bound to a execution context.
type ReduceableExpandFunc func(ctx context.Context, resultChan chan<- ExpandResult)
// AlwaysFailExpand is a ReduceableExpandFunc which will always fail when reduced.
func AlwaysFailExpand(_ context.Context, resultChan chan<- ExpandResult) {
resultChan <- expandResultError(NewAlwaysFailErr(), emptyMetadata)
}
// ExpandReducer is a type for the functions Any and All which combine check results.
type ExpandReducer func(
ctx context.Context,
start *core.ObjectAndRelation,
requests []ReduceableExpandFunc,
) ExpandResult
func decrementDepth(md *v1.ResolverMeta) *v1.ResolverMeta {
return &v1.ResolverMeta{
AtRevision: md.AtRevision,
DepthRemaining: md.DepthRemaining - 1,
}
}
func max(x, y uint32) uint32 {
if x < y {
return y
}
return x
}
var emptyMetadata = &v1.ResponseMeta{}
func ensureMetadata(subProblemMetadata *v1.ResponseMeta) *v1.ResponseMeta {
if subProblemMetadata == nil {
subProblemMetadata = emptyMetadata
}
return &v1.ResponseMeta{
DispatchCount: subProblemMetadata.DispatchCount,
DepthRequired: subProblemMetadata.DepthRequired,
CachedDispatchCount: subProblemMetadata.CachedDispatchCount,
DebugInfo: subProblemMetadata.DebugInfo,
}
}
func addCallToResponseMetadata(metadata *v1.ResponseMeta) *v1.ResponseMeta {
// + 1 for the current call.
return &v1.ResponseMeta{
DispatchCount: metadata.DispatchCount + 1,
DepthRequired: metadata.DepthRequired + 1,
CachedDispatchCount: metadata.CachedDispatchCount,
DebugInfo: metadata.DebugInfo,
}
}