-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
144 lines (112 loc) · 2.58 KB
/
key.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
package destination
import (
"context"
"fmt"
"sync"
"github.com/auho/go-toolkit/flow/storage"
"github.com/auho/go-toolkit/flow/storage/redis"
"github.com/auho/go-toolkit/redis/client"
)
var _ storage.Destinationer[storage.MapEntry] = (*key[storage.MapEntry])(nil)
type keyer[E storage.Entry] interface {
redis.Keyer
accept(itemsChan <-chan []E, c *client.Redis, key string, pageSize int64)
stateAmount() int64
}
type key[E storage.Entry] struct {
storage.Storage
concurrency int
isTruncate bool
pageSize int64
keyName string
isDone bool
itemsChan chan []E
doWg sync.WaitGroup
client *client.Redis
keyer keyer[E]
state *storage.State
}
func newKey[E storage.Entry](config Config, keyer keyer[E]) (*key[E], error) {
k := &key[E]{}
k.keyer = keyer
err := k.config(config)
if err != nil {
return nil, err
}
return k, nil
}
func (k *key[E]) GetClient() *client.Redis {
return k.client
}
func (k *key[E]) Accept() error {
k.state.StatusAccept()
k.state.DurationStart()
if k.isTruncate {
_, err := k.keyer.Truncate(context.Background(), k.client, k.keyName)
if err != nil {
return err
}
}
k.itemsChan = make(chan []E, k.concurrency)
for i := 0; i < k.concurrency; i++ {
k.doWg.Add(1)
go func() {
k.keyer.accept(k.itemsChan, k.client, k.keyName, k.pageSize)
k.doWg.Done()
}()
}
return nil
}
func (k *key[E]) Receive(items []E) {
k.itemsChan <- items
}
func (k *key[E]) Done() {
k.state.StatusDone()
if k.isDone {
return
}
k.isDone = true
close(k.itemsChan)
}
func (k *key[E]) Finish() {
k.doWg.Wait()
k.state.StatusFinish()
k.state.DurationStop()
}
func (k *key[E]) Summary() []string {
return []string{fmt.Sprintf("%s Concurrency:%d; page size:%d", k.Title(), k.concurrency, k.pageSize)}
}
func (k *key[E]) State() []string {
k.state.SetAmount(k.keyer.stateAmount())
return []string{k.state.Overview()}
}
func (k *key[E]) Title() string {
return fmt.Sprintf("Destiantion redis[%s] %s", k.keyer.Type(), k.keyName)
}
func (k *key[E]) Close() error {
return k.client.Close()
}
func (k *key[E]) config(config Config) error {
k.isTruncate = config.IsTruncate
k.concurrency = config.Concurrency
k.pageSize = config.PageSize
k.keyName = config.Key
if k.concurrency <= 0 {
k.concurrency = 1
}
if k.pageSize <= 0 {
k.pageSize = 20
}
if k.keyName == "" {
k.LogFatalWithTitle("key name is empty")
}
var err error
k.client, err = client.NewRedisClient(config.Options)
if err != nil {
return err
}
k.state = storage.NewState()
k.state.Title = k.Title()
k.state.StatusConfig()
return nil
}