-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquerycomponent.go
More file actions
448 lines (405 loc) · 13.5 KB
/
querycomponent.go
File metadata and controls
448 lines (405 loc) · 13.5 KB
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
package gocbcorex
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/couchbase/gocbcorex/cbhttpx"
"github.com/couchbase/gocbcorex/cbqueryx"
"go.uber.org/zap"
)
type QueryOptions struct {
Args []json.RawMessage
AtrCollection string
AutoExecute bool
ClientContextId string
Compression cbqueryx.Compression
Controls bool
Creds []cbqueryx.CredsJson
DurabilityLevel cbqueryx.DurabilityLevel
EncodedPlan string
Encoding cbqueryx.Encoding
Format cbqueryx.Format
KvTimeout time.Duration
MaxParallelism uint32
MemoryQuota uint32
Metrics bool
Namespace string
NumAtrs uint32
PipelineBatch uint32
PipelineCap uint32
Prepared string
PreserveExpiry bool
Pretty bool
Profile cbqueryx.ProfileMode
QueryContext string
ReadOnly bool
ScanCap uint32
ScanConsistency cbqueryx.ScanConsistency
ScanVector cbqueryx.ScanVectors
ScanVectors map[string]cbqueryx.ScanVectors
ScanWait time.Duration
Signature bool
Statement string
Timeout time.Duration
TxData json.RawMessage
TxId string
TxImplicit bool
TxStmtNum uint32
TxTimeout time.Duration
UseCbo bool
UseFts bool
NamedArgs map[string]json.RawMessage
Raw map[string]json.RawMessage
OnBehalfOf *cbhttpx.OnBehalfOfInfo
Endpoint string
}
type QueryResultStream interface {
cbqueryx.ResultStream
Endpoint() string
}
type PreparedStatementCache = cbqueryx.PreparedStatementCache
type QueryComponent struct {
baseHttpComponent
logger *zap.Logger
retries RetryManager
preparedCache *PreparedStatementCache
}
type QueryComponentConfig struct {
HttpRoundTripper http.RoundTripper
Endpoints map[string]string
Authenticator Authenticator
}
type QueryComponentOptions struct {
Logger *zap.Logger
UserAgent string
}
func OrchestrateQueryEndpoint[RespT any](
ctx context.Context,
w *QueryComponent,
endpointId string,
fn func(roundTripper http.RoundTripper, endpointId, endpoint, username, password string) (RespT, error),
) (RespT, error) {
if endpointId != "" {
roundTripper, endpoint, username, password, err := w.SelectSpecificEndpoint(endpointId)
if err != nil {
var emptyResp RespT
return emptyResp, err
}
return fn(roundTripper, endpointId, endpoint, username, password)
}
roundTripper, endpointId, endpoint, username, password, err := w.SelectEndpoint(nil)
if err != nil {
var emptyResp RespT
return emptyResp, err
}
if endpoint == "" {
var emptyResp RespT
return emptyResp, serviceNotAvailableError{Service: ServiceTypeQuery}
}
return fn(roundTripper, endpointId, endpoint, username, password)
}
func OrchestrateQueryMgmtCall[OptsT any, RespT any](
ctx context.Context,
w *QueryComponent,
execFn func(o cbqueryx.Query, ctx context.Context, req OptsT) (RespT, error),
opts OptsT,
) (RespT, error) {
return OrchestrateRetries(ctx, w.retries, func() (RespT, error) {
return OrchestrateQueryEndpoint(ctx, w, "",
func(roundTripper http.RoundTripper, _, endpoint, username, password string) (RespT, error) {
return execFn(cbqueryx.Query{
Logger: w.logger,
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Auth: &cbhttpx.BasicAuth{
Username: username,
Password: password,
},
}, ctx, opts)
})
})
}
func OrchestrateNoResQueryMgmtCall[OptsT any](
ctx context.Context,
w *QueryComponent,
execFn func(o cbqueryx.Query, ctx context.Context, req OptsT) error,
opts OptsT,
) error {
return OrchestrateNoResponseRetries(ctx, w.retries, func() error {
_, err := OrchestrateQueryEndpoint(ctx, w, "",
func(roundTripper http.RoundTripper, _, endpoint, username, password string) (interface{}, error) {
return nil, execFn(cbqueryx.Query{
Logger: w.logger,
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Auth: &cbhttpx.BasicAuth{
Username: username,
Password: password,
},
}, ctx, opts)
})
return err
})
}
func NewQueryComponent(retries RetryManager, config *QueryComponentConfig, opts *QueryComponentOptions) *QueryComponent {
return &QueryComponent{
baseHttpComponent: baseHttpComponent{
serviceType: ServiceTypeQuery,
userAgent: opts.UserAgent,
state: &baseHttpComponentState{
httpRoundTripper: config.HttpRoundTripper,
endpoints: config.Endpoints,
authenticator: config.Authenticator,
},
},
logger: opts.Logger,
retries: retries,
preparedCache: cbqueryx.NewPreparedStatementCache(),
}
}
func (w *QueryComponent) Reconfigure(config *QueryComponentConfig) error {
w.updateState(baseHttpComponentState{
httpRoundTripper: config.HttpRoundTripper,
endpoints: config.Endpoints,
authenticator: config.Authenticator,
})
return nil
}
type GetQueryEndpointResult struct {
RoundTripper http.RoundTripper
EndpointId string
Endpoint string
Username string
Password string
}
func (w *QueryComponent) GetEndpoint(ctx context.Context) (*GetQueryEndpointResult, error) {
return OrchestrateQueryEndpoint(ctx, w, "",
func(roundTripper http.RoundTripper, endpointId, endpoint, username, password string) (*GetQueryEndpointResult, error) {
return &GetQueryEndpointResult{
RoundTripper: roundTripper,
EndpointId: endpointId,
Endpoint: endpoint,
Username: username,
Password: password,
}, nil
})
}
type queryResultStream struct {
cbqueryx.ResultStream
endpoint string
}
func (s *queryResultStream) Endpoint() string {
return s.endpoint
}
func (w *QueryComponent) Query(ctx context.Context, opts *QueryOptions) (QueryResultStream, error) {
return OrchestrateRetries(ctx, w.retries, func() (QueryResultStream, error) {
return OrchestrateQueryEndpoint(ctx, w, opts.Endpoint,
func(roundTripper http.RoundTripper, endpointId, endpoint, username, password string) (QueryResultStream, error) {
res, err := cbqueryx.Query{
Logger: w.logger,
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Auth: &cbhttpx.BasicAuth{
Username: username,
Password: password,
},
}.Query(ctx, &cbqueryx.QueryOptions{
Args: opts.Args,
AtrCollection: opts.AtrCollection,
AutoExecute: opts.AutoExecute,
ClientContextId: opts.ClientContextId,
Compression: opts.Compression,
Controls: opts.Controls,
Creds: opts.Creds,
DurabilityLevel: opts.DurabilityLevel,
EncodedPlan: opts.EncodedPlan,
Encoding: opts.Encoding,
Format: opts.Format,
KvTimeout: opts.KvTimeout,
MaxParallelism: opts.MaxParallelism,
MemoryQuota: opts.MemoryQuota,
Metrics: opts.Metrics,
Namespace: opts.Namespace,
NumAtrs: opts.NumAtrs,
PipelineBatch: opts.PipelineBatch,
PipelineCap: opts.PipelineCap,
Prepared: opts.Prepared,
PreserveExpiry: opts.PreserveExpiry,
Pretty: opts.Pretty,
Profile: opts.Profile,
QueryContext: opts.QueryContext,
ReadOnly: opts.ReadOnly,
ScanCap: opts.ScanCap,
ScanConsistency: opts.ScanConsistency,
ScanVector: opts.ScanVector,
ScanVectors: opts.ScanVectors,
ScanWait: opts.ScanWait,
Signature: opts.Signature,
Statement: opts.Statement,
Timeout: opts.Timeout,
TxData: opts.TxData,
TxId: opts.TxId,
TxImplicit: opts.TxImplicit,
TxStmtNum: opts.TxStmtNum,
TxTimeout: opts.TxTimeout,
UseCbo: opts.UseCbo,
UseFts: opts.UseFts,
NamedArgs: opts.NamedArgs,
Raw: opts.Raw,
OnBehalfOf: opts.OnBehalfOf,
})
if err != nil {
return nil, err
}
return &queryResultStream{
ResultStream: res,
endpoint: endpointId,
}, nil
})
})
}
func (w *QueryComponent) PreparedQuery(ctx context.Context, opts *QueryOptions) (QueryResultStream, error) {
return OrchestrateRetries(ctx, w.retries, func() (QueryResultStream, error) {
return OrchestrateQueryEndpoint(ctx, w, opts.Endpoint,
func(roundTripper http.RoundTripper, endpointId, endpoint, username, password string) (QueryResultStream, error) {
res, err := cbqueryx.PreparedQuery{
Executor: cbqueryx.Query{
Logger: w.logger,
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Auth: &cbhttpx.BasicAuth{
Username: username,
Password: password,
},
},
Cache: w.preparedCache,
}.PreparedQuery(ctx, &cbqueryx.QueryOptions{
Args: opts.Args,
AtrCollection: opts.AtrCollection,
AutoExecute: opts.AutoExecute,
ClientContextId: opts.ClientContextId,
Compression: opts.Compression,
Controls: opts.Controls,
Creds: opts.Creds,
DurabilityLevel: opts.DurabilityLevel,
EncodedPlan: opts.EncodedPlan,
Encoding: opts.Encoding,
Format: opts.Format,
KvTimeout: opts.KvTimeout,
MaxParallelism: opts.MaxParallelism,
MemoryQuota: opts.MemoryQuota,
Metrics: opts.Metrics,
Namespace: opts.Namespace,
NumAtrs: opts.NumAtrs,
PipelineBatch: opts.PipelineBatch,
PipelineCap: opts.PipelineCap,
Prepared: opts.Prepared,
PreserveExpiry: opts.PreserveExpiry,
Pretty: opts.Pretty,
Profile: opts.Profile,
QueryContext: opts.QueryContext,
ReadOnly: opts.ReadOnly,
ScanCap: opts.ScanCap,
ScanConsistency: opts.ScanConsistency,
ScanVector: opts.ScanVector,
ScanVectors: opts.ScanVectors,
ScanWait: opts.ScanWait,
Signature: opts.Signature,
Statement: opts.Statement,
Timeout: opts.Timeout,
TxData: opts.TxData,
TxId: opts.TxId,
TxImplicit: opts.TxImplicit,
TxStmtNum: opts.TxStmtNum,
TxTimeout: opts.TxTimeout,
UseCbo: opts.UseCbo,
UseFts: opts.UseFts,
NamedArgs: opts.NamedArgs,
Raw: opts.Raw,
OnBehalfOf: opts.OnBehalfOf,
})
if err != nil {
return nil, err
}
return &queryResultStream{
ResultStream: res,
endpoint: endpointId,
}, nil
})
})
}
type EnsureQueryIndexCreatedOptions struct {
BucketName string
ScopeName string
CollectionName string
IndexName string
OnBehalfOf *cbhttpx.OnBehalfOfInfo
}
func (w *QueryComponent) EnsureIndexCreated(ctx context.Context, opts *EnsureQueryIndexCreatedOptions) error {
hlpr := cbqueryx.EnsureIndexHelper{
Logger: w.logger.Named("ensure-index"),
UserAgent: w.userAgent,
OnBehalfOf: opts.OnBehalfOf,
BucketName: opts.BucketName,
ScopeName: opts.ScopeName,
CollectionName: opts.CollectionName,
IndexName: opts.IndexName,
}
backoff := ExponentialBackoff(100*time.Millisecond, 1*time.Second, 1.5)
return w.ensureResource(ctx, backoff, func(ctx context.Context, roundTripper http.RoundTripper,
ensureTargets baseHttpTargets) (bool, error) {
return hlpr.PollCreated(ctx, &cbqueryx.EnsureIndexPollOptions{
Transport: roundTripper,
Targets: ensureTargets.ToQueryx(),
})
})
}
type EnsureQueryIndexDroppedOptions struct {
BucketName string
ScopeName string
CollectionName string
IndexName string
OnBehalfOf *cbhttpx.OnBehalfOfInfo
}
func (w *QueryComponent) EnsureIndexDropped(ctx context.Context, opts *EnsureQueryIndexDroppedOptions) error {
hlpr := cbqueryx.EnsureIndexHelper{
Logger: w.logger.Named("ensure-index"),
UserAgent: w.userAgent,
OnBehalfOf: opts.OnBehalfOf,
BucketName: opts.BucketName,
ScopeName: opts.ScopeName,
CollectionName: opts.CollectionName,
IndexName: opts.IndexName,
}
backoff := ExponentialBackoff(100*time.Millisecond, 1*time.Second, 1.5)
return w.ensureResource(ctx, backoff, func(ctx context.Context, roundTripper http.RoundTripper,
ensureTargets baseHttpTargets) (bool, error) {
return hlpr.PollDropped(ctx, &cbqueryx.EnsureIndexPollOptions{
Transport: roundTripper,
Targets: ensureTargets.ToQueryx(),
})
})
}
func (w *QueryComponent) GetAllIndexes(ctx context.Context, opts *cbqueryx.GetAllIndexesOptions) ([]cbqueryx.Index, error) {
return OrchestrateQueryMgmtCall(ctx, w, cbqueryx.Query.GetAllIndexes, opts)
}
func (w *QueryComponent) CreatePrimaryIndex(ctx context.Context, opts *cbqueryx.CreatePrimaryIndexOptions) error {
return OrchestrateNoResQueryMgmtCall(ctx, w, cbqueryx.Query.CreatePrimaryIndex, opts)
}
func (w *QueryComponent) CreateIndex(ctx context.Context, opts *cbqueryx.CreateIndexOptions) error {
return OrchestrateNoResQueryMgmtCall(ctx, w, cbqueryx.Query.CreateIndex, opts)
}
func (w *QueryComponent) DropPrimaryIndex(ctx context.Context, opts *cbqueryx.DropPrimaryIndexOptions) error {
return OrchestrateNoResQueryMgmtCall(ctx, w, cbqueryx.Query.DropPrimaryIndex, opts)
}
func (w *QueryComponent) DropIndex(ctx context.Context, opts *cbqueryx.DropIndexOptions) error {
return OrchestrateNoResQueryMgmtCall(ctx, w, cbqueryx.Query.DropIndex, opts)
}
func (w *QueryComponent) BuildDeferredIndexes(ctx context.Context, opts *cbqueryx.BuildDeferredIndexesOptions) ([]cbqueryx.DeferredIndexName, error) {
return OrchestrateQueryMgmtCall(ctx, w, cbqueryx.Query.BuildDeferredIndexes, opts)
}