forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describetable.go
269 lines (233 loc) · 7.1 KB
/
describetable.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
package dynamo
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Description contains information about a table.
type Description struct {
Name string
ARN string
Status Status
Created time.Time
// Attribute name of the hash key (a.k.a. partition key).
HashKey string
HashKeyType KeyType
// Attribute name of the range key (a.k.a. sort key) or blank if nonexistant.
RangeKey string
RangeKeyType KeyType
// Provisioned throughput for this table.
Throughput Throughput
// The number of items of the table, updated every 6 hours.
Items int64
// The size of this table in bytes, updated every 6 hours.
Size int64
// Global secondary indexes.
GSI []Index
// Local secondary indexes.
LSI []Index
StreamEnabled bool
StreamView StreamView
LatestStreamARN string
LatestStreamLabel string
}
func (d Description) Active() bool {
return d.Status == ActiveStatus
}
type Throughput struct {
// Read capacity units.
Read int64
// Write capacity units.
Write int64
// Time at which throughput was last increased for this table.
LastInc time.Time
// Time at which throughput was last decreased for this table.
LastDec time.Time
// The number of throughput decreases in this UTC calendar day.
DecsToday int64
}
type Index struct {
Name string
ARN string
Status Status
Backfilling bool // only for GSI
// Local is true when this index is a local secondary index, otherwise it is a global secondary index.
Local bool
// Attribute name of the hash key (a.k.a. partition key).
HashKey string
HashKeyType KeyType
// Attribute name of the range key (a.k.a. sort key) or blank if nonexistant.
RangeKey string
RangeKeyType KeyType
// The provisioned throughput for this index.
Throughput Throughput
Items int64
Size int64
ProjectionType IndexProjection
// Non-key attributes for this index's projection (if ProjectionType is IncludeProjection).
ProjectionAttribs []string
}
func newDescription(table *dynamodb.TableDescription) Description {
desc := Description{
Name: *table.TableName,
}
if table.TableArn != nil {
desc.ARN = *table.TableArn
}
if table.TableStatus != nil {
desc.Status = Status(*table.TableStatus)
}
if table.CreationDateTime != nil {
desc.Created = *table.CreationDateTime
}
desc.HashKey, desc.RangeKey = schemaKeys(table.KeySchema)
desc.HashKeyType = lookupADType(table.AttributeDefinitions, desc.HashKey)
desc.RangeKeyType = lookupADType(table.AttributeDefinitions, desc.RangeKey)
if table.ProvisionedThroughput != nil {
desc.Throughput = newThroughput(table.ProvisionedThroughput)
}
if table.ItemCount != nil {
desc.Items = *table.ItemCount
}
if table.TableSizeBytes != nil {
desc.Size = *table.TableSizeBytes
}
for _, index := range table.GlobalSecondaryIndexes {
idx := Index{
Name: *index.IndexName,
ARN: *index.IndexArn,
Status: Status(*index.IndexStatus),
Throughput: newThroughput(index.ProvisionedThroughput),
}
if index.Projection != nil && index.Projection.ProjectionType != nil {
idx.ProjectionType = IndexProjection(*index.Projection.ProjectionType)
idx.ProjectionAttribs = aws.StringValueSlice(index.Projection.NonKeyAttributes)
}
if index.Backfilling != nil {
idx.Backfilling = *index.Backfilling
}
idx.HashKey, idx.RangeKey = schemaKeys(index.KeySchema)
idx.HashKeyType = lookupADType(table.AttributeDefinitions, idx.HashKey)
idx.RangeKeyType = lookupADType(table.AttributeDefinitions, idx.RangeKey)
if index.ItemCount != nil {
idx.Items = *index.ItemCount
}
if index.IndexSizeBytes != nil {
idx.Size = *index.IndexSizeBytes
}
desc.GSI = append(desc.GSI, idx)
}
for _, index := range table.LocalSecondaryIndexes {
idx := Index{
Name: *index.IndexName,
ARN: *index.IndexArn,
Status: ActiveStatus, // local secondary index is always active (technically, it has no status)
Local: true,
Throughput: desc.Throughput, // has the same throughput as the table
}
if index.Projection != nil && index.Projection.ProjectionType != nil {
idx.ProjectionType = IndexProjection(*index.Projection.ProjectionType)
idx.ProjectionAttribs = aws.StringValueSlice(index.Projection.NonKeyAttributes)
}
idx.HashKey, idx.RangeKey = schemaKeys(index.KeySchema)
idx.HashKeyType = lookupADType(table.AttributeDefinitions, idx.HashKey)
idx.RangeKeyType = lookupADType(table.AttributeDefinitions, idx.RangeKey)
if index.ItemCount != nil {
idx.Items = *index.ItemCount
}
if index.IndexSizeBytes != nil {
idx.Size = *index.IndexSizeBytes
}
desc.LSI = append(desc.LSI, idx)
}
if table.StreamSpecification != nil {
if table.StreamSpecification.StreamEnabled != nil {
desc.StreamEnabled = *table.StreamSpecification.StreamEnabled
}
if table.StreamSpecification.StreamViewType != nil {
desc.StreamView = StreamView(*table.StreamSpecification.StreamViewType)
}
}
if table.LatestStreamArn != nil {
desc.LatestStreamARN = *table.LatestStreamArn
}
if table.LatestStreamLabel != nil {
desc.LatestStreamLabel = *table.LatestStreamLabel
}
return desc
}
// DescribeTable is a request for information about a table and its indexes.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeTable.html
type DescribeTable struct {
table Table
}
// Describe begins a new request to describe this table.
func (table Table) Describe() *DescribeTable {
return &DescribeTable{table: table}
}
// Run executes this request and describe the table.
func (dt *DescribeTable) Run() (Description, error) {
ctx, cancel := defaultContext()
defer cancel()
return dt.RunWithContext(ctx)
}
func (dt *DescribeTable) RunWithContext(ctx aws.Context) (Description, error) {
input := dt.input()
var result *dynamodb.DescribeTableOutput
err := retry(ctx, func() error {
var err error
result, err = dt.table.db.client.DescribeTableWithContext(ctx, input)
return err
})
if err != nil {
return Description{}, err
}
return newDescription(result.Table), nil
}
func (dt *DescribeTable) input() *dynamodb.DescribeTableInput {
name := dt.table.Name()
return &dynamodb.DescribeTableInput{
TableName: &name,
}
}
func newThroughput(td *dynamodb.ProvisionedThroughputDescription) Throughput {
if td == nil {
return Throughput{}
}
thru := Throughput{
Read: *td.ReadCapacityUnits,
Write: *td.WriteCapacityUnits,
}
if td.LastIncreaseDateTime != nil {
thru.LastInc = *td.LastIncreaseDateTime
}
if td.LastDecreaseDateTime != nil {
thru.LastDec = *td.LastDecreaseDateTime
}
if td.NumberOfDecreasesToday != nil {
thru.DecsToday = *td.NumberOfDecreasesToday
}
return thru
}
func schemaKeys(schema []*dynamodb.KeySchemaElement) (hashKey, rangeKey string) {
for _, ks := range schema {
switch *ks.KeyType {
case dynamodb.KeyTypeHash:
hashKey = *ks.AttributeName
case dynamodb.KeyTypeRange:
rangeKey = *ks.AttributeName
}
}
return
}
func lookupADType(ads []*dynamodb.AttributeDefinition, name string) KeyType {
if name == "" {
return ""
}
for _, ad := range ads {
if *ad.AttributeName == name {
return KeyType(*ad.AttributeType)
}
}
return ""
}