-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted.go
More file actions
338 lines (282 loc) · 8.15 KB
/
sorted.go
File metadata and controls
338 lines (282 loc) · 8.15 KB
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
329
330
331
332
333
334
335
336
337
338
package redifu
import (
"context"
"errors"
"time"
"github.com/21strive/item"
"github.com/redis/go-redis/v9"
)
type SortedWithPipeline[T item.Blueprint] struct {
sorted *Sorted[T]
pipeline redis.Pipeliner
}
func (sw *SortedWithPipeline[T]) AddItem(ctx context.Context, item T, keyParams ...string) error {
return sw.sorted.addItem(ctx, sw.pipeline, item, keyParams...)
}
func (sw *SortedWithPipeline[T]) RemoveItem(ctx context.Context, item T, keyParams ...string) error {
return sw.sorted.removeItem(ctx, sw.pipeline, item, keyParams...)
}
type Sorted[T item.Blueprint] struct {
client redis.UniversalClient
baseClient *Base[T]
sortedSetClient *SortedSet[T]
sortingReference string
relation map[string]Relation
timeToLive time.Duration
}
func NewSorted[T item.Blueprint](client redis.UniversalClient, baseClient *Base[T], keyFormat string, timeToLive time.Duration) *Sorted[T] {
sortedSetClient := &SortedSet[T]{}
sortedSetClient.Init(client, keyFormat)
sorted := &Sorted[T]{}
sorted.Init(client, baseClient, sortedSetClient, timeToLive)
return sorted
}
func (srtd *Sorted[T]) Init(client redis.UniversalClient, baseClient *Base[T], sortedSetClient *SortedSet[T], timeToLive time.Duration) {
srtd.client = client
srtd.baseClient = baseClient
srtd.sortedSetClient = sortedSetClient
srtd.timeToLive = timeToLive
}
func (cr *Sorted[T]) AddRelation(identifier string, relationBase Relation) {
if cr.relation == nil {
cr.relation = make(map[string]Relation)
}
cr.relation[identifier] = relationBase
}
func (cr *Sorted[T]) GetRelation() map[string]Relation {
return cr.relation
}
func (srtd *Sorted[T]) SetSortingReference(sortingReference string) {
srtd.sortingReference = sortingReference
}
func (srtd *Sorted[T]) SetExpiration(ctx context.Context, pipe redis.Pipeliner, keyParams ...string) {
srtd.sortedSetClient.SetExpiration(ctx, pipe, srtd.timeToLive, keyParams...)
}
func (srtd *Sorted[T]) Count(ctx context.Context, keyParams ...string) int64 {
return srtd.sortedSetClient.Count(ctx, keyParams...)
}
func (srtd *Sorted[T]) WithPipeline(pipe redis.Pipeliner) *SortedWithPipeline[T] {
return &SortedWithPipeline[T]{
sorted: srtd,
pipeline: pipe,
}
}
func (srtd *Sorted[T]) addItem(ctx context.Context, pipe redis.Pipeliner, item T, keyParams ...string) error {
_, errGet := srtd.baseClient.Get(ctx, item.GetRandId())
if errGet != nil && !errors.Is(errGet, redis.Nil) {
return errGet
}
var selfPipe bool
if pipe == nil {
pipe = srtd.client.Pipeline()
selfPipe = true
}
if errors.Is(errGet, redis.Nil) {
errSet := srtd.baseClient.WithPipeline(pipe).Set(ctx, item)
if errSet != nil {
return errSet
}
}
errIngest := srtd.IngestItem(ctx, pipe, item, false, keyParams...)
if errIngest != nil {
return errIngest
}
if selfPipe {
_, errPipe := pipe.Exec(ctx)
return errPipe
}
return nil
}
func (srtd *Sorted[T]) AddItem(ctx context.Context, item T, keyParams ...string) error {
return srtd.addItem(ctx, nil, item, keyParams...)
}
func (srtd *Sorted[T]) IngestItem(ctx context.Context, pipe redis.Pipeliner, item T, seed bool, keyParams ...string) error {
score, err := getItemScore(item, srtd.sortingReference)
if err != nil {
return err
}
if !seed {
isBlankPage, errGet := srtd.IsEmpty(ctx, keyParams...)
if errGet != nil {
return errGet
}
if isBlankPage {
errDelBlankPage := srtd.HasData(ctx, pipe, keyParams...)
if errDelBlankPage != nil {
return errDelBlankPage
}
}
if srtd.sortedSetClient.Count(ctx, keyParams...) > 0 {
srtd.sortedSetClient.SetItem(ctx, pipe, score, item, keyParams...)
}
} else {
srtd.sortedSetClient.SetItem(ctx, pipe, score, item, keyParams...)
}
return nil
}
func (srtd *Sorted[T]) removeItem(ctx context.Context, pipe redis.Pipeliner, item T, keyParams ...string) error {
var selfPipe bool
if pipe == nil {
selfPipe = true
pipe = srtd.client.Pipeline()
}
errDelBase := srtd.baseClient.WithPipeline(pipe).Del(ctx, item)
if errDelBase != nil {
return errDelBase
}
errDel := srtd.sortedSetClient.RemoveItem(ctx, pipe, item, keyParams...)
if errDel != nil {
return errDel
}
if selfPipe {
_, errPipe := pipe.Exec(ctx)
return errPipe
}
return nil
}
func (srtd *Sorted[T]) RemoveItem(ctx context.Context, item T, keyParams ...string) error {
return srtd.removeItem(ctx, nil, item, keyParams...)
}
func (srtd *Sorted[T]) Fetch(direction string) *sortedFetchBuilder[T] {
return &sortedFetchBuilder[T]{
direction: direction,
sorted: srtd,
}
}
func (srtd *Sorted[T]) MarkEmpty(ctx context.Context, pipe redis.Pipeliner, keyParams ...string) {
sortedSetKey := joinParam(srtd.sortedSetClient.sortedSetKeyFormat, keyParams)
lastPageKey := sortedSetKey + ":blankpage"
pipe.Set(
ctx,
lastPageKey,
1,
srtd.timeToLive,
)
}
func (srtd *Sorted[T]) HasData(ctx context.Context, pipe redis.Pipeliner, keyParams ...string) error {
sortedSetKey := joinParam(srtd.sortedSetClient.sortedSetKeyFormat, keyParams)
lastPageKey := sortedSetKey + ":blankpage"
pipe.Del(ctx, lastPageKey)
return nil
}
func (srtd *Sorted[T]) IsEmpty(ctx context.Context, keyParams ...string) (bool, error) {
sortedSetKey := joinParam(srtd.sortedSetClient.sortedSetKeyFormat, keyParams)
lastPageKey := sortedSetKey + ":blankpage"
getLastPageKey := srtd.client.Get(ctx, lastPageKey)
if getLastPageKey.Err() != nil {
if getLastPageKey.Err() == redis.Nil {
return false, nil
} else {
return false, getLastPageKey.Err()
}
}
if getLastPageKey.Val() == "1" {
return true, nil
}
return false, nil
}
func (srtd *Sorted[T]) RequiresSeeding(ctx context.Context, keyParams ...string) (bool, error) {
isBlankPage, err := srtd.IsEmpty(ctx, keyParams...)
if err != nil {
return false, err
}
if !isBlankPage {
if srtd.sortedSetClient.Count(ctx, keyParams...) > 0 {
return false, nil
}
return true, nil
} else {
return false, nil
}
}
func (srtd *Sorted[T]) Remove() *sortedRemoveBuilder[T] {
return &sortedRemoveBuilder[T]{
srtd: srtd,
keyParams: nil,
purge: false,
}
}
func (srtd *Sorted[T]) Purge() *sortedRemoveBuilder[T] {
return &sortedRemoveBuilder[T]{
srtd: srtd,
keyParams: nil,
purge: true,
}
}
type sortedFetchBuilder[T item.Blueprint] struct {
direction string
sorted *Sorted[T]
keyParams []string
processor func(*T, []interface{})
processorArgs []interface{}
byScore bool
lowerbound int64
upperbound int64
}
func (s *sortedFetchBuilder[T]) WithParams(params ...string) *sortedFetchBuilder[T] {
s.keyParams = params
return s
}
func (s *sortedFetchBuilder[T]) WithProcessor(processor func(*T, []interface{}), processorArgs ...interface{}) *sortedFetchBuilder[T] {
s.processor = processor
s.processorArgs = processorArgs
return s
}
func (s *sortedFetchBuilder[T]) WithRange(lowerbound int64, upperbound int64) *sortedFetchBuilder[T] {
s.byScore = true
s.lowerbound = lowerbound
s.upperbound = upperbound
return s
}
func (s *sortedFetchBuilder[T]) Exec(ctx context.Context) ([]T, error) {
if !s.byScore {
return s.sorted.sortedSetClient.Fetch(
ctx,
s.sorted.baseClient,
s.direction,
s.processor,
s.processorArgs,
s.sorted.relation,
0, -1, false, s.keyParams...)
} else {
return s.sorted.sortedSetClient.Fetch(
ctx,
s.sorted.baseClient,
s.direction,
s.processor,
s.processorArgs,
s.sorted.relation,
s.lowerbound,
s.upperbound, true, s.keyParams...)
}
}
type sortedRemoveBuilder[T item.Blueprint] struct {
srtd *Sorted[T]
keyParams []string
purge bool
}
func (s *sortedRemoveBuilder[T]) WithParams(params ...string) *sortedRemoveBuilder[T] {
s.keyParams = params
return s
}
func (s *sortedRemoveBuilder[T]) Exec(ctx context.Context) error {
pipe := s.srtd.client.Pipeline()
if s.purge {
fetchedItems, err := s.srtd.Fetch(Ascending).WithParams(s.keyParams...).Exec(ctx)
if err != nil {
return err
}
for _, fetchedItem := range fetchedItems {
errDelItem := s.srtd.baseClient.WithPipeline(pipe).Del(ctx, fetchedItem)
if errDelItem != nil {
return errDelItem
}
}
}
err := s.srtd.sortedSetClient.Delete(ctx, pipe, s.keyParams...)
if err != nil {
return err
}
_, errPipe := pipe.Exec(ctx)
return errPipe
}