-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_delete_items.go
54 lines (48 loc) · 1.4 KB
/
batch_delete_items.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
package dynago
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
/**
* Used to batch delete records from dynamodb
* @param input slice of record want to put to DB
* @return error
*/
func (t *Client) BatchDeleteItems(ctx context.Context, input []map[string]types.AttributeValue) []map[string]types.AttributeValue {
items := make([]types.WriteRequest, 0, len(input))
errorRequests := make([]types.WriteRequest, 0, len(input))
failedItems := make([]map[string]types.AttributeValue, 0, len(input))
table := t.TableName
for _, model := range input {
items = append(items,
types.WriteRequest{
DeleteRequest: &types.DeleteRequest{
Key: model,
},
},
)
}
chunkedItems := chunkBy(items, ChunkSize)
for _, chunkedBatch := range chunkedItems {
output, err := t.client.BatchWriteItem(ctx, &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]types.WriteRequest{
table: chunkedBatch,
},
})
if err != nil {
errorRequests = append(errorRequests, chunkedBatch...)
} else {
if len(output.UnprocessedItems) > 0 {
unprocessedItems := output.UnprocessedItems[table]
errorRequests = append(errorRequests, unprocessedItems...)
}
}
}
if len(errorRequests) > 0 {
for _, failedReq := range errorRequests {
failedItems = append(failedItems, failedReq.DeleteRequest.Key)
}
}
return failedItems
}