forked from mercari/datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
132 lines (114 loc) · 2.66 KB
/
batch.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
package boom
import (
"sync"
"go.mercari.io/datastore"
)
// Batch can queue operations on Datastore and process them in batch.
// Batch does nothing until you call Exec().
// This helps to reduce the number of RPCs.
type Batch struct {
m sync.Mutex
bm *Boom
b *datastore.Batch
earlyErrors []error
}
// Boom object that is the source of the Batch object is returned.
func (b *Batch) Boom() *Boom {
return b.bm
}
// Put Entity operation into the queue.
// This operation doesn't Put to Datastore immediately.
// If a h is provided, it passes the processing result to the handler, and treats the return value as the value of the result of Putting.
func (b *Batch) Put(src interface{}, h datastore.BatchPutHandler) {
keys, err := b.bm.extractKeys([]interface{}{src})
if err != nil {
if h != nil {
err = h(nil, err)
}
if err != nil {
b.m.Lock()
b.earlyErrors = append(b.earlyErrors, err)
b.m.Unlock()
}
return
}
b.b.Put(keys[0], src, func(key datastore.Key, err error) error {
if err != nil {
if h != nil {
err = h(key, err)
}
return err
}
err = b.bm.setStructKey(src, key)
if err != nil {
if h != nil {
err = h(key, err)
}
if err != nil {
b.m.Lock()
b.earlyErrors = append(b.earlyErrors, err)
b.m.Unlock()
}
return err
}
if h != nil {
return h(key, nil)
}
return nil
})
}
// Get Entity operation into the queue.
func (b *Batch) Get(dst interface{}, h datastore.BatchErrHandler) {
keys, err := b.bm.extractKeys([]interface{}{dst})
if err != nil {
if h != nil {
err = h(err)
}
if err != nil {
b.m.Lock()
b.earlyErrors = append(b.earlyErrors, err)
b.m.Unlock()
}
return
}
b.b.Get(keys[0], dst, h)
}
// Delete Entity operation into the queue.
func (b *Batch) Delete(dst interface{}, h datastore.BatchErrHandler) {
keys, err := b.bm.extractKeys([]interface{}{dst})
if err != nil {
if h != nil {
err = h(err)
}
if err != nil {
b.m.Lock()
b.earlyErrors = append(b.earlyErrors, err)
b.m.Unlock()
}
return
}
b.b.Delete(keys[0], h)
}
// Exec will perform all the processing that was queued.
// This process is done recursively until the queue is empty.
// The return value may be MultiError, but the order of contents is not guaranteed.
func (b *Batch) Exec() error {
err := b.b.Exec(b.bm.Context)
b.m.Lock()
defer b.m.Unlock()
if merr, ok := err.(datastore.MultiError); ok {
merr = append(merr, b.earlyErrors...)
b.earlyErrors = nil
if len(merr) == 0 {
return nil
}
return merr
} else if err != nil {
return err
} else if len(b.earlyErrors) != 0 {
errs := b.earlyErrors
b.earlyErrors = nil
return datastore.MultiError(errs)
}
return nil
}