forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
118 lines (105 loc) · 3.04 KB
/
table.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
package dynamo
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Status is an enumeration of table and index statuses.
type Status string
const (
// The table or index is ready for use.
ActiveStatus Status = "ACTIVE"
// The table or index is being created.
CreatingStatus = "CREATING"
// The table or index is being updated.
UpdatingStatus = "UPDATING"
// The table or index is being deleted.
DeletingStatus = "DELETING"
)
// Table is a DynamoDB table.
type Table struct {
name string
db *DB
}
// Table returns a Table handle specified by name.
func (db *DB) Table(name string) Table {
return Table{
name: name,
db: db,
}
}
// Name returns this table's name.
func (table Table) Name() string {
return table.name
}
// DeleteTable is a request to delete a table.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteTable.html
type DeleteTable struct {
table Table
}
// DeleteTable begins a new request to delete this table.
func (table Table) DeleteTable() *DeleteTable {
return &DeleteTable{table: table}
}
// Run executes this request and deletes the table.
func (dt *DeleteTable) Run() error {
ctx, cancel := defaultContext()
defer cancel()
return dt.RunWithContext(ctx)
}
// RunWithContext executes this request and deletes the table.
func (dt *DeleteTable) RunWithContext(ctx aws.Context) error {
input := dt.input()
return retry(ctx, func() error {
_, err := dt.table.db.client.DeleteTableWithContext(ctx, input)
return err
})
}
func (dt *DeleteTable) input() *dynamodb.DeleteTableInput {
name := dt.table.Name()
return &dynamodb.DeleteTableInput{
TableName: &name,
}
}
// ConsumedCapacity represents the amount of throughput capacity consumed during an operation.
type ConsumedCapacity struct {
// Total is the total number of capacity units consumed during this operation.
Total float64
// GSI is a map of Global Secondary Index names to consumed capacity units.
GSI map[string]float64
// GSI is a map of Local Secondary Index names to consumed capacity units.
LSI map[string]float64
// Table is the amount of throughput consumed by the table.
Table float64
// TableName is the name of the table affected by this operation.
TableName string
}
func addConsumedCapacity(cc *ConsumedCapacity, raw *dynamodb.ConsumedCapacity) {
if cc == nil || raw == nil {
return
}
if raw.CapacityUnits != nil {
cc.Total += *raw.CapacityUnits
}
if len(raw.GlobalSecondaryIndexes) > 0 {
if cc.GSI == nil {
cc.GSI = make(map[string]float64, len(raw.GlobalSecondaryIndexes))
}
for name, consumed := range raw.GlobalSecondaryIndexes {
cc.GSI[name] = cc.GSI[name] + *consumed.CapacityUnits
}
}
if len(raw.LocalSecondaryIndexes) > 0 {
if cc.LSI == nil {
cc.LSI = make(map[string]float64, len(raw.LocalSecondaryIndexes))
}
for name, consumed := range raw.LocalSecondaryIndexes {
cc.LSI[name] = cc.LSI[name] + *consumed.CapacityUnits
}
}
if raw.Table != nil {
cc.Table += *raw.Table.CapacityUnits
}
if raw.TableName != nil {
cc.TableName = *raw.TableName
}
}