forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrudcomponent_subdoc.go
404 lines (347 loc) · 11.8 KB
/
crudcomponent_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
package gocbcore
import (
"encoding/binary"
"time"
"github.com/couchbase/gocbcore/v10/memd"
)
type subdocOpList struct {
ops []SubDocOp
indexes []int
}
func (sol *subdocOpList) Reorder(ops []SubDocOp) {
var xAttrOps []SubDocOp
var xAttrIndexes []int
var sops []SubDocOp
var opIndexes []int
for i, op := range ops {
if op.Flags&memd.SubdocFlagXattrPath != 0 {
xAttrOps = append(xAttrOps, op)
xAttrIndexes = append(xAttrIndexes, i)
} else {
sops = append(sops, op)
opIndexes = append(opIndexes, i)
}
}
sol.ops = append(xAttrOps, sops...)
sol.indexes = append(xAttrIndexes, opIndexes...)
}
func (crud *crudComponent) LookupIn(opts LookupInOptions, cb LookupInCallback) (PendingOp, error) {
tracer := crud.tracer.StartTelemeteryHandler(metricValueServiceKeyValue, "LookupIn", opts.TraceContext)
results := make([]SubDocResult, len(opts.Ops))
var subdocs subdocOpList
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil &&
!isErrorStatus(err, memd.StatusSubDocMultiPathFailureDeleted) &&
!isErrorStatus(err, memd.StatusSubDocSuccessDeleted) &&
!isErrorStatus(err, memd.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 := memd.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
}
if resError != memd.StatusSuccess {
results[subdocs.indexes[i]].Err = crud.makeSubDocError(i, resError, req, resp)
}
results[subdocs.indexes[i]].Value = resp.Value[respIter+6 : respIter+6+resValueLen]
respIter += 6 + resValueLen
}
tracer.Finish()
cb(&LookupInResult{
Cas: Cas(resp.Cas),
Ops: results,
Internal: struct{ IsDeleted bool }{
IsDeleted: isErrorStatus(err, memd.StatusSubDocSuccessDeleted) ||
isErrorStatus(err, memd.StatusSubDocMultiPathFailureDeleted),
},
}, nil)
}
subdocs.Reorder(opts.Ops)
pathBytesList := make([][]byte, len(opts.Ops))
pathBytesTotal := 0
for i, op := range subdocs.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 subdocs.ops {
if op.Op != memd.SubDocOpGet && op.Op != memd.SubDocOpExists &&
op.Op != memd.SubDocOpGetDoc && op.Op != memd.SubDocOpGetCount {
return nil, errInvalidArgument
}
if op.Value != nil {
return nil, errInvalidArgument
}
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 userFrame *memd.UserImpersonationFrame
if len(opts.User) > 0 {
userFrame = &memd.UserImpersonationFrame{
User: []byte(opts.User),
}
}
var extraBuf []byte
if opts.Flags != 0 {
extraBuf = append(extraBuf, uint8(opts.Flags))
}
if opts.RetryStrategy == nil {
opts.RetryStrategy = crud.defaultRetryStrategy
}
req := &memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSubDocMultiLookup,
Datatype: 0,
Cas: 0,
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
CollectionID: opts.CollectionID,
UserImpersonationFrame: userFrame,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
RetryStrategy: opts.RetryStrategy,
}
op, err := crud.cidMgr.Dispatch(req)
if err != nil {
tracer.Finish()
return nil, err
}
if !opts.Deadline.IsZero() {
start := time.Now()
req.SetTimer(time.AfterFunc(opts.Deadline.Sub(start), func() {
connInfo := req.ConnectionInfo()
count, reasons := req.Retries()
req.cancelWithCallbackAndFinishTracer(&TimeoutError{
InnerError: errUnambiguousTimeout,
OperationID: "LookupIn",
Opaque: req.Identifier(),
TimeObserved: time.Since(start),
RetryReasons: reasons,
RetryAttempts: count,
LastDispatchedTo: connInfo.lastDispatchedTo,
LastDispatchedFrom: connInfo.lastDispatchedFrom,
LastConnectionID: connInfo.lastConnectionID,
}, tracer)
}))
}
return op, nil
}
func (crud *crudComponent) MutateIn(opts MutateInOptions, cb MutateInCallback) (PendingOp, error) {
if len(opts.Ops) == 0 {
return nil, wrapError(errInvalidArgument, "at least one op must be present")
}
tracer := crud.tracer.StartTelemeteryHandler(metricValueServiceKeyValue, "MutateIn", opts.TraceContext)
results := make([]SubDocResult, len(opts.Ops))
var subdocs subdocOpList
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil &&
!isErrorStatus(err, memd.StatusSubDocSuccessDeleted) &&
!isErrorStatus(err, memd.StatusSubDocBadMulti) {
tracer.Finish()
cb(nil, err)
return
}
if isErrorStatus(err, memd.StatusSubDocBadMulti) {
if len(resp.Value) != 3 {
tracer.Finish()
cb(nil, errProtocol)
return
}
opIndex := int(resp.Value[0])
resError := memd.StatusCode(binary.BigEndian.Uint16(resp.Value[1:]))
err := crud.makeSubDocError(opIndex, resError, req, resp)
tracer.Finish()
cb(nil, err)
return
}
for readPos := uint32(0); readPos < uint32(len(resp.Value)); {
opIndex := int(resp.Value[readPos+0])
opStatus := memd.StatusCode(binary.BigEndian.Uint16(resp.Value[readPos+1:]))
results[subdocs.indexes[opIndex]].Err = crud.makeSubDocError(opIndex, opStatus, req, resp)
readPos += 3
if opStatus == memd.StatusSuccess {
valLength := binary.BigEndian.Uint32(resp.Value[readPos:])
results[subdocs.indexes[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)
}
var duraLevelFrame *memd.DurabilityLevelFrame
var duraTimeoutFrame *memd.DurabilityTimeoutFrame
if opts.DurabilityLevel > 0 {
if crud.featureVerifier.HasBucketCapabilityStatus(BucketCapabilityDurableWrites, BucketCapabilityStatusUnsupported) {
return nil, errFeatureNotAvailable
}
duraLevelFrame = &memd.DurabilityLevelFrame{
DurabilityLevel: opts.DurabilityLevel,
}
duraTimeoutFrame = &memd.DurabilityTimeoutFrame{
DurabilityTimeout: opts.DurabilityLevelTimeout,
}
}
var userFrame *memd.UserImpersonationFrame
if len(opts.User) > 0 {
userFrame = &memd.UserImpersonationFrame{
User: []byte(opts.User),
}
}
var preserveExpiryFrame *memd.PreserveExpiryFrame
if opts.PreserveExpiry {
if opts.Flags|memd.SubdocDocFlagAddDoc == 1 {
return nil, wrapError(errInvalidArgument, "cannot use preserve expiry with add doc flags")
}
if opts.Expiry != 0 && opts.PreserveExpiry && opts.Flags|memd.SubdocDocFlagNone == 1 {
return nil, wrapError(errInvalidArgument, "cannot use preserve expiry with expiry and no doc flags")
}
preserveExpiryFrame = &memd.PreserveExpiryFrame{}
}
if opts.Flags&memd.SubdocDocFlagCreateAsDeleted != 0 {
// We can get here before support status is actually known, we'll send the request unless we know for a fact
// that this is unsupported.
if crud.featureVerifier.HasBucketCapabilityStatus(BucketCapabilityCreateAsDeleted, BucketCapabilityStatusUnsupported) {
return nil, errFeatureNotAvailable
}
}
subdocs.Reorder(opts.Ops)
pathBytesList := make([][]byte, len(opts.Ops))
pathBytesTotal := 0
valueBytesTotal := 0
for i, op := range subdocs.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 subdocs.ops {
if op.Op != memd.SubDocOpDictAdd && op.Op != memd.SubDocOpDictSet &&
op.Op != memd.SubDocOpDelete && op.Op != memd.SubDocOpReplace &&
op.Op != memd.SubDocOpArrayPushLast && op.Op != memd.SubDocOpArrayPushFirst &&
op.Op != memd.SubDocOpArrayInsert && op.Op != memd.SubDocOpArrayAddUnique &&
op.Op != memd.SubDocOpCounter && op.Op != memd.SubDocOpSetDoc &&
op.Op != memd.SubDocOpAddDoc && op.Op != memd.SubDocOpDeleteDoc &&
op.Op != memd.SubDocOpReplaceBodyWithXattr {
return nil, errInvalidArgument
}
if op.Op == memd.SubDocOpReplaceBodyWithXattr {
// We can get here before support status is actually known, we'll send the request unless we know for a fact
// that this is unsupported.
if crud.featureVerifier.HasBucketCapabilityStatus(BucketCapabilityReplaceBodyWithXattr, BucketCapabilityStatusUnsupported) {
return nil, errFeatureNotAvailable
}
}
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))
}
if opts.RetryStrategy == nil {
opts.RetryStrategy = crud.defaultRetryStrategy
}
req := &memdQRequest{
Packet: memd.Packet{
Magic: memd.CmdMagicReq,
Command: memd.CmdSubDocMultiMutation,
Datatype: 0,
Cas: uint64(opts.Cas),
Extras: extraBuf,
Key: opts.Key,
Value: valueBuf,
DurabilityLevelFrame: duraLevelFrame,
DurabilityTimeoutFrame: duraTimeoutFrame,
CollectionID: opts.CollectionID,
UserImpersonationFrame: userFrame,
PreserveExpiryFrame: preserveExpiryFrame,
},
Callback: handler,
RootTraceContext: tracer.RootContext(),
CollectionName: opts.CollectionName,
ScopeName: opts.ScopeName,
RetryStrategy: opts.RetryStrategy,
}
op, err := crud.cidMgr.Dispatch(req)
if err != nil {
tracer.Finish()
return nil, err
}
if !opts.Deadline.IsZero() {
start := time.Now()
req.SetTimer(time.AfterFunc(opts.Deadline.Sub(start), func() {
connInfo := req.ConnectionInfo()
count, reasons := req.Retries()
req.cancelWithCallbackAndFinishTracer(&TimeoutError{
InnerError: errAmbiguousTimeout,
OperationID: "MutateIn",
Opaque: req.Identifier(),
TimeObserved: time.Since(start),
RetryReasons: reasons,
RetryAttempts: count,
LastDispatchedTo: connInfo.lastDispatchedTo,
LastDispatchedFrom: connInfo.lastDispatchedFrom,
LastConnectionID: connInfo.lastConnectionID,
}, tracer)
}))
}
return op, nil
}
func (crud *crudComponent) makeSubDocError(index int, code memd.StatusCode, req *memdQRequest, resp *memdQResponse) error {
err := getKvStatusCodeError(code)
err = translateMemdError(err, req)
err = crud.errMapManager.EnhanceKvError(err, resp, req)
return SubDocumentError{
Index: index,
InnerError: err,
}
}