/
op_Query.go
524 lines (424 loc) · 14.4 KB
/
op_Query.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
package dyno
import (
"context"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression"
ddb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
ddbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ericmaustin/dyno/condition"
"sync"
)
// Query executes Query operation and returns a Query operation
func (s *Session) Query(ctx context.Context, input *ddb.QueryInput, mw ...QueryMiddleWare) *Query {
return NewQuery(input, mw...).Invoke(ctx, s.ddb)
}
// Query executes a Query operation with a QueryInput in this pool and returns the Query operation
func (p *Pool) Query(input *ddb.QueryInput, mw ...QueryMiddleWare) *Query {
op := NewQuery(input, mw...)
p.Do(op) // run the operation in the pool
return op
}
// QueryAll executes Query operation and returns a Query operation that runs as many queries as it takes
// to get all of the values
func (s *Session) QueryAll(ctx context.Context, input *ddb.QueryInput, mw ...QueryMiddleWare) *Query {
return NewQueryAll(input, mw...).Invoke(ctx, s.ddb)
}
// QueryAll executes a Query operation with a QueryInput in this pool and returns a Query operation
// that runs as many queries as it takes to get all of the values
func (p *Pool) QueryAll(input *ddb.QueryInput, mw ...QueryMiddleWare) *Query {
op := NewQueryAll(input, mw...)
p.Do(op) // run the operation in the pool
return op
}
// QueryContext represents an exhaustive Query operation request context
type QueryContext struct {
context.Context
Input *ddb.QueryInput
Client *ddb.Client
}
// QueryOutput represents the output for the Query opration
type QueryOutput struct {
out *ddb.QueryOutput
err error
mu sync.RWMutex
}
// Set sets the output
func (o *QueryOutput) Set(out *ddb.QueryOutput, err error) {
o.mu.Lock()
o.out = out
o.err = err
o.mu.Unlock()
}
// Get gets the output
func (o *QueryOutput) Get() (out *ddb.QueryOutput, err error) {
o.mu.Lock()
out = o.out
err = o.err
o.mu.Unlock()
return
}
// QueryHandler represents a handler for Query requests
type QueryHandler interface {
HandleQuery(ctx *QueryContext, output *QueryOutput)
}
// QueryHandlerFunc is a QueryHandler function
type QueryHandlerFunc func(ctx *QueryContext, output *QueryOutput)
// HandleQuery implements QueryHandler
func (h QueryHandlerFunc) HandleQuery(ctx *QueryContext, output *QueryOutput) {
h(ctx, output)
}
// QueryFinalHandler is the final QueryHandler that executes a dynamodb Query operation
type QueryFinalHandler struct{}
// HandleQuery implements the QueryHandler
func (h *QueryFinalHandler) HandleQuery(ctx *QueryContext, output *QueryOutput) {
output.Set(ctx.Client.Query(ctx, ctx.Input))
}
// QueryAllFinalHandler is the final QueryAllHandler that executes a dynamodb QueryAll operation
type QueryAllFinalHandler struct{}
// HandleQuery implements the QueryHandler
func (h *QueryAllFinalHandler) HandleQuery(ctx *QueryContext, output *QueryOutput) {
var (
out, finalOut *ddb.QueryOutput
err error
)
finalOut = new(ddb.QueryOutput)
defer func() { output.Set(finalOut, err) }()
// copy the scan so we're not mutating the original
input := CopyQuery(ctx.Input)
for {
if out, err = ctx.Client.Query(ctx, input); err != nil {
return
}
finalOut.Items = append(finalOut.Items, out.Items...)
finalOut.Count += out.Count
finalOut.ScannedCount += out.ScannedCount
if out.LastEvaluatedKey == nil || len(out.LastEvaluatedKey) == 0 {
// no more keys left
return
}
input.ExclusiveStartKey = out.LastEvaluatedKey
}
}
// QueryMiddleWare is a middleware function use for wrapping QueryHandler requests
type QueryMiddleWare interface {
QueryMiddleWare(next QueryHandler) QueryHandler
}
// QueryMiddleWareFunc is a functional QueryMiddleWare
type QueryMiddleWareFunc func(next QueryHandler) QueryHandler
// QueryMiddleWare implements the QueryMiddleWare interface
func (mw QueryMiddleWareFunc) QueryMiddleWare(next QueryHandler) QueryHandler {
return mw(next)
}
// Query represents a Query operation
type Query struct {
*BaseOperation
Handler QueryHandler
input *ddb.QueryInput
middleWares []QueryMiddleWare
}
// NewQuery creates a new Query
func NewQuery(input *ddb.QueryInput, mws ...QueryMiddleWare) *Query {
return &Query{
BaseOperation: NewOperation(),
Handler: new(QueryFinalHandler),
input: input,
middleWares: mws,
}
}
// NewQueryAll creates a new QueryAll
func NewQueryAll(input *ddb.QueryInput, mws ...QueryMiddleWare) *Query {
return &Query{
BaseOperation: NewOperation(),
Handler: new(QueryAllFinalHandler),
input: input,
middleWares: mws,
}
}
// Invoke invokes the Query operation in a goroutine and returns a BatchGetItemAllPromise
func (op *Query) Invoke(ctx context.Context, client *ddb.Client) *Query {
op.SetRunning() // promise now waiting for a response
go op.InvokeDynoOperation(ctx, client)
return op
}
// InvokeDynoOperation invokes the Query operation
func (op *Query) InvokeDynoOperation(ctx context.Context, client *ddb.Client) {
output := new(QueryOutput)
defer func() { op.SetResponse(output.Get()) }()
var h QueryHandler
h = op.Handler
// no middlewares
if len(op.middleWares) > 0 {
// loop in reverse to preserve middleware order
for i := len(op.middleWares) - 1; i >= 0; i-- {
h = op.middleWares[i].QueryMiddleWare(h)
}
}
requestCtx := &QueryContext{
Context: ctx,
Client: client,
Input: op.input,
}
h.HandleQuery(requestCtx, output)
}
// Await waits for the QueryPromise to be fulfilled and then returns a QueryOutput and error
func (op *Query) Await() (*ddb.QueryOutput, error) {
out, err := op.BaseOperation.Await()
if out == nil {
return nil, err
}
return out.(*ddb.QueryOutput), err
}
// NewQueryInput creates a new QueryInput with a table name
func NewQueryInput(tableName *string) *ddb.QueryInput {
return &ddb.QueryInput{
TableName: tableName,
ReturnConsumedCapacity: ddbTypes.ReturnConsumedCapacityNone,
Select: ddbTypes.SelectAllAttributes,
}
}
// QueryBuilder dynamically constructs a QueryInput
type QueryBuilder struct {
*ddb.QueryInput
keyCnd condition.KeyConditionBuilder
filter condition.Builder
projection *expression.ProjectionBuilder
}
// NewQueryBuilder creates a new QueryBuilder builder with QueryOpt
func NewQueryBuilder(input *ddb.QueryInput) *QueryBuilder {
if input != nil {
return &QueryBuilder{QueryInput: input}
}
return &QueryBuilder{QueryInput: NewQueryInput(nil)}
}
// SetAscOrder sets the query to return in ascending order
func (bld *QueryBuilder) SetAscOrder() *QueryBuilder {
b := true
bld.ScanIndexForward = &b
return bld
}
// SetDescOrder sets the query to return in descending order
func (bld *QueryBuilder) SetDescOrder() *QueryBuilder {
b := false
bld.ScanIndexForward = &b
return bld
}
// AddKeyCondition adds a key condition to this update
// adding multiple conditions by calling this multiple times will join the conditions with
// an AND
func (bld *QueryBuilder) AddKeyCondition(cnd expression.KeyConditionBuilder) *QueryBuilder {
bld.keyCnd.And(cnd)
return bld
}
// AddKeyEquals adds a equality key condition for the given fieldName and value
// this is a shortcut for adding an equality condition which is common for queries
func (bld *QueryBuilder) AddKeyEquals(fieldName string, value interface{}) *QueryBuilder {
return bld.AddKeyCondition(condition.KeyEqual(fieldName, value))
}
// AddFilter adds a filter condition to this update
// adding multiple conditions by calling this multiple times will join the conditions with
// an AND
func (bld *QueryBuilder) AddFilter(cnd expression.ConditionBuilder) *QueryBuilder {
bld.filter.And(cnd)
return bld
}
// AddProjection additional fields to the projection
func (bld *QueryBuilder) AddProjection(names interface{}) *QueryBuilder {
addProjection(&bld.projection, names)
return bld
}
// AddProjectionNames adds additional field names to the projection
func (bld *QueryBuilder) AddProjectionNames(names ...string) *QueryBuilder {
addProjectionNames(&bld.projection, names)
return bld
}
// SetAttributesToGet sets the AttributesToGet field's value.
func (bld *QueryBuilder) SetAttributesToGet(v []string) *QueryBuilder {
bld.AttributesToGet = v
return bld
}
// SetConditionalOperator sets the ConditionalOperator field's value.
func (bld *QueryBuilder) SetConditionalOperator(v ddbTypes.ConditionalOperator) *QueryBuilder {
bld.ConditionalOperator = v
return bld
}
// SetConsistentRead sets the ConsistentRead field's value.
func (bld *QueryBuilder) SetConsistentRead(v bool) *QueryBuilder {
bld.ConsistentRead = &v
return bld
}
// SetExclusiveStartKey sets the ExclusiveStartKey field's value.
func (bld *QueryBuilder) SetExclusiveStartKey(v map[string]ddbTypes.AttributeValue) *QueryBuilder {
bld.ExclusiveStartKey = v
return bld
}
// SetExpressionAttributeNames sets the ExpressionAttributeNames field's value.
func (bld *QueryBuilder) SetExpressionAttributeNames(v map[string]string) *QueryBuilder {
bld.ExpressionAttributeNames = v
return bld
}
// SetExpressionAttributeValues sets the ExpressionAttributeValues field's value.
func (bld *QueryBuilder) SetExpressionAttributeValues(v map[string]ddbTypes.AttributeValue) *QueryBuilder {
bld.ExpressionAttributeValues = v
return bld
}
// SetFilterExpression sets the FilterExpression field's value.
func (bld *QueryBuilder) SetFilterExpression(v string) *QueryBuilder {
bld.FilterExpression = &v
return bld
}
// SetIndexName sets the IndexName field's value.
func (bld *QueryBuilder) SetIndexName(v string) *QueryBuilder {
bld.IndexName = &v
return bld
}
// SetKeyConditionExpression sets the KeyConditionExpression field's value.
func (bld *QueryBuilder) SetKeyConditionExpression(v string) *QueryBuilder {
bld.KeyConditionExpression = &v
return bld
}
// SetKeyConditions sets the KeyConditions field's value.
func (bld *QueryBuilder) SetKeyConditions(v map[string]ddbTypes.Condition) *QueryBuilder {
bld.KeyConditions = v
return bld
}
// SetLimit sets the Limit field's value.
func (bld *QueryBuilder) SetLimit(v int32) *QueryBuilder {
bld.Limit = &v
return bld
}
// SetProjectionExpression sets the ProjectionExpression field's value.
func (bld *QueryBuilder) SetProjectionExpression(v string) *QueryBuilder {
bld.ProjectionExpression = &v
return bld
}
// SetQueryFilter sets the QueryFilter field's value.
func (bld *QueryBuilder) SetQueryFilter(v map[string]ddbTypes.Condition) *QueryBuilder {
bld.QueryFilter = v
return bld
}
// SetReturnConsumedCapacity sets the ReturnConsumedCapacity field's value.
func (bld *QueryBuilder) SetReturnConsumedCapacity(v ddbTypes.ReturnConsumedCapacity) *QueryBuilder {
bld.ReturnConsumedCapacity = v
return bld
}
// SetScanIndexForward sets the ScanIndexForward field's value.
func (bld *QueryBuilder) SetScanIndexForward(v bool) *QueryBuilder {
bld.ScanIndexForward = &v
return bld
}
// SetSelect sets the Select field's value.
func (bld *QueryBuilder) SetSelect(v ddbTypes.Select) *QueryBuilder {
bld.Select = v
return bld
}
// SetTableName sets the TableName field's value.
func (bld *QueryBuilder) SetTableName(v string) *QueryBuilder {
bld.TableName = &v
return bld
}
// Build builds the dynamodb.QueryInput
func (bld *QueryBuilder) Build() (*ddb.QueryInput, error) {
if bld.projection == nil && bld.keyCnd.Empty() && bld.filter.Empty() {
// no expression builder is needed
return bld.QueryInput, nil
}
builder := expression.NewBuilder()
// add projection
if bld.projection != nil {
builder = builder.WithProjection(*bld.projection)
bld.Select = ddbTypes.SelectSpecificAttributes
}
// add key condition
if !bld.keyCnd.Empty() {
builder = builder.WithKeyCondition(bld.keyCnd.Builder())
}
// add filter
if !bld.filter.Empty() {
builder = builder.WithFilter(bld.filter.Builder())
}
// build the Expression
expr, err := builder.Build()
if err != nil {
return nil, err
}
bld.ExpressionAttributeNames = expr.Names()
bld.ExpressionAttributeValues = expr.Values()
bld.FilterExpression = expr.Filter()
bld.KeyConditionExpression = expr.KeyCondition()
bld.ProjectionExpression = expr.Projection()
return bld.QueryInput, nil
}
// CopyQuery creates a deep copy of a QueryInput
// note: CopyQuery does not copy legacy parameters
func CopyQuery(input *ddb.QueryInput) *ddb.QueryInput {
clone := &ddb.QueryInput{
ConditionalOperator: input.ConditionalOperator,
ReturnConsumedCapacity: input.ReturnConsumedCapacity,
Select: input.Select,
}
if input.TableName != nil {
clone.TableName = new(string)
*clone.TableName = *input.TableName
}
if input.AttributesToGet != nil {
copy(clone.AttributesToGet, input.AttributesToGet)
}
if input.ConsistentRead != nil {
clone.ConsistentRead = new(bool)
*clone.ConsistentRead = *input.ConsistentRead
}
if input.ExclusiveStartKey != nil {
clone.ExclusiveStartKey = make(map[string]ddbTypes.AttributeValue, len(input.ExclusiveStartKey))
for k, v := range input.ExclusiveStartKey {
clone.ExclusiveStartKey[k] = CopyAttributeValue(v)
}
}
if input.ExpressionAttributeNames != nil {
clone.ExpressionAttributeNames = make(map[string]string, len(input.ExpressionAttributeNames))
for k, v := range input.ExpressionAttributeNames {
clone.ExpressionAttributeNames[k] = v
}
}
if input.ExpressionAttributeValues != nil {
clone.ExpressionAttributeValues = make(map[string]ddbTypes.AttributeValue, len(input.ExpressionAttributeValues))
for k, v := range input.ExpressionAttributeValues {
clone.ExpressionAttributeValues[k] = CopyAttributeValue(v)
}
}
if input.KeyConditions != nil {
clone.KeyConditions = make(map[string]ddbTypes.Condition)
for k, v := range input.KeyConditions {
clone.KeyConditions[k] = CopyCondition(v)
}
}
if input.KeyConditionExpression != nil {
clone.KeyConditionExpression = new(string)
*clone.KeyConditionExpression = *input.KeyConditionExpression
}
if input.FilterExpression != nil {
clone.FilterExpression = new(string)
*clone.FilterExpression = *input.FilterExpression
}
if input.IndexName != nil {
clone.IndexName = new(string)
*clone.IndexName = *input.IndexName
}
if input.Limit != nil {
clone.Limit = new(int32)
*clone.Limit = *input.Limit
}
if input.ProjectionExpression != nil {
clone.ProjectionExpression = new(string)
*clone.ProjectionExpression = *input.ProjectionExpression
}
if input.QueryFilter != nil {
clone.QueryFilter = make(map[string]ddbTypes.Condition, len(input.QueryFilter))
for k, v := range input.QueryFilter {
clone.QueryFilter[k] = CopyCondition(v)
}
}
if input.TableName != nil {
clone.TableName = new(string)
*clone.TableName = *input.TableName
}
return clone
}