-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
396 lines (355 loc) · 7.6 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package red
import (
"errors"
"fmt"
"sync"
"github.com/alxarch/red/resp"
)
// Batch is a batch of redis commands bound to replies
type Batch struct {
batchAPI
}
// Pipeline is a connection manager for easy pipelining
type Pipeline struct {
Batch
mc managedConn
}
var pipelinePool = sync.Pool{
New: func() interface{} {
return new(Pipeline)
},
}
func getPipeline() *Pipeline {
return pipelinePool.Get().(*Pipeline)
}
func putPipeline(p *Pipeline) {
if p == nil {
return
}
_ = p.Close()
p.Reset()
pipelinePool.Put(p)
}
// Pipeline manages a connection for easier usage
func (c *Conn) Pipeline() (*Pipeline, error) {
if err := c.Err(); err != nil {
return nil, err
}
if c.managed {
return nil, errConnManaged
}
if c.state.CountReplies() > 0 {
return nil, ErrReplyPending
}
c.managed = true
p := Pipeline{
mc: managedConn{
Conn: c,
},
}
return &p, nil
}
// Sync syncs all queued commands in the pipeline
func (p *Pipeline) Sync() error {
if err := p.mc.Err(); err != nil {
return err
}
p.mc.Conn.managed = false
if err := p.mc.doBatch(&p.batchAPI); err != nil {
_ = p.Close()
return err
}
p.mc.Conn.managed = true
return nil
}
// Close closes a pipeline releasing the managed connection
func (p *Pipeline) Close() error {
if p == nil {
return nil
}
err := p.mc.Close()
return err
}
// Tx is a MULTI/EXEC transaction block
type Tx struct {
batchAPI
}
// Multi queues a MULTI/EXEC transaction
func (b *Batch) Multi(tx *Tx) *ReplyTX {
reply := ReplyTX{
batchReply: batchReply{
dest: tx.replies,
},
}
tx.replies = nil
_ = b.w.WriteCommand("MULTI")
b.replies = append(b.replies, &reply.batchReply)
_ = tx.w.WriteTo(&b.w)
_ = b.w.WriteCommand("EXEC")
tx.reset()
return &reply
}
var batchPool = sync.Pool{
New: func() interface{} {
return new(Batch)
},
}
// AcquireBatch gets an empty Batch from a package-wide pool
func AcquireBatch() *Batch {
return batchPool.Get().(*Batch)
}
// ReleaseBatch releases a Batch to a package-wide pool
func ReleaseBatch(b *Batch) {
if b == nil {
return
}
b.Reset()
batchPool.Put(b)
}
type batchAPI struct {
args ArgBuilder
w batchWriter
replies []*batchReply
}
func (b *batchAPI) Do(cmd string, args ...Arg) *ReplyAny {
b.args.Append(args...)
return b.doAny(cmd)
}
// Reset clears all pending commands
func (b *Batch) Reset() {
b.batchAPI.reset()
}
func (b *batchAPI) reset() {
b.args.Clear()
b.w.Reset()
for i := range b.replies {
b.replies[i] = nil
}
*b = batchAPI{
args: b.args,
w: b.w,
replies: b.replies[:0],
}
}
func (b *batchAPI) do(cmd string, reply *batchReply) {
_ = b.w.WriteCommand(cmd, b.args.Args()...)
b.args.Reset()
b.replies = append(b.replies, reply)
}
func (b *batchAPI) doInteger(cmd string) *ReplyInteger {
reply := ReplyInteger{}
reply.Bind(&reply.n)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doAny(cmd string) *ReplyAny {
reply := ReplyAny{}
reply.Bind(&reply.value)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doSimpleStringOK(cmd string, mode Mode) *ReplyOK {
reply := ReplyOK{ok: AssertOK{Mode: mode}}
reply.Bind(&reply.ok)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doBulkString(cmd string) *ReplyBulkString {
reply := ReplyBulkString{}
reply.Bind(&reply.str)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doSimpleString(cmd string) *ReplySimpleString {
reply := ReplySimpleString{}
reply.Bind(&reply.status)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doBulkStringArray(cmd string) *ReplyBulkStringArray {
reply := ReplyBulkStringArray{}
reply.Bind(&reply.values)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doFloat(cmd string) *ReplyFloat {
reply := ReplyFloat{}
reply.Bind(&reply.f)
b.do(cmd, &reply.batchReply)
return &reply
}
func (b *batchAPI) doBool(cmd string) *ReplyBool {
reply := ReplyBool{}
reply.Bind(&reply.n)
b.do(cmd, &reply.batchReply)
return &reply
}
// DoBatch executes a batch
func (c *Conn) DoBatch(b *Batch) error {
return c.doBatch(&b.batchAPI)
}
// ErrReplyPending is the error of a reply until a `Client.Sync` is called
var ErrReplyPending = errors.New("Reply pending")
// ErrDiscarded is the error of a reply if it's part of a transaction that got discarded
var ErrDiscarded = errors.New("MULTI/EXEC Transaction discarded")
func (c *Conn) doBatch(b *batchAPI) error {
if err := c.Err(); err != nil {
return err
}
if c.state.CountReplies() > 0 {
return ErrReplyPending
}
defer b.reset()
if err := b.w.WriteTo(c); err != nil {
return err
}
if c.state.IsMulti() {
if err := c.WriteCommand("EXEC"); err != nil {
return err
}
}
return c.scanBatch(b.replies)
}
type batchExec []*batchReply
func (tx *batchExec) UnmarshalRESP(value resp.Value) (err error) {
queued := *tx
var execAbort resp.Error
switch {
case value.Len() == int64(len(queued)):
iter := value.Iter()
defer iter.Close()
for _, reply := range queued {
v := iter.Value()
if reply.dest != nil {
reply.err = v.Decode(reply.dest)
}
iter.Next()
}
case value.NullArray():
err = resp.ErrNull
case execAbort.UnmarshalRESP(value) == nil:
err = execAbort
default:
err = fmt.Errorf("Invalid EXEC reply %v", value.Any())
}
if err != nil {
for _, reply := range queued {
reply.reject(err)
}
}
return
}
func unwrapDecodeError(err error) error {
if err, ok := err.(*resp.DecodeError); ok {
return err.Reason
}
return err
}
func (c *Conn) scanBatch(replies []*batchReply) error {
if len(replies) == 0 {
return nil
}
if err := c.flush(); err != nil {
for _, reply := range replies {
reply.reject(err)
}
return err
}
// var tx batchTx
for i := 0; 0 <= i && i < len(replies); i++ {
reply := replies[i]
if queued, ok := reply.dest.([]*batchReply); ok {
ok := AssertOK{}
if err := c.Scan(&ok); err != nil {
err = unwrapDecodeError(err)
for i := range queued {
queued[i].reject(err)
}
reply.reject(err)
continue
}
for _, reply := range queued {
q := assertQueued{}
if err := c.Scan(&q); err != nil {
err = unwrapDecodeError(err)
reply.reject(err)
}
}
tx := batchExec(queued)
reply.err = unwrapDecodeError(c.Scan(&tx))
} else {
reply.err = unwrapDecodeError(c.Scan(reply.dest))
}
if err := c.Err(); err != nil {
for _, reply := range replies[i:] {
reply.reject(err)
}
return err
}
}
return nil
}
type batchWriter struct {
args []Arg
commands []batchCmd
}
func (w *batchWriter) WriteCommand(name string, args ...Arg) error {
argv := len(w.args)
w.args = append(w.args, args...)
w.commands = append(w.commands, batchCmd{
name: name,
argv: uint32(argv),
argc: uint32(len(args)),
})
return nil
}
func (w *batchWriter) Reset() {
for i := range w.args {
w.args[i] = Arg{}
}
for i := range w.commands {
w.commands[i] = batchCmd{}
}
*w = batchWriter{
args: w.args[:0],
commands: w.commands[:0],
}
}
func (w *batchWriter) WriteTo(dest CommandWriter) error {
for i := range w.commands {
cmd := &w.commands[i]
args := cmd.Args(w.args)
if err := dest.WriteCommand(cmd.name, args...); err != nil {
return err
}
}
return nil
}
type batchCmd struct {
name string
argv, argc uint32
}
func (b *batchCmd) Args(args []Arg) []Arg {
if b.argv < uint32(len(args)) {
args = args[b.argv:]
if b.argc <= uint32(len(args)) {
return args[:b.argc]
}
}
return nil
}
type batchReply struct {
dest interface{}
err error
}
func (r *batchReply) reject(err error) {
if r.err == nil {
r.err = err
}
}
func (r *batchReply) Err() error {
return r.err
}
func (r *batchReply) Bind(dest interface{}) {
r.dest = dest
}