/
dataloader.go
186 lines (143 loc) · 4.05 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
package graphql
import (
"context"
"sync"
"github.com/graph-gophers/dataloader"
"github.com/spaceuptech/space-cloud/model"
"github.com/spaceuptech/space-cloud/utils"
)
type resultsHolder struct {
sync.Mutex
results []*dataloader.Result
whereClauses []interface{}
}
func (holder *resultsHolder) getResults() []*dataloader.Result {
holder.Lock()
defer holder.Unlock()
return holder.results
}
func (holder *resultsHolder) addResult(i int, result *dataloader.Result) {
holder.Lock()
holder.results[i] = result
holder.Unlock()
}
func (holder *resultsHolder) getWhereClauses() []interface{} {
holder.Lock()
defer holder.Unlock()
return holder.whereClauses
}
func (holder *resultsHolder) addWhereClause(whereClause map[string]interface{}) {
holder.Lock()
holder.whereClauses = append(holder.whereClauses, whereClause)
holder.Unlock()
}
func (holder *resultsHolder) fillResults(res []interface{}) {
holder.Lock()
defer holder.Unlock()
// Create a where clause index
index := 0
length := len(holder.results)
for i := 0; i < length; i++ {
// Continue if result already has a value
if holder.results[i] != nil {
continue
}
// Get the where clause
whereClause := holder.whereClauses[index]
docs := []interface{}{}
for _, doc := range res {
if utils.Validate(whereClause.(map[string]interface{}), doc) {
docs = append(docs, doc)
}
}
// Increment the where clause index
index++
// Store the matched docs in result
holder.results[i] = &dataloader.Result{Data: docs}
}
}
func (holder *resultsHolder) fillErrorMessage(err error) {
holder.Lock()
length := len(holder.results)
for i := 0; i < length; i++ {
if holder.results[i] == nil {
holder.results[i] = &dataloader.Result{Error: err}
}
}
holder.Unlock()
}
type loaderMap struct {
lock sync.Mutex
m map[string]*dataloader.Loader
}
func newLoaderMap() *loaderMap {
return &loaderMap{m: map[string]*dataloader.Loader{}}
}
func (l *loaderMap) get(key string, graph *Module) *dataloader.Loader {
l.lock.Lock()
defer l.lock.Unlock()
if _, ok := l.m[key]; !ok {
l.m[key] = graph.createLoader()
}
return l.m[key]
}
func (graph *Module) createLoader() *dataloader.Loader {
// DataLoaderBatchFn is the batch function of the data loader
cache := &dataloader.NoCache{}
return dataloader.NewBatchedLoader(graph.dataLoaderBatchFn, dataloader.WithCache(cache))
}
func (graph *Module) dataLoaderBatchFn(c context.Context, keys dataloader.Keys) []*dataloader.Result {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(c)
defer cancel()
var dbType, col string
// Return if there are no keys
if len(keys) == 0 {
return []*dataloader.Result{}
}
holder := resultsHolder{
results: make([]*dataloader.Result, len(keys)),
whereClauses: []interface{}{},
}
for index, key := range keys {
req := key.(model.ReadRequestKey)
dbType = req.DBType
col = req.Col
// Execute query immediately if it has options
if req.HasOptions {
// Add task to wait group
wg.Add(1)
go func(i int) {
defer wg.Done()
// Execute the query
res, err := graph.crud.Read(ctx, req.DBType, graph.project, req.Col, &req.Req)
if err != nil {
// Cancel the context and add the error response to the result
cancel()
holder.addResult(i, &dataloader.Result{Error: err})
return
}
// Add the response to the result
holder.addResult(i, &dataloader.Result{Data: res})
}(index)
// Continue to the next key
continue
}
// Append the where clause to the list
holder.addWhereClause(req.Req.Find)
}
// Wait for all results to be done
wg.Wait()
// Prepare a merged request
req := model.ReadRequest{Find: map[string]interface{}{"$or": holder.getWhereClauses()}, Operation: utils.All, Options: &model.ReadOptions{}}
// Fire the merged request
res, err := graph.crud.Read(ctx, dbType, graph.project, col, &req)
if err != nil {
holder.fillErrorMessage(err)
} else {
holder.fillResults(res.([]interface{}))
}
// do some async work to get data for specified keys
// append to this list resolved values
return holder.getResults()
}