This repository has been archived by the owner on Jul 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
batch_operation.go
169 lines (144 loc) · 3.88 KB
/
batch_operation.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
package keyvaluestore
type DeleteResult interface {
Result() (bool, error)
}
type GetResult interface {
Result() (*string, error)
}
type SMembersResult interface {
Result() ([]string, error)
}
type ErrorResult interface {
Result() error
}
type BatchOperation interface {
Get(key string) GetResult
Delete(key string) DeleteResult
Set(key string, value interface{}) ErrorResult
SMembers(key string) SMembersResult
SAdd(key string, member interface{}, members ...interface{}) ErrorResult
SRem(key string, member interface{}, members ...interface{}) ErrorResult
ZAdd(key string, member interface{}, score float64) ErrorResult
ZRem(key string, member interface{}) ErrorResult
Exec() error
}
// FallbackBatchOperation provides a suitable fallback for stores that don't supported optimized
// batching.
type FallbackBatchOperation struct {
Backend Backend
fs []func()
firstError error
}
type fboGetResult struct {
value *string
err error
}
func (r *fboGetResult) Result() (*string, error) {
return r.value, r.err
}
func (op *FallbackBatchOperation) Get(key string) GetResult {
result := &fboGetResult{}
op.fs = append(op.fs, func() {
result.value, result.err = op.Backend.Get(key)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
type fboErrorResult struct {
err error
}
func (r *fboErrorResult) Result() error {
return r.err
}
func (op *FallbackBatchOperation) Set(key string, value interface{}) ErrorResult {
result := &fboErrorResult{}
op.fs = append(op.fs, func() {
result.err = op.Backend.Set(key, value)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
type fboDeleteResult struct {
success bool
err error
}
func (r *fboDeleteResult) Result() (bool, error) {
return r.success, r.err
}
func (op *FallbackBatchOperation) Delete(key string) DeleteResult {
result := &fboDeleteResult{}
op.fs = append(op.fs, func() {
result.success, result.err = op.Backend.Delete(key)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
type fboSMembersResult struct {
value []string
err error
}
func (r *fboSMembersResult) Result() ([]string, error) {
return r.value, r.err
}
func (op *FallbackBatchOperation) SMembers(key string) SMembersResult {
result := &fboSMembersResult{}
op.fs = append(op.fs, func() {
result.value, result.err = op.Backend.SMembers(key)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
func (op *FallbackBatchOperation) SAdd(key string, member interface{}, members ...interface{}) ErrorResult {
result := &fboErrorResult{}
op.fs = append(op.fs, func() {
result.err = op.Backend.SAdd(key, member, members...)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
func (op *FallbackBatchOperation) SRem(key string, member interface{}, members ...interface{}) ErrorResult {
result := &fboErrorResult{}
op.fs = append(op.fs, func() {
result.err = op.Backend.SRem(key, member, members...)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
func (op *FallbackBatchOperation) ZAdd(key string, member interface{}, score float64) ErrorResult {
result := &fboErrorResult{}
op.fs = append(op.fs, func() {
result.err = op.Backend.ZAdd(key, member, score)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
func (op *FallbackBatchOperation) ZRem(key string, member interface{}) ErrorResult {
result := &fboErrorResult{}
op.fs = append(op.fs, func() {
result.err = op.Backend.ZRem(key, member)
if result.err != nil && op.firstError == nil {
op.firstError = result.err
}
})
return result
}
func (op *FallbackBatchOperation) Exec() error {
for _, f := range op.fs {
f()
}
return op.firstError
}