-
Notifications
You must be signed in to change notification settings - Fork 75
/
sharding.go
337 lines (300 loc) · 8.7 KB
/
sharding.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
package sharding
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"github.com/longbridgeapp/sqlparser"
"gorm.io/gorm"
)
var (
ErrMissingShardingKey = errors.New("sharding key or id required, and use operator =")
ErrInvalidID = errors.New("invalid id format")
)
type Sharding struct {
*gorm.DB
ConnPool *ConnPool
Resolvers map[string]Resolver
querys sync.Map
}
// Resolver composed by five configurable fields.
type Resolver struct {
// EnableFullTable represents whether to enable full table.
// When enabled, data will double write to both main table and sharding table.
EnableFullTable bool
// ShardingColumn specifies the table column you want to used for sharding the table rows.
// For example, for a product order table, you may want to split the rows by `user_id`.
ShardingColumn string
// ShardingAlgorithm specifies a function to generate the sharding
// table's suffix by the column value.
// For example, this function implements a mod sharding algorithm.
//
// func(value interface{}) (suffix string, err error) {
// if uid, ok := value.(int64);ok {
// return fmt.Sprintf("_%02d", user_id % 64), nil
// }
// return "", errors.New("invalid user_id")
// }
ShardingAlgorithm func(columnValue interface{}) (suffix string, err error)
// ShardingAlgorithmByPrimaryKey specifies a function to generate the sharding
// table's suffix by the primary key. Used when no sharding key specified.
// For example, this function use the Keygen library to generate the suffix.
//
// func(id int64) (suffix string) {
// return fmt.Sprintf("_%02d", keygen.TableIdx(id))
// }
ShardingAlgorithmByPrimaryKey func(id int64) (suffix string)
// PrimaryKeyGenerate specifies a function to generate the primary key.
// Used only when insert and the record does not contains an id field.
// We recommend you use the
// [keygen](https://github.com/longbridgeapp/gorm-sharding/tree/main/keygen) component,
// it is a distributed primary key generator.
// When use auto-increment like generator, the tableIdx argument could ignored.
//
// For example, this function use the Keygen library to generate the primary key.
//
// func(tableIdx int64) int64 {
// return keygen.Next(tableIdx)
// }
PrimaryKeyGenerate func(tableIdx int64) int64
}
// Register takes a map, key is the original table name
// and value is a Resolver.
func Register(resolvers map[string]Resolver) Sharding {
return Sharding{Resolvers: resolvers}
}
// Name plugin name for Gorm plugin interface
func (s *Sharding) Name() string {
return "gorm:sharding"
}
// LastQuery get last SQL query
func (s *Sharding) LastQuery() string {
if query, ok := s.querys.Load("last_query"); ok {
return query.(string)
}
return ""
}
// Initialize implement for Gorm plugin interface
func (s *Sharding) Initialize(db *gorm.DB) error {
s.DB = db
s.registerConnPool(db)
return nil
}
// resolve split the old query to full table query and sharding table query
func (s *Sharding) resolve(query string, args ...interface{}) (ftQuery, stQuery, tableName string, err error) {
ftQuery = query
stQuery = query
if len(s.Resolvers) == 0 {
return
}
expr, err := sqlparser.NewParser(strings.NewReader(query)).ParseStatement()
if err != nil {
return ftQuery, stQuery, tableName, nil
}
var table *sqlparser.TableName
var condition sqlparser.Expr
var isInsert bool
var insertNames []*sqlparser.Ident
var insertValues []sqlparser.Expr
switch stmt := expr.(type) {
case *sqlparser.SelectStatement:
tbl, ok := stmt.FromItems.(*sqlparser.TableName)
if !ok {
return
}
if stmt.Hint != nil && stmt.Hint.Value == "nosharding" {
return
}
table = tbl
condition = stmt.Condition
case *sqlparser.InsertStatement:
table = stmt.TableName
isInsert = true
insertNames = stmt.ColumnNames
insertValues = stmt.Expressions[0].Exprs
case *sqlparser.UpdateStatement:
condition = stmt.Condition
table = stmt.TableName
case *sqlparser.DeleteStatement:
condition = stmt.Condition
table = stmt.TableName
default:
return ftQuery, stQuery, "", sqlparser.ErrNotImplemented
}
tableName = table.Name.Name
r, ok := s.Resolvers[tableName]
if !ok {
return
}
var value interface{}
var id int64
var keyFind bool
if isInsert {
value, id, keyFind, err = s.insertValue(r.ShardingColumn, insertNames, insertValues, args...)
if err != nil {
return
}
} else {
value, id, keyFind, err = s.nonInsertValue(r.ShardingColumn, condition, args...)
if err != nil {
return
}
}
var suffix string
if keyFind {
suffix, err = r.ShardingAlgorithm(value)
if err != nil {
return
}
} else {
if r.ShardingAlgorithmByPrimaryKey == nil {
err = fmt.Errorf("there is not sharding key and ShardingAlgorithmByPrimaryKey is not configured")
return
}
suffix = r.ShardingAlgorithmByPrimaryKey(id)
}
newTable := &sqlparser.TableName{Name: &sqlparser.Ident{Name: tableName + suffix}}
fillID := true
if isInsert {
for _, name := range insertNames {
if name.Name == "id" {
fillID = false
break
}
}
if fillID {
tblIdx, err := strconv.Atoi(strings.Replace(suffix, "_", "", 1))
if err != nil {
return ftQuery, stQuery, tableName, err
}
id := r.PrimaryKeyGenerate(int64(tblIdx))
insertNames = append(insertNames, &sqlparser.Ident{Name: "id"})
insertValues = append(insertValues, &sqlparser.NumberLit{Value: strconv.FormatInt(id, 10)})
}
}
switch stmt := expr.(type) {
case *sqlparser.InsertStatement:
if fillID {
stmt.ColumnNames = insertNames
stmt.Expressions[0].Exprs = insertValues
}
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
case *sqlparser.SelectStatement:
ftQuery = stmt.String()
stmt.FromItems = newTable
stmt.OrderBy = replaceOrderByTableName(stmt.OrderBy, tableName, newTable.Name.Name)
stQuery = stmt.String()
case *sqlparser.UpdateStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
case *sqlparser.DeleteStatement:
ftQuery = stmt.String()
stmt.TableName = newTable
stQuery = stmt.String()
}
return
}
func (s *Sharding) insertValue(key string, names []*sqlparser.Ident, exprs []sqlparser.Expr, args ...interface{}) (value interface{}, id int64, keyFind bool, err error) {
if len(names) != len(exprs) {
return nil, 0, keyFind, errors.New("column names and expressions mismatch")
}
for i, name := range names {
if name.Name == key {
switch expr := exprs[i].(type) {
case *sqlparser.BindExpr:
value, err = getBindValue(expr.Name, args)
if err != nil {
return nil, 0, keyFind, err
}
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return nil, 0, keyFind, sqlparser.ErrNotImplemented
}
keyFind = true
break
}
}
if !keyFind {
return nil, 0, keyFind, ErrMissingShardingKey
}
return
}
func (s *Sharding) nonInsertValue(key string, condition sqlparser.Expr, args ...interface{}) (value interface{}, id int64, keyFind bool, err error) {
err = sqlparser.Walk(sqlparser.VisitFunc(func(node sqlparser.Node) error {
if n, ok := node.(*sqlparser.BinaryExpr); ok {
if x, ok := n.X.(*sqlparser.Ident); ok {
if x.Name == key && n.Op == sqlparser.EQ {
keyFind = true
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
value, err = getBindValue(expr.Name, args)
if err != nil {
return err
}
case *sqlparser.StringLit:
value = expr.Value
case *sqlparser.NumberLit:
value = expr.Value
default:
return sqlparser.ErrNotImplemented
}
return nil
} else if x.Name == "id" && n.Op == sqlparser.EQ {
switch expr := n.Y.(type) {
case *sqlparser.BindExpr:
v, err := getBindValue(expr.Name, args)
if err != nil {
return err
}
var ok bool
if id, ok = v.(int64); !ok {
return fmt.Errorf("ID should be int64 type")
}
case *sqlparser.NumberLit:
id, err = strconv.ParseInt(expr.Value, 10, 64)
if err != nil {
return err
}
default:
return ErrInvalidID
}
return nil
}
}
}
return nil
}), condition)
if err != nil {
return
}
if !keyFind && id == 0 {
return nil, 0, keyFind, ErrMissingShardingKey
}
return
}
func replaceOrderByTableName(orderBy []*sqlparser.OrderingTerm, oldName, newName string) []*sqlparser.OrderingTerm {
for i, term := range orderBy {
if x, ok := term.X.(*sqlparser.QualifiedRef); ok {
if x.Table.Name == oldName {
x.Table.Name = newName
orderBy[i].X = x
}
}
}
return orderBy
}
func getBindValue(value interface{}, args []interface{}) (interface{}, error) {
bindPos := strings.Replace(value.(string), "$", "", 1)
pos, err := strconv.Atoi(bindPos)
if err != nil {
return nil, err
}
return args[pos-1], nil
}