forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttl.go
130 lines (112 loc) · 3.84 KB
/
ttl.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
package dynamo
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// UpdateTTL is a request to enable or disable a table's time to live functionality.
// Note that when time to live is enabled, items will typically be deleted within 48 hours
// and items that are expired but not yet deleted will still appear in your database.
// See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTimeToLive.html
type UpdateTTL struct {
table Table
attrib string
enabled bool
}
// UpdateTTL begins a new request to enable or disable this table's time to live.
// The name of the attribute to use for expiring items is specified by attribute.
// TTL will be enabled when enabled is true and disabled when it is false.
// The time to live attribute must be stored as Unix time in seconds.
// Items without this attribute won't be deleted.
func (table Table) UpdateTTL(attribute string, enabled bool) *UpdateTTL {
return &UpdateTTL{
table: table,
attrib: attribute,
enabled: enabled,
}
}
// Run executes this request.
func (ttl *UpdateTTL) Run() error {
ctx, cancel := defaultContext()
defer cancel()
return ttl.RunWithContext(ctx)
}
// RunWithContext executes this request.
func (ttl *UpdateTTL) RunWithContext(ctx aws.Context) error {
input := ttl.input()
err := retry(ctx, func() error {
_, err := ttl.table.db.client.UpdateTimeToLiveWithContext(ctx, input)
return err
})
return err
}
func (ttl *UpdateTTL) input() *dynamodb.UpdateTimeToLiveInput {
return &dynamodb.UpdateTimeToLiveInput{
TableName: aws.String(ttl.table.Name()),
TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{
Enabled: aws.Bool(ttl.enabled),
AttributeName: aws.String(ttl.attrib),
},
}
}
// DescribeTTL is a request to obtain details about a table's time to live configuration.
type DescribeTTL struct {
table Table
}
// DescribeTTL begins a new request to obtain details about this table's time to live configuration.
func (table Table) DescribeTTL() *DescribeTTL {
return &DescribeTTL{table}
}
// Run executes this request and returns details about time to live, or an error.
func (d *DescribeTTL) Run() (TTLDescription, error) {
ctx, cancel := defaultContext()
defer cancel()
return d.RunWithContext(ctx)
}
// RunWithContext executes this request and returns details about time to live, or an error.
func (d *DescribeTTL) RunWithContext(ctx aws.Context) (TTLDescription, error) {
input := d.input()
var result *dynamodb.DescribeTimeToLiveOutput
err := retry(ctx, func() error {
var err error
result, err = d.table.db.client.DescribeTimeToLiveWithContext(ctx, input)
return err
})
if err != nil {
return TTLDescription{}, err
}
desc := TTLDescription{
Status: TTLDisabled,
}
if result.TimeToLiveDescription.TimeToLiveStatus != nil {
desc.Status = TTLStatus(*result.TimeToLiveDescription.TimeToLiveStatus)
}
if result.TimeToLiveDescription.AttributeName != nil {
desc.Attribute = *result.TimeToLiveDescription.AttributeName
}
return desc, nil
}
func (d *DescribeTTL) input() *dynamodb.DescribeTimeToLiveInput {
return &dynamodb.DescribeTimeToLiveInput{
TableName: aws.String(d.table.Name()),
}
}
// TTLDescription represents time to live configuration details for a table.
type TTLDescription struct {
// Attribute is the name of the time to live attribute for the table. Empty if disabled.
Attribute string
// Status is the table's time to live status.
Status TTLStatus
}
// Enabled returns true if time to live is enabled (and has finished enabling).
func (td TTLDescription) Enabled() bool {
return td.Status == TTLEnabled
}
// TTLStatus represents a table's time to live status.
type TTLStatus string
// Possible time to live statuses.
const (
TTLEnabled TTLStatus = "ENABLED"
TTLEnabling TTLStatus = "ENABLING"
TTLDisabled TTLStatus = "DISABLED"
TTLDisabling TTLStatus = "DISABLING"
)