-
Notifications
You must be signed in to change notification settings - Fork 104
/
collection_subdoc.go
359 lines (309 loc) · 9.54 KB
/
collection_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
package gocb
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/couchbase/gocbcore/v10/memd"
"github.com/couchbase/gocbcore/v10"
)
// LookupInOptions are the set of options available to LookupIn.
type LookupInOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
// Internal: This should never be used and is not supported.
Internal struct {
DocFlags SubdocDocFlag
User string
}
noMetrics bool
}
// LookupIn performs a set of subdocument lookup operations on the document identified by id.
func (c *Collection) LookupIn(id string, ops []LookupInSpec, opts *LookupInOptions) (docOut *LookupInResult, errOut error) {
if opts == nil {
opts = &LookupInOptions{}
}
opm := c.newKvOpManager("lookup_in", opts.ParentSpan)
defer opm.Finish(opts.noMetrics)
opm.SetDocumentID(id)
opm.SetRetryStrategy(opts.RetryStrategy)
opm.SetTimeout(opts.Timeout)
opm.SetImpersonate(opts.Internal.User)
opm.SetContext(opts.Context)
if err := opm.CheckReadyForOp(); err != nil {
return nil, err
}
return c.internalLookupIn(opm, ops, memd.SubdocDocFlag(opts.Internal.DocFlags))
}
func (c *Collection) internalLookupIn(
opm *kvOpManager,
ops []LookupInSpec,
flags memd.SubdocDocFlag,
) (docOut *LookupInResult, errOut error) {
var subdocs []gocbcore.SubDocOp
for _, op := range ops {
if op.op == memd.SubDocOpGet && op.path == "" {
if op.isXattr {
return nil, errors.New("invalid xattr fetch with no path")
}
subdocs = append(subdocs, gocbcore.SubDocOp{
Op: memd.SubDocOpGetDoc,
Flags: memd.SubdocFlag(SubdocFlagNone),
})
continue
} else if op.op == memd.SubDocOpDictSet && op.path == "" {
if op.isXattr {
return nil, errors.New("invalid xattr set with no path")
}
subdocs = append(subdocs, gocbcore.SubDocOp{
Op: memd.SubDocOpSetDoc,
Flags: memd.SubdocFlag(SubdocFlagNone),
})
continue
}
flags := memd.SubdocFlagNone
if op.isXattr {
flags |= memd.SubdocFlagXattrPath
}
subdocs = append(subdocs, gocbcore.SubDocOp{
Op: op.op,
Path: op.path,
Flags: flags,
})
}
agent, err := c.getKvProvider()
if err != nil {
return nil, err
}
err = opm.Wait(agent.LookupIn(gocbcore.LookupInOptions{
Key: opm.DocumentID(),
Ops: subdocs,
CollectionName: opm.CollectionName(),
ScopeName: opm.ScopeName(),
RetryStrategy: opm.RetryStrategy(),
TraceContext: opm.TraceSpanContext(),
Deadline: opm.Deadline(),
Flags: flags,
User: opm.Impersonate(),
}, func(res *gocbcore.LookupInResult, err error) {
if err != nil && res == nil {
errOut = opm.EnhanceErr(err)
}
if res != nil {
docOut = &LookupInResult{}
docOut.cas = Cas(res.Cas)
docOut.contents = make([]lookupInPartial, len(subdocs))
for i, opRes := range res.Ops {
docOut.contents[i].err = opm.EnhanceErr(opRes.Err)
docOut.contents[i].data = json.RawMessage(opRes.Value)
}
}
if err == nil {
opm.Resolve(nil)
} else {
opm.Reject()
}
}))
if err != nil {
errOut = err
}
return
}
// StoreSemantics is used to define the document level action to take during a MutateIn operation.
type StoreSemantics uint8
const (
// StoreSemanticsReplace signifies to Replace the document, and fail if it does not exist.
// This is the default action
StoreSemanticsReplace StoreSemantics = iota
// StoreSemanticsUpsert signifies to replace the document or create it if it doesn't exist.
StoreSemanticsUpsert
// StoreSemanticsInsert signifies to create the document, and fail if it exists.
StoreSemanticsInsert
)
// MutateInOptions are the set of options available to MutateIn.
type MutateInOptions struct {
Expiry time.Duration
Cas Cas
PersistTo uint
ReplicateTo uint
DurabilityLevel DurabilityLevel
StoreSemantic StoreSemantics
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
PreserveExpiry bool
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
// Internal: This should never be used and is not supported.
Internal struct {
DocFlags SubdocDocFlag
User string
}
}
// MutateIn performs a set of subdocument mutations on the document specified by id.
func (c *Collection) MutateIn(id string, ops []MutateInSpec, opts *MutateInOptions) (mutOut *MutateInResult, errOut error) {
if opts == nil {
opts = &MutateInOptions{}
}
opm := c.newKvOpManager("mutate_in", opts.ParentSpan)
defer opm.Finish(false)
opm.SetDocumentID(id)
opm.SetRetryStrategy(opts.RetryStrategy)
opm.SetTimeout(opts.Timeout)
opm.SetImpersonate(opts.Internal.User)
opm.SetContext(opts.Context)
opm.SetPreserveExpiry(opts.PreserveExpiry)
if err := opm.CheckReadyForOp(); err != nil {
return nil, err
}
return c.internalMutateIn(opm, opts.StoreSemantic, opts.Expiry, opts.Cas, ops, memd.SubdocDocFlag(opts.Internal.DocFlags))
}
func jsonMarshalMultiArray(in interface{}) ([]byte, error) {
out, err := json.Marshal(in)
if err != nil {
return nil, err
}
// Assert first character is a '['
if len(out) < 2 || out[0] != '[' {
return nil, makeInvalidArgumentsError("not a JSON array")
}
out = out[1 : len(out)-1]
return out, nil
}
func jsonMarshalMutateSpec(op MutateInSpec) ([]byte, memd.SubdocFlag, error) {
if op.value == nil {
// If the mutation is to write, then this is a json `null` value
switch op.op {
case memd.SubDocOpDictAdd,
memd.SubDocOpDictSet,
memd.SubDocOpReplace,
memd.SubDocOpArrayPushLast,
memd.SubDocOpArrayPushFirst,
memd.SubDocOpArrayInsert,
memd.SubDocOpArrayAddUnique,
memd.SubDocOpSetDoc,
memd.SubDocOpAddDoc:
return []byte("null"), memd.SubdocFlagNone, nil
}
return nil, memd.SubdocFlagNone, nil
}
if macro, ok := op.value.(MutationMacro); ok {
return []byte(macro), memd.SubdocFlagExpandMacros | memd.SubdocFlagXattrPath, nil
}
if op.multiValue {
bytes, err := jsonMarshalMultiArray(op.value)
return bytes, memd.SubdocFlagNone, err
}
bytes, err := json.Marshal(op.value)
return bytes, memd.SubdocFlagNone, err
}
func (c *Collection) internalMutateIn(
opm *kvOpManager,
action StoreSemantics,
expiry time.Duration,
cas Cas,
ops []MutateInSpec,
docFlags memd.SubdocDocFlag,
) (mutOut *MutateInResult, errOut error) {
preserveTTL := opm.PreserveExpiry()
if action == StoreSemanticsReplace {
// this is the default behaviour
if expiry > 0 && preserveTTL {
return nil, makeInvalidArgumentsError("cannot use preserve ttl with expiry for replace store semantics")
}
} else if action == StoreSemanticsUpsert {
docFlags |= memd.SubdocDocFlagMkDoc
} else if action == StoreSemanticsInsert {
if preserveTTL {
return nil, makeInvalidArgumentsError("cannot use preserve ttl with insert store semantics")
}
docFlags |= memd.SubdocDocFlagAddDoc
} else {
return nil, makeInvalidArgumentsError("invalid StoreSemantics value provided")
}
var subdocs []gocbcore.SubDocOp
for _, op := range ops {
if op.path == "" {
switch op.op {
case memd.SubDocOpDictAdd:
return nil, makeInvalidArgumentsError("cannot specify a blank path with InsertSpec")
case memd.SubDocOpDictSet:
return nil, makeInvalidArgumentsError("cannot specify a blank path with UpsertSpec")
case memd.SubDocOpDelete:
op.op = memd.SubDocOpDeleteDoc
case memd.SubDocOpReplace:
op.op = memd.SubDocOpSetDoc
default:
}
}
etrace := c.startKvOpTrace("request_encoding", opm.TraceSpanContext(), true)
bytes, flags, err := jsonMarshalMutateSpec(op)
etrace.End()
if err != nil {
return nil, err
}
if op.createPath {
flags |= memd.SubdocFlagMkDirP
}
if op.isXattr {
flags |= memd.SubdocFlagXattrPath
}
subdocs = append(subdocs, gocbcore.SubDocOp{
Op: op.op,
Flags: flags,
Path: op.path,
Value: bytes,
})
}
agent, err := c.getKvProvider()
if err != nil {
return nil, err
}
err = opm.Wait(agent.MutateIn(gocbcore.MutateInOptions{
Key: opm.DocumentID(),
Flags: docFlags,
Cas: gocbcore.Cas(cas),
Ops: subdocs,
Expiry: durationToExpiry(expiry),
CollectionName: opm.CollectionName(),
ScopeName: opm.ScopeName(),
DurabilityLevel: opm.DurabilityLevel(),
DurabilityLevelTimeout: opm.DurabilityTimeout(),
RetryStrategy: opm.RetryStrategy(),
TraceContext: opm.TraceSpanContext(),
Deadline: opm.Deadline(),
User: opm.Impersonate(),
PreserveExpiry: preserveTTL,
}, func(res *gocbcore.MutateInResult, err error) {
if err != nil {
// GOCBC-1019: Due to a previous bug in gocbcore we need to convert cas mismatch back to exists.
if kvErr, ok := err.(*gocbcore.KeyValueError); ok {
if errors.Is(kvErr.InnerError, ErrCasMismatch) {
kvErr.InnerError = ErrDocumentExists
}
}
errOut = opm.EnhanceErr(err)
opm.Reject()
return
}
mutOut = &MutateInResult{}
mutOut.cas = Cas(res.Cas)
mutOut.mt = opm.EnhanceMt(res.MutationToken)
mutOut.contents = make([]mutateInPartial, len(res.Ops))
for i, op := range res.Ops {
mutOut.contents[i] = mutateInPartial{data: op.Value}
}
opm.Resolve(mutOut.mt)
}))
if err != nil {
errOut = err
}
return
}