-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_map_store.go
268 lines (243 loc) · 7.09 KB
/
redis_map_store.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package storage
import (
"sort"
"strings"
"github.com/TheThingsNetwork/go-utils/encoding"
"github.com/TheThingsNetwork/ttn/utils/errors"
redis "gopkg.in/redis.v5"
)
// RedisMapStore stores structs as HMaps in Redis
type RedisMapStore struct {
*RedisStore
encoder func(input interface{}, properties ...string) (map[string]string, error)
decoder func(input map[string]string) (output interface{}, err error)
migrations map[string]MigrateFunction
}
// NewRedisMapStore returns a new RedisMapStore that talks to the given Redis client and respects the given prefix
func NewRedisMapStore(client *redis.Client, prefix string) *RedisMapStore {
if !strings.HasSuffix(prefix, ":") {
prefix += ":"
}
return &RedisMapStore{
RedisStore: NewRedisStore(client, prefix),
migrations: make(map[string]MigrateFunction),
}
}
// SetBase sets the base struct for automatically encoding and decoding to and from Redis format
func (s *RedisMapStore) SetBase(base interface{}, tagName string) {
if tagName == "" {
tagName = "redis"
}
s.SetEncoder(func(input interface{}, properties ...string) (map[string]string, error) {
return encoding.ToStringStringMap(tagName, input, properties...)
})
s.SetDecoder(func(input map[string]string) (output interface{}, err error) {
return encoding.FromStringStringMap(tagName, base, input)
})
}
// SetEncoder sets the encoder to convert structs to Redis format
func (s *RedisMapStore) SetEncoder(encoder func(input interface{}, properties ...string) (map[string]string, error)) {
s.encoder = encoder
}
// SetDecoder sets the decoder to convert structs from Redis format
func (s *RedisMapStore) SetDecoder(decoder func(input map[string]string) (output interface{}, err error)) {
s.decoder = decoder
}
// GetAll returns all results for the given keys, prepending the prefix to the keys if necessary
// This function will migrate outdated results to newer versions if migrations are set
func (s *RedisMapStore) GetAll(keys []string, options *ListOptions) ([]interface{}, error) {
if len(keys) == 0 {
return []interface{}{}, nil
}
for i, key := range keys {
if !strings.HasPrefix(key, s.prefix) {
keys[i] = s.prefix + key
}
}
sort.Strings(keys)
selectedKeys := selectKeys(keys, options)
if len(selectedKeys) == 0 {
return []interface{}{}, nil
}
pipe := s.client.Pipeline()
defer pipe.Close()
// Add all commands to pipeline
cmds := make(map[string]*redis.StringStringMapCmd)
for _, key := range selectedKeys {
cmds[key] = pipe.HGetAll(key)
}
// Execute pipeline
_, err := pipe.Exec()
if err != nil {
return nil, err
}
// Get all results from pipeline
results := make([]interface{}, len(selectedKeys))
for i, key := range selectedKeys {
if result, err := cmds[key].Result(); err == nil {
result, _ = s.migrate(key, result)
if result, err := s.decoder(result); err == nil {
results[i] = result
}
}
}
return results, nil
}
// List all results matching the selector, prepending the prefix to the selector if necessary
func (s *RedisMapStore) List(selector string, options *ListOptions) ([]interface{}, error) {
allKeys, err := s.Keys(selector)
if err != nil {
return nil, err
}
return s.GetAll(allKeys, options)
}
// Get one result, prepending the prefix to the key if necessary
// This function will migrate outdated results to newer versions if migrations are set
func (s *RedisMapStore) Get(key string) (interface{}, error) {
if !strings.HasPrefix(key, s.prefix) {
key = s.prefix + key
}
result, err := s.client.HGetAll(key).Result()
if err == redis.Nil || len(result) == 0 {
return nil, errors.NewErrNotFound(key)
}
if err != nil {
return nil, err
}
result, _ = s.migrate(key, result)
i, err := s.decoder(result)
if err != nil {
return nil, err
}
return i, nil
}
// GetFields for a record, prepending the prefix to the key if necessary
// This function does *not* migrate outdated results to newer versions
func (s *RedisMapStore) GetFields(key string, fields ...string) (interface{}, error) {
if !strings.HasPrefix(key, s.prefix) {
key = s.prefix + key
}
result, err := s.client.HMGet(key, fields...).Result()
if err == redis.Nil {
return nil, errors.NewErrNotFound(key)
}
if err != nil {
return nil, err
}
res := make(map[string]string)
for i, field := range fields {
if str, ok := result[i].(string); ok {
res[field] = str
}
}
i, err := s.decoder(res)
if err != nil {
return nil, err
}
return i, nil
}
// ChangedFielder interface is used to see what fields to update
type ChangedFielder interface {
ChangedFields() []string
}
func (s *RedisMapStore) prepare(key string, value interface{}, properties ...string) (fullKey string, vmap map[string]string, err error) {
if !strings.HasPrefix(key, s.prefix) {
fullKey = s.prefix + key
}
if len(properties) == 0 {
if i, ok := value.(ChangedFielder); ok {
properties = i.ChangedFields()
if len(properties) == 0 {
return
}
}
}
vmap, err = s.encoder(value, properties...)
if err != nil {
return
}
if len(vmap) == 0 {
return
}
if v, ok := value.(hasDBVersion); ok {
vmap[VersionKey] = v.DBVersion()
}
return
}
// Set a record, prepending the prefix to the key if necessary, optionally setting only the given properties
func (s *RedisMapStore) Set(key string, value interface{}, properties ...string) error {
key, vmap, err := s.prepare(key, value, properties...)
if err != nil {
return err
}
if len(vmap) == 0 {
return nil
}
return s.client.HMSet(key, vmap).Err()
}
// Create a new record, prepending the prefix to the key if necessary, optionally setting only the given properties
// This function returns an error if the record already exists
func (s *RedisMapStore) Create(key string, value interface{}, properties ...string) error {
key, vmap, err := s.prepare(key, value, properties...)
if err != nil {
return err
}
if len(vmap) == 0 {
return nil
}
err = s.client.Watch(func(tx *redis.Tx) error {
exists, err := tx.Exists(key).Result()
if err != nil {
return err
}
if exists {
return errors.NewErrAlreadyExists(key)
}
_, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
pipe.HMSet(key, vmap)
return nil
})
if err != nil {
return err
}
return nil
}, key)
if err != nil {
return err
}
return nil
}
// Update an existing record, prepending the prefix to the key if necessary, optionally setting only the given properties
// This function returns an error if the record does not exist
func (s *RedisMapStore) Update(key string, value interface{}, properties ...string) error {
key, vmap, err := s.prepare(key, value, properties...)
if err != nil {
return err
}
if len(vmap) == 0 {
return nil
}
err = s.client.Watch(func(tx *redis.Tx) error {
exists, err := tx.Exists(key).Result()
if err != nil {
return err
}
if !exists {
return errors.NewErrNotFound(key)
}
_, err = tx.Pipelined(func(pipe *redis.Pipeline) error {
pipe.HMSet(key, vmap)
return nil
})
if err != nil {
return err
}
return nil
}, key)
if err != nil {
return err
}
return nil
}