-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
159 lines (123 loc) · 3.8 KB
/
compile.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
package redisLuaScriptUtils
import (
"context"
"fmt"
"sync"
"github.com/go-redis/redis/v8"
)
type RedisScriptArguments map[string]interface{}
type CompiledRedisScript struct {
script RedisScript
scriptText string
keys []*RedisKey
args []string
redisScript *redis.Script
mx sync.RWMutex
}
func getUniqueKeys(keys []*RedisKey) []*RedisKey {
uniqueKeys := make(map[string]bool, 0)
uniqueKeysSlice := []*RedisKey{}
for _, key := range keys {
if !uniqueKeys[key.Key()] {
uniqueKeysSlice = append(uniqueKeysSlice, key)
uniqueKeys[key.Key()] = true
} else {
panic("Duplicate key: " + key.Key())
}
}
return uniqueKeysSlice
}
func getUniqueUsedKeys(scripts []*RedisScript, keys []*RedisKey) []*RedisKey {
usedKeys := make(map[string]bool, 0)
for _, script := range scripts {
for _, key := range script.keys {
usedKeys[key] = true
}
}
uniqueKeys := make(map[string]bool, 0)
keysSlice := []*RedisKey{}
for _, key := range keys {
if usedKeys[key.Key()] {
if !uniqueKeys[key.Key()] {
keysSlice = append(keysSlice, key)
uniqueKeys[key.Key()] = true
}
}
}
return keysSlice
}
func CompileRedisScripts(scripts []*RedisScript, keys []*RedisKey) (*CompiledRedisScript, error) {
suppliedKeys := make(map[string]*RedisKey)
for _, key := range keys {
suppliedKeys[key.Key()] = key
}
for _, key := range keys {
if suppliedKeys[key.Key()] == nil {
return nil, fmt.Errorf("Missing required LUA script key: %v", key)
}
}
uniqueKeys := getUniqueUsedKeys(scripts, keys)
uniqueArgs := getScriptsUniqueArgNames(scripts)
script := joinRedisScripts(scripts, uniqueKeys, uniqueArgs)
result := &CompiledRedisScript{
script: *script,
scriptText: script.scriptText,
keys: uniqueKeys,
args: uniqueArgs,
}
return result, nil
}
func (this *CompiledRedisScript) String() string {
return this.scriptText
}
func (this *CompiledRedisScript) Keys(args *RedisScriptArguments) []string {
var result []string = []string{}
for _, key := range this.keys {
result = append(result, key.Value(args))
}
return result
}
func (this *CompiledRedisScript) Args(args *RedisScriptArguments) ([]interface{}, error) {
var result []interface{} = []interface{}{}
for _, arg := range this.script.args {
value, ok := (*args)[arg]
if !ok {
return nil, fmt.Errorf("Missing required Redis LUA script argument: %v", arg)
}
result = append(result, value)
}
return result, nil
}
func (this *CompiledRedisScript) Run(ctx context.Context, client *redis.Client, args *RedisScriptArguments) *redis.Cmd {
if this.redisScript == nil {
this.mx.Lock()
this.redisScript = redis.NewScript(this.scriptText)
this.mx.Unlock()
}
if orderedArgsValues, err := this.Args(args); err == nil {
result := this.redisScript.Run(ctx, client, this.Keys(args), orderedArgsValues)
if result.Err() != nil && ctx.Err() == nil {
panic(fmt.Sprintf("Script run error: %v\nKeys: %v\nArgs: %v\nScript: %v\n\n", result.Err(), this.Keys(args), orderedArgsValues, this.scriptText))
}
return result
} else {
panic(err)
}
}
func (this *CompiledRedisScript) RunDebug(ctx context.Context, client *redis.Client, args *RedisScriptArguments) *redis.Cmd {
if this.redisScript == nil {
this.mx.Lock()
this.redisScript = redis.NewScript(this.scriptText)
this.mx.Unlock()
}
if orderedArgsValues, err := this.Args(args); err == nil {
fmt.Printf("RunDebug:\n\tKeys: %v\n\tArgs: %v\nScript: %v\n\n\n", this.Keys(args), orderedArgsValues, this.scriptText)
result := this.redisScript.Run(ctx, client, this.Keys(args), orderedArgsValues)
if result.Err() != nil && ctx.Err() == nil {
panic(fmt.Sprintf("Script run error: %v\nKeys: %v\nArgs: %v\nScript: %v\n\n", result.Err(), this.Keys(args), orderedArgsValues, this.scriptText))
}
return result
} else {
panic(err)
}
}