forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_subdoc.go
591 lines (511 loc) · 17.3 KB
/
bucket_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
package gocb
import (
"encoding/json"
"gopkg.in/couchbase/gocbcore.v7"
"log"
)
type subDocResult struct {
path string
data []byte
err error
}
// DocumentFragment represents multiple chunks of a full Document.
type DocumentFragment struct {
cas Cas
mt MutationToken
contents []subDocResult
pathMap map[string]int
}
// Cas returns the Cas of the Document
func (frag *DocumentFragment) Cas() Cas {
return frag.cas
}
// MutationToken returns the MutationToken for the change represented by this DocumentFragment.
func (frag *DocumentFragment) MutationToken() MutationToken {
return frag.mt
}
// ContentByIndex retrieves the value of the operation by its index. The index is the position of
// the operation as it was added to the builder.
func (frag *DocumentFragment) ContentByIndex(idx int, valuePtr interface{}) error {
res := frag.contents[idx]
if res.err != nil {
return res.err
}
if valuePtr == nil {
return nil
}
if valuePtr, ok := valuePtr.(*[]byte); ok {
*valuePtr = res.data
return nil
}
return json.Unmarshal(res.data, valuePtr)
}
// Content retrieves the value of the operation by its path. The path is the path provided
// to the operation
func (frag *DocumentFragment) Content(path string, valuePtr interface{}) error {
if frag.pathMap == nil {
frag.pathMap = make(map[string]int)
for i, v := range frag.contents {
frag.pathMap[v.path] = i
}
}
return frag.ContentByIndex(frag.pathMap[path], valuePtr)
}
// Exists checks whether the indicated path exists in this DocumentFragment and no
// errors were returned from the server.
func (frag *DocumentFragment) Exists(path string) bool {
err := frag.Content(path, nil)
return err == nil
}
// LookupInBuilder is a builder used to create a set of sub-document lookup operations.
type LookupInBuilder struct {
bucket *Bucket
name string
flags gocbcore.SubdocDocFlag
ops []gocbcore.SubDocOp
}
// Execute executes this set of lookup operations on the bucket.
func (set *LookupInBuilder) Execute() (*DocumentFragment, error) {
return set.bucket.lookupIn(set)
}
// GetEx allows you to perform a sub-document Get operation with flags
func (set *LookupInBuilder) GetEx(path string, flags SubdocFlag) *LookupInBuilder {
if path == "" {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpGetDoc,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpGet,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
// Get indicates a path to be retrieved from the document. The value of the path
// can later be retrieved (after .Execute()) using the Content or ContentByIndex
// method. The path syntax follows N1QL's path syntax (e.g. `foo.bar.baz`).
func (set *LookupInBuilder) Get(path string) *LookupInBuilder {
return set.GetEx(path, SubdocFlagNone)
}
// ExistsEx allows you to perform a sub-document Exists operation with flags
func (set *LookupInBuilder) ExistsEx(path string, flags SubdocFlag) *LookupInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpExists,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
// Exists is similar to Get(), but does not actually retrieve the value from the server.
// This may save bandwidth if you only need to check for the existence of a
// path (without caring for its content). You can check the status of this
// operation by using .Content (and ignoring the value) or .Exists()
func (set *LookupInBuilder) Exists(path string) *LookupInBuilder {
return set.ExistsEx(path, SubdocFlagNone)
}
// GetCountEx allows you to perform a sub-document GetCount operation with flags
func (set *LookupInBuilder) GetCountEx(path string, flags SubdocFlag) *LookupInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpGetCount,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
// GetCount allows you to retrieve the number of items in an array or keys within an
// dictionary within an element of a document.
func (set *LookupInBuilder) GetCount(path string) *LookupInBuilder {
return set.GetCountEx(path, SubdocFlagNone)
}
func (b *Bucket) lookupIn(set *LookupInBuilder) (resOut *DocumentFragment, errOut error) {
signal := make(chan bool, 1)
op, err := b.client.SubDocLookup([]byte(set.name), set.ops, set.flags,
func(results []gocbcore.SubDocResult, cas gocbcore.Cas, err error) {
errOut = err
{
resSet := &DocumentFragment{}
resSet.contents = make([]subDocResult, len(results))
resSet.cas = Cas(cas)
for i := range results {
resSet.contents[i].path = set.ops[i].Path
resSet.contents[i].err = results[i].Err
if results[i].Value != nil {
resSet.contents[i].data = append([]byte(nil), results[i].Value...)
}
}
resOut = resSet
}
signal <- true
})
if err != nil {
return nil, err
}
timeoutTmr := gocbcore.AcquireTimer(b.opTimeout)
select {
case <-signal:
gocbcore.ReleaseTimer(timeoutTmr, false)
return
case <-timeoutTmr.C:
gocbcore.ReleaseTimer(timeoutTmr, true)
if !op.Cancel() {
<-signal
return
}
return nil, ErrTimeout
}
}
// LookupInEx creates a sub-document lookup operation builder.
func (b *Bucket) LookupInEx(key string, flags SubdocDocFlag) *LookupInBuilder {
return &LookupInBuilder{
bucket: b,
name: key,
flags: gocbcore.SubdocDocFlag(flags),
}
}
// LookupIn creates a sub-document lookup operation builder.
func (b *Bucket) LookupIn(key string) *LookupInBuilder {
return b.LookupInEx(key, 0)
}
// MutateInBuilder is a builder used to create a set of sub-document mutation operations.
type MutateInBuilder struct {
bucket *Bucket
name string
flags gocbcore.SubdocDocFlag
cas gocbcore.Cas
expiry uint32
ops []gocbcore.SubDocOp
errs MultiError
}
// Execute executes this set of mutation operations on the bucket.
func (set *MutateInBuilder) Execute() (*DocumentFragment, error) {
return set.bucket.mutateIn(set)
}
func (set *MutateInBuilder) marshalValue(value interface{}) []byte {
if value, ok := value.([]byte); ok {
return value
}
if value, ok := value.(*[]byte); ok {
return *value
}
bytes, err := json.Marshal(value)
if err != nil {
set.errs.add(err)
return nil
}
return bytes
}
// InsertEx allows you to perform a sub-document Insert operation with flags
func (set *MutateInBuilder) InsertEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
if path == "" {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpAddDoc,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDictAdd,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
// Insert adds an insert operation to this mutation operation set.
func (set *MutateInBuilder) Insert(path string, value interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.InsertEx(path, value, flags)
}
// UpsertEx allows you to perform a sub-document Upsert operation with flags
func (set *MutateInBuilder) UpsertEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
if path == "" {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpSetDoc,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDictSet,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
// Upsert adds an upsert operation to this mutation operation set.
func (set *MutateInBuilder) Upsert(path string, value interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.UpsertEx(path, value, flags)
}
// ReplaceEx allows you to perform a sub-document Replace operation with flags
func (set *MutateInBuilder) ReplaceEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpReplace,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
// Replace adds an replace operation to this mutation operation set.
func (set *MutateInBuilder) Replace(path string, value interface{}) *MutateInBuilder {
return set.ReplaceEx(path, value, SubdocFlagNone)
}
func (set *MutateInBuilder) marshalArrayMulti(in interface{}) (out []byte) {
out, err := json.Marshal(in)
if err != nil {
log.Panic(err)
}
// Assert first character is a '['
if len(out) < 2 || out[0] != '[' {
log.Panic("Not a JSON array")
}
out = out[1 : len(out)-1]
return
}
// RemoveEx allows you to perform a sub-document Remove operation with flags
func (set *MutateInBuilder) RemoveEx(path string, flags SubdocFlag) *MutateInBuilder {
if path == "" {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDeleteDoc,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDelete,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
}
set.ops = append(set.ops, op)
return set
}
// Remove adds an remove operation to this mutation operation set.
func (set *MutateInBuilder) Remove(path string) *MutateInBuilder {
return set.RemoveEx(path, SubdocFlagNone)
}
// ArrayPrependEx allows you to perform a sub-document ArrayPrepend operation with flags
func (set *MutateInBuilder) ArrayPrependEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayPrependValue(path, set.marshalValue(value), flags)
}
// ArrayPrepend adds an element to the beginning (i.e. left) of an array
func (set *MutateInBuilder) ArrayPrepend(path string, value interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.ArrayPrependEx(path, value, flags)
}
func (set *MutateInBuilder) arrayPrependValue(path string, bytes []byte, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushFirst,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: bytes,
}
set.ops = append(set.ops, op)
return set
}
// ArrayAppendEx allows you to perform a sub-document ArrayAppend operation with flags
func (set *MutateInBuilder) ArrayAppendEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayAppendValue(path, set.marshalValue(value), flags)
}
// ArrayAppend adds an element to the end (i.e. right) of an array
func (set *MutateInBuilder) ArrayAppend(path string, value interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.ArrayAppendEx(path, value, flags)
}
func (set *MutateInBuilder) arrayAppendValue(path string, bytes []byte, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushLast,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: bytes,
}
set.ops = append(set.ops, op)
return set
}
// ArrayInsertEx allows you to perform a sub-document ArrayInsert operation with flags
func (set *MutateInBuilder) ArrayInsertEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayInsertValue(path, set.marshalValue(value), flags)
}
// ArrayInsert inserts an element at a given position within an array. The position should be
// specified as part of the path, e.g. path.to.array[3]
func (set *MutateInBuilder) ArrayInsert(path string, value interface{}) *MutateInBuilder {
return set.ArrayInsertEx(path, value, SubdocFlagNone)
}
func (set *MutateInBuilder) arrayInsertValue(path string, bytes []byte, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayInsert,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: bytes,
}
set.ops = append(set.ops, op)
return set
}
// ArrayAppendMultiEx allows you to perform a sub-document ArrayAppendMulti operation with flags
func (set *MutateInBuilder) ArrayAppendMultiEx(path string, values interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayAppendValue(path, set.marshalArrayMulti(values), flags)
}
// ArrayAppendMulti adds multiple values as elements to an array.
// `values` must be an array type
// ArrayAppendMulti("path", []int{1,2,3,4}, true) =>
// "path" [..., 1,2,3,4]
//
// This is a more efficient version (at both the network and server levels)
// of doing
// ArrayAppend("path", 1, true).ArrayAppend("path", 2, true).ArrayAppend("path", 3, true)
//
// See ArrayAppend() for more information
func (set *MutateInBuilder) ArrayAppendMulti(path string, values interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.ArrayAppendMultiEx(path, values, flags)
}
// ArrayPrependMultiEx allows you to perform a sub-document ArrayPrependMulti operation with flags
func (set *MutateInBuilder) ArrayPrependMultiEx(path string, values interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayPrependValue(path, set.marshalArrayMulti(values), flags)
}
// ArrayPrependMulti adds multiple values at the beginning of an array.
// See ArrayAppendMulti for more information about multiple element operations
// and ArrayPrepend for the semantics of this operation
func (set *MutateInBuilder) ArrayPrependMulti(path string, values interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.ArrayPrependMultiEx(path, values, flags)
}
// ArrayInsertMultiEx allows you to perform a sub-document ArrayInsertMulti operation with flags
func (set *MutateInBuilder) ArrayInsertMultiEx(path string, values interface{}, flags SubdocFlag) *MutateInBuilder {
return set.arrayInsertValue(path, set.marshalArrayMulti(values), flags)
}
// ArrayInsertMulti inserts multiple elements at a specified position within the
// array. See ArrayAppendMulti for more information about multiple element
// operations, and ArrayInsert for more information about array insertion operations
func (set *MutateInBuilder) ArrayInsertMulti(path string, values interface{}) *MutateInBuilder {
return set.ArrayInsertMultiEx(path, values, SubdocFlagNone)
}
// ArrayAddUniqueEx allows you to perform a sub-document ArrayAddUnique operation with flags
func (set *MutateInBuilder) ArrayAddUniqueEx(path string, value interface{}, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayAddUnique,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(value),
}
set.ops = append(set.ops, op)
return set
}
// ArrayAddUnique adds an dictionary add unique operation to this mutation operation set.
func (set *MutateInBuilder) ArrayAddUnique(path string, value interface{}, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.ArrayAddUniqueEx(path, value, flags)
}
// CounterEx allows you to perform a sub-document Counter operation with flags
func (set *MutateInBuilder) CounterEx(path string, delta int64, flags SubdocFlag) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpCounter,
Path: path,
Flags: gocbcore.SubdocFlag(flags),
Value: set.marshalValue(delta),
}
set.ops = append(set.ops, op)
return set
}
// Counter adds an counter operation to this mutation operation set.
func (set *MutateInBuilder) Counter(path string, delta int64, createParents bool) *MutateInBuilder {
var flags SubdocFlag
if createParents {
flags |= SubdocFlagCreatePath
}
return set.CounterEx(path, delta, flags)
}
func (b *Bucket) mutateIn(set *MutateInBuilder) (resOut *DocumentFragment, errOut error) {
errOut = set.errs.get()
if errOut != nil {
return
}
signal := make(chan bool, 1)
op, err := b.client.SubDocMutate([]byte(set.name), set.ops, set.flags, set.cas, set.expiry,
func(results []gocbcore.SubDocResult, cas gocbcore.Cas, mt gocbcore.MutationToken, err error) {
errOut = err
if errOut == nil {
resSet := &DocumentFragment{
cas: Cas(cas),
mt: MutationToken{mt, b},
}
resSet.contents = make([]subDocResult, len(results))
for i := range results {
resSet.contents[i].path = set.ops[i].Path
resSet.contents[i].err = results[i].Err
if results[i].Value != nil {
resSet.contents[i].data = append([]byte(nil), results[i].Value...)
}
}
resOut = resSet
}
signal <- true
})
if err != nil {
return nil, err
}
timeoutTmr := gocbcore.AcquireTimer(b.opTimeout)
select {
case <-signal:
gocbcore.ReleaseTimer(timeoutTmr, false)
return
case <-timeoutTmr.C:
gocbcore.ReleaseTimer(timeoutTmr, true)
if !op.Cancel() {
<-signal
return
}
return nil, ErrTimeout
}
}
// MutateInEx creates a sub-document mutation operation builder.
func (b *Bucket) MutateInEx(key string, flags SubdocDocFlag, cas Cas, expiry uint32) *MutateInBuilder {
return &MutateInBuilder{
bucket: b,
name: key,
flags: gocbcore.SubdocDocFlag(flags),
cas: gocbcore.Cas(cas),
expiry: expiry,
}
}
// MutateIn creates a sub-document mutation operation builder.
func (b *Bucket) MutateIn(key string, cas Cas, expiry uint32) *MutateInBuilder {
return b.MutateInEx(key, 0, cas, expiry)
}