forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_queryindexes.go
378 lines (323 loc) · 11.5 KB
/
cluster_queryindexes.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
package gocb
import (
"context"
"fmt"
"time"
)
// QueryIndexManager provides methods for performing Couchbase query index management.
type QueryIndexManager struct {
base *baseQueryIndexManager
}
func (qm *QueryIndexManager) maybeAddScopeCollectionToBucket(bucketName, scope, collection string) string {
if scope != "" && collection != "" {
bucketName = fmt.Sprintf("`%s`.`%s`.`%s`", bucketName, scope, collection)
} else if collection == "" && scope != "" {
bucketName = fmt.Sprintf("`%s`.`%s`.`_default", bucketName, scope)
} else if collection != "" && scope == "" {
bucketName = fmt.Sprintf("`%s`.`_default`.`%s", bucketName, collection)
} else {
bucketName = "`" + bucketName + "`"
}
return bucketName
}
func (qm *QueryIndexManager) validateScopeCollection(scope, collection string) error {
if scope == "" && collection != "" {
return makeInvalidArgumentsError("if collection is set then scope must be set")
} else if scope != "" && collection == "" {
return makeInvalidArgumentsError("if scope is set then collection must be set")
}
return nil
}
// CreateQueryIndexOptions is the set of options available to the query indexes CreateIndex operation.
type CreateQueryIndexOptions struct {
IgnoreIfExists bool
Deferred bool
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// CreateIndex creates an index over the specified fields.
// The SDK will automatically escape the provided index keys. For more advanced use cases like index keys using keywords
// cluster.Query or scope.Query should be used with the query directly.
func (qm *QueryIndexManager) CreateIndex(bucketName, indexName string, keys []string, opts *CreateQueryIndexOptions) error {
if opts == nil {
opts = &CreateQueryIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{
message: "an invalid index name was specified",
}
}
if len(keys) <= 0 {
return invalidArgumentsError{
message: "you must specify at least one index-key to index",
}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return err
}
return qm.base.CreateIndex(
opts.Context,
opts.ParentSpan,
qm.maybeAddScopeCollectionToBucket(bucketName, opts.ScopeName, opts.CollectionName),
indexName,
keys,
createQueryIndexOptions{
IgnoreIfExists: opts.IgnoreIfExists,
Deferred: opts.Deferred,
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
},
)
}
// CreatePrimaryQueryIndexOptions is the set of options available to the query indexes CreatePrimaryIndex operation.
type CreatePrimaryQueryIndexOptions struct {
IgnoreIfExists bool
Deferred bool
CustomName string
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// CreatePrimaryIndex creates a primary index. An empty customName uses the default naming.
func (qm *QueryIndexManager) CreatePrimaryIndex(bucketName string, opts *CreatePrimaryQueryIndexOptions) error {
if opts == nil {
opts = &CreatePrimaryQueryIndexOptions{}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return err
}
return qm.base.CreateIndex(
opts.Context,
opts.ParentSpan,
qm.maybeAddScopeCollectionToBucket(bucketName, opts.ScopeName, opts.CollectionName),
opts.CustomName,
nil,
createQueryIndexOptions{
IgnoreIfExists: opts.IgnoreIfExists,
Deferred: opts.Deferred,
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
})
}
// DropQueryIndexOptions is the set of options available to the query indexes DropIndex operation.
type DropQueryIndexOptions struct {
IgnoreIfNotExists bool
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// DropIndex drops a specific index by name.
func (qm *QueryIndexManager) DropIndex(bucketName, indexName string, opts *DropQueryIndexOptions) error {
if opts == nil {
opts = &DropQueryIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{
message: "an invalid index name was specified",
}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return err
}
return qm.base.DropIndex(
opts.Context,
opts.ParentSpan,
qm.maybeAddScopeCollectionToBucket(bucketName, opts.ScopeName, opts.CollectionName),
indexName,
dropQueryIndexOptions{
IgnoreIfNotExists: opts.IgnoreIfNotExists,
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
UseCollectionsSyntax: opts.ScopeName != "" || opts.CollectionName != "",
})
}
// DropPrimaryQueryIndexOptions is the set of options available to the query indexes DropPrimaryIndex operation.
type DropPrimaryQueryIndexOptions struct {
IgnoreIfNotExists bool
CustomName string
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// DropPrimaryIndex drops the primary index. Pass an empty customName for unnamed primary indexes.
func (qm *QueryIndexManager) DropPrimaryIndex(bucketName string, opts *DropPrimaryQueryIndexOptions) error {
if opts == nil {
opts = &DropPrimaryQueryIndexOptions{}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return err
}
return qm.base.DropIndex(
opts.Context,
opts.ParentSpan,
qm.maybeAddScopeCollectionToBucket(bucketName, opts.ScopeName, opts.CollectionName),
opts.CustomName,
dropQueryIndexOptions{
IgnoreIfNotExists: opts.IgnoreIfNotExists,
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
UseCollectionsSyntax: opts.ScopeName != "" || opts.CollectionName != "",
})
}
// GetAllQueryIndexesOptions is the set of options available to the query indexes GetAllIndexes operation.
type GetAllQueryIndexesOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
func (qm *QueryIndexManager) buildGetAllIndexesWhereClause(
bucketName,
scopeName,
collectionName string,
) (string, map[string]interface{}) {
bucketCond := "bucket_id = $bucketName"
scopeCond := "(" + bucketCond + " AND scope_id = $scopeName)"
collectionCond := "(" + scopeCond + " AND keyspace_id = $collectionName)"
params := map[string]interface{}{
"bucketName": bucketName,
}
var where string
if collectionName != "" {
where = collectionCond
params["scopeName"] = scopeName
params["collectionName"] = collectionName
} else if scopeName != "" {
where = scopeCond
params["scopeName"] = scopeName
} else {
where = bucketCond
}
if collectionName == "_default" || collectionName == "" {
defaultColCond := "(bucket_id IS MISSING AND keyspace_id = $bucketName)"
where = "(" + where + " OR " + defaultColCond + ")"
}
return where, params
}
// GetAllIndexes returns a list of all currently registered indexes.
func (qm *QueryIndexManager) GetAllIndexes(bucketName string, opts *GetAllQueryIndexesOptions) ([]QueryIndex, error) {
if opts == nil {
opts = &GetAllQueryIndexesOptions{}
}
where, params := qm.buildGetAllIndexesWhereClause(bucketName, opts.ScopeName, opts.CollectionName)
return qm.base.GetAllIndexes(
opts.Context,
opts.ParentSpan,
where,
params,
getAllQueryIndexesOptions{
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
})
}
// BuildDeferredQueryIndexOptions is the set of options available to the query indexes BuildDeferredIndexes operation.
type BuildDeferredQueryIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// BuildDeferredIndexes builds all indexes which are currently in deferred state.
// If no collection and scope names are specified in the options then *only* indexes created on the bucket directly
// will be built.
func (qm *QueryIndexManager) BuildDeferredIndexes(bucketName string, opts *BuildDeferredQueryIndexOptions) ([]string, error) {
if opts == nil {
opts = &BuildDeferredQueryIndexOptions{}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return nil, err
}
var where string
params := make(map[string]interface{})
if opts.CollectionName == "" {
where = "(keyspace_id = $bucketName AND bucket_id IS MISSING)"
params["bucketName"] = bucketName
} else {
where = "bucket_id = $bucketName AND scope_id = $scopeName AND keyspace_id = $collectionName"
params["bucketName"] = bucketName
params["scopeName"] = opts.ScopeName
params["collectionName"] = opts.CollectionName
}
return qm.base.BuildDeferredIndexes(
qm.maybeAddScopeCollectionToBucket(bucketName, opts.ScopeName, opts.CollectionName),
where,
params,
buildDeferredQueryIndexOptions{
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
},
)
}
// WatchQueryIndexOptions is the set of options available to the query indexes Watch operation.
type WatchQueryIndexOptions struct {
WatchPrimary bool
RetryStrategy RetryStrategy
ParentSpan RequestSpan
ScopeName string
CollectionName string
// 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
}
// WatchIndexes waits for a set of indexes to come online.
func (qm *QueryIndexManager) WatchIndexes(bucketName string, watchList []string, timeout time.Duration, opts *WatchQueryIndexOptions) error {
if opts == nil {
opts = &WatchQueryIndexOptions{}
}
if err := qm.validateScopeCollection(opts.ScopeName, opts.CollectionName); err != nil {
return err
}
where, params := qm.buildGetAllIndexesWhereClause(bucketName, opts.ScopeName, opts.CollectionName)
return qm.base.WatchIndexes(
where,
params,
watchList,
timeout,
watchQueryIndexOptions{
WatchPrimary: opts.WatchPrimary,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
},
)
}