forked from AdRoll/goamz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
185 lines (160 loc) · 5.05 KB
/
scan.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
package dynamodb
import (
"errors"
"fmt"
"log"
"time"
simplejson "github.com/bitly/go-simplejson"
)
func (t *Table) FetchPartialResults(query *Query) ([]map[string]*Attribute, *Key, error) {
jsonResponse, err := t.Server.queryServer(target("Scan"), query)
if err != nil {
return nil, nil, err
}
json, err := simplejson.NewJson(jsonResponse)
if err != nil {
return nil, nil, err
}
itemCount, err := json.Get("Count").Int()
if err != nil {
message := fmt.Sprintf("Unexpected response %s", jsonResponse)
return nil, nil, errors.New(message)
}
results := make([]map[string]*Attribute, itemCount)
for i, _ := range results {
item, err := json.Get("Items").GetIndex(i).Map()
if err != nil {
message := fmt.Sprintf("Unexpected response %s", jsonResponse)
return nil, nil, errors.New(message)
}
results[i] = parseAttributes(item)
}
var lastEvaluatedKey *Key
if lastKeyMap := json.Get("LastEvaluatedKey").MustMap(); lastKeyMap != nil {
lastEvaluatedKey = parseKey(t, lastKeyMap)
}
return results, lastEvaluatedKey, nil
}
func (t *Table) FetchResultCallbackIterator(query *Query, cb func(map[string]*Attribute) error) error {
for {
var results []map[string]*Attribute
var lastEvaluatedKey *Key
var err error
err = exponentialBackoff(func() error {
results, lastEvaluatedKey, err = t.FetchPartialResults(query)
return err
}, 15)
if err != nil {
return err
}
for _, item := range results {
if err := cb(item); err != nil {
return err
}
}
if lastEvaluatedKey == nil {
break
}
query.AddExclusiveStartKey(t, lastEvaluatedKey)
}
return nil
}
func (t *Table) ScanPartial(attributeComparisons []AttributeComparison, exclusiveStartKey *Key) ([]map[string]*Attribute, *Key, error) {
return t.ParallelScanPartialLimit(attributeComparisons, exclusiveStartKey, 0, 0, 0)
}
func (t *Table) ScanPartialLimit(attributeComparisons []AttributeComparison, exclusiveStartKey *Key, limit int64) ([]map[string]*Attribute, *Key, error) {
return t.ParallelScanPartialLimit(attributeComparisons, exclusiveStartKey, 0, 0, limit)
}
func (t *Table) ParallelScanPartial(attributeComparisons []AttributeComparison, exclusiveStartKey *Key, segment, totalSegments int) ([]map[string]*Attribute, *Key, error) {
return t.ParallelScanPartialLimit(attributeComparisons, exclusiveStartKey, segment, totalSegments, 0)
}
func (t *Table) ParallelScanPartialLimit(attributeComparisons []AttributeComparison, exclusiveStartKey *Key, segment, totalSegments int, limit int64) ([]map[string]*Attribute, *Key, error) {
q := NewQuery(t)
q.AddScanFilter(attributeComparisons)
if exclusiveStartKey != nil {
q.AddExclusiveStartKey(t, exclusiveStartKey)
}
if totalSegments > 0 {
q.AddParallelScanConfiguration(segment, totalSegments)
}
if limit > 0 {
q.AddLimit(limit)
}
return t.FetchPartialResults(q)
}
func (t *Table) FetchResults(query *Query) ([]map[string]*Attribute, error) {
results, _, err := t.FetchPartialResults(query)
return results, err
}
func (t *Table) Scan(attributeComparisons []AttributeComparison) ([]map[string]*Attribute, error) {
q := NewQuery(t)
q.AddScanFilter(attributeComparisons)
return t.FetchResults(q)
}
func (t *Table) ParallelScan(attributeComparisons []AttributeComparison, segment int, totalSegments int) ([]map[string]*Attribute, error) {
q := NewQuery(t)
q.AddScanFilter(attributeComparisons)
q.AddParallelScanConfiguration(segment, totalSegments)
return t.FetchResults(q)
}
func (t *Table) ScanCallbackIterator(attributeComparisons []AttributeComparison, cb func(map[string]*Attribute) error) error {
q := NewQuery(t)
q.AddScanFilter(attributeComparisons)
return t.FetchResultCallbackIterator(q, cb)
}
func parseKey(t *Table, s map[string]interface{}) *Key {
k := &Key{}
hk := t.Key.KeyAttribute
if v, ok := s[hk.Name].(map[string]interface{}); ok {
switch hk.Type {
case TYPE_NUMBER, TYPE_STRING, TYPE_BINARY:
if key, ok := v[hk.Type].(string); ok {
k.HashKey = key
} else {
log.Printf("type assertion to string failed for : %s\n", hk.Type)
return nil
}
default:
log.Printf("invalid primary key hash type : %s\n", hk.Type)
return nil
}
} else {
log.Printf("type assertion to map[string]interface{} failed for : %s\n", hk.Name)
return nil
}
if t.Key.HasRange() {
rk := t.Key.RangeAttribute
if v, ok := s[rk.Name].(map[string]interface{}); ok {
switch rk.Type {
case TYPE_NUMBER, TYPE_STRING, TYPE_BINARY:
if key, ok := v[rk.Type].(string); ok {
k.RangeKey = key
} else {
log.Printf("type assertion to string failed for : %s\n", rk.Type)
return nil
}
default:
log.Printf("invalid primary key range type : %s\n", rk.Type)
return nil
}
} else {
log.Printf("type assertion to map[string]interface{} failed for : %s\n", rk.Name)
return nil
}
}
return k
}
func exponentialBackoff(f func() error, maxRetry uint) error {
var err error
currentRetry := uint(0)
for {
if err = f(); err == nil {
return nil
}
if currentRetry >= maxRetry {
return err
}
time.Sleep((1 << currentRetry) * 50 * time.Millisecond)
currentRetry++
}
}