-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_updater.go
156 lines (139 loc) · 4.42 KB
/
batch_updater.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
package dynamodb
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"log"
"reflect"
)
type BatchUpdater struct {
DB *dynamodb.DynamoDB
tableName string
Map func(ctx context.Context, model interface{}) (interface{}, error)
keys []string
}
func NewBatchUpdaterById(database *dynamodb.DynamoDB, tableName string, modelType reflect.Type, fieldName string, keys []string, options ...func(context.Context, interface{}) (interface{}, error)) *BatchUpdater {
var mp func(context.Context, interface{}) (interface{}, error)
if len(options) >= 1 {
mp = options[0]
}
if len(fieldName) == 0 {
_, idName, _ := FindIdField(modelType)
fieldName = idName
}
return &BatchUpdater{Map: mp, DB: database,tableName: tableName, keys: keys}
}
func NewBatchUpdater(database *dynamodb.DynamoDB, tableName string, modelType reflect.Type, keys []string, options ...func(context.Context, interface{}) (interface{}, error)) *BatchUpdater {
return NewBatchUpdaterById(database, tableName, modelType, "", keys, options...)
}
func (w *BatchUpdater) Write(ctx context.Context, models interface{}) ([]int, []int, error) {
successIndices := make([]int, 0)
failIndices := make([]int, 0)
s := reflect.ValueOf(models)
var er1 error
if w.Map != nil {
m2, er0 := w.Map(ctx, models)
if er0 != nil {
return successIndices, failIndices, er0
}
_, _, er1 = UpdateMany(ctx, w.DB, w.tableName, w.keys, m2)
} else {
_, _, er1 = UpdateMany(ctx, w.DB, w.tableName, w.keys, models)
}
if er1 == nil {
for i := 0; i < s.Len(); i++ {
successIndices = append(successIndices, i)
}
return successIndices, failIndices, er1
}
return successIndices, failIndices, er1
}
func TransactionUpdate(ctx context.Context, db *dynamodb.DynamoDB, data []interface{}, tableName string, keys []string) (*dynamodb.TransactWriteItemsOutput, error) {
var listTransaction = make([]*dynamodb.TransactWriteItem, 0)
for _, d := range data {
ids := getIdValueFromModel(d, keys)
isExit, err := Exist(ctx, db, tableName, keys, ids)
if !isExit {
return nil, err
}
keyMap, err := buildKeyMap(keys, ids)
if err != nil {
return nil, err
}
av, _ := dynamodbattribute.MarshalMap(d)
expressionValues, expressionNames, expression := BuildUpdate(av, keys)
update := &dynamodb.Update{
ConditionExpression: nil,
ExpressionAttributeNames: expressionNames,
ExpressionAttributeValues: expressionValues,
Key: keyMap,
TableName: &tableName,
UpdateExpression: &expression,
}
transaction := &dynamodb.TransactWriteItem{
Update: update,
}
listTransaction = append(listTransaction, transaction)
}
input := &dynamodb.TransactWriteItemsInput{
TransactItems: listTransaction,
}
err := input.Validate()
if err != nil {
return nil, err
}
rs, err := db.TransactWriteItems(input)
if err != nil {
return nil, err
}
return rs, nil
}
func CheckKeys(k string, keys []string) bool {
for _, key := range keys {
if key == k {
return true
}
}
return false
}
func UpdateMany(ctx context.Context, db *dynamodb.DynamoDB, tableName string, keys []string, models interface{}) (interface{}, interface{}, error) {
arr := make([]interface{}, 0)
modelsType := reflect.TypeOf(models)
insertedFails := reflect.New(modelsType).Interface()
switch reflect.TypeOf(models).Kind() {
case reflect.Slice:
values := reflect.ValueOf(models)
if values.Len() == 0 {
return insertedFails, insertedFails, nil
}
for i := 0; i < values.Len(); i++ {
arr = append(arr, values.Index(i).Interface())
}
}
rs, err := TransactionUpdate(ctx, db, arr, tableName, keys)
if err != nil {
return nil, nil, err
}
log.Println(rs)
return nil, nil, err
}
func BuildUpdate(av map[string]*dynamodb.AttributeValue, keys []string) (map[string]*dynamodb.AttributeValue, map[string]*string, string) {
experssionValues := make(map[string]*dynamodb.AttributeValue)
expressionNames := make(map[string]*string)
updateExperssion := "set "
for i, v := range av {
if CheckKeys(i, keys) {
continue
}
key := fmt.Sprintf(":%s1", i)
experssionValues[key] = v
keyName := fmt.Sprintf("#%s", i)
keyNamePtr := i
expressionNames[keyName] = &keyNamePtr
str := fmt.Sprintf("%s = %s ,", keyName, key)
updateExperssion += str
}
updateExperssion = updateExperssion[:len(updateExperssion)-1]
return experssionValues, expressionNames, updateExperssion
}