/
cursor.go
124 lines (106 loc) · 2.8 KB
/
cursor.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
package utils
import (
"math/rand"
"github.com/samber/lo"
)
// Cursor contains basic cursor data
type Cursor[K comparable] struct {
Seed *int64 `json:"seed"`
RandomFactor float64 `json:"randomProportion"`
CurrentIndex int `json:"currentIndex"`
}
// NewCursor creates a new cursor
func NewCursor[K comparable](withSeed bool, randomFactor float64) *Cursor[K] {
cursor := &Cursor[K]{
RandomFactor: randomFactor,
}
if withSeed {
seed := rand.Int63()
cursor.Seed = &seed
}
return cursor
}
// Encode marshals to json and encodes to base64
func (c Cursor[K]) Encode() (string, error) {
return MarshalAndBase64Encode(c)
}
// ParseCursor decodes from base64 and will unmarshal from json
func ParseCursor[K comparable](cursorString string) (*Cursor[K], error) {
return Base64DecodeAndUnmarshal[Cursor[K]](cursorString)
}
// ApplyTo applies the cursor to a collection of keys
func (c Cursor[K]) ApplyTo(keys []K) []K {
var result []K
for i, key := range keys {
if i > c.CurrentIndex {
result = append(result, key)
}
}
return result
}
// ApplyToSegments applies the cursor to segments of keys
func (c Cursor[K]) ApplyToSegments(segments [][]K, minimumSegmentLength int) []K {
var keys []K
if c.Seed != nil {
keys = ShuffleSegmentedArray(segments, minimumSegmentLength, c.RandomFactor, *c.Seed)
} else {
keys = lo.Flatten(segments)
}
return c.ApplyTo(keys)
}
// ItemCursor contains cursor data for pagination
type ItemCursor[K comparable] struct {
Keys []K `json:"keys"`
CurrentIndex int `json:"currentIndex"`
}
// Encode encodes the cursor to a base64 string
func (c *ItemCursor[K]) Encode() (string, error) {
return MarshalAndBase64Encode(c)
}
// ParseItemCursor parses the base64 encoded cursor into a ItemCursor struct
func ParseItemCursor[K comparable](cursorString string) (*ItemCursor[K], error) {
cursor, err := Base64DecodeAndUnmarshal[ItemCursor[K]](cursorString)
if err != nil {
return nil, err
}
if cursor.Keys == nil {
return nil, err
}
return cursor, nil
}
// CursorFor returns the cursor for the specified string
func (c *ItemCursor[K]) CursorFor(id K) *ItemCursor[K] {
if len(c.Keys) == 0 {
return nil
}
index := lo.IndexOf(c.Keys, id)
if index < 0 {
return nil
}
return &ItemCursor[K]{
Keys: c.Keys,
CurrentIndex: index,
}
}
// NextKeys returns the next keys with this specified limit
func (c *ItemCursor[K]) NextKeys(limit int) []K {
if c.CurrentIndex >= len(c.Keys)-1 {
return nil
}
from := c.CurrentIndex + 1
to := lo.Min[int](
[]int{
c.CurrentIndex + 1 + limit,
len(c.Keys),
},
)
return c.Keys[from:to]
}
// ToItemCursor returns a cursor for the specified ids
func ToItemCursor[K comparable](ids []K, id K) *ItemCursor[K] {
index := lo.IndexOf(ids, id)
return &ItemCursor[K]{
Keys: ids,
CurrentIndex: index,
}
}