forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_subdoc.go
400 lines (348 loc) · 10.9 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
package gocb
import (
"encoding/json"
"gopkg.in/couchbase/gocbcore.v2"
"log"
)
type subDocResult struct {
path string
data []byte
err error
}
// Represents multiple chunks of a full Document.
type DocumentFragment struct {
cas Cas
mt MutationToken
contents []subDocResult
pathMap map[string]int
}
// Returns the Cas of the Document
func (frag *DocumentFragment) Cas() Cas {
return frag.cas
}
// Returns the MutationToken for the change represented by this DocumentFragment.
func (frag *DocumentFragment) MutationToken() MutationToken {
return frag.mt
}
// Retrieve 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
}
return json.Unmarshal(res.data, valuePtr)
}
// Retrieve 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)
}
// 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
}
// Builder used to create a set of sub-document lookup operations.
type LookupInBuilder struct {
bucket *Bucket
name string
ops []gocbcore.SubDocOp
}
// Executes this set of lookup operations on the bucket.
func (set *LookupInBuilder) Execute() (*DocumentFragment, error) {
return set.bucket.lookupIn(set)
}
// Indicate 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 {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpGet,
Path: path,
}
set.ops = append(set.ops, op)
return set
}
// 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 {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpExists,
Path: path,
}
set.ops = append(set.ops, op)
return set
}
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,
func(results []gocbcore.SubDocResult, cas gocbcore.Cas, err error) {
errOut = err
{
resSet := &DocumentFragment{}
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
}
}
// Creates a sub-document lookup operation builder.
func (b *Bucket) LookupIn(key string) *LookupInBuilder {
return &LookupInBuilder{
bucket: b,
name: key,
}
}
// Builder used to create a set of sub-document mutation operations.
type MutateInBuilder struct {
bucket *Bucket
name string
cas gocbcore.Cas
expiry uint32
ops []gocbcore.SubDocOp
}
// Executes this set of mutation operations on the bucket.
func (set *MutateInBuilder) Execute() (*DocumentFragment, error) {
return set.bucket.mutateIn(set)
}
// Adds an insert operation to this mutation operation set.
func (set *MutateInBuilder) Insert(path string, value interface{}, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDictAdd,
Path: path,
}
op.Value, _ = json.Marshal(value)
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an upsert operation to this mutation operation set.
func (set *MutateInBuilder) Upsert(path string, value interface{}, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDictSet,
Path: path,
}
op.Value, _ = json.Marshal(value)
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an replace operation to this mutation operation set.
func (set *MutateInBuilder) Replace(path string, value interface{}) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpReplace,
Path: path,
}
op.Value, _ = json.Marshal(value)
set.ops = append(set.ops, op)
return set
}
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
}
// Adds an remove operation to this mutation operation set.
func (set *MutateInBuilder) Remove(path string) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpDelete,
Path: path,
}
set.ops = append(set.ops, op)
return set
}
// Adds an element to the beginning (i.e. left) of an array
func (set *MutateInBuilder) ArrayPrepend(path string, value interface{}, createParents bool) *MutateInBuilder {
jsonVal, _ := json.Marshal(value)
return set.arrayPrependValue(path, jsonVal, createParents)
}
func (set *MutateInBuilder) arrayPrependValue(path string, bytes []byte, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushFirst,
Path: path,
}
op.Value = bytes
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an element to the end (i.e. right) of an array
func (set *MutateInBuilder) ArrayAppend(path string, value interface{}, createParents bool) *MutateInBuilder {
jsonVal, _ := json.Marshal(value)
return set.arrayAppendValue(path, jsonVal, createParents)
}
func (set *MutateInBuilder) arrayAppendValue(path string, bytes []byte, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushLast,
Path: path,
}
op.Value = bytes
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// 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 {
jsonVal, _ := json.Marshal(value)
return set.arrayInsertValue(path, jsonVal)
}
func (set *MutateInBuilder) arrayInsertValue(path string, bytes []byte) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayInsert,
Path: path,
}
op.Value = bytes
set.ops = append(set.ops, op)
return set
}
// 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 {
return set.arrayAppendValue(path, set.marshalArrayMulti(values), createParents)
}
// 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 {
return set.arrayPrependValue(path, set.marshalArrayMulti(values), createParents)
}
// 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.arrayInsertValue(path, set.marshalArrayMulti(values))
}
// Adds an dictionary add unique operation to this mutation operation set.
func (set *MutateInBuilder) ArrayAddUnique(path string, value interface{}, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayAddUnique,
Path: path,
}
op.Value, _ = json.Marshal(value)
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an counter operation to this mutation operation set.
func (set *MutateInBuilder) Counter(path string, delta int64, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpCounter,
Path: path,
}
op.Value, _ = json.Marshal(delta)
if createParents {
op.Flags |= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
func (b *Bucket) mutateIn(set *MutateInBuilder) (resOut *DocumentFragment, errOut error) {
signal := make(chan bool, 1)
op, err := b.client.SubDocMutate([]byte(set.name), set.ops, 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
}
}
// Creates a sub-document mutation operation builder.
func (b *Bucket) MutateIn(key string, cas Cas, expiry uint32) *MutateInBuilder {
return &MutateInBuilder{
bucket: b,
name: key,
cas: gocbcore.Cas(cas),
expiry: expiry,
}
}