-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra.go
467 lines (421 loc) · 12.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package nosqlorm
import (
"errors"
"fmt"
"github.com/gocql/gocql"
"log"
"reflect"
"slices"
"strings"
"sync"
"time"
"unsafe"
)
const cqlTAG = "cql"
const jsonTAG = "json"
var modelCache sync.Map
type tableSchema struct {
fields []string
fieldMap map[string]tableField
}
type tableField struct {
fieldName string
isPartitionKey bool
isClusteringKey bool
isStatic bool
dataType reflect.Kind
isPointer bool
isList bool
offSet uintptr
}
type cqlOrm[T interface{}] struct {
sess *gocql.Session
}
// NewCqlOrm Create a new object to access specific Cassandra table
func NewCqlOrm[T interface{}](session *gocql.Session) (*cqlOrm[T], error) {
// Cache table schema to memory
var t T
typ := reflect.TypeOf(t)
typName := typ.String()
if typ.Kind() != reflect.Struct {
panic("Must be struct")
}
if _, existing := modelCache.Load(typName); !existing {
schema := tableSchema{
fields: make([]string, 0),
fieldMap: make(map[string]tableField),
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag
// Validate tag
fieldName := getFieldName(tag)
if fieldName == "" {
return nil, errors.New("invalid CQL Tag: must have a json name")
}
if !isValidCqlTag(tag) {
return nil, errors.New(fmt.Sprintf("Invalid CQL Tag for filed %s: %s", fieldName, tag.Get(cqlTAG)))
}
// Validate whether it is valid type
dbType, isPointer, err := getFieldDBType(field.Type.String(), isDateFiled(tag))
if err != nil {
log.Fatal("Invalid field type " + field.Type.String())
return nil, err
}
fieldType := field.Type.Kind()
if isPointer {
fieldType = field.Type.Elem().Kind()
}
if strings.HasPrefix(dbType, "list") {
fieldType = field.Type.Elem().Kind()
}
schema.fields = append(schema.fields, fieldName)
schema.fieldMap[fieldName] = tableField{
fieldName: fieldName,
isPartitionKey: isPartitionKey(tag),
isClusteringKey: isClusterKey(tag),
isStatic: isStaticFiled(tag),
dataType: fieldType,
isPointer: isPointer,
isList: strings.HasPrefix(dbType, "list"),
offSet: field.Offset,
}
}
modelCache.Store(typName, schema)
}
orm := &cqlOrm[T]{sess: session}
return orm, nil
}
// Auto create or update table for Cassandra
func CreateCassandraTables(sess *gocql.Session, tables ...interface{}) error {
for _, table := range tables {
typ := reflect.TypeOf(table)
tableName := strings.ToLower(typ.Name())
fields := make([]string, 0)
pkKeys := make([]string, 0)
ckKeys := make([]string, 0)
for i := 0; i < typ.NumField(); i++ {
tag := typ.Field(i).Tag
filedName := getFieldName(tag)
if filedName == "-" {
continue
}
filedDBType, _, err := getFieldDBType(typ.Field(i).Type.String(), isDateFiled(tag))
if err != nil {
return err
}
isStatic := ""
if isStaticFiled(tag) {
isStatic = " static"
}
fields = append(fields, fmt.Sprintf("%s %s%s", filedName, filedDBType, isStatic))
if isPartitionKey(tag) {
pkKeys = append(pkKeys, filedName)
}
if isClusterKey(tag) {
ckKeys = append(ckKeys, filedName)
}
}
fieldSql := strings.Join(fields, ", ")
pkSql := strings.Join(pkKeys, ", ")
if len(pkKeys) > 1 {
pkSql = "(" + pkSql + ")"
} else if len(pkKeys) == 0 {
panic(tableName + " must have at least one Partition Key")
}
ckSql := strings.Join(ckKeys, ", ")
if len(ckKeys) > 0 {
ckSql = ", " + ckSql
}
sql := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s, PRIMARY KEY (%s%s));", tableName, fieldSql, pkSql, ckSql)
println(sql)
err := sess.Query(sql).Exec()
if err != nil {
return errors.New(fmt.Sprintf("Create Cassandra Tables %s failed: %s\n", tableName, err.Error()))
}
fmt.Printf("Create Cassandra table %s success\n", tableName)
}
return nil
}
func (ctx *cqlOrm[T]) Insert(obj T) error {
val := reflect.ValueOf(obj)
typ := val.Type()
tableName := strings.ToLower(typ.Name())
schema, ok := modelCache.Load(typ.String())
if !ok {
return errors.New(fmt.Sprintf("Table %s not found", tableName))
}
tableFields := schema.(tableSchema).fields
insertFields := make([]string, 0)
fieldPlaceHolders := make([]string, 0)
sqlValues := make([]interface{}, 0)
for i := range tableFields {
if tableFields[i] == "-" {
continue
}
fieldVal := convertToNormalValue(val.Field(i))
if fieldVal == nil {
continue
}
insertFields = append(insertFields, tableFields[i])
fieldPlaceHolders = append(fieldPlaceHolders, "?")
sqlValues = append(sqlValues, fieldVal)
}
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);", tableName, strings.Join(insertFields, ","), strings.Join(fieldPlaceHolders, ","))
return ctx.sess.Query(sql, sqlValues...).Exec()
}
func (ctx *cqlOrm[T]) Select(obj T) ([]T, error) {
val := reflect.ValueOf(obj)
typ := val.Type()
tableName := strings.ToLower(typ.Name())
schema, ok := modelCache.Load(typ.String())
tableFields := schema.(tableSchema).fields
if !ok {
return []T{}, errors.New(fmt.Sprintf("Table %s not found", tableName))
}
selectFields := make([]string, 0)
whereClause := make([]string, 0)
whereValues := make([]interface{}, 0)
for i, fieldName := range tableFields {
if fieldName == "-" {
continue
}
selectFields = append(selectFields, fieldName)
if schema.(tableSchema).fieldMap[fieldName].isClusteringKey || schema.(tableSchema).fieldMap[fieldName].isPartitionKey {
fieldVal := convertToNormalValue(val.Field(i))
if fieldVal == nil {
continue
}
whereClause = append(whereClause, fmt.Sprintf("%s=?", fieldName))
whereValues = append(whereValues, fieldVal)
}
}
sql := fmt.Sprintf("SELECT %s FROM %s WHERE %s;", strings.Join(selectFields, ", "), tableName, strings.Join(whereClause, " AND "))
selectResult := make([]T, 0)
iter := ctx.sess.Query(sql, whereValues...).Iter()
defer func() {
err := iter.Close()
if err != nil {
fmt.Printf("Select %s failed: %s\n", tableName, err.Error())
}
}()
for {
var tableObj T
if !iter.Scan(getPointersOfStructElements(unsafe.Pointer(&tableObj), selectFields, schema.(tableSchema).fieldMap)...) {
break
}
selectResult = append(selectResult, tableObj)
}
return selectResult, nil
}
func (ctx *cqlOrm[T]) Update(obj T) error {
val := reflect.ValueOf(obj)
typ := val.Type()
tableName := strings.ToLower(typ.Name())
schema, ok := modelCache.Load(typ.String())
if !ok {
return errors.New(fmt.Sprintf("Table %s not found", tableName))
}
tableFields := schema.(tableSchema).fields
fields := make([]string, 0)
whereClause := make([]string, 0)
sqlValues := make([]interface{}, 0)
whereValues := make([]interface{}, 0)
for i, filedName := range tableFields {
if filedName == "-" {
continue
}
fieldVal := convertToNormalValue(val.Field(i))
if fieldVal == nil {
continue
}
if schema.(tableSchema).fieldMap[filedName].isPartitionKey || schema.(tableSchema).fieldMap[filedName].isClusteringKey {
whereClause = append(whereClause, fmt.Sprintf("%s=?", filedName))
whereValues = append(whereValues, fieldVal)
} else {
fields = append(fields, filedName+"=?")
sqlValues = append(sqlValues, fieldVal)
}
}
sql := fmt.Sprintf("UPDATE %s SET %s WHERE %s;", tableName, strings.Join(fields, ","), strings.Join(whereClause, " AND "))
//fmt.Println(sql)
//fmt.Println(sqlValues)
sqlParams := sqlValues
sqlParams = append(sqlParams, whereValues...)
return ctx.sess.Query(sql, sqlParams...).Exec()
}
func (ctx *cqlOrm[T]) Delete(obj T) error {
val := reflect.ValueOf(obj)
typ := val.Type()
tableName := strings.ToLower(typ.Name())
schema, ok := modelCache.Load(typ.String())
if !ok {
return errors.New(fmt.Sprintf("Table %s not found", tableName))
}
whereClause := make([]string, 0)
whereValues := make([]interface{}, 0)
for i, fieldName := range schema.(tableSchema).fields {
if fieldName == "-" {
continue
}
if schema.(tableSchema).fieldMap[fieldName].isPartitionKey || schema.(tableSchema).fieldMap[fieldName].isClusteringKey {
fieldVal := convertToNormalValue(val.Field(i))
if fieldVal == nil {
continue
}
whereClause = append(whereClause, fmt.Sprintf("%s=?", fieldName))
whereValues = append(whereValues, fieldVal)
}
}
sql := fmt.Sprintf("DELETE FROM %s WHERE %s;", tableName, strings.Join(whereClause, " AND "))
//fmt.Println(sql)
//fmt.Println(whereValues)
return ctx.sess.Query(sql, whereValues...).Exec()
}
// Mapping golang type to CS data type
func getFieldDBType(typeName string, isDate bool) (string, bool, error) {
isPointer := false
if typeName[0:1] == "*" {
typeName = typeName[1:]
isPointer = true
}
if typeName[0:2] == "[]" {
typeName = typeName[2:]
listDateType, _, _ := getFieldDBType(typeName, isDate)
return fmt.Sprintf("list<%s>", listDateType), isPointer, nil
}
switch typeName {
case "bool":
return "boolean", isPointer, nil
case "string":
return "text", isPointer, nil
case "int8":
return "tinyint", isPointer, nil
case "int16":
return "smallint", isPointer, nil
case "int32":
return "int", isPointer, nil
case "int":
return "bigint", isPointer, nil
case "int64":
return "bigint", isPointer, nil
case "float32":
return "float", isPointer, nil
case "float64":
return "double", isPointer, nil
case "time.Time":
if isDate {
return "date", isPointer, nil
}
return "timestamp", isPointer, nil
default:
return "", isPointer, errors.New("Invalid type: " + typeName)
}
}
func isValidCqlTag(tag reflect.StructTag) bool {
fieldName := tag.Get(jsonTAG)
tagStr := tag.Get(cqlTAG)
if fieldName == "-" && tagStr != "" {
log.Fatal("Json ignored field should not contain CQL tag: ", tagStr)
return false
}
if tagStr != "" {
keys := strings.Split(tagStr, ",")
allowKeys := []string{"pk", "ck", "static", "date"}
for _, key := range keys {
if !slices.Contains(allowKeys, key) {
log.Fatal(fmt.Sprintf("Invalid tag: %s, key word %s is not allowed", tagStr, key))
return false
}
}
isPk := isPartitionKey(tag)
isCk := isClusterKey(tag)
isStatic := isStaticFiled(tag)
if isPk && isCk {
log.Fatal(fmt.Sprintf("Invalid tag: %s, a field could not be both clustering key and partition key", tagStr))
return false
}
if (isPk || isCk) && isStatic {
log.Fatal(fmt.Sprintf("Invalid tag: %s, static field could not be part of primary key", tagStr))
return false
}
}
return true
}
func isPartitionKey(tag reflect.StructTag) bool {
return strings.Contains(tag.Get(cqlTAG), "pk")
}
func isClusterKey(tag reflect.StructTag) bool {
return strings.Contains(tag.Get(cqlTAG), "ck")
}
func isStaticFiled(tag reflect.StructTag) bool {
return strings.Contains(tag.Get(cqlTAG), "static")
}
func isDateFiled(tag reflect.StructTag) bool {
return strings.Contains(tag.Get(cqlTAG), "date")
}
// Get pointers of struct elements for data scanning usage.
func getPointersOfStructElements(basePoint unsafe.Pointer, selectFields []string, fieldsMap map[string]tableField) []interface{} {
fieldsPtr := make([]interface{}, 0)
for _, val := range selectFields {
field, _ := fieldsMap[val]
switch field.dataType {
case reflect.Bool:
appendPtr[bool](&fieldsPtr, basePoint, field)
case reflect.Int:
appendPtr[int](&fieldsPtr, basePoint, field)
case reflect.Int8:
appendPtr[int8](&fieldsPtr, basePoint, field)
case reflect.Int16:
appendPtr[int16](&fieldsPtr, basePoint, field)
case reflect.Int32:
appendPtr[int32](&fieldsPtr, basePoint, field)
case reflect.Int64:
appendPtr[int64](&fieldsPtr, basePoint, field)
case reflect.Uint:
appendPtr[uint](&fieldsPtr, basePoint, field)
case reflect.Uint8:
appendPtr[uint8](&fieldsPtr, basePoint, field)
case reflect.Uint16:
appendPtr[uint16](&fieldsPtr, basePoint, field)
case reflect.Uint32:
appendPtr[uint32](&fieldsPtr, basePoint, field)
case reflect.Uint64:
appendPtr[uint64](&fieldsPtr, basePoint, field)
case reflect.Uintptr:
appendPtr[uintptr](&fieldsPtr, basePoint, field)
case reflect.Float32:
appendPtr[float32](&fieldsPtr, basePoint, field)
case reflect.Float64:
appendPtr[float64](&fieldsPtr, basePoint, field)
case reflect.Complex64:
appendPtr[complex64](&fieldsPtr, basePoint, field)
case reflect.Complex128:
appendPtr[complex128](&fieldsPtr, basePoint, field)
case reflect.String:
appendPtr[string](&fieldsPtr, basePoint, field)
case reflect.Slice:
appendPtr[string](&fieldsPtr, basePoint, field)
case reflect.Struct:
appendPtr[time.Time](&fieldsPtr, basePoint, field) // Only allow time.Time struct
default:
fieldsPtr = append(fieldsPtr, nil)
}
}
return fieldsPtr
}
func appendPtr[T any](fieldsPtr *[]interface{}, basePoint unsafe.Pointer, field tableField) {
if field.isList {
if !field.isPointer {
*fieldsPtr = append(*fieldsPtr, (*[]T)(unsafe.Add(basePoint, field.offSet)))
} else {
*fieldsPtr = append(*fieldsPtr, (**[]T)(unsafe.Add(basePoint, field.offSet)))
}
return
}
if field.isPointer {
*fieldsPtr = append(*fieldsPtr, (**T)(unsafe.Add(basePoint, field.offSet)))
return
}
*fieldsPtr = append(*fieldsPtr, (*T)(unsafe.Add(basePoint, field.offSet)))
}