-
Notifications
You must be signed in to change notification settings - Fork 247
/
caveat.go
171 lines (149 loc) · 5 KB
/
caveat.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
package crdb
import (
"context"
"errors"
"fmt"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"github.com/authzed/spicedb/internal/datastore/revisions"
"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
var (
upsertCaveatSuffix = fmt.Sprintf(
"ON CONFLICT (%s) DO UPDATE SET %s = excluded.%s",
colCaveatName,
colCaveatDefinition,
colCaveatDefinition,
)
writeCaveat = psql.Insert(tableCaveat).Columns(colCaveatName, colCaveatDefinition).Suffix(upsertCaveatSuffix)
readCaveat = psql.Select(colCaveatDefinition, colTimestamp)
listCaveat = psql.Select(colCaveatName, colCaveatDefinition, colTimestamp).From(tableCaveat).OrderBy(colCaveatName)
deleteCaveat = psql.Delete(tableCaveat)
)
const (
errWriteCaveat = "unable to write new caveat revision: %w"
errReadCaveat = "unable to read new caveat `%s`: %w"
errListCaveats = "unable to list caveat: %w"
errDeleteCaveats = "unable to delete caveats: %w"
)
func (cr *crdbReader) ReadCaveatByName(ctx context.Context, name string) (*core.CaveatDefinition, datastore.Revision, error) {
query := cr.fromBuilder(readCaveat, tableCaveat).Where(sq.Eq{colCaveatName: name})
sql, args, err := query.ToSql()
if err != nil {
return nil, datastore.NoRevision, fmt.Errorf(errReadCaveat, name, err)
}
var definitionBytes []byte
var timestamp time.Time
err = cr.query.QueryRowFunc(ctx, func(ctx context.Context, row pgx.Row) error {
return row.Scan(&definitionBytes, ×tamp)
}, sql, args...)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
err = datastore.NewCaveatNameNotFoundErr(name)
}
return nil, datastore.NoRevision, fmt.Errorf(errReadCaveat, name, err)
}
loaded := &core.CaveatDefinition{}
if err := loaded.UnmarshalVT(definitionBytes); err != nil {
return nil, datastore.NoRevision, fmt.Errorf(errReadCaveat, name, err)
}
cr.addOverlapKey(name)
return loaded, revisions.NewHLCForTime(timestamp), nil
}
func (cr *crdbReader) LookupCaveatsWithNames(ctx context.Context, caveatNames []string) ([]datastore.RevisionedCaveat, error) {
if len(caveatNames) == 0 {
return nil, nil
}
return cr.lookupCaveats(ctx, caveatNames)
}
func (cr *crdbReader) ListAllCaveats(ctx context.Context) ([]datastore.RevisionedCaveat, error) {
return cr.lookupCaveats(ctx, nil)
}
type bytesAndTimestamp struct {
bytes []byte
timestamp time.Time
}
func (cr *crdbReader) lookupCaveats(ctx context.Context, caveatNames []string) ([]datastore.RevisionedCaveat, error) {
caveatsWithNames := cr.fromBuilder(listCaveat, tableCaveat)
if len(caveatNames) > 0 {
caveatsWithNames = caveatsWithNames.Where(sq.Eq{colCaveatName: caveatNames})
}
sql, args, err := caveatsWithNames.ToSql()
if err != nil {
return nil, fmt.Errorf(errListCaveats, err)
}
var allDefinitionBytes []bytesAndTimestamp
err = cr.query.QueryFunc(ctx, func(ctx context.Context, rows pgx.Rows) error {
for rows.Next() {
var defBytes []byte
var name string
var timestamp time.Time
err = rows.Scan(&name, &defBytes, ×tamp)
if err != nil {
return fmt.Errorf(errListCaveats, err)
}
allDefinitionBytes = append(allDefinitionBytes, bytesAndTimestamp{bytes: defBytes, timestamp: timestamp})
cr.addOverlapKey(name)
}
return nil
}, sql, args...)
if err != nil {
return nil, fmt.Errorf(errListCaveats, err)
}
caveats := make([]datastore.RevisionedCaveat, 0, len(allDefinitionBytes))
for _, bat := range allDefinitionBytes {
loaded := &core.CaveatDefinition{}
if err := loaded.UnmarshalVT(bat.bytes); err != nil {
return nil, fmt.Errorf(errListCaveats, err)
}
caveats = append(caveats, datastore.RevisionedCaveat{
Definition: loaded,
LastWrittenRevision: revisions.NewHLCForTime(bat.timestamp),
})
}
return caveats, nil
}
func (rwt *crdbReadWriteTXN) WriteCaveats(ctx context.Context, caveats []*core.CaveatDefinition) error {
if len(caveats) == 0 {
return nil
}
write := writeCaveat
writtenCaveatNames := make([]string, 0, len(caveats))
for _, caveat := range caveats {
definitionBytes, err := caveat.MarshalVT()
if err != nil {
return fmt.Errorf(errWriteCaveat, err)
}
valuesToWrite := []any{caveat.Name, definitionBytes}
write = write.Values(valuesToWrite...)
writtenCaveatNames = append(writtenCaveatNames, caveat.Name)
}
// store the new caveat
sql, args, err := write.ToSql()
if err != nil {
return fmt.Errorf(errWriteCaveat, err)
}
for _, val := range writtenCaveatNames {
rwt.addOverlapKey(val)
}
if _, err := rwt.tx.Exec(ctx, sql, args...); err != nil {
return fmt.Errorf(errWriteCaveat, err)
}
return nil
}
func (rwt *crdbReadWriteTXN) DeleteCaveats(ctx context.Context, names []string) error {
deleteCaveatClause := deleteCaveat.Where(sq.Eq{colCaveatName: names})
sql, args, err := deleteCaveatClause.ToSql()
if err != nil {
return fmt.Errorf(errDeleteCaveats, err)
}
for _, val := range names {
rwt.addOverlapKey(val)
}
if _, err := rwt.tx.Exec(ctx, sql, args...); err != nil {
return fmt.Errorf(errDeleteCaveats, err)
}
return nil
}