-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicate.go
233 lines (225 loc) · 7.33 KB
/
predicate.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
package rds
import (
"fmt"
"strings"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/schema/query"
)
// normalizePredicate turns implicit AND on list of params of rest-layer query into an explicit AND-predicate
func normalizePredicate(predicate query.Predicate) query.Predicate {
if len(predicate) > 1 {
return query.Predicate{&query.And{predicate}}
}
return predicate
}
// translatePredicate interprets rest-layer query to a Lua query script to be fed to Redis.
// This results in a Lua query that ultimately creates a Redis sorted-set with the IDs of the items corresponding
// to the initial query. Also you get a key in which this set is stored and a list a temporary keys
// you should delete later
// Return: lastKeyWhereResultCanBeFound, luaQuery, allCreatedKeys, error
func translatePredicate(entityName string, predicate query.Predicate) (string, string, []string, error) {
var tempKeys []string
newKey := func() string {
k := tmpVar()
tempKeys = append(tempKeys, k)
return k
}
// If no predicate given (we need all existing items to be retrieved) - use the set of all IDs as a source
if len(predicate) == 0 {
return sKeyIDsAll(entityName), "", tempKeys, nil
}
for _, exp := range predicate {
switch t := exp.(type) {
case *query.And:
var subs, keys []string
var key string
for _, subExp := range *t {
k, res, _, err := translatePredicate(entityName, query.Predicate{subExp})
if err != nil {
return "", "", nil, err
}
keys = append(keys, k)
subs = append(subs, res)
}
if len(keys) > 1 {
key = newKey()
andClause := fmt.Sprintf(
"redis.call('ZINTERSTORE', '%[1]s', unpack(%[2]s))",
key, makeLuaTableFromStrings(keys))
subs = append(subs, andClause)
} else {
// Nothing to intersect here - we have only one Set(ZSet)
key = keys[len(keys)-1]
}
return key, strings.Join(subs, "\n"), tempKeys, nil
case *query.Or:
var subs, keys []string
var key string
for _, subExp := range *t {
k, res, _, err := translatePredicate(entityName, query.Predicate{subExp})
if err != nil {
return "", "", nil, err
}
keys = append(keys, k)
subs = append(subs, res)
}
if len(keys) > 1 {
key = newKey()
orClause := fmt.Sprintf(
"redis.call('ZUNIONSTORE', '%[1]s', unpack(%[2]s))",
key, makeLuaTableFromStrings(keys))
subs = append(subs, orClause)
} else {
// Nothing to union here - we have only one Set(ZSet)
key = keys[len(keys)-1]
}
return key, strings.Join(subs, "\n"), tempKeys, nil
case *query.In:
key1 := newKey()
key2 := newKey()
key3 := newKey()
var1 := tmpVar()
var2 := tmpVar()
if isNumeric(t.Values...) {
result := fmt.Sprintf(`
local %[1]s = %[2]s
for x = %[1]s do
local ys = redis.call('ZRANGEBYSCORE', '%[3]s', x, x)
if next(ys) ~= nil then
redis.call('SADD', '%[4]s', unpack(ys))
end
end
`, var1, makeLuaTableFromValues(t.Values), zKey(entityName, t.Field), key1)
return key1, result, tempKeys, nil
}
var inKeys []string
for _, v := range t.Values {
inKeys = append(inKeys, sKey(entityName, t.Field, v))
}
// todo: we don't need local local %[1]s = %[2]s - just inline!
result := fmt.Sprintf(`
local %[1]s = %[2]s
if next(%[1]s) ~= nil then
redis.call('SADD', '%[3]s', unpack(%[1]s))
end
local %[4]s = redis.call('KEYS', '%[5]s')
if next(%[4]s) ~= nil then
redis.call('SADD', '%[6]s', unpack(%[4]s))
end
redis.call('SINTERSTORE', '%[7]s', '%[3]s', '%[6]s')
`, var1, makeLuaTableFromStrings(inKeys), key1, var2, sKeyLastAll(entityName, t.Field), key2, key3)
return key3, result, tempKeys, nil
case *query.NotIn:
key1 := newKey()
key2 := newKey()
key3 := newKey()
if isNumeric(t.Values) {
pairs, err := getRangeNumericPairs(t.Values)
if err != nil {
return "", "", nil, err
}
result := fmt.Sprintf(`
for x = %[1]s do
local ys = redis.call('ZRANGEBYSCORE', '%[2]s', '(' .. x[1], '(' .. x[2])
if next(ys) ~= nil then
redis.call('SADD', '%[3]s', unpack(ys))
end
end
`, makeLuaTableFromStrings(pairs), zKey(entityName, t.Field), key1)
return key1, result, tempKeys, nil
}
var inKeys []string
var1 := tmpVar()
var2 := tmpVar()
for _, v := range t.Values {
inKeys = append(inKeys, sKey(entityName, t.Field, v))
}
// todo: we don't need local local %[1]s = %[2]s - just inline!
result := fmt.Sprintf(`
local %[1]s = %[2]s
if next(%[1]s) ~= nil then
redis.call('SADD', '%[3]s', unpack(%[1]s))
end
local %[4]s = redis.call('KEYS', '%[5]s')
if next(%[1]s) ~= nil then
redis.call('SADD', '%[6]s', unpack(%[4]s))
end
redis.call('SDIFFSTORE', '%[7]s', '%[6]s', '%[3]s')
`, var1, makeLuaTableFromStrings(inKeys), key1, var2, sKeyLastAll(entityName, t.Field), key2, key3)
return key3, result, tempKeys, nil
case *query.Equal:
var result string
key := newKey()
if isNumeric(t.Value) {
result = fmt.Sprintf(`
local %[5]s = redis.call('ZRANGEBYSCORE', '%[2]s', %[3]v, %[4]v)
if next(%[5]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[5]s))
end
`, key, zKey(entityName, t.Field), t.Value, t.Value, tmpVar())
} else {
result = fmt.Sprintf(`
local %[3]s = redis.call('SMEMBERS', '%[2]s')
if next(%[3]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[3]s))
end
`, key, sKey(entityName, t.Field, t.Value), tmpVar())
}
return key, result, tempKeys, nil
case *query.NotEqual:
var result string
key := newKey()
if isNumeric(t.Value) {
result = fmt.Sprintf(`
redis.call('ZUNIONSTORE', '%s', 1, '%s')
redis.call('ZREMRANGEBYSCORE', '%s', %v, %v)
`, key, zKey(entityName, t.Field), key, t.Value, t.Value)
} else {
result = fmt.Sprintf(`
redis.call('SDIFFSTORE', '%s', '%s', '%s')
`, key, sKeyIDsAll(entityName), sKey(entityName, t.Field, t.Value))
}
return key, result, tempKeys, nil
case *query.GreaterThan:
key := newKey()
result := fmt.Sprintf(`
local %[4]s = redis.call('ZRANGEBYSCORE', '%[2]s', '(%[3]f', '+inf')
if next(%[4]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[4]s))
end
`, key, zKey(entityName, t.Field), t.Value, tmpVar())
return key, result, tempKeys, nil
case *query.GreaterOrEqual:
key := newKey()
result := fmt.Sprintf(`
local %[4]s = redis.call('ZRANGEBYSCORE', '%[2]s', %[3]f, '+inf')
if next(%[4]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[4]s))
end
`, key, zKey(entityName, t.Field), t.Value, tmpVar())
return key, result, tempKeys, nil
case *query.LowerThan:
key := newKey()
result := fmt.Sprintf(`
local %[4]s = redis.call('ZRANGEBYSCORE', '%[2]s', '-inf', '(%[3]f')
if next(%[4]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[4]s))
end
`, key, zKey(entityName, t.Field), t.Value, tmpVar())
return key, result, tempKeys, nil
case *query.LowerOrEqual:
key := newKey()
tempKeys = append(tempKeys, key)
result := fmt.Sprintf(`
local %[4]s = redis.call('ZRANGEBYSCORE', '%[2]s', '-inf', %[3]f)
if next(%[4]s) ~= nil then
redis.call('SADD', '%[1]s', unpack(%[4]s))
end
`, key, zKey(entityName, t.Field), t.Value, tmpVar())
return key, result, tempKeys, nil
default:
return "", "", nil, resource.ErrNotImplemented
}
}
return "", "", tempKeys, nil
}