forked from monzo/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyspace.go
248 lines (223 loc) · 6.66 KB
/
keyspace.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
package gocassa
import (
"fmt"
"strings"
"time"
)
type tableFactory interface {
NewTable(string, interface{}, map[string]interface{}, Keys) Table
}
type k struct {
qe QueryExecutor
name string
debugMode bool
tableFactory tableFactory
}
// Connect to a certain keyspace directly. Same as using Connect().KeySpace(keySpaceName)
func ConnectToKeySpace(keySpace string, nodeIps []string, username, password string) (KeySpace, error) {
c, err := Connect(nodeIps, username, password)
if err != nil {
return nil, err
}
return c.KeySpace(keySpace), nil
}
func (k *k) DebugMode(b bool) {
k.debugMode = b
}
func (k *k) Table(name string, entity interface{}, keys Keys) Table {
n := name + "__" + strings.Join(keys.PartitionKeys, "_") + "__" + strings.Join(keys.ClusteringColumns, "_")
m, ok := toMap(entity)
if !ok {
panic("Unrecognized row type")
}
return k.NewTable(n, entity, m, keys)
}
func (k *k) NewTable(name string, entity interface{}, fields map[string]interface{}, keys Keys) Table {
// Act both as a proxy to a tableFactory, and as the tableFactory itself (in most situations, a k will be its own
// tableFactory, but not always [ie. mocking])
if k.tableFactory != k {
return k.tableFactory.NewTable(name, entity, fields, keys)
} else {
ti := newTableInfo(k.name, name, keys, entity, fields)
return &t{
keySpace: k,
info: ti,
options: Options{},
}
}
}
func (k *k) MapTable(name, id string, row interface{}) MapTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
return &mapT{
t: k.NewTable(fmt.Sprintf("%s_map_%s", name, id), row, m, Keys{
PartitionKeys: []string{id},
}),
idField: id,
}
}
func (k *k) SetKeysSpaceName(name string) {
k.name = name
}
func (k *k) MultimapTable(name, fieldToIndexBy, id string, row interface{}) MultimapTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
return &multimapT{
t: k.NewTable(fmt.Sprintf("%s_multimap_%s_%s", name, fieldToIndexBy, id), row, m, Keys{
PartitionKeys: []string{fieldToIndexBy},
ClusteringColumns: []string{id},
}),
idField: id,
fieldToIndexBy: fieldToIndexBy,
}
}
func (k *k) MultimapMultiKeyTable(name string, fieldToIndexBy, id []string, row interface{}) MultimapMkTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
return &multimapMkT{
t: k.NewTable(fmt.Sprintf("%s_multimapMk", name), row, m, Keys{
PartitionKeys: fieldToIndexBy,
ClusteringColumns: id,
}),
idField: id,
fieldsToIndexBy: fieldToIndexBy,
}
}
func (k *k) TimeSeriesTable(name, timeField, idField string, bucketSize time.Duration, row interface{}) TimeSeriesTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
m[bucketFieldName] = time.Now()
return &timeSeriesT{
t: k.NewTable(fmt.Sprintf("%s_timeSeries_%s_%s_%s", name, timeField, idField, bucketSize), row, m, Keys{
PartitionKeys: []string{bucketFieldName},
ClusteringColumns: []string{timeField, idField},
}),
timeField: timeField,
idField: idField,
bucketSize: bucketSize,
}
}
func (k *k) MultiTimeSeriesTable(name, indexField, timeField, idField string, bucketSize time.Duration, row interface{}) MultiTimeSeriesTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
m[bucketFieldName] = time.Now()
return &multiTimeSeriesT{
t: k.NewTable(fmt.Sprintf("%s_multiTimeSeries_%s_%s_%s_%s", name, indexField, timeField, idField, bucketSize.String()), row, m, Keys{
PartitionKeys: []string{indexField, bucketFieldName},
ClusteringColumns: []string{timeField, idField},
}),
indexField: indexField,
timeField: timeField,
idField: idField,
bucketSize: bucketSize,
}
}
func (k *k) MultiKeyTimeSeriesTable(name string, indexFields []string, timeField string, idFields []string, bucketSize time.Duration, row interface{}) MultiKeyTimeSeriesTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
partitionKeys := indexFields
partitionKeys = append(partitionKeys, bucketFieldName)
clusteringColumns := []string{timeField}
clusteringColumns = append(clusteringColumns, idFields...)
m[bucketFieldName] = time.Now()
return &multiKeyTimeSeriesT{
t: k.NewTable(fmt.Sprintf("%s_multiKeyTimeSeries_%s_%s", name, timeField, bucketSize.String()), row, m, Keys{
PartitionKeys: partitionKeys,
ClusteringColumns: clusteringColumns,
}),
indexFields: indexFields,
timeField: timeField,
idFields: idFields,
bucketSize: bucketSize,
}
}
func (k *k) FlakeSeriesTable(name, idField string, bucketSize time.Duration, row interface{}) FlakeSeriesTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
m[flakeTimestampFieldName] = time.Now()
m[bucketFieldName] = time.Now()
return &flakeSeriesT{
t: k.NewTable(fmt.Sprintf("%s_flakeSeries_%s_%s", name, idField, bucketSize.String()), row, m, Keys{
PartitionKeys: []string{bucketFieldName},
ClusteringColumns: []string{flakeTimestampFieldName, idField},
}),
idField: idField,
bucketSize: bucketSize,
}
}
func (k *k) MultiFlakeSeriesTable(name, indexField, idField string, bucketSize time.Duration, row interface{}) MultiFlakeSeriesTable {
m, ok := toMap(row)
if !ok {
panic("Unrecognized row type")
}
m[flakeTimestampFieldName] = time.Now()
m[bucketFieldName] = time.Now()
return &multiFlakeSeriesT{
t: k.NewTable(fmt.Sprintf("%s_multiflakeSeries_%s_%s_%s", name, indexField, idField, bucketSize.String()), row, m, Keys{
PartitionKeys: []string{indexField, bucketFieldName},
ClusteringColumns: []string{flakeTimestampFieldName, idField},
}),
idField: idField,
bucketSize: bucketSize,
indexField: indexField,
}
}
type tableInfoMarshal struct {
TableName string `cql:"table_name"`
}
// Returns table names in a keyspace
func (k *k) Tables() ([]string, error) {
if k.qe == nil {
return nil, fmt.Errorf("no query executor configured")
}
res := []tableInfoMarshal{}
stmt := SelectStatement{
keyspace: "system_schema",
table: "tables",
fields: []string{"table_name"},
where: []Relation{Eq("keyspace_name", k.name)},
}
err := k.qe.Query(stmt, NewScanner(stmt, &res))
if err != nil {
return nil, err
}
ret := []string{}
for _, v := range res {
ret = append(ret, v.TableName)
}
return ret, nil
}
func (k *k) Exists(cf string) (bool, error) {
ts, err := k.Tables()
if err != nil {
return false, err
}
for _, v := range ts {
if strings.ToLower(v) == strings.ToLower(cf) {
return true, nil
}
}
return false, nil
}
func (k *k) DropTable(cf string) error {
query := fmt.Sprintf("DROP TABLE IF EXISTS %s.%s", k.name, cf)
stmt := cqlStatement{query: query}
return k.qe.Execute(stmt)
}
func (k *k) Name() string {
return k.name
}