forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
302 lines (273 loc) · 7.85 KB
/
tx.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
package dynamo
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/gofrs/uuid"
)
type getTxOp interface {
getTxItem() (*dynamodb.TransactGetItem, error)
}
// GetTx is a transaction to retrieve items.
// It can contain up to 10 operations and works across multiple tables.
// GetTx is analogous to TransactGetItems in DynamoDB's API.
// See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactGetItems.html
type GetTx struct {
db *DB
items []getTxOp
unmarshalers map[getTxOp]interface{}
cc *ConsumedCapacity
}
// GetTx begins a new get transaction.
func (db *DB) GetTx() *GetTx {
return &GetTx{
db: db,
}
}
// Get adds a get request to this transaction.
func (tx *GetTx) Get(q *Query) *GetTx {
tx.items = append(tx.items, q)
return tx
}
// GetOne adds a get request to this transaction, and specifies out to which the results are marshaled.
// Out must be a pointer. You can use this multiple times in one transaction.
func (tx *GetTx) GetOne(q *Query, out interface{}) *GetTx {
if tx.unmarshalers == nil {
tx.unmarshalers = make(map[getTxOp]interface{})
}
tx.items = append(tx.items, q)
tx.unmarshalers[q] = out
return tx
}
// ConsumedCapacity will measure the throughput capacity consumed by this transaction and add it to cc.
func (tx *GetTx) ConsumedCapacity(cc *ConsumedCapacity) *GetTx {
tx.cc = cc
return tx
}
// Run executes this transaction and unmarshals everything specified by GetOne.
func (tx *GetTx) Run() error {
ctx, cancel := defaultContext()
defer cancel()
return tx.RunWithContext(ctx)
}
// RunWithContext executes this transaction and unmarshals everything specified by GetOne.
func (tx *GetTx) RunWithContext(ctx aws.Context) error {
input, err := tx.input()
if err != nil {
return err
}
var resp *dynamodb.TransactGetItemsOutput
err = retry(ctx, func() error {
var err error
resp, err = tx.db.client.TransactGetItemsWithContext(ctx, input)
if tx.cc != nil && resp != nil {
for _, cc := range resp.ConsumedCapacity {
addConsumedCapacity(tx.cc, cc)
}
}
return err
})
if err != nil {
return err
}
if isResponsesEmpty(resp.Responses) {
return ErrNotFound
}
return tx.unmarshal(resp)
}
func (tx *GetTx) unmarshal(resp *dynamodb.TransactGetItemsOutput) error {
for i, item := range resp.Responses {
if item.Item == nil {
continue
}
if target := tx.unmarshalers[tx.items[i]]; target != nil {
if err := UnmarshalItem(item.Item, target); err != nil {
return err
}
}
}
return nil
}
// All executes this transaction and unmarshals every value to out, which must be a pointer to a slice.
func (tx *GetTx) All(out interface{}) error {
ctx, cancel := defaultContext()
defer cancel()
return tx.AllWithContext(ctx, out)
}
// AllWithContext executes this transaction and unmarshals every value to out, which must be a pointer to a slice.
func (tx *GetTx) AllWithContext(ctx aws.Context, out interface{}) error {
input, err := tx.input()
if err != nil {
return err
}
var resp *dynamodb.TransactGetItemsOutput
err = retry(ctx, func() error {
var err error
resp, err = tx.db.client.TransactGetItems(input)
if tx.cc != nil && resp != nil {
for _, cc := range resp.ConsumedCapacity {
addConsumedCapacity(tx.cc, cc)
}
}
return err
})
if err != nil {
return err
}
if isResponsesEmpty(resp.Responses) {
return ErrNotFound
}
if err := tx.unmarshal(resp); err != nil {
return err
}
for _, item := range resp.Responses {
if item.Item == nil {
continue
}
if err := unmarshalAppend(item.Item, out); err != nil {
return err
}
}
return nil
}
func (tx *GetTx) input() (*dynamodb.TransactGetItemsInput, error) {
input := &dynamodb.TransactGetItemsInput{}
for _, item := range tx.items {
tgi, err := item.getTxItem()
if err != nil {
return nil, err
}
input.TransactItems = append(input.TransactItems, tgi)
}
if tx.cc != nil {
input.ReturnConsumedCapacity = aws.String(dynamodb.ReturnConsumedCapacityIndexes)
}
return input, nil
}
type writeTxOp interface {
writeTxItem() (*dynamodb.TransactWriteItem, error)
}
// WriteTx is a transaction to delete, put, update, and check items.
// It can contain up to 10 operations and works across multiple tables.
// Two operations cannot target the same item.
// WriteTx is analogous to TransactWriteItems in DynamoDB's API.
// See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html
type WriteTx struct {
db *DB
items []writeTxOp
token string
cc *ConsumedCapacity
err error
}
// WriteTx begins a new write transaction.
func (db *DB) WriteTx() *WriteTx {
return &WriteTx{
db: db,
}
}
// Delete adds a new delete operation to this transaction.
func (tx *WriteTx) Delete(d *Delete) *WriteTx {
tx.items = append(tx.items, d)
return tx
}
// Put adds a put operation to this transaction.
func (tx *WriteTx) Put(p *Put) *WriteTx {
tx.items = append(tx.items, p)
return tx
}
// Update adds an update operation to this transaction.
func (tx *WriteTx) Update(u *Update) *WriteTx {
tx.items = append(tx.items, u)
return tx
}
// Check adds a conditional check to this transaction.
func (tx *WriteTx) Check(check *ConditionCheck) *WriteTx {
tx.items = append(tx.items, check)
return tx
}
// Idempotent marks this transaction as idempotent when enabled is true.
// This automatically generates a unique idempotency token for you.
// An idempotent transaction ran multiple times will have the same effect as being run once.
// An idempotent request is only good for 10 minutes, after that it will be considered a new request.
func (tx *WriteTx) Idempotent(enabled bool) *WriteTx {
if tx.token != "" && enabled {
return tx
}
if enabled {
uuid, err := uuid.NewV4()
tx.setError(err)
tx.token = uuid.String()
} else {
tx.token = ""
}
return tx
}
// IdempotentWithToken marks this transaction as idempotent and explicitly specifies the token value.
// If token is empty, idempotency will be disabled instead.
// Unless you have special circumstances that require a custom token, consider using Idempotent to generate a token for you.
// An idempotent transaction ran multiple times will have the same effect as being run once.
// An idempotent request (token) is only good for 10 minutes, after that it will be considered a new request.
func (tx *WriteTx) IdempotentWithToken(token string) *WriteTx {
tx.token = token
return tx
}
// ConsumedCapacity will measure the throughput capacity consumed by this transaction and add it to cc.
func (tx *WriteTx) ConsumedCapacity(cc *ConsumedCapacity) *WriteTx {
tx.cc = cc
return tx
}
// Run executes this transaction.
func (tx *WriteTx) Run() error {
ctx, cancel := defaultContext()
defer cancel()
return tx.RunWithContext(ctx)
}
// RunWithContext executes this transaction.
func (tx *WriteTx) RunWithContext(ctx aws.Context) error {
if tx.err != nil {
return tx.err
}
input, err := tx.input()
if err != nil {
return err
}
err = retry(ctx, func() error {
out, err := tx.db.client.TransactWriteItemsWithContext(ctx, input)
if tx.cc != nil && out != nil {
for _, cc := range out.ConsumedCapacity {
addConsumedCapacity(tx.cc, cc)
}
}
return err
})
return err
}
func (tx *WriteTx) input() (*dynamodb.TransactWriteItemsInput, error) {
input := &dynamodb.TransactWriteItemsInput{}
for _, item := range tx.items {
wti, err := item.writeTxItem()
if err != nil {
return nil, err
}
input.TransactItems = append(input.TransactItems, wti)
}
if tx.token != "" {
input.ClientRequestToken = aws.String(tx.token)
}
if tx.cc != nil {
input.ReturnConsumedCapacity = aws.String(dynamodb.ReturnConsumedCapacityIndexes)
}
return input, nil
}
func (tx *WriteTx) setError(err error) {
if tx.err == nil {
tx.err = err
}
}
func isResponsesEmpty(resps []*dynamodb.ItemResponse) bool {
for _, resp := range resps {
if resp.Item != nil {
return false
}
}
return true
}