-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra.go
275 lines (215 loc) · 7.01 KB
/
cassandra.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package mutable
import (
"context"
"fmt"
"github.com/bleemeo/squirreldb/cassandra/connection"
"github.com/gocql/gocql"
"github.com/rs/zerolog"
)
type Options struct {
Connection *connection.Connection
ReadOnly bool
Logger zerolog.Logger
}
type cassandra struct {
options Options
}
type Store interface {
AssociatedNames(ctx context.Context, tenant string) (map[string]string, error)
AssociatedValues(ctx context.Context, tenant, name string) (map[string][]string, error)
DeleteAssociatedName(ctx context.Context, tenant, name string) error
DeleteAssociatedValues(ctx context.Context, label Label) error
SetAssociatedName(ctx context.Context, label LabelWithName) error
SetAssociatedValues(ctx context.Context, label LabelWithValues) error
AllMutableLabels(ctx context.Context) ([]LabelWithName, error)
}
// NewCassandraStore returns a new Cassandra mutable label store.
func NewCassandraStore(ctx context.Context, options Options) (Store, error) {
cp := &cassandra{
options: options,
}
if err := cp.createTables(ctx); err != nil {
return nil, err
}
return cp, nil
}
// createTables creates the mutable labels table if it doesn't exist.
func (cp *cassandra) createTables(ctx context.Context) error {
if cp.options.ReadOnly {
cp.options.Logger.Debug().Msg("Read-only mode is activated. Not trying to create tables and assuming they exist")
return nil
}
session, err := cp.options.Connection.Session()
if err != nil {
return err
}
defer session.Close()
queryMutableLabelValues := session.Query(`
CREATE TABLE IF NOT EXISTS mutable_label_values (
tenant text,
name text,
value text,
associated_values frozen<list<text>>,
PRIMARY KEY ((tenant, name), value)
)`,
).WithContext(ctx)
queryMutableLabelNames := session.Query(`
CREATE TABLE IF NOT EXISTS mutable_label_names (
tenant text,
name text,
associated_name text,
PRIMARY KEY (tenant, name)
)`,
).WithContext(ctx)
for _, query := range []*gocql.Query{queryMutableLabelValues, queryMutableLabelNames} {
query.Consistency(gocql.All)
if err := query.Exec(); err != nil {
return fmt.Errorf("create tables: %w", err)
}
}
return nil
}
// AssociatedNames return the associated names map[mutable label name] -> non mutable label name.
func (cp *cassandra) AssociatedNames(ctx context.Context, tenant string) (map[string]string, error) {
session, err := cp.options.Connection.SessionReadOnly()
if err != nil {
return nil, err
}
defer session.Close()
iter := session.Query(`
SELECT name, associated_name FROM mutable_label_names
WHERE tenant = ?`,
tenant,
).WithContext(ctx).Iter()
var (
associatedNames = make(map[string]string)
name string
associatedName string
)
for iter.Scan(&name, &associatedName) {
associatedNames[name] = associatedName
}
if err := iter.Close(); err != nil {
return nil, fmt.Errorf("select labels: %w", err)
}
return associatedNames, nil
}
// AssociatedValues returns the associated values for a mutable label name and tenant.
func (cp *cassandra) AssociatedValues(ctx context.Context, tenant, name string) (map[string][]string, error) {
session, err := cp.options.Connection.SessionReadOnly()
if err != nil {
return nil, err
}
defer session.Close()
iter := session.Query(`
SELECT value, associated_values FROM mutable_label_values
WHERE tenant = ? AND name = ?`,
tenant, name,
).WithContext(ctx).Iter()
var (
values = make(map[string][]string)
value string
associatedValues []string
)
for iter.Scan(&value, &associatedValues) {
values[value] = associatedValues
}
if err := iter.Close(); err != nil {
return nil, fmt.Errorf("select values: %w", err)
}
return values, nil
}
// SetAssociatedValues sets the non mutable label values associated to a mutable label.
func (cp *cassandra) SetAssociatedValues(ctx context.Context, label LabelWithValues) error {
session, err := cp.options.Connection.Session()
if err != nil {
return err
}
defer session.Close()
query := session.Query(`
INSERT INTO mutable_label_values (tenant, name, value, associated_values)
VALUES (?, ?, ?, ?)`,
label.Tenant, label.Name, label.Value, label.AssociatedValues,
).WithContext(ctx)
if err := query.Exec(); err != nil {
return fmt.Errorf("insert associated values: %w", err)
}
return nil
}
// DeleteAssociatedValues deletes the non mutable label values associated to a mutable label.
func (cp *cassandra) DeleteAssociatedValues(ctx context.Context, label Label) error {
session, err := cp.options.Connection.Session()
if err != nil {
return err
}
defer session.Close()
query := session.Query(`
DELETE FROM mutable_label_values
WHERE tenant = ? AND name = ? AND value = ?`,
label.Tenant, label.Name, label.Value,
).WithContext(ctx)
if err := query.Exec(); err != nil {
return fmt.Errorf("delete associated values: %w", err)
}
return nil
}
// SetAssociatedName sets the non mutable label name associated to a mutable label name.
func (cp *cassandra) SetAssociatedName(ctx context.Context, label LabelWithName) error {
session, err := cp.options.Connection.Session()
if err != nil {
return err
}
defer session.Close()
query := session.Query(`
INSERT INTO mutable_label_names (tenant, name, associated_name)
VALUES (?, ?, ?)`,
label.Tenant, label.Name, label.AssociatedName,
).WithContext(ctx)
if err := query.Exec(); err != nil {
return fmt.Errorf("insert associated name: %w", err)
}
return nil
}
// DeleteAssociatedName deletes a mutable label name.
func (cp *cassandra) DeleteAssociatedName(ctx context.Context, tenant, name string) error {
session, err := cp.options.Connection.Session()
if err != nil {
return err
}
defer session.Close()
queryDeleteLabelNames := session.Query(`
DELETE FROM mutable_label_names
WHERE tenant = ? AND name = ?`,
tenant, name,
).WithContext(ctx)
if err := queryDeleteLabelNames.Exec(); err != nil {
return fmt.Errorf("delete label name from mutable_label_names: %w", err)
}
queryDeleteLabelValues := session.Query(`
DELETE FROM squirreldb.mutable_label_values
WHERE tenant = ? AND name = ?`,
tenant, name,
).WithContext(ctx)
if err := queryDeleteLabelValues.Exec(); err != nil {
return fmt.Errorf("delete label name from mutable_label_values: %w", err)
}
return nil
}
// AllMutableLabels return the list of all known mutable labels from all tenants.
// Currently this function is not optimized (it does a Cassandra full scan) and should be avoided if possible.
func (cp *cassandra) AllMutableLabels(ctx context.Context) ([]LabelWithName, error) {
session, err := cp.options.Connection.SessionReadOnly()
if err != nil {
return nil, err
}
result := make([]LabelWithName, 0)
iter := session.Query("SELECT tenant, name, associated_name FROM mutable_label_names").WithContext(ctx).Iter()
var currentLabel LabelWithName
for iter.Scan(¤tLabel.Tenant, ¤tLabel.Name, ¤tLabel.AssociatedName) {
result = append(result, currentLabel)
}
if err := iter.Close(); err != nil {
return nil, fmt.Errorf("select tenants: %w", err)
}
return result, ctx.Err()
}