-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
248 lines (211 loc) · 6.63 KB
/
store.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
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schemaregistry
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/conduitio/conduit-commons/database"
"github.com/twmb/franz-go/pkg/sr"
)
var errEmptySubject = errors.New("subject cannot be empty")
const (
// schemaStoreKeyPrefix is added to all keys before storing them in store.
schemaStoreKeyPrefix = "schemaregistry:schema:"
)
// schemaStore handles the persistence and fetching of schemas.
type schemaStore struct {
db database.DB
}
func newSchemaStore(db database.DB) *schemaStore {
return &schemaStore{
db: db,
}
}
func (s *schemaStore) Set(ctx context.Context, id int, sch sr.Schema) error {
raw, err := s.encode(sch)
if err != nil {
return err
}
err = s.db.Set(ctx, s.toKey(id), raw)
if err != nil {
return fmt.Errorf("failed to store schema with ID %q: %w", id, err)
}
return nil
}
func (s *schemaStore) Get(ctx context.Context, id int) (sr.Schema, error) {
raw, err := s.db.Get(ctx, s.toKey(id))
if err != nil {
return sr.Schema{}, fmt.Errorf("failed to get schema with ID %q: %w", id, err)
}
return s.decode(raw)
}
func (s *schemaStore) Delete(ctx context.Context, id int) error {
err := s.db.Set(ctx, s.toKey(id), nil)
if err != nil {
return fmt.Errorf("failed to delete schema with ID %q: %w", id, err)
}
return nil
}
// store is namespaced, meaning that keys all have the same prefix.
func (*schemaStore) toKey(id int) string {
return schemaStoreKeyPrefix + strconv.Itoa(id)
}
// encode from sr.Schema to []byte.
func (*schemaStore) encode(s sr.Schema) ([]byte, error) {
var b bytes.Buffer
enc := json.NewEncoder(&b)
err := enc.Encode(s)
if err != nil {
return nil, fmt.Errorf("failed to encode schema: %w", err)
}
return b.Bytes(), nil
}
// decode from []byte to sr.Schema.
func (s *schemaStore) decode(raw []byte) (sr.Schema, error) {
var out sr.Schema
r := bytes.NewReader(raw)
dec := json.NewDecoder(r)
err := dec.Decode(&out)
if err != nil {
return sr.Schema{}, fmt.Errorf("failed to decode schema: %w", err)
}
return out, nil
}
const (
// subjectSchemaStoreKeyPrefix is added to all keys before storing them in store.
subjectSchemaStoreKeyPrefix = "schemaregistry:subjectschema:"
)
// subjectSchemaStore handles the persistence and fetching of schemas.
type subjectSchemaStore struct {
db database.DB
}
func newSubjectSchemaStore(db database.DB) *subjectSchemaStore {
return &subjectSchemaStore{
db: db,
}
}
func (s *subjectSchemaStore) Set(ctx context.Context, subject string, version int, sch sr.SubjectSchema) error {
if subject == "" {
return fmt.Errorf("can't store subject schema: %w", errEmptySubject)
}
raw, err := s.encode(sch)
if err != nil {
return err
}
key := s.toKey(subject, version)
err = s.db.Set(ctx, key, raw)
if err != nil {
return fmt.Errorf("failed to store subject schema with subject:version %q:%q: %w", subject, version, err)
}
return nil
}
func (s *subjectSchemaStore) Get(ctx context.Context, subject string, version int) (sr.SubjectSchema, error) {
raw, err := s.db.Get(ctx, s.toKey(subject, version))
if err != nil {
return sr.SubjectSchema{}, fmt.Errorf("failed to get subject schema with subject:version %q:%q: %w", subject, version, err)
}
return s.decode(raw)
}
func (s *subjectSchemaStore) GetAll(ctx context.Context) ([]sr.SubjectSchema, error) {
keys, err := s.db.GetKeys(ctx, subjectSchemaStoreKeyPrefix)
if err != nil {
return nil, fmt.Errorf("failed to retrieve keys: %w", err)
}
schemas := make([]sr.SubjectSchema, len(keys))
for i, key := range keys {
raw, err := s.db.Get(ctx, key)
if err != nil {
return nil, fmt.Errorf("failed to get subject schema with subject:version %q: %w", key, err)
}
ss, err := s.decode(raw)
if err != nil {
return nil, fmt.Errorf("failed to decode subject schema with subject:version %q: %w", key, err)
}
schemas[i] = ss
}
return schemas, nil
}
func (s *subjectSchemaStore) Delete(ctx context.Context, subject string, version int) error {
if subject == "" {
return fmt.Errorf("can't delete subject schema: %w", errEmptySubject)
}
err := s.db.Set(ctx, s.toKey(subject, version), nil)
if err != nil {
return fmt.Errorf("failed to delete subject schema with subject:version %q:%q: %w", subject, version, err)
}
return nil
}
// store is namespaced, meaning that keys all have the same prefix.
func (*subjectSchemaStore) toKey(subject string, version int) string {
return subjectSchemaStoreKeyPrefix + subject + ":" + strconv.Itoa(version)
}
// encode from sr.SubjectSchema to []byte.
func (*subjectSchemaStore) encode(ss sr.SubjectSchema) ([]byte, error) {
var b bytes.Buffer
enc := json.NewEncoder(&b)
err := enc.Encode(ss)
if err != nil {
return nil, fmt.Errorf("failed to encode subject schema: %w", err)
}
return b.Bytes(), nil
}
// decode from []byte to sr.SubjectSchema.
func (s *subjectSchemaStore) decode(raw []byte) (sr.SubjectSchema, error) {
var out sr.SubjectSchema
r := bytes.NewReader(raw)
dec := json.NewDecoder(r)
err := dec.Decode(&out)
if err != nil {
return sr.SubjectSchema{}, fmt.Errorf("failed to decode subject schema: %w", err)
}
return out, nil
}
const (
// sequenceStoreKeyPrefix is added to all keys before storing them in store.
sequenceStoreKeyPrefix = "schemaregistry:sequence:"
)
// sequenceStore handles the persistence and fetching of schemas.
type sequenceStore struct {
db database.DB
key string
}
func newSequenceStore(db database.DB, sequenceName string) *sequenceStore {
return &sequenceStore{
db: db,
key: sequenceStoreKeyPrefix + sequenceName,
}
}
func (s *sequenceStore) Set(ctx context.Context, sequence int) error {
raw := []byte(strconv.Itoa(sequence))
err := s.db.Set(ctx, s.key, raw)
if err != nil {
return fmt.Errorf("failed to store sequence %q: %w", s.key, err)
}
return nil
}
func (s *sequenceStore) Get(ctx context.Context) (int, error) {
raw, err := s.db.Get(ctx, s.key)
if err != nil {
return 0, fmt.Errorf("failed to get sequence %q: %w", s.key, err)
}
sequence, err := strconv.Atoi(string(raw))
if err != nil {
return 0, fmt.Errorf("failed to convert sequence %q to int: %w", raw, err)
}
return sequence, nil
}