-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
156 lines (140 loc) · 3.83 KB
/
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
package pagination
import (
"context"
"encoding/json"
"github.com/jinzhu/copier"
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect"
"time"
)
const (
suffixFront = ":front"
suffixBack = ":back"
defaultExpire = time.Minute * 5
)
var copierOpt = &copier.Option{Converters: []copier.TypeConverter{{
SrcType: primitive.ObjectID{},
DstType: copier.String,
Fn: func(src interface{}) (interface{}, error) {
return src.(primitive.ObjectID).Hex(), nil
},
}}}
var copierTimeOpt = &copier.Option{Converters: []copier.TypeConverter{{
SrcType: time.Time{},
DstType: time.Time{},
Fn: func(src interface{}) (interface{}, error) {
return src.(time.Time), nil
},
}}}
var copierStringOpt = &copier.Option{Converters: []copier.TypeConverter{{
SrcType: copier.String,
DstType: copier.String,
Fn: func(src interface{}) (interface{}, error) {
return src.(string), nil
},
}}}
type Store interface {
GetCursor() any
LoadCursor(ctx context.Context, lastToken string, backward bool) error
StoreCursor(_ context.Context, lastToken *string, first, last any) (*string, error)
StoreStringCursor(_ context.Context, lastToken *string, first, last any) (*string, error)
StoreTimeCursor(_ context.Context, lastToken *string, first, last any) (*string, error)
}
type RawStore struct {
cursor any
cursorType reflect.Type
}
func NewRawStore(cursor any) *RawStore {
t := reflect.TypeOf(cursor)
for t.Kind() == reflect.Interface || t.Kind() == reflect.Pointer {
t = t.Elem()
}
return &RawStore{
cursor: cursor,
cursorType: t,
}
}
// GetCursor 获取游标
func (s *RawStore) GetCursor() any {
return s.cursor
}
// LoadCursor 加载游标
func (s *RawStore) LoadCursor(_ context.Context, lastToken string, backward bool) error {
cursors := reflect.New(reflect.ArrayOf(2, reflect.PointerTo(s.cursorType)))
err := json.Unmarshal([]byte(lastToken), cursors.Interface())
if backward {
s.cursor = cursors.Elem().Index(0).Interface()
} else {
s.cursor = cursors.Elem().Index(1).Interface()
}
if err != nil {
return err
}
return nil
}
// StoreCursor 生成游标
func (s *RawStore) StoreCursor(_ context.Context, lastToken *string, first, last any) (*string, error) {
front := reflect.New(s.cursorType).Interface()
err := copier.CopyWithOption(front, first, *copierOpt)
if err != nil {
return nil, err
}
back := reflect.New(s.cursorType).Interface()
err = copier.CopyWithOption(back, last, *copierOpt)
if err != nil {
return nil, err
}
bytes, err := json.Marshal([2]any{front, back})
if err != nil {
return nil, err
}
if lastToken == nil {
lastToken = new(string)
}
*lastToken = string(bytes)
return lastToken, nil
}
// StoreCursor 生成游标
func (s *RawStore) StoreStringCursor(_ context.Context, lastToken *string, first, last any) (*string, error) {
front := reflect.New(s.cursorType).Interface()
err := copier.CopyWithOption(front, first, *copierStringOpt)
if err != nil {
return nil, err
}
back := reflect.New(s.cursorType).Interface()
err = copier.CopyWithOption(back, last, *copierStringOpt)
if err != nil {
return nil, err
}
bytes, err := json.Marshal([2]any{front, back})
if err != nil {
return nil, err
}
if lastToken == nil {
lastToken = new(string)
}
*lastToken = string(bytes)
return lastToken, nil
}
// StoreCursor 生成游标
func (s *RawStore) StoreTimeCursor(_ context.Context, lastToken *string, first, last any) (*string, error) {
front := reflect.New(s.cursorType).Interface()
err := copier.CopyWithOption(front, first, *copierTimeOpt)
if err != nil {
return nil, err
}
back := reflect.New(s.cursorType).Interface()
err = copier.CopyWithOption(back, last, *copierTimeOpt)
if err != nil {
return nil, err
}
bytes, err := json.Marshal([2]any{front, back})
if err != nil {
return nil, err
}
if lastToken == nil {
lastToken = new(string)
}
*lastToken = string(bytes)
return lastToken, nil
}