-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_BatchGetItem.go
376 lines (300 loc) · 11.8 KB
/
op_BatchGetItem.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
package dyno
import (
"context"
"fmt"
"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"
"sync"
)
// BatchGetItem executes BatchGetItem operation and returns a BatchGetItem operation
func (s *Session) BatchGetItem(ctx context.Context, input *ddb.BatchGetItemInput, mw ...BatchGetItemMiddleWare) *BatchGetItem {
return NewBatchGetItem(input, mw...).Invoke(ctx, s.ddb)
}
// BatchGetItem executes a BatchGetItem operation with a BatchGetItemInput in this pool and returns the BatchGetItem operation
func (p *Pool) BatchGetItem(input *ddb.BatchGetItemInput, mw ...BatchGetItemMiddleWare) *BatchGetItem {
op := NewBatchGetItem(input, mw...)
p.Do(op) // run the operation in the pool
return op
}
// BatchGetItemAll executes BatchGetItem operation and returns a BatchGetItem operation
func (s *Session) BatchGetItemAll(ctx context.Context, input *ddb.BatchGetItemInput, mw ...BatchGetItemMiddleWare) *BatchGetItem {
return NewBatchGetItemAll(input, mw...).Invoke(ctx, s.ddb)
}
// BatchGetItemAll executes a BatchGetItem operation with a BatchGetItemInput in this pool and returns the BatchGetItem operation
func (p *Pool) BatchGetItemAll(input *ddb.BatchGetItemInput, mw ...BatchGetItemMiddleWare) *BatchGetItem {
op := NewBatchGetItemAll(input, mw...)
p.Do(op) // run the operation in the pool
return op
}
// BatchGetItemContext represents an exhaustive BatchGetItem operation request context
type BatchGetItemContext struct {
context.Context
Input *ddb.BatchGetItemInput
Client *ddb.Client
}
// BatchGetItemOutput represents the output for the BatchGetItem opration
type BatchGetItemOutput struct {
out *ddb.BatchGetItemOutput
err error
mu sync.RWMutex
}
// Set sets the output
func (o *BatchGetItemOutput) Set(out *ddb.BatchGetItemOutput, err error) {
o.mu.Lock()
o.out = out
o.err = err
o.mu.Unlock()
}
// Get gets the output
func (o *BatchGetItemOutput) Get() (out *ddb.BatchGetItemOutput, err error) {
o.mu.Lock()
out = o.out
err = o.err
o.mu.Unlock()
return
}
// BatchGetItemHandler represents a handler for BatchGetItem requests
type BatchGetItemHandler interface {
HandleBatchGetItem(ctx *BatchGetItemContext, output *BatchGetItemOutput)
}
// BatchGetItemHandlerFunc is a BatchGetItemHandler function
type BatchGetItemHandlerFunc func(ctx *BatchGetItemContext, output *BatchGetItemOutput)
// HandleBatchGetItem implements BatchGetItemHandler
func (h BatchGetItemHandlerFunc) HandleBatchGetItem(ctx *BatchGetItemContext, output *BatchGetItemOutput) {
h(ctx, output)
}
// BatchGetItemFinalHandler is the final BatchGetItemHandler that executes a dynamodb BatchGetItem operation
type BatchGetItemFinalHandler struct{}
// HandleBatchGetItem implements the BatchGetItemHandler
func (h *BatchGetItemFinalHandler) HandleBatchGetItem(ctx *BatchGetItemContext, output *BatchGetItemOutput) {
output.Set(ctx.Client.BatchGetItem(ctx, ctx.Input))
}
// BatchGetItemAllFinalHandler is the final BatchGetItemAllHandler that executes a dynamodb BatchGetItemAll operation
type BatchGetItemAllFinalHandler struct{}
// HandleBatchGetItem implements the BatchGetItemHandler
func (h *BatchGetItemAllFinalHandler) HandleBatchGetItem(ctx *BatchGetItemContext, output *BatchGetItemOutput) {
var (
out, finalOut *ddb.BatchGetItemOutput
err error
)
finalOut = &ddb.BatchGetItemOutput{
Responses: make(map[string][]map[string]ddbTypes.AttributeValue),
UnprocessedKeys: make(map[string]ddbTypes.KeysAndAttributes),
}
defer func() { output.Set(finalOut, err) }()
// copy the scan so we're not mutating the original
input := CopyBatchGetItem(ctx.Input)
for {
if out, err = ctx.Client.BatchGetItem(ctx, input); err != nil {
return
}
finalOut.ConsumedCapacity = append(finalOut.ConsumedCapacity, out.ConsumedCapacity...)
for k, avs := range out.Responses {
if _, ok := finalOut.Responses[k]; !ok {
finalOut.Responses[k] = []map[string]ddbTypes.AttributeValue{}
}
for _, av := range avs {
finalOut.Responses[k] = append(finalOut.Responses[k], CopyAttributeValueMap(av))
}
}
if out.UnprocessedKeys == nil || len(out.UnprocessedKeys) == 0 {
// no more work
return
}
input.RequestItems = CopyKeysAndAttributesMap(out.UnprocessedKeys)
}
}
// BatchGetItemMiddleWare is a middleware function use for wrapping BatchGetItemHandler requests
type BatchGetItemMiddleWare interface {
BatchGetItemMiddleWare(next BatchGetItemHandler) BatchGetItemHandler
}
// BatchGetItemMiddleWareFunc is a functional BatchGetItemMiddleWare
type BatchGetItemMiddleWareFunc func(next BatchGetItemHandler) BatchGetItemHandler
// BatchGetItemMiddleWare implements the BatchGetItemMiddleWare interface
func (mw BatchGetItemMiddleWareFunc) BatchGetItemMiddleWare(next BatchGetItemHandler) BatchGetItemHandler {
return mw(next)
}
// BatchGetItem represents a BatchGetItem operation
type BatchGetItem struct {
*BaseOperation
Handler BatchGetItemHandler
input *ddb.BatchGetItemInput
middleWares []BatchGetItemMiddleWare
}
// NewBatchGetItem creates a new BatchGetItem
func NewBatchGetItem(input *ddb.BatchGetItemInput, mws ...BatchGetItemMiddleWare) *BatchGetItem {
return &BatchGetItem{
BaseOperation: NewOperation(),
Handler: new(BatchGetItemFinalHandler),
input: input,
middleWares: mws,
}
}
// NewBatchGetItemAll creates a new BatchGetItemAll
func NewBatchGetItemAll(input *ddb.BatchGetItemInput, mws ...BatchGetItemMiddleWare) *BatchGetItem {
return &BatchGetItem{
BaseOperation: NewOperation(),
Handler: new(BatchGetItemAllFinalHandler),
input: input,
middleWares: mws,
}
}
// Invoke invokes the BatchGetItem operation in a goroutine and returns a BatchGetItemAllPromise
func (op *BatchGetItem) Invoke(ctx context.Context, client *ddb.Client) *BatchGetItem {
op.SetRunning() // promise now waiting for a response
go op.InvokeDynoOperation(ctx, client)
return op
}
// InvokeDynoOperation invokes the BatchGetItem operation
func (op *BatchGetItem) InvokeDynoOperation(ctx context.Context, client *ddb.Client) {
output := new(BatchGetItemOutput)
defer func() { op.SetResponse(output.Get()) }()
var h BatchGetItemHandler
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].BatchGetItemMiddleWare(h)
}
}
requestCtx := &BatchGetItemContext{
Context: ctx,
Client: client,
Input: op.input,
}
h.HandleBatchGetItem(requestCtx, output)
}
// Await waits for the BatchGetItemPromise to be fulfilled and then returns a BatchGetItemOutput and error
func (op *BatchGetItem) Await() (*ddb.BatchGetItemOutput, error) {
out, err := op.BaseOperation.Await()
if out == nil {
return nil, err
}
return out.(*ddb.BatchGetItemOutput), err
}
func NewBatchGetItemInput() *ddb.BatchGetItemInput {
return &ddb.BatchGetItemInput{
RequestItems: make(map[string]ddbTypes.KeysAndAttributes),
ReturnConsumedCapacity: ddbTypes.ReturnConsumedCapacityNone,
}
}
// BatchGetItemBuilder used to dynamically build a BatchGetItemBuilder
type BatchGetItemBuilder struct {
*ddb.BatchGetItemInput
projection *expression.ProjectionBuilder
}
// NewBatchGetBuilder creates a new BatchGetItemBuilder
func NewBatchGetBuilder(input *ddb.BatchGetItemInput) *BatchGetItemBuilder {
if input != nil {
return &BatchGetItemBuilder{BatchGetItemInput: input}
}
return &BatchGetItemBuilder{BatchGetItemInput: NewBatchGetItemInput()}
}
// SetInput sets the BatchGetItemBuilder's dynamodb.BatchGetItemBuilder explicitly
func (bld *BatchGetItemBuilder) SetInput(input *ddb.BatchGetItemInput) {
bld.BatchGetItemInput = input
}
func (bld *BatchGetItemBuilder) initTable(tableName string) {
if _, ok := bld.RequestItems[tableName]; !ok {
bld.BatchGetItemInput.RequestItems[tableName] = ddbTypes.KeysAndAttributes{}
}
}
// AddProjection adds additional field names to the projection
func (bld *BatchGetItemBuilder) AddProjection(projection interface{}) *BatchGetItemBuilder {
addProjection(&bld.projection, projection)
return bld
}
// AddProjectionNames adds additional field names to the projection with strings
func (bld *BatchGetItemBuilder) AddProjectionNames(names ...string) *BatchGetItemBuilder {
addProjectionNames(&bld.projection, names)
return bld
}
// SetKeysAndAttributes sets the keys and attributes to get from the given table
func (bld *BatchGetItemBuilder) SetKeysAndAttributes(tableName string, keysAndAttributes ddbTypes.KeysAndAttributes) *BatchGetItemBuilder {
bld.RequestItems[tableName] = keysAndAttributes
return bld
}
// AddKey adds one or more keys to the request item map
func (bld *BatchGetItemBuilder) AddKey(tableName string, keys ...map[string]ddbTypes.AttributeValue) *BatchGetItemBuilder {
bld.initTable(tableName)
existingKeysAndAttributes := bld.RequestItems[tableName]
existingKeysAndAttributes.Keys = append(existingKeysAndAttributes.Keys, keys...)
bld.RequestItems[tableName] = existingKeysAndAttributes
return bld
}
// SetRequestItems sets the RequestItems field's value.
func (bld *BatchGetItemBuilder) SetRequestItems(v map[string]ddbTypes.KeysAndAttributes) *BatchGetItemBuilder {
bld.RequestItems = v
return bld
}
// SetReturnConsumedCapacity sets the ReturnConsumedCapacity field's value.
func (bld *BatchGetItemBuilder) SetReturnConsumedCapacity(v ddbTypes.ReturnConsumedCapacity) *BatchGetItemBuilder {
bld.ReturnConsumedCapacity = v
return bld
}
// Build builds and returns the dynamodb.BatchGetItemOutput
func (bld *BatchGetItemBuilder) Build() (*ddb.BatchGetItemInput, error) {
// remove all inputs that don't have any keys associated
for tableName, keys := range bld.RequestItems {
if keys.Keys == nil || len(keys.Keys) < 1 {
delete(bld.RequestItems, tableName)
}
}
if bld.projection != nil {
// only use expression builder if we have a projection or a filter
eb := expression.NewBuilder()
eb = eb.WithProjection(*bld.projection)
// build the Expression
expr, err := eb.Build()
if err != nil {
return nil, fmt.Errorf("BatchGetItemBuilder.GetDynamoGlobalSecondaryIndex() encountered an error while building an expression: %v", err)
}
exprNames := expr.Names()
exprProj := expr.Projection()
for _, item := range bld.RequestItems {
item.ExpressionAttributeNames = exprNames
item.ProjectionExpression = exprProj
}
}
return bld.BatchGetItemInput, nil
}
// CopyBatchGetItem creates a deep copy of a BatchGetItemInput
func CopyBatchGetItem(input *ddb.BatchGetItemInput) *ddb.BatchGetItemInput {
clone := &ddb.BatchGetItemInput{
ReturnConsumedCapacity: input.ReturnConsumedCapacity,
}
if input.RequestItems != nil {
input.RequestItems = CopyKeysAndAttributesMap(input.RequestItems)
}
return clone
}
// ChunkBatchGetItemInputs chunks the input dynamodb.BatchGetItemBuilder into chunks of the given chunkSize
// note: chunks are not deep copies!!
func ChunkBatchGetItemInputs(input *ddb.BatchGetItemInput, chunkSize int) (out []*ddb.BatchGetItemInput) {
for tableName, keysAndAttributes := range input.RequestItems {
sliceOfKeys := make([][]map[string]ddbTypes.AttributeValue, 0)
for i := 0; i < len(keysAndAttributes.Keys); i += chunkSize {
end := i + chunkSize
if end > len(keysAndAttributes.Keys) {
end = len(keysAndAttributes.Keys)
}
sliceOfKeys = append(sliceOfKeys, keysAndAttributes.Keys[i:end])
}
for _, slice := range sliceOfKeys {
newInput := CopyBatchGetItem(input)
newInput.RequestItems = map[string]ddbTypes.KeysAndAttributes{
tableName: {
AttributesToGet: keysAndAttributes.AttributesToGet,
ConsistentRead: keysAndAttributes.ConsistentRead,
ExpressionAttributeNames: keysAndAttributes.ExpressionAttributeNames,
Keys: slice,
ProjectionExpression: keysAndAttributes.ProjectionExpression,
},
}
out = append(out, newInput)
}
}
return
}