-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
325 lines (287 loc) · 8.37 KB
/
api.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package firestorm
import (
"cloud.google.com/go/firestore"
"context"
"errors"
"log"
"reflect"
"strings"
"sync"
)
var transCacheKey = contextKey("transactionCache")
// DoInTransaction wraps any updates that needs to run in a transaction.
// Use the transaction context tctx for any calls that need to be part of the transaction.
// Do reads before writes as required by firestore
func (fsc *FSClient) DoInTransaction(ctx context.Context, f func(tctx context.Context) error) error {
// if nested transaction - reuse existing transaction and cache
if _, ok := getTransaction(ctx); ok {
return f(ctx)
}
err := fsc.Client.RunTransaction(ctx, func(ctx context.Context, t *firestore.Transaction) error {
// add a new cache to context
cache := newDefaultCache()
tctx := context.WithValue(ctx, transactionCtxKey, t)
tctx = context.WithValue(tctx, SessionCacheKey, make(map[string]EntityMap))
tctx = context.WithValue(tctx, transCacheKey, newCacheWrapper(fsc.Client, cache, nil))
// do the updates
if err := f(tctx); err != nil {
return err
}
// update cache with transaction cache. For now we just delete all modified keys
if err := fsc.getCache(ctx).SetMulti(ctx, cache.getSetRec(tctx)); err != nil {
log.Printf("Could not set values in cache: %#v", err)
}
if err := fsc.getCache(ctx).DeleteMulti(ctx, cache.getDeleteRec(tctx)); err != nil {
log.Printf("Could not delete keys from cache: %#v", err)
}
return nil
})
return err
}
func (fsc *FSClient) getEntities(ctx context.Context, req *Request, sliceVal reflect.Value) func() ([]interface{}, error) {
slice := sliceVal
result := make([]interface{}, 0, slice.Len())
asyncFunc := func() error {
var nfErr error
refs := make([]*firestore.DocumentRef, slice.Len())
for i := 0; i < slice.Len(); i++ {
refs[i] = req.ToRef(slice.Index(i).Interface())
}
crefs, err := fsc.getCachedEntities(ctx, refs)
if err != nil {
return err
}
resolver := newResolver(fsc, req.loadPaths...)
res, err := resolver.ResolveCacheRef(ctx, crefs)
if err != nil {
if err, ok := err.(NotFoundError); ok {
nfErr = err
} else {
return err
}
}
for i, v := range res {
if len(v) > 0 {
fsc.MapFromDB.MapToStruct(v, slice.Index(i).Interface())
result = append(result, slice.Index(i).Interface())
}
}
return nfErr
}
af := runAsync(ctx, asyncFunc)
return func() (entities []interface{}, e error) {
err := af()
return result, err
}
}
func (fsc *FSClient) getCachedEntities(ctx context.Context, refs []*firestore.DocumentRef) ([]cacheRef, error) {
res := make([]cacheRef, len(refs))
load := make([]*firestore.DocumentRef, 0, len(refs))
// check cache and collect refs not loaded yet
if getMulti, err := fsc.getCache(ctx).GetMulti(ctx, refs); err != nil {
log.Printf("Cache error but continue: %+v", err)
load = append(load, refs...)
} else {
for i, ref := range refs {
if e, ok := getMulti[ref]; ok {
res[i] = e // we found it
} else {
load = append(load, ref)
}
}
}
// get the unloaded refs
docs, err := getAll(ctx, fsc.Client, load)
if err != nil {
return nil, err
}
// fill the res slice with the DB results
i := 0
multi := make(map[string]EntityMap, len(docs))
for _, doc := range docs {
ref := newCacheRef(doc.Data(), doc.Ref)
multi[doc.Ref.Path] = doc.Data()
for _, v := range res[i:] {
if v.Ref == nil {
res[i] = ref
i++
break
}
i++
}
}
if err = fsc.getCache(ctx).SetMulti(ctx, multi); err != nil {
log.Printf("Cache error but continue: %+v", err)
}
return res, nil
}
func (fsc *FSClient) queryEntities(ctx context.Context, req *Request, p firestore.Query, toSlicePtr interface{}) FutureFunc {
asyncFunc := func() error {
docs, err := query(ctx, p)
if err != nil {
return err
}
multi := make(map[string]EntityMap, len(docs))
for _, doc := range docs {
multi[doc.Ref.Path] = doc.Data()
}
if err = fsc.getCache(ctx).SetMulti(ctx, multi); err != nil {
log.Printf("Cache error but continue: %+v", err)
}
resolver := newResolver(fsc, req.loadPaths...)
res, err := resolver.ResolveDocs(ctx, docs)
if err != nil {
return err
}
return fsc.toEntities(ctx, res, toSlicePtr)
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) createEntity(ctx context.Context, req *Request, entity interface{}) FutureFunc {
asyncFunc := func() error {
m, err := fsc.MapToDB.StructToMap(entity)
if err != nil {
return err
}
ref := req.ToRef(entity)
// if we need a fixed ID use that
if req.GetID(entity) == "" {
ref = req.ToCollection(entity).NewDoc() // otherwise create new id
req.SetID(entity, ref.ID)
}
req.mapperFunc(m)
if err := create(ctx, ref, m); err != nil {
return err
}
if err := fsc.getCache(ctx).Set(ctx, ref.Path, m); err != nil {
log.Printf("Cache error but continue: %+v", err)
}
return nil
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) createEntities(ctx context.Context, req *Request, sliceVal reflect.Value) FutureFunc {
asyncFunc := func() error {
slice := sliceVal
futures := make([]FutureFunc, slice.Len())
var errs []string
// kick off all updates and collect futures
for i := 0; i < slice.Len(); i++ {
futures[i] = fsc.createEntity(ctx, req, slice.Index(i).Interface())
}
// wait for all futures to finish
for _, f := range futures {
if err := f(); err != nil {
errs = append(errs, err.Error())
}
}
// check the errors
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) updateEntity(ctx context.Context, req *Request, entity interface{}) FutureFunc {
asyncFunc := func() error {
m, err := fsc.MapToDB.StructToMap(entity)
if err != nil {
return err
}
ref := req.ToRef(entity)
req.mapperFunc(m)
if err := set(ctx, ref, m); err != nil {
return err
}
if err := fsc.getCache(ctx).Set(ctx, ref.Path, m); err != nil {
log.Printf("Cache error but continue: %+v", err)
}
return nil
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) updateEntities(ctx context.Context, req *Request, sliceVal reflect.Value) FutureFunc {
asyncFunc := func() error {
slice := sliceVal
futures := make([]FutureFunc, slice.Len())
var errs []string
// kick off all updates and collect futures
for i := 0; i < slice.Len(); i++ {
futures[i] = fsc.updateEntity(ctx, req, slice.Index(i).Interface())
}
// wait for all futures to finish
for _, f := range futures {
if err := f(); err != nil {
errs = append(errs, err.Error())
}
}
// check the errors
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) deleteEntity(ctx context.Context, req *Request, entity interface{}) FutureFunc {
asyncFunc := func() error {
ref := req.ToRef(entity)
if err := del(ctx, ref); err != nil {
return err
}
if err := fsc.getCache(ctx).Delete(ctx, ref.Path); err != nil {
log.Printf("Cache error but continue: %+v", err)
}
return nil
}
return runAsync(ctx, asyncFunc)
}
func (fsc *FSClient) deleteEntities(ctx context.Context, req *Request, sliceVal reflect.Value) FutureFunc {
asyncFunc := func() error {
slice := sliceVal
futures := make([]FutureFunc, slice.Len())
var errs []string
// kick off all updates and collect futures
for i := 0; i < slice.Len(); i++ {
futures[i] = fsc.deleteEntity(ctx, req, slice.Index(i).Interface())
}
// wait for all futures to finish
for _, f := range futures {
if err := f(); err != nil {
errs = append(errs, err.Error())
}
}
// check the errors
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
return runAsync(ctx, asyncFunc)
}
type asyncFunc func() error
// FutureFunc is a function that when called blocks until the result is ready
type FutureFunc func() error
func runAsync(ctx context.Context, fun asyncFunc) FutureFunc {
if _, ok := getTransaction(ctx); ok {
// transactions are not thread safe so just execute the func
//==================
//WARNING: DATA RACE
//Read at 0x00c0004bde90 by goroutine 99:
// cloud.google.com/go/firestore.(*Transaction).addWrites()
// /home/jens/go/pkg/mod/cloud.google.com/go@v0.28.0/firestore/transaction.go:270 +0x124
return FutureFunc(fun)
}
var err error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err = fun()
}()
return func() error {
wg.Wait()
return err
}
}