forked from couchbase/gocb
/
bucket_subdoc.go
334 lines (299 loc) · 8.68 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
package gocb
import (
"encoding/json"
"github.com/Vellocet/gocb/gocbcore"
)
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
}
}
// *VOLATILE*
// 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
}
// 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 array push front operation to this mutation operation set.
func (set *MutateInBuilder) PushFront(path string, value interface{}, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushFirst,
Path: path,
}
op.Value, _ = json.Marshal(value)
if createParents {
op.Flags &= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an array push back operation to this mutation operation set.
func (set *MutateInBuilder) PushBack(path string, value interface{}, createParents bool) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayPushLast,
Path: path,
}
op.Value, _ = json.Marshal(value)
if createParents {
op.Flags &= gocbcore.SubDocFlagMkDirP
}
set.ops = append(set.ops, op)
return set
}
// Adds an array insert operation to this mutation operation set.
func (set *MutateInBuilder) ArrayInsert(path string, value interface{}) *MutateInBuilder {
op := gocbcore.SubDocOp{
Op: gocbcore.SubDocOpArrayInsert,
Path: path,
}
op.Value, _ = json.Marshal(value)
set.ops = append(set.ops, op)
return set
}
// Adds an dictionary add unique operation to this mutation operation set.
func (set *MutateInBuilder) AddUnique(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),
}
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
}
}
// *VOLATILE*
// 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,
}
}