/
locks.go
354 lines (278 loc) · 9.15 KB
/
locks.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
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package function
import (
"fmt"
"strings"
"time"
"gopkg.in/src-d/go-errors.v1"
"github.com/Ciyfly/FakerMysql/sql"
"github.com/Ciyfly/FakerMysql/sql/expression"
)
// ErrIllegalLockNameArgType is a kind of error that is thrown when the parameter passed as a lock name is not a string.
var ErrIllegalLockNameArgType = errors.NewKind("Illegal parameter data type %s for operation '%s'")
// lockFuncLogic is the logic executed when one of the single argument named lock functions is executed
type lockFuncLogic func(ctx *sql.Context, ls *sql.LockSubsystem, lockName string) (interface{}, error)
func (nl *NamedLockFunction) evalLockLogic(ctx *sql.Context, fn lockFuncLogic, row sql.Row) (interface{}, error) {
lock, err := nl.GetLockName(ctx, row)
if err != nil {
return nil, err
}
if lock == nil {
return nil, nil
}
return fn(ctx, nl.ls, *lock)
}
// NamedLockFunction is a sql function that takes just the name of a lock as an argument
type NamedLockFunction struct {
expression.UnaryExpression
ls *sql.LockSubsystem
funcName string
retType sql.Type
}
// FunctionName implements sql.FunctionExpression
func (nl *NamedLockFunction) FunctionName() string {
return nl.funcName
}
// Eval implements the Expression interface.
func (nl *NamedLockFunction) GetLockName(ctx *sql.Context, row sql.Row) (*string, error) {
if nl.Child == nil {
return nil, nil
}
val, err := nl.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
lockName, ok := val.(string)
if !ok {
return nil, ErrIllegalLockNameArgType.New(nl.Child.Type().String(), nl.funcName)
}
return &lockName, nil
}
// String implements the fmt.Stringer interface.
func (nl *NamedLockFunction) String() string {
return fmt.Sprintf("%s(%s)", strings.ToUpper(nl.funcName), nl.Child.String())
}
// IsNullable implements the Expression interface.
func (nl *NamedLockFunction) IsNullable() bool {
return nl.Child.IsNullable()
}
// Type implements the Expression interface.
func (nl *NamedLockFunction) Type() sql.Type {
return nl.retType
}
// ReleaseLockFunc is the function logic that is executed when the release_lock function is called.
func ReleaseLockFunc(ctx *sql.Context, ls *sql.LockSubsystem, lockName string) (interface{}, error) {
err := ls.Unlock(ctx, lockName)
if err != nil {
if sql.ErrLockDoesNotExist.Is(err) {
return nil, nil
} else if sql.ErrLockNotOwned.Is(err) {
return int8(0), nil
}
return nil, err
}
return int8(1), nil
}
type IsFreeLock struct {
NamedLockFunction
}
var _ sql.FunctionExpression = &IsFreeLock{}
func NewIsFreeLock(ls *sql.LockSubsystem) sql.CreateFunc1Args {
return func(e sql.Expression) sql.Expression {
return &IsFreeLock{
NamedLockFunction: NamedLockFunction{
UnaryExpression: expression.UnaryExpression{e},
ls: ls,
funcName: "is_free_lock",
retType: sql.Int8,
},
}
}
}
func (i *IsFreeLock) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return i.evalLockLogic(ctx, IsFreeLockFunc, row)
}
func (i *IsFreeLock) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewIsFreeLock(i.ls)(children[0]), nil
}
type IsUsedLock struct {
NamedLockFunction
}
var _ sql.FunctionExpression = &IsUsedLock{}
func NewIsUsedLock(ls *sql.LockSubsystem) sql.CreateFunc1Args {
return func(e sql.Expression) sql.Expression {
return &IsUsedLock{
NamedLockFunction: NamedLockFunction{
UnaryExpression: expression.UnaryExpression{e},
ls: ls,
funcName: "is_used_lock",
retType: sql.Uint32,
},
}
}
}
func (i *IsUsedLock) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return i.evalLockLogic(ctx, IsUsedLockFunc, row)
}
func (i *IsUsedLock) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewIsUsedLock(i.ls)(children[0]), nil
}
type ReleaseLock struct {
NamedLockFunction
}
var _ sql.FunctionExpression = &ReleaseLock{}
func NewReleaseLock(ls *sql.LockSubsystem) sql.CreateFunc1Args {
return func(e sql.Expression) sql.Expression {
return &ReleaseLock{
NamedLockFunction: NamedLockFunction{
UnaryExpression: expression.UnaryExpression{e},
ls: ls,
funcName: "release_lock",
retType: sql.Int8,
},
}
}
}
func (i *ReleaseLock) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return i.evalLockLogic(ctx, ReleaseLockFunc, row)
}
func (i *ReleaseLock) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewReleaseLock(i.ls)(children[0]), nil
}
// IsFreeLockFunc is the function logic that is executed when the is_free_lock function is called.
func IsFreeLockFunc(_ *sql.Context, ls *sql.LockSubsystem, lockName string) (interface{}, error) {
state, _ := ls.GetLockState(lockName)
switch state {
case sql.LockInUse:
return int8(0), nil
default: // return 1 if the lock is free. If the lock doesn't exist yet it is free
return int8(1), nil
}
}
// IsUsedLockFunc is the function logic that is executed when the is_used_lock function is called.
func IsUsedLockFunc(ctx *sql.Context, ls *sql.LockSubsystem, lockName string) (interface{}, error) {
state, owner := ls.GetLockState(lockName)
switch state {
case sql.LockInUse:
return owner, nil
default:
return nil, nil
}
}
// GetLock is a SQL function implementing get_lock
type GetLock struct {
expression.BinaryExpression
ls *sql.LockSubsystem
}
var _ sql.FunctionExpression = (*GetLock)(nil)
// CreateNewGetLock returns a new GetLock object
func CreateNewGetLock(ls *sql.LockSubsystem) func(e1, e2 sql.Expression) sql.Expression {
return func(e1, e2 sql.Expression) sql.Expression {
return &GetLock{expression.BinaryExpression{e1, e2}, ls}
}
}
// FunctionName implements sql.FunctionExpression
func (gl *GetLock) FunctionName() string {
return "get_lock"
}
// Eval implements the Expression interface.
func (gl *GetLock) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if gl.Left == nil {
return nil, nil
}
leftVal, err := gl.Left.Eval(ctx, row)
if err != nil {
return nil, err
}
if leftVal == nil {
return nil, nil
}
if gl.Right == nil {
return nil, nil
}
rightVal, err := gl.Right.Eval(ctx, row)
if err != nil {
return nil, err
}
if rightVal == nil {
return nil, nil
}
lockName, ok := leftVal.(string)
if !ok {
return nil, ErrIllegalLockNameArgType.New(gl.Left.Type().String(), "get_lock")
}
timeout, err := sql.Int64.Convert(rightVal)
if err != nil {
return nil, fmt.Errorf("illegal value for timeout %v", timeout)
}
err = gl.ls.Lock(ctx, lockName, time.Second*time.Duration(timeout.(int64)))
if err != nil {
if sql.ErrLockTimeout.Is(err) {
return int8(0), nil
}
return nil, err
}
return int8(1), nil
}
// String implements the fmt.Stringer interface.
func (gl *GetLock) String() string {
return fmt.Sprintf("get_lock(%s, %s)", gl.Left.String(), gl.Right.String())
}
// IsNullable implements the Expression interface.
func (gl *GetLock) IsNullable() bool {
return false
}
// WithChildren implements the Expression interface.
func (gl *GetLock) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(gl, len(children), 1)
}
return &GetLock{expression.BinaryExpression{Left: children[0], Right: children[1]}, gl.ls}, nil
}
// Type implements the Expression interface.
func (gl *GetLock) Type() sql.Type {
return sql.Int8
}
type ReleaseAllLocks struct {
NoArgFunc
ls *sql.LockSubsystem
}
var _ sql.FunctionExpression = ReleaseAllLocks{}
func NewReleaseAllLocks(ls *sql.LockSubsystem) func() sql.Expression {
return func() sql.Expression {
return ReleaseAllLocks{
NoArgFunc: NoArgFunc{"release_all_locks", sql.Int32},
ls: ls,
}
}
}
func (r ReleaseAllLocks) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return r.ls.ReleaseAll(ctx)
}
func (r ReleaseAllLocks) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NoArgFuncWithChildren(r, children)
}