-
Notifications
You must be signed in to change notification settings - Fork 14
/
aws_dynamodb.go
181 lines (151 loc) · 5.25 KB
/
aws_dynamodb.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
package kv
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/brexhq/substation/config"
"github.com/brexhq/substation/internal/aws"
"github.com/brexhq/substation/internal/aws/dynamodb"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
)
// kvAWSDynamoDB is a read-write key-value store that is backed by an AWS DynamoDB table.
//
// This KV store supports per-item time-to-live (TTL) and has some limitations when
// interacting with DynamoDB:
//
// - Does not support Global Secondary Indexes
type kvAWSDynamoDB struct {
AWS iconfig.AWS `json:"aws"`
Retry iconfig.Retry `json:"retry"`
// TableName is the DynamoDB table that items are read and written to.
TableName string `json:"table_name"`
Attributes struct {
// PartitionKey is the table's parition key attribute.
//
// This is required for all tables.
PartitionKey string `json:"partition_key"`
// SortKey is the table's sort (range) key attribute.
//
// This must be used if the table uses a composite primary key schema
// (partition key and sort key). Only string types are supported.
SortKey string `json:"sort_key"`
// Value is the table attribute where values are read from and written to.
Value string `json:"value"`
// TTL is the table attribute where time-to-live is stored.
//
// This option requires the DynamoDB table to be configured with TTL. Learn more
// about DynamoDB's TTL implementation here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html.
TTL string `json:"ttl"`
} `json:"attributes"`
// ConsistentRead specifies whether or not to use strongly consistent reads.
//
// This is optional and defaults to false (eventually consistent reads).
ConsistentRead bool `json:"consistent_read"`
client dynamodb.API
}
// Create a new AWS DynamoDB KV store.
func newKVAWSDynamoDB(cfg config.Config) (*kvAWSDynamoDB, error) {
var store kvAWSDynamoDB
if err := iconfig.Decode(cfg.Settings, &store); err != nil {
return nil, err
}
if store.TableName == "" {
return nil, fmt.Errorf("kv: aws_dynamodb: table %+v: %v", &store, errors.ErrMissingRequiredOption)
}
return &store, nil
}
func (store *kvAWSDynamoDB) String() string {
return toString(store)
}
// Get retrieves an item from the DynamoDB table. If the item had a time-to-live (TTL)
// configured when it was added and the TTL has passed, then nothing is returned.
//
// This method uses the GetItem API call, which retrieves a single item from the table.
// Learn more about the differences between GetItem and other item retrieval API calls here:
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SQLtoNoSQL.ReadData.html.
func (store *kvAWSDynamoDB) Get(ctx context.Context, key string) (interface{}, error) {
m := map[string]interface{}{
store.Attributes.PartitionKey: key,
}
if store.Attributes.SortKey != "" {
m[store.Attributes.SortKey] = "substation:kv_store"
}
resp, err := store.client.GetItem(ctx, store.TableName, m, store.ConsistentRead)
if err != nil {
return "", err
}
if val, found := resp.Item[store.Attributes.Value]; found {
var i interface{}
if err := dynamodbattribute.Unmarshal(val, &i); err != nil {
return nil, err
}
return i, nil
}
return nil, nil
}
// SetWithTTL adds an item to the DynamoDB table.
func (store *kvAWSDynamoDB) Set(ctx context.Context, key string, val interface{}) error {
m := map[string]interface{}{
store.Attributes.PartitionKey: key,
store.Attributes.Value: val,
}
if store.Attributes.SortKey != "" {
m[store.Attributes.SortKey] = "substation:kv_store"
}
record, err := dynamodbattribute.MarshalMap(m)
if err != nil {
return err
}
if _, err := store.client.PutItem(ctx, store.TableName, record); err != nil {
return err
}
return nil
}
// SetWithTTL adds an item to the DynamoDB table with a time-to-live (TTL) attribute.
func (store *kvAWSDynamoDB) SetWithTTL(ctx context.Context, key string, val interface{}, ttl int64) error {
if store.Attributes.TTL == "" {
return errors.ErrMissingRequiredOption
}
m := map[string]interface{}{
store.Attributes.PartitionKey: key,
store.Attributes.Value: val,
store.Attributes.TTL: ttl,
}
if store.Attributes.SortKey != "" {
m[store.Attributes.SortKey] = "substation:kv_store"
}
record, err := dynamodbattribute.MarshalMap(m)
if err != nil {
return err
}
if _, err := store.client.PutItem(ctx, store.TableName, record); err != nil {
return err
}
return nil
}
// IsEnabled returns true if the DynamoDB client is ready for use.
func (store *kvAWSDynamoDB) IsEnabled() bool {
return store.client.IsEnabled()
}
// Setup creates a new DynamoDB client.
func (store *kvAWSDynamoDB) Setup(ctx context.Context) error {
if store.TableName == "" || store.Attributes.PartitionKey == "" {
return errors.ErrMissingRequiredOption
}
// Avoids unnecessary setup.
if store.client.IsEnabled() {
return nil
}
store.client.Setup(aws.Config{
Region: store.AWS.Region,
RoleARN: store.AWS.RoleARN,
MaxRetries: store.Retry.Count,
RetryableErrors: store.Retry.ErrorMessages,
})
return nil
}
// Close is unused since connections to DynamoDB are not stateful.
func (store *kvAWSDynamoDB) Close() error {
return nil
}