-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensure.go
96 lines (92 loc) · 2.51 KB
/
ensure.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
package simplets
import (
"fmt"
"reflect"
. "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)
func EnsureTable(client *TableStoreClient, r interface{}, opts ...EnsureTableOption) {
var opt EnsureTableOption
if len(opts) == 1 {
opt = opts[0]
}
table, pkFields, _ := getTableInfoFromStruct(r)
resp, err := client.DescribeTable(&DescribeTableRequest{TableName: table})
if err != nil {
tableNotExistErr := IsObjectNotExist(err)
if !tableNotExistErr {
panic(err)
}
if tableNotExistErr && opt.PanicIfTableNotExist {
panic("table is not exist")
}
// create the table on demanded
meta := &TableMeta{
TableName: table,
}
for _, field := range pkFields {
var pkType PrimaryKeyType
switch field.kind {
case reflect.Int64:
pkType = PrimaryKeyType_INTEGER
case reflect.String:
pkType = PrimaryKeyType_STRING
case reflect.Slice:
if reflect.ValueOf(field.value).Type() == typeOfBytes {
pkType = PrimaryKeyType_BINARY
} else {
panic("primary key type can only be string, int64, binary")
}
default:
panic("primary key type can only be string, int64, binary")
}
if field.isAutoIncPk {
meta.AddPrimaryKeyColumnOption(field.fieldName, pkType, AUTO_INCREMENT)
} else {
meta.AddPrimaryKeyColumn(field.fieldName, pkType)
}
}
// use the most simple option if user not provide
option := &TableOption{
TimeToAlive: -1,
MaxVersion: 1,
}
if opt.TableOption != nil {
option = opt.TableOption
}
throughput := new(ReservedThroughput)
if opt.ReservedThroughput != nil {
throughput = opt.ReservedThroughput
}
req := &CreateTableRequest{
TableMeta: meta,
TableOption: option,
ReservedThroughput: throughput,
StreamSpec: opt.StreamSpec,
IndexMetas: opt.IndexMetas,
}
_, err = client.CreateTable(req)
if err != nil {
panic(err)
}
return
}
mustMatchPrimaryKeys(pkFields, resp.TableMeta.SchemaEntry)
//todo: check DefinedColumns
}
func mustMatchPrimaryKeys(fields []*fieldInfo, pkSchemas []*PrimaryKeySchema) {
if len(fields) != len(pkSchemas) {
panic("primary key count is not match")
}
for i, schema := range pkSchemas {
f := fields[i]
if *schema.Name != f.fieldName {
panic(fmt.Sprintf("primary key name is not match, %s != %s", *schema.Name, f.fieldName))
}
if schema.Option == nil {
continue
}
if *schema.Option == AUTO_INCREMENT && !f.isAutoIncPk {
panic(fmt.Sprintf("primary key is AUTO_INCREMENT, %s, but tag is not define by ts_pk_+", f.fieldName))
}
}
}