-
Notifications
You must be signed in to change notification settings - Fork 481
/
queries.go
328 lines (278 loc) · 8.5 KB
/
queries.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
/*******************************************************************************
* Copyright 2018 Redis Labs 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 redis
import (
"encoding/json"
"strconv"
"github.com/gomodule/redigo/redis"
"github.com/google/uuid"
"github.com/edgexfoundry/edgex-go/internal/pkg/db"
"github.com/edgexfoundry/edgex-go/internal/pkg/db/redis/models"
)
func getObjectById(conn redis.Conn, id string, unmarshal unmarshalFunc, out interface{}) error {
object, err := redis.Bytes(conn.Do("GET", id))
if err == redis.ErrNil {
return db.ErrNotFound
} else if err != nil {
return err
}
return unmarshal(object, out)
}
// TODO: Discuss this with Andre as a possibly replacement for getObjectByHash
// 1.) key/value seems clearer to me than hash/field for equivalent concepts. However the latter
// may be more consistently used in the Redis community. If so, revert.
// 2.) Not sure the custom "unmarshal" function is necessary when no domain logic is encapsulated
// within the Redis-based models. If the signatures of the Redis models are the same as contract
// then just use contract. However we have the capability to specialize the Redis models as
// needed now should a future requirement arise.
func getObjectByKey(conn redis.Conn, key string, value string, out interface{}) error {
id, err := redis.String(conn.Do("HGET", key, value))
if err == redis.ErrNil {
return db.ErrNotFound
} else if err != nil {
return err
}
object, err := redis.Bytes(conn.Do("GET", id))
if err != nil {
return err
}
return json.Unmarshal(object, out)
}
func getObjectByHash(conn redis.Conn, hash string, field string, unmarshal unmarshalFunc, out interface{}) error {
id, err := redis.String(conn.Do("HGET", hash, field))
if err == redis.ErrNil {
return db.ErrNotFound
} else if err != nil {
return err
}
object, err := redis.Bytes(conn.Do("GET", id))
if err != nil {
return err
}
return unmarshal(object, out)
}
func getObjectsByValue(conn redis.Conn, v string) ([][]byte, error) {
ids, err := redis.Values(conn.Do("SMEMBERS", v))
if err != nil {
return nil, err
}
if len(ids) == 0 {
return nil, nil
}
objects, err := redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
return objects, nil
}
func getObjectsByValues(conn redis.Conn, vals ...string) ([][]byte, error) {
args := redis.Args{}
for _, v := range vals {
args = args.Add(v)
}
ids, err := redis.Values(conn.Do("SINTER", args...))
if err != nil {
return nil, err
}
if len(ids) == 0 {
return nil, nil
}
objects, err := redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
return objects, nil
}
// getObjectsByRange retrieves the entries for keys enumerated in a sorted set.
// The entries are retrieved in the sorted set order.
func getObjectsByRange(conn redis.Conn, key string, start, end int) ([][]byte, error) {
return getObjectsBySomeRange(conn, "ZRANGE", key, start, end)
}
// getObjectsByRevRange retrieves the entries for keys enumerated in a sorted set.
// The entries are retrieved in the reverse sorted set order.
func getObjectsByRevRange(conn redis.Conn, key string, start int, end int) ([][]byte, error) {
return getObjectsBySomeRange(conn, "ZREVRANGE", key, start, end)
}
// getObjectsBySomeRange retrieves the entries for keys enumerated in a sorted set using the specified Redis range
// command (i.e. RANGE, REVRANGE). The entries are retrieved in the order specified by the supplied Redis command.
func getObjectsBySomeRange(conn redis.Conn, command string, key string, start int, end int) ([][]byte, error) {
ids, err := redis.Values(conn.Do(command, key, start, end))
if err != nil && err != redis.ErrNil {
return nil, err
}
var result [][]byte
if len(ids) > 0 {
result, err = redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
}
var objects [][]byte
for _, obj := range result {
if obj != nil {
objects = append(objects, obj)
}
}
return objects, nil
}
// Return objects by a score from a zset
// if limit is 0, all are returned
// if end is negative, it is considered as positive infinity
func getObjectsByRangeFilter(conn redis.Conn, key string, filter string, start, end int) ([][]byte, error) {
ids, err := redis.Values(conn.Do("ZRANGE", key, start, end))
if err != nil && err != redis.ErrNil {
return nil, err
}
var objects [][]byte
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
fids := ids[:0]
if len(ids) > 0 {
for _, id := range ids {
err := conn.Send("ZSCORE", filter, id)
if err != nil {
return nil, err
}
}
scores, err := redis.Strings(conn.Do(""))
if err != nil {
return nil, err
}
for i, score := range scores {
if score != "" {
fids = append(fids, ids[i])
}
}
objects, err = redis.ByteSlices(conn.Do("MGET", fids...))
if err != nil {
return nil, err
}
}
return objects, nil
}
func getObjectsByScore(conn redis.Conn, key string, start, end int64, limit int) ([][]byte, error) {
args := []interface{}{key, start}
if end < 0 {
args = append(args, "+inf")
} else {
args = append(args, end)
}
if limit != 0 {
args = append(args, "LIMIT")
args = append(args, 0)
args = append(args, limit)
}
ids, err := redis.Values(conn.Do("ZRANGEBYSCORE", args...))
if err != nil && err != redis.ErrNil {
return nil, err
}
var objects [][]byte
if len(ids) > 0 {
objects, err = redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
}
return objects, nil
}
// addObject is responsible for setting the object's primary record and then sending the appropriate
// follow-on commands as provided by the caller.
// Transactions are managed outside of this function.
func addObject(data []byte, adder models.Adder, id string, conn redis.Conn) {
_ = conn.Send("SET", id, data)
for _, cmd := range adder.Add() {
switch cmd.Command {
case "ZADD":
_ = conn.Send(cmd.Command, cmd.Hash, cmd.Rank, cmd.Key)
case "SADD":
_ = conn.Send(cmd.Command, cmd.Hash, cmd.Key)
case "HSET":
_ = conn.Send(cmd.Command, cmd.Hash, cmd.Key, cmd.Value)
}
}
}
// deleteObject is responsible for removing the object's primary record and then sending the appropriate
// follow-on commands as provided by the caller.
//
// Transactions are managed outside of this function.
func deleteObject(remover models.Remover, id string, conn redis.Conn) {
_ = conn.Send("DEL", id)
for _, cmd := range remover.Remove() {
switch cmd.Command {
case "ZREM":
fallthrough
case "SREM":
fallthrough
case "HDEL":
_ = conn.Send(cmd.Command, cmd.Hash, cmd.Key)
}
}
}
func getUnionObjectsByValues(conn redis.Conn, vals ...string) ([][]byte, error) {
args := redis.Args{}
for _, v := range vals {
args = args.Add(v)
}
ids, err := redis.Values(conn.Do("SUNION", args...))
if err != nil {
return nil, err
}
if len(ids) == 0 {
return nil, nil
}
objects, err := redis.ByteSlices(conn.Do("MGET", ids...))
if err != nil {
return nil, err
}
return objects, nil
}
func getObjectsByValuesSorted(conn redis.Conn, limit int, vals ...string) ([][]byte, error) {
args := redis.Args{}
cacheSet := uuid.New().String()
args = append(args, cacheSet)
args = append(args, strconv.Itoa(len(vals)))
for _, val := range vals {
args = append(args, val)
}
_, err := conn.Do("ZINTERSTORE", args...)
if err != nil {
return nil, err
}
ids, err := redis.Values(conn.Do("ZREVRANGE", cacheSet, 0, -1))
if err != nil {
return nil, err
}
if limit < 0 || limit > len(ids) {
limit = len(ids)
}
objects, err := redis.ByteSlices(conn.Do("MGET", ids[0:limit]...))
if err != nil {
return nil, err
}
// clean up now unused ZINTERSTORE cache
_, err = redis.Int(conn.Do("DEL", cacheSet))
if err != nil {
return nil, err
}
return objects, nil
}
func validateKeyExists(conn redis.Conn, key string) error {
count, err := redis.Int(conn.Do("EXISTS", key))
if err != nil {
return err
}
if count == 1 {
return nil
}
return db.ErrNotFound
}