-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_list.go
305 lines (240 loc) · 6.96 KB
/
unique_list.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
package kredis
import (
"time"
"github.com/redis/go-redis/v9"
)
type UniqueList[T KredisTyped] struct {
Proxy
limit uint64
typed *T
}
// UniqueList[bool] type
func NewBoolUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[bool], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[bool]{Proxy: *proxy, limit: limit, typed: new(bool)}, nil
}
func NewBoolUniqueListWithDefault(key string, limit uint64, defaultElements []bool, opts ...ProxyOption) (l *UniqueList[bool], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[bool]{Proxy: *proxy, limit: limit, typed: new(bool)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// UniqueList[int] type
func NewIntegerUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[int], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[int]{Proxy: *proxy, limit: limit, typed: new(int)}, nil
}
func NewIntegerUniqueListWithDefault(key string, limit uint64, defaultElements []int, opts ...ProxyOption) (l *UniqueList[int], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[int]{Proxy: *proxy, limit: limit, typed: new(int)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// UniqueList[int] type
func NewFloatUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[float64], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[float64]{Proxy: *proxy, limit: limit, typed: new(float64)}, nil
}
func NewFloatUniqueListWithDefault(key string, limit uint64, defaultElements []float64, opts ...ProxyOption) (l *UniqueList[float64], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[float64]{Proxy: *proxy, limit: limit, typed: new(float64)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// UniqueList[string] type
func NewStringUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[string], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[string]{Proxy: *proxy, limit: limit, typed: new(string)}, nil
}
func NewStringUniqueListWithDefault(key string, limit uint64, defaultElements []string, opts ...ProxyOption) (l *UniqueList[string], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[string]{Proxy: *proxy, limit: limit, typed: new(string)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// UniqueList[time.Time] type
func NewTimeUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[time.Time], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[time.Time]{Proxy: *proxy, limit: limit, typed: new(time.Time)}, nil
}
func NewTimeUniqueListWithDefault(key string, limit uint64, defaultElements []time.Time, opts ...ProxyOption) (l *UniqueList[time.Time], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[time.Time]{Proxy: *proxy, limit: limit, typed: new(time.Time)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// UniqueList[KredisJSON] type
func NewJSONUniqueList(key string, limit uint64, opts ...ProxyOption) (*UniqueList[KredisJSON], error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &UniqueList[KredisJSON]{Proxy: *proxy, limit: limit, typed: new(KredisJSON)}, nil
}
func NewJSONUniqueListWithDefault(key string, limit uint64, defaultElements []KredisJSON, opts ...ProxyOption) (l *UniqueList[KredisJSON], err error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return
}
l = &UniqueList[KredisJSON]{Proxy: *proxy, limit: limit, typed: new(KredisJSON)}
err = proxy.watch(func() error {
_, err := l.Append(defaultElements...)
return err
})
if err != nil {
return nil, err
}
return
}
// generic UniqueList functions
// NOTE we're using Do() for the "LRANGE" command instead of LRange() as to
// seemingly avoid an []string allocation from StringSliceCmd#Result()
func (l *UniqueList[T]) Elements(elements []T, opts ...RangeOption) (total int64, err error) {
rangeOptions := RangeOptions{0}
for _, opt := range opts {
opt(&rangeOptions)
}
stop := rangeOptions.start + int64(len(elements))
slice, err := l.client.Do(l.ctx, "lrange", l.key, rangeOptions.start, stop).Slice()
if err != nil {
return
}
total = int64(copyCmdSliceTo(slice, elements))
return
}
func (l *UniqueList[T]) Remove(elements ...T) (err error) {
for _, element := range elements {
value := typeToInterface(element)
if err = l.client.LRem(l.ctx, l.key, 0, value).Err(); err != nil {
return
}
}
return
}
func (l UniqueList[T]) Prepend(elements ...T) (int64, error) {
if len(elements) < 1 {
return 0, nil
}
return l.update(elements, func(pipe redis.Pipeliner, uniq []interface{}) *redis.IntCmd {
return pipe.LPush(l.ctx, l.key, uniq...)
})
}
func (l *UniqueList[T]) Append(elements ...T) (int64, error) {
if len(elements) < 1 {
return 0, nil
}
return l.update(elements, func(pipe redis.Pipeliner, uniq []interface{}) *redis.IntCmd {
return pipe.RPush(l.ctx, l.key, uniq...)
})
}
func (l *UniqueList[T]) Clear() error {
_, err := l.client.Del(l.ctx, l.key).Result()
if err != nil {
return err
}
return nil
}
func (l *UniqueList[T]) Length() (llen int64, err error) {
llen, err = l.client.LLen(l.ctx, l.key).Result()
if err == redis.Nil {
err = nil
}
return
}
func (l UniqueList[T]) Last() (T, bool) {
slice, err := l.client.Do(l.ctx, "lrange", l.key, -1, -1).Slice()
if err != nil || len(slice) < 1 {
return any(*l.typed).(T), false
}
elements := make([]T, 1)
copyCmdSliceTo(slice, elements)
return elements[0], true
}
func (l UniqueList[T]) LastN(elements []T) (total int64, err error) {
slice, err := l.client.Do(l.ctx, "lrange", l.key, -len(elements), -1).Slice()
if err != nil {
return
}
total = copyCmdSliceTo(slice, elements)
return
}
func (l *UniqueList[T]) SetLimit(limit uint64) {
l.limit = limit
}
func (l *UniqueList[T]) update(elements []T, updateFn func(redis.Pipeliner, []interface{}) *redis.IntCmd) (int64, error) {
uniq := newIter(elements).unique()
pipe := l.client.TxPipeline()
for _, u := range uniq {
pipe.LRem(l.ctx, l.key, 0, u)
}
llen := updateFn(pipe, uniq)
if l.limit > 0 {
pipe.LTrim(l.ctx, l.key, -int64(l.limit), -1)
}
_, err := pipe.Exec(l.ctx)
if err != nil {
return 0, err
}
l.RefreshTTL()
return llen.Val(), nil
}