/
agentops_subdoc.go
741 lines (631 loc) · 20.7 KB
/
agentops_subdoc.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package gocbcore
import (
"encoding/binary"
"github.com/opentracing/opentracing-go"
)
// SubDocResult encapsulates the results from a single sub-document operation.
type SubDocResult struct {
Err error
Value []byte
}
// GetInOptions encapsulates the parameters for a GetInEx operation.
type GetInOptions struct {
Key []byte
Path string
Flags SubdocFlag
CollectionName string
ScopeName string
TraceContext opentracing.SpanContext
}
// GetInResult encapsulates the result of a GetInEx operation.
type GetInResult struct {
Value []byte
Cas Cas
}
// GetInExCallback is invoked upon completion of a GetInEx operation.
type GetInExCallback func(*GetInResult, error)
// GetInEx retrieves the value at a particular path within a JSON document.
func (agent *Agent) GetInEx(opts GetInOptions, cb GetInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetInEx", nil)
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
tracer.Finish()
cb(&GetInResult{
Value: resp.Value,
Cas: Cas(resp.Cas),
}, nil)
}
pathBytes := []byte(opts.Path)
extraBuf := make([]byte, 3)
binary.BigEndian.PutUint16(extraBuf[0:], uint16(len(pathBytes)))
extraBuf[2] = uint8(opts.Flags)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdSubDocGet,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: pathBytes,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// ExistsInOptions encapsulates the parameters for a ExistsInEx operation.
type ExistsInOptions struct {
Key []byte
Path string
Flags SubdocFlag
CollectionName string
ScopeName string
TraceContext opentracing.SpanContext
}
// ExistsInResult encapsulates the result of a ExistsInEx operation.
type ExistsInResult struct {
Cas Cas
}
// ExistsInExCallback is invoked upon completion of a ExistsInEx operation.
type ExistsInExCallback func(*ExistsInResult, error)
// ExistsInEx returns whether a particular path exists within a document.
func (agent *Agent) ExistsInEx(opts ExistsInOptions, cb ExistsInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("ExistsInEx", nil)
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
tracer.Finish()
cb(&ExistsInResult{
Cas: Cas(resp.Cas),
}, nil)
}
pathBytes := []byte(opts.Path)
extraBuf := make([]byte, 3)
binary.BigEndian.PutUint16(extraBuf[0:], uint16(len(pathBytes)))
extraBuf[2] = uint8(opts.Flags)
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdSubDocExists,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: pathBytes,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// StoreInOptions encapsulates the parameters for a SetInEx, AddInEx, ReplaceInEx,
// PushFrontInEx, PushBackInEx, ArrayInsertInEx or AddUniqueInEx operation.
type StoreInOptions struct {
Key []byte
Path string
Value []byte
Flags SubdocFlag
Cas Cas
Expiry uint32
CollectionName string
ScopeName string
TraceContext opentracing.SpanContext
DurabilityLevel DurabilityLevel
DurabilityLevelTimeout uint16
}
// StoreInResult encapsulates the result of a SetInEx, AddInEx, ReplaceInEx,
// PushFrontInEx, PushBackInEx, ArrayInsertInEx or AddUniqueInEx operation.
type StoreInResult struct {
Cas Cas
MutationToken MutationToken
}
// StoreInExCallback is invoked upon completion of a SetInEx, AddInEx,
// ReplaceInEx, PushFrontInEx, PushBackInEx, ArrayInsertInEx or
// AddUniqueInEx operation.
type StoreInExCallback func(*StoreInResult, error)
func (agent *Agent) storeInEx(opName string, opcode commandCode, opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace(opName, nil)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&StoreInResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
magic := reqMagic
var flexibleFrameExtras *memdFrameExtras
if opts.DurabilityLevel > 0 {
flexibleFrameExtras = &memdFrameExtras{}
flexibleFrameExtras.DurabilityLevel = opts.DurabilityLevel
flexibleFrameExtras.DurabilityLevelTimeout = opts.DurabilityLevelTimeout
magic = altReqMagic
}
pathBytes := []byte(opts.Path)
valueBuf := make([]byte, len(pathBytes)+len(opts.Value))
copy(valueBuf[0:], pathBytes)
copy(valueBuf[len(pathBytes):], opts.Value)
var extraBuf []byte
if opts.Expiry != 0 {
extraBuf = make([]byte, 7)
} else {
extraBuf = make([]byte, 3)
}
binary.BigEndian.PutUint16(extraBuf[0:], uint16(len(pathBytes)))
extraBuf[2] = uint8(opts.Flags)
if len(extraBuf) >= 7 {
binary.BigEndian.PutUint32(extraBuf[3:], opts.Expiry)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: magic,
Opcode: opcode,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
FrameExtras: flexibleFrameExtras,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// SetInEx sets the value at a path within a document.
func (agent *Agent) SetInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("SetInEx", cmdSubDocDictSet, opts, cb)
}
// AddInEx adds a value at the path within a document. This method
// works like SetIn, but only only succeeds if the path does not
// currently exist.
func (agent *Agent) AddInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("AddInEx", cmdSubDocDictAdd, opts, cb)
}
// ReplaceInEx replaces the value at the path within a document.
// This method works like SetIn, but only only succeeds
// if the path currently exists.
func (agent *Agent) ReplaceInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("ReplaceInEx", cmdSubDocReplace, opts, cb)
}
// PushFrontInEx pushes an entry to the front of an array at a path within a document.
func (agent *Agent) PushFrontInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("PushFrontInEx", cmdSubDocArrayPushFirst, opts, cb)
}
// PushBackInEx pushes an entry to the back of an array at a path within a document.
func (agent *Agent) PushBackInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("PushBackInEx", cmdSubDocArrayPushLast, opts, cb)
}
// ArrayInsertInEx inserts an entry to an array at a path within the document.
func (agent *Agent) ArrayInsertInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("ArrayInsertInEx", cmdSubDocArrayInsert, opts, cb)
}
// AddUniqueInEx adds an entry to an array at a path but only if the value doesn't already exist in the array.
func (agent *Agent) AddUniqueInEx(opts StoreInOptions, cb StoreInExCallback) (PendingOp, error) {
return agent.storeInEx("AddUniqueInEx", cmdSubDocArrayAddUnique, opts, cb)
}
// CounterInOptions encapsulates the parameters for a CounterInEx operation.
type CounterInOptions StoreInOptions
// CounterInResult encapsulates the result of a CounterInEx operation.
type CounterInResult struct {
Value []byte
Cas Cas
MutationToken MutationToken
}
// CounterInExCallback is invoked upon completion of a CounterInEx operation.
type CounterInExCallback func(*CounterInResult, error)
// CounterInEx performs an arithmetic add or subtract on a value at a path in the document.
func (agent *Agent) CounterInEx(opts CounterInOptions, cb CounterInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("CounterInEx", nil)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&CounterInResult{
Value: resp.Value,
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
pathBytes := []byte(opts.Path)
valueBuf := make([]byte, len(pathBytes)+len(opts.Value))
copy(valueBuf[0:], pathBytes)
copy(valueBuf[len(pathBytes):], opts.Value)
magic := reqMagic
var flexibleFrameExtras *memdFrameExtras
if opts.DurabilityLevel > 0 {
flexibleFrameExtras = &memdFrameExtras{}
flexibleFrameExtras.DurabilityLevel = opts.DurabilityLevel
flexibleFrameExtras.DurabilityLevelTimeout = opts.DurabilityLevelTimeout
magic = altReqMagic
}
var extraBuf []byte
if opts.Expiry != 0 {
extraBuf = make([]byte, 7)
} else {
extraBuf = make([]byte, 3)
}
binary.BigEndian.PutUint16(extraBuf[0:], uint16(len(pathBytes)))
extraBuf[2] = uint8(opts.Flags)
if len(extraBuf) >= 7 {
binary.BigEndian.PutUint32(extraBuf[3:], opts.Expiry)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: magic,
Opcode: cmdSubDocCounter,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
FrameExtras: flexibleFrameExtras,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// DeleteInOptions encapsulates the parameters for a DeleteInEx operation.
type DeleteInOptions struct {
Key []byte
Path string
Cas Cas
Expiry uint32
Flags SubdocFlag
CollectionName string
ScopeName string
DurabilityLevel DurabilityLevel
DurabilityLevelTimeout uint16
TraceContext opentracing.SpanContext
}
// DeleteInResult encapsulates the result of a DeleteInEx operation.
type DeleteInResult struct {
Cas Cas
MutationToken MutationToken
}
// DeleteInExCallback is invoked upon completion of a DeleteInEx operation.
type DeleteInExCallback func(*DeleteInResult, error)
// DeleteInEx removes the value at a path within the document.
func (agent *Agent) DeleteInEx(opts DeleteInOptions, cb DeleteInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("DeleteInEx", nil)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
tracer.Finish()
cb(nil, err)
return
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&DeleteInResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
}, nil)
}
pathBytes := []byte(opts.Path)
magic := reqMagic
var flexibleFrameExtras *memdFrameExtras
if opts.DurabilityLevel > 0 {
flexibleFrameExtras = &memdFrameExtras{}
flexibleFrameExtras.DurabilityLevel = opts.DurabilityLevel
flexibleFrameExtras.DurabilityLevelTimeout = opts.DurabilityLevelTimeout
magic = altReqMagic
}
var extraBuf []byte
if opts.Expiry != 0 {
extraBuf = make([]byte, 7)
} else {
extraBuf = make([]byte, 3)
}
binary.BigEndian.PutUint16(extraBuf[0:], uint16(len(pathBytes)))
extraBuf[2] = uint8(opts.Flags)
if len(extraBuf) >= 7 {
binary.BigEndian.PutUint32(extraBuf[3:], opts.Expiry)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: magic,
Opcode: cmdSubDocDelete,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: pathBytes,
FrameExtras: flexibleFrameExtras,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// SubDocOp defines a per-operation structure to be passed to MutateIn
// or LookupIn for performing many sub-document operations.
type SubDocOp struct {
Op SubDocOpType
Flags SubdocFlag
Path string
Value []byte
}
// LookupInOptions encapsulates the parameters for a LookupInEx operation.
type LookupInOptions struct {
Key []byte
Flags SubdocDocFlag
Ops []SubDocOp
CollectionName string
ScopeName string
TraceContext opentracing.SpanContext
}
// LookupInResult encapsulates the result of a LookupInEx operation.
type LookupInResult struct {
Cas Cas
Ops []SubDocResult
}
// LookupInExCallback is invoked upon completion of a LookupInEx operation.
type LookupInExCallback func(*LookupInResult, error)
// LookupInEx performs a multiple-lookup sub-document operation on a document.
func (agent *Agent) LookupInEx(opts LookupInOptions, cb LookupInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("LookupInEx", opts.TraceContext)
results := make([]SubDocResult, len(opts.Ops))
handler := func(resp *memdQResponse, _ *memdQRequest, err error) {
if err != nil &&
!IsErrorStatus(err, StatusSubDocMultiPathFailureDeleted) &&
!IsErrorStatus(err, StatusSubDocSuccessDeleted) &&
!IsErrorStatus(err, StatusSubDocBadMulti) {
tracer.Finish()
cb(nil, err)
return
}
respIter := 0
for i := range results {
if respIter+6 > len(resp.Value) {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
resError := StatusCode(binary.BigEndian.Uint16(resp.Value[respIter+0:]))
resValueLen := int(binary.BigEndian.Uint32(resp.Value[respIter+2:]))
if respIter+6+resValueLen > len(resp.Value) {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
results[i].Err = agent.makeBasicMemdError(resError, resp.Opaque)
results[i].Value = resp.Value[respIter+6 : respIter+6+resValueLen]
respIter += 6 + resValueLen
}
tracer.Finish()
cb(&LookupInResult{
Cas: Cas(resp.Cas),
Ops: results,
}, err)
}
pathBytesList := make([][]byte, len(opts.Ops))
pathBytesTotal := 0
for i, op := range opts.Ops {
pathBytes := []byte(op.Path)
pathBytesList[i] = pathBytes
pathBytesTotal += len(pathBytes)
}
valueBuf := make([]byte, len(opts.Ops)*4+pathBytesTotal)
valueIter := 0
for i, op := range opts.Ops {
if op.Op != SubDocOpGet && op.Op != SubDocOpExists &&
op.Op != SubDocOpGetDoc && op.Op != SubDocOpGetCount {
return nil, ErrInvalidArgs
}
if op.Value != nil {
return nil, ErrInvalidArgs
}
pathBytes := pathBytesList[i]
pathBytesLen := len(pathBytes)
valueBuf[valueIter+0] = uint8(op.Op)
valueBuf[valueIter+1] = uint8(op.Flags)
binary.BigEndian.PutUint16(valueBuf[valueIter+2:], uint16(pathBytesLen))
copy(valueBuf[valueIter+4:], pathBytes)
valueIter += 4 + pathBytesLen
}
var extraBuf []byte
if opts.Flags != 0 {
extraBuf = append(extraBuf, uint8(opts.Flags))
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdSubDocMultiLookup,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}
// MutateInOptions encapsulates the parameters for a MutateInEx operation.
type MutateInOptions struct {
Key []byte
Flags SubdocDocFlag
Cas Cas
Expiry uint32
Ops []SubDocOp
CollectionName string
ScopeName string
DurabilityLevel DurabilityLevel
DurabilityLevelTimeout uint16
TraceContext opentracing.SpanContext
}
// MutateInResult encapsulates the result of a MutateInEx operation.
type MutateInResult struct {
Cas Cas
MutationToken MutationToken
Ops []SubDocResult
}
// MutateInExCallback is invoked upon completion of a MutateInEx operation.
type MutateInExCallback func(*MutateInResult, error)
// MutateInEx performs a multiple-mutation sub-document operation on a document.
func (agent *Agent) MutateInEx(opts MutateInOptions, cb MutateInExCallback) (PendingOp, error) {
tracer := agent.createOpTrace("MutateInEx", opts.TraceContext)
results := make([]SubDocResult, len(opts.Ops))
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil &&
!IsErrorStatus(err, StatusSubDocSuccessDeleted) &&
!IsErrorStatus(err, StatusSubDocBadMulti) {
tracer.Finish()
cb(nil, err)
return
}
if IsErrorStatus(err, StatusSubDocBadMulti) {
if len(resp.Value) != 3 {
tracer.Finish()
cb(nil, ErrProtocol)
return
}
opIndex := int(resp.Value[0])
resError := StatusCode(binary.BigEndian.Uint16(resp.Value[1:]))
err := SubDocMutateError{
Err: agent.makeBasicMemdError(resError, resp.Opaque),
OpIndex: opIndex,
}
tracer.Finish()
cb(nil, err)
return
}
for readPos := uint32(0); readPos < uint32(len(resp.Value)); {
opIndex := int(resp.Value[readPos+0])
opStatus := StatusCode(binary.BigEndian.Uint16(resp.Value[readPos+1:]))
results[opIndex].Err = agent.makeBasicMemdError(opStatus, resp.Opaque)
readPos += 3
if opStatus == StatusSuccess {
valLength := binary.BigEndian.Uint32(resp.Value[readPos:])
results[opIndex].Value = resp.Value[readPos+4 : readPos+4+valLength]
readPos += 4 + valLength
}
}
mutToken := MutationToken{}
if len(resp.Extras) >= 16 {
mutToken.VbId = req.Vbucket
mutToken.VbUuid = VbUuid(binary.BigEndian.Uint64(resp.Extras[0:]))
mutToken.SeqNo = SeqNo(binary.BigEndian.Uint64(resp.Extras[8:]))
}
tracer.Finish()
cb(&MutateInResult{
Cas: Cas(resp.Cas),
MutationToken: mutToken,
Ops: results,
}, nil)
}
magic := reqMagic
var flexibleFrameExtras *memdFrameExtras
if opts.DurabilityLevel > 0 {
if agent.durabilityLevelStatus == 2 {
return nil, ErrEnhancedDurabilityUnsupported
}
flexibleFrameExtras = &memdFrameExtras{}
flexibleFrameExtras.DurabilityLevel = opts.DurabilityLevel
flexibleFrameExtras.DurabilityLevelTimeout = opts.DurabilityLevelTimeout
magic = altReqMagic
}
pathBytesList := make([][]byte, len(opts.Ops))
pathBytesTotal := 0
valueBytesTotal := 0
for i, op := range opts.Ops {
pathBytes := []byte(op.Path)
pathBytesList[i] = pathBytes
pathBytesTotal += len(pathBytes)
valueBytesTotal += len(op.Value)
}
valueBuf := make([]byte, len(opts.Ops)*8+pathBytesTotal+valueBytesTotal)
valueIter := 0
for i, op := range opts.Ops {
if op.Op != SubDocOpDictAdd && op.Op != SubDocOpDictSet &&
op.Op != SubDocOpDelete && op.Op != SubDocOpReplace &&
op.Op != SubDocOpArrayPushLast && op.Op != SubDocOpArrayPushFirst &&
op.Op != SubDocOpArrayInsert && op.Op != SubDocOpArrayAddUnique &&
op.Op != SubDocOpCounter && op.Op != SubDocOpSetDoc &&
op.Op != SubDocOpAddDoc && op.Op != SubDocOpDeleteDoc {
return nil, ErrInvalidArgs
}
pathBytes := pathBytesList[i]
pathBytesLen := len(pathBytes)
valueBytesLen := len(op.Value)
valueBuf[valueIter+0] = uint8(op.Op)
valueBuf[valueIter+1] = uint8(op.Flags)
binary.BigEndian.PutUint16(valueBuf[valueIter+2:], uint16(pathBytesLen))
binary.BigEndian.PutUint32(valueBuf[valueIter+4:], uint32(valueBytesLen))
copy(valueBuf[valueIter+8:], pathBytes)
copy(valueBuf[valueIter+8+pathBytesLen:], op.Value)
valueIter += 8 + pathBytesLen + valueBytesLen
}
var extraBuf []byte
if opts.Expiry != 0 {
tmpBuf := make([]byte, 4)
binary.BigEndian.PutUint32(tmpBuf[0:], opts.Expiry)
extraBuf = append(extraBuf, tmpBuf...)
}
if opts.Flags != 0 {
extraBuf = append(extraBuf, uint8(opts.Flags))
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: magic,
Opcode: cmdSubDocMultiMutation,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
FrameExtras: flexibleFrameExtras,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
}
return agent.dispatchOp(req)
}