-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvsync.go
199 lines (163 loc) · 3.79 KB
/
kvsync.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
package kvsync
import (
"context"
"errors"
"gorm.io/gorm"
"reflect"
)
// KVStore is the interface for a key-value store
type KVStore interface {
Put(key string, value any) error
Fetch(key string, dest any) error
}
// Syncable is the interface for a Gorm model that can be synced with a KVStore
type Syncable interface {
SyncKeys() map[string]string
}
// Report is a struct that represents a report of a sync operation
type Report struct {
Model any
KeyName string
Key string
Err error
}
type ReportCallback func(Report)
// KVSync is the interface for a service that syncs Gorm models with a KVStore
type KVSync interface {
Fetch(dest Syncable, keyName string) error
GormCallback() func(db *gorm.DB)
Sync(entity any) error
}
// Options is a struct that contains options for creating a KVSync instance
type Options struct {
Store KVStore
Workers int
ReportCallback ReportCallback
}
// NewKVSync creates a new KVSync instance
func NewKVSync(ctx context.Context, options Options) KVSync {
workers := options.Workers
if workers < 1 {
workers = 1
}
k := &kvSync{
store: options.Store,
ctx: ctx,
queue: make(chan queueItem, options.Workers),
workers: workers,
reports: make(chan Report),
reportCallback: options.ReportCallback,
}
k.launchWorkers()
go func() {
for {
select {
case <-k.ctx.Done():
return
case r := <-k.reports:
if k.reportCallback != nil {
k.reportCallback(r)
}
}
}
}()
return k
}
type queueItem struct {
entity any
keyName string
key string
}
// kvSync is a struct that syncs a Gorm model with a KVStore
type kvSync struct {
store KVStore
queue chan queueItem
reports chan Report
ctx context.Context
workers int
reportCallback ReportCallback
}
func (k *kvSync) launchWorkers() {
for i := 0; i < k.workers; i++ {
go func() {
for {
select {
case <-k.ctx.Done():
return
case item := <-k.queue:
k.syncByKey(item.entity, item.key, true)
}
}
}()
}
}
// Fetch fetches a Syncable model from a KVStore and populates a new model with the data
func (k *kvSync) Fetch(dest Syncable, keyName string) error {
if reflect.TypeOf(dest).Kind() != reflect.Ptr {
return errors.New("destination must be a pointer")
}
return k.store.Fetch(dest.SyncKeys()[keyName], dest)
}
// GormCallback returns a Gorm callback that syncs a model with a KVStore
func (k *kvSync) GormCallback() func(db *gorm.DB) {
return func(db *gorm.DB) {
model := resolvePointer(db.Statement.Dest)
if reflect.TypeOf(model).Kind() == reflect.Slice {
val := reflect.ValueOf(model)
for i := 0; i < val.Len(); i++ {
item := val.Index(i).Interface()
go k.enqueue(item)
}
return
} else {
go k.enqueue(model)
}
}
}
// Sync syncs a model with a KVStore synchronously
func (k *kvSync) Sync(entity any) error {
entity = resolvePointer(entity)
syncable, ok := entity.(Syncable)
if !ok {
return errors.New("model is not syncable")
}
for _, key := range syncable.SyncKeys() {
k.syncByKey(entity, key, false)
}
return nil
}
func (k *kvSync) syncByKey(entity any, key string, report bool) {
entity = resolvePointer(entity)
err := k.store.Put(key, entity)
if !report {
return
}
k.reports <- Report{
Model: entity,
Key: key,
Err: err,
}
}
func (k *kvSync) enqueue(entity any) {
entity = resolvePointer(entity)
syncable, ok := entity.(Syncable)
if !ok {
return
}
for keyName, key := range syncable.SyncKeys() {
k.queue <- queueItem{
entity: entity,
keyName: keyName,
key: key,
}
}
}
func resolvePointer(item interface{}) interface{} {
for {
val := reflect.ValueOf(item)
if val.Kind() != reflect.Ptr {
return item
}
item = reflect.Indirect(val).Interface()
}
}