/
transaction.go
67 lines (57 loc) · 1.59 KB
/
transaction.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
package dynamodbstore
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/pkg/errors"
"github.com/SKF/go-eventsource/eventsource"
)
type transaction struct {
store *store
ctx context.Context
records []eventsource.Record
saved []eventsource.Record
}
func (store *store) NewTransaction(ctx context.Context, records ...eventsource.Record) (eventsource.StoreTransaction, error) {
return &transaction{
store: store,
ctx: ctx,
records: records,
saved: []eventsource.Record{},
}, nil
}
// Commit ...
func (tx *transaction) Commit() (err error) {
for _, record := range tx.records {
result, err := dynamodbattribute.MarshalMap(record)
if err != nil {
return errors.Wrap(err, "couldn't marshal record")
}
_, err = tx.store.db.PutItemWithContext(tx.ctx, &dynamodb.PutItemInput{
TableName: &tx.store.tableName,
Item: result,
})
if err != nil {
return errors.Wrap(err, "couldn't put record to dynamodb store")
}
tx.saved = append(tx.saved, record)
}
return
}
func (tx *transaction) Rollback() error {
for _, record := range tx.saved {
_, err := tx.store.db.DeleteItemWithContext(tx.ctx, &dynamodb.DeleteItemInput{
TableName: &tx.store.tableName,
Key: map[string]*dynamodb.AttributeValue{
"aggregateId": {S: &record.AggregateID},
"timestamp": {N: aws.String(fmt.Sprintf("%d", record.Timestamp))},
},
})
if err != nil {
return errors.Wrap(err, "couldn't delete record in dynamodb store")
}
}
return nil
}