-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.go
224 lines (202 loc) · 4.98 KB
/
dataloader.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
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
package dataloader
import (
"sync"
)
// DataLoader is threadsafe map for loading data, and handles batching/dedupping.
// It never expires. It is inspired by github.com/facebook/dataloader.
type DataLoader struct {
mu sync.RWMutex
cache map[interface{}]Value
pending map[interface{}]interface{}
fetchDone *Notification
batchLoader func(keys []interface{}) []Value
sch *Scheduler
}
// New creates a new dataloader.
func New(sch *Scheduler, batchLoader func(keys []interface{}) []Value) *DataLoader {
return &DataLoader{
cache: make(map[interface{}]Value),
pending: make(map[interface{}]interface{}),
batchLoader: batchLoader,
sch: sch,
}
}
// Parallel is convenient helper to convert a single fetch to a multi-fetch that execute
// the individual single fetch in parallel.
func Parallel(f func(interface{}) Value) func(keys []interface{}) []Value {
return func(keys []interface{}) []Value {
if len(keys) == 1 {
return []Value{f(keys[0])}
}
values := make([]Value, len(keys))
var wg sync.WaitGroup
for i := range keys {
wg.Add(1)
i := i
go func() {
defer wg.Done()
values[i] = f(keys[i])
}()
}
wg.Wait()
return values
}
}
// Serial is convenient helper to convert a single fetch to a multi-fetch that execute
// the individual single fetch serially.
func Serial(f func(interface{}) Value) func(keys []interface{}) []Value {
return func(keys []interface{}) []Value {
values := make([]Value, len(keys))
for i, k := range keys {
values[i] = f(k)
}
return values
}
}
// Value wraps the value and error.
type Value struct {
V interface{}
Err error
}
// Unbox is a helper function to unbox the value.
func (v Value) Unbox() (interface{}, error) {
return v.V, v.Err
}
// NewValue creates a new value.
func NewValue(v interface{}, err error) Value {
return Value{V: v, Err: err}
}
func (dl *DataLoader) scheduleFetch() *Notification {
// Must be called with dl.mu locked.
if dl.fetchDone != nil {
return dl.fetchDone
}
if dl.sch == nil {
dl.mu.Unlock()
dl.fetchPending()
dl.mu.Lock()
return nil
}
n := NewNotification(dl.sch)
dl.fetchDone = n
dl.sch.SpawnLow(func() {
dl.fetchPending()
n.Notify()
})
return n
}
func (dl *DataLoader) fetchPending() {
dl.mu.Lock()
defer func() {
dl.pending = make(map[interface{}]interface{})
dl.fetchDone = nil
dl.mu.Unlock()
}()
keys := make([]interface{}, 0, len(dl.pending))
mkeys := make([]interface{}, 0, len(dl.pending))
for mkey, key := range dl.pending {
if _, ok := dl.cache[mkey]; ok {
continue
}
mkeys = append(mkeys, mkey)
keys = append(keys, key)
}
if len(keys) == 0 {
return
}
// TODO: Handle panic here?
// TODO: The locking can be optimized here.
values := dl.batchLoader(keys)
for i, v := range values {
dl.cache[mkeys[i]] = v
}
}
// Load loads a single value.
func (dl *DataLoader) Load(key interface{}) Value {
return dl.LoadMany([]interface{}{
key,
})[0]
}
type MapKeyer interface {
MapKey() interface{}
}
func getMapKey(key interface{}) interface{} {
if v, ok := key.(MapKeyer); ok {
return v.MapKey()
}
return key
}
// LoadMany loads multiple values.
func (dl *DataLoader) LoadMany(keys []interface{}) []Value {
values := make([]Value, len(keys))
var keysToFetch []interface{}
var mkeysToFetch []interface{}
var keysToFetchIndex []int
func() {
dl.mu.RLock()
defer dl.mu.RUnlock()
for i, key := range keys {
mkey := getMapKey(key)
v, ok := dl.cache[mkey]
if ok {
values[i] = v
continue
}
keysToFetch = append(keysToFetch, key)
mkeysToFetch = append(mkeysToFetch, mkey)
keysToFetchIndex = append(keysToFetchIndex, i)
}
}()
if len(keysToFetch) > 0 {
n := func() *Notification {
dl.mu.Lock()
defer dl.mu.Unlock()
for i := 0; i < len(keysToFetch); i++ {
key := keysToFetch[i]
mkey := mkeysToFetch[i]
v, ok := dl.cache[mkey]
if ok {
values[keysToFetchIndex[i]] = v
keysToFetch[i] = keysToFetch[len(keysToFetch)-1]
keysToFetch = keysToFetch[:len(keysToFetch)-1]
keysToFetchIndex[i] = keysToFetchIndex[len(keysToFetchIndex)-1]
keysToFetchIndex = keysToFetchIndex[:len(keysToFetchIndex)-1]
continue
}
dl.pending[mkey] = key
}
return dl.scheduleFetch()
}()
if len(keysToFetch) > 0 {
if n != nil {
n.Wait()
}
for vsi, vi := range keysToFetchIndex {
values[vi] = dl.cache[mkeysToFetch[vsi]]
}
}
}
return values
}
// Prime put a single value into the cache. No-op if the value already exists.
func (dl *DataLoader) Prime(key interface{}, v Value) {
dl.mu.Lock()
defer dl.mu.Unlock()
if _, ok := dl.cache[key]; ok {
// If you want to override, call Clear first.
return
}
dl.cache[key] = v
}
// Clear removes a single value from the cache.
func (dl *DataLoader) Clear(key interface{}) {
dl.mu.Lock()
defer dl.mu.Unlock()
delete(dl.cache, key)
}
// ClearAll removes all values from the cache.
func (dl *DataLoader) ClearAll() {
dl.mu.Lock()
defer dl.mu.Unlock()
dl.cache = make(map[interface{}]Value)
}