-
Notifications
You must be signed in to change notification settings - Fork 246
/
reader.go
256 lines (212 loc) · 6.65 KB
/
reader.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package crdb
import (
"context"
"errors"
"fmt"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"github.com/authzed/spicedb/internal/datastore/common"
pgxcommon "github.com/authzed/spicedb/internal/datastore/postgres/common"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/options"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
const (
errUnableToReadConfig = "unable to read namespace config: %w"
errUnableToListNamespaces = "unable to list namespaces: %w"
)
var (
queryReadNamespace = psql.Select(colConfig, colTimestamp)
queryTuples = psql.Select(
colNamespace,
colObjectID,
colRelation,
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCaveatContextName,
colCaveatContext,
)
schema = common.NewSchemaInformation(
colNamespace,
colObjectID,
colRelation,
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCaveatContextName,
common.ExpandedLogicComparison,
)
)
type crdbReader struct {
query pgxcommon.DBFuncQuerier
executor common.QueryExecutor
keyer overlapKeyer
overlapKeySet keySet
fromBuilder func(query sq.SelectBuilder, fromStr string) sq.SelectBuilder
}
func (cr *crdbReader) ReadNamespaceByName(
ctx context.Context,
nsName string,
) (*core.NamespaceDefinition, datastore.Revision, error) {
config, timestamp, err := cr.loadNamespace(ctx, cr.query, nsName)
if err != nil {
if errors.As(err, &datastore.ErrNamespaceNotFound{}) {
return nil, datastore.NoRevision, err
}
return nil, datastore.NoRevision, fmt.Errorf(errUnableToReadConfig, err)
}
return config, revisionFromTimestamp(timestamp), nil
}
func (cr *crdbReader) ListAllNamespaces(ctx context.Context) ([]datastore.RevisionedNamespace, error) {
nsDefs, err := loadAllNamespaces(ctx, cr.query, cr.fromBuilder)
if err != nil {
return nil, fmt.Errorf(errUnableToListNamespaces, err)
}
return nsDefs, nil
}
func (cr *crdbReader) LookupNamespacesWithNames(ctx context.Context, nsNames []string) ([]datastore.RevisionedNamespace, error) {
if len(nsNames) == 0 {
return nil, nil
}
nsDefs, err := cr.lookupNamespaces(ctx, cr.query, nsNames)
if err != nil {
return nil, fmt.Errorf(errUnableToListNamespaces, err)
}
return nsDefs, nil
}
func (cr *crdbReader) QueryRelationships(
ctx context.Context,
filter datastore.RelationshipsFilter,
opts ...options.QueryOptionsOption,
) (iter datastore.RelationshipIterator, err error) {
query := cr.fromBuilder(queryTuples, tableTuple)
qBuilder, err := common.NewSchemaQueryFilterer(schema, query).FilterWithRelationshipsFilter(filter)
if err != nil {
return nil, err
}
return cr.executor.ExecuteQuery(ctx, qBuilder, nil, opts...)
}
func (cr *crdbReader) ReverseQueryRelationships(
ctx context.Context,
subjectsFilter datastore.SubjectsFilter,
opts ...options.ReverseQueryOptionsOption,
) (iter datastore.RelationshipIterator, err error) {
query := cr.fromBuilder(queryTuples, tableTuple)
qBuilder, err := common.NewSchemaQueryFilterer(schema, query).
FilterWithSubjectsSelectors(subjectsFilter.AsSelector())
if err != nil {
return nil, err
}
queryOpts := options.NewReverseQueryOptionsWithOptions(opts...)
if queryOpts.ResRelation != nil {
qBuilder = qBuilder.
FilterToResourceType(queryOpts.ResRelation.Namespace).
FilterToRelation(queryOpts.ResRelation.Relation)
}
return cr.executor.ExecuteQuery(
ctx,
qBuilder,
nil,
options.WithLimit(queryOpts.LimitForReverse),
options.WithAfter(queryOpts.AfterForReverse),
options.WithSort(queryOpts.SortForReverse))
}
func (cr crdbReader) loadNamespace(ctx context.Context, tx pgxcommon.DBFuncQuerier, nsName string) (*core.NamespaceDefinition, time.Time, error) {
query := cr.fromBuilder(queryReadNamespace, tableNamespace).Where(sq.Eq{colNamespace: nsName})
sql, args, err := query.ToSql()
if err != nil {
return nil, time.Time{}, err
}
var config []byte
var timestamp time.Time
err = tx.QueryRowFunc(ctx, func(ctx context.Context, row pgx.Row) error {
return row.Scan(&config, ×tamp)
}, sql, args...)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
err = datastore.NewNamespaceNotFoundErr(nsName)
}
return nil, time.Time{}, err
}
loaded := &core.NamespaceDefinition{}
if err := loaded.UnmarshalVT(config); err != nil {
return nil, time.Time{}, err
}
return loaded, timestamp, nil
}
func (cr crdbReader) lookupNamespaces(ctx context.Context, tx pgxcommon.DBFuncQuerier, nsNames []string) ([]datastore.RevisionedNamespace, error) {
clause := sq.Or{}
for _, nsName := range nsNames {
clause = append(clause, sq.Eq{colNamespace: nsName})
}
query := cr.fromBuilder(queryReadNamespace, tableNamespace).Where(clause)
sql, args, err := query.ToSql()
if err != nil {
return nil, err
}
var nsDefs []datastore.RevisionedNamespace
err = tx.QueryFunc(ctx, func(ctx context.Context, rows pgx.Rows) error {
for rows.Next() {
var config []byte
var timestamp time.Time
if err := rows.Scan(&config, ×tamp); err != nil {
return err
}
loaded := &core.NamespaceDefinition{}
if err := loaded.UnmarshalVT(config); err != nil {
return fmt.Errorf(errUnableToReadConfig, err)
}
nsDefs = append(nsDefs, datastore.RevisionedNamespace{
Definition: loaded,
LastWrittenRevision: revisionFromTimestamp(timestamp),
})
}
if rows.Err() != nil {
return fmt.Errorf(errUnableToReadConfig, rows.Err())
}
return nil
}, sql, args...)
if err != nil {
return nil, err
}
return nsDefs, nil
}
func loadAllNamespaces(ctx context.Context, tx pgxcommon.DBFuncQuerier, fromBuilder func(sq.SelectBuilder, string) sq.SelectBuilder) ([]datastore.RevisionedNamespace, error) {
query := fromBuilder(queryReadNamespace, tableNamespace)
sql, args, err := query.ToSql()
if err != nil {
return nil, err
}
var nsDefs []datastore.RevisionedNamespace
err = tx.QueryFunc(ctx, func(ctx context.Context, rows pgx.Rows) error {
for rows.Next() {
var config []byte
var timestamp time.Time
if err := rows.Scan(&config, ×tamp); err != nil {
return err
}
loaded := &core.NamespaceDefinition{}
if err := loaded.UnmarshalVT(config); err != nil {
return fmt.Errorf(errUnableToReadConfig, err)
}
nsDefs = append(nsDefs, datastore.RevisionedNamespace{
Definition: loaded,
LastWrittenRevision: revisionFromTimestamp(timestamp),
})
}
if rows.Err() != nil {
return fmt.Errorf(errUnableToReadConfig, rows.Err())
}
return nil
}, sql, args...)
if err != nil {
return nil, err
}
return nsDefs, nil
}
func (cr *crdbReader) addOverlapKey(namespace string) {
cr.keyer.addKey(cr.overlapKeySet, namespace)
}
var _ datastore.Reader = &crdbReader{}