/
stream.go
222 lines (177 loc) · 5.38 KB
/
stream.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
package dynamo
import (
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/pkg/errors"
)
// Stream is inspired from Java streams to chain transformations in a functional programming style
type Stream interface {
HasNext() bool // HasNext returns true if it has another element
Next() map[string]*dynamodb.AttributeValue // Next return current element and move forward the cursor
Error() error // Error returns the error that interrupted the Stream
Count() int64 // Count return the number of element found so far
}
// arrayStream is a wrapper around a slice ; this slice can be replaced when IsLast() is true
type arrayStream struct {
err error
results []map[string]*dynamodb.AttributeValue
count int64
}
func NewArrayStream(results []map[string]*dynamodb.AttributeValue) Stream {
return &arrayStream{
results: results,
}
}
type queryStream struct {
*Rep
internalStream arrayStream // arrayStream is wrapping the current internalStream
queries []*dynamodb.QueryInput // queries first item is the one to use as long as queryHasNextPage is true
nextPageToken map[string]*dynamodb.AttributeValue // nextPageToken is only usable when queryHasNextPage is true
}
func NewQueryStream(rep *Rep, queries []*dynamodb.QueryInput) Stream {
if len(queries) == 0 {
return NewArrayStream(nil)
}
stream := &queryStream{
Rep: rep,
internalStream: arrayStream{},
queries: queries,
}
stream.populateNextChunk()
return stream
}
type getStream struct {
db *dynamodb.DynamoDB
table string
projectionExpression *string
internalStream arrayStream
keys []map[string]*dynamodb.AttributeValue
buffer []map[string]*dynamodb.AttributeValue
}
func NewGetStream(rep *Rep, keys []map[string]*dynamodb.AttributeValue, projectionExpression *string, bufferSize int64) Stream {
stream := &getStream{
buffer: make([]map[string]*dynamodb.AttributeValue, 0, bufferSize),
db: rep.db,
internalStream: arrayStream{},
keys: keys,
projectionExpression: projectionExpression,
table: rep.table,
}
stream.populateNextChunk()
return stream
}
// ** ARRAY STREAM
func (s *arrayStream) HasNext() bool {
return s.err == nil && len(s.results) > 0
}
func (s *arrayStream) IsLast() bool {
return s.err == nil && len(s.results) > 1
}
func (s *arrayStream) Next() (current map[string]*dynamodb.AttributeValue) {
if s.err != nil {
panic(errors.Wrapf(s.err, "Next() can't be called when an error occured"))
}
if !s.HasNext() {
panic(errors.Wrapf(s.err, "Next() can't be called when an HasNext() returns false"))
}
current, s.results = s.results[0], s.results[1:]
s.count++
return current
}
func (s *arrayStream) AppendNextChunk(chunk []map[string]*dynamodb.AttributeValue) {
results := make([]map[string]*dynamodb.AttributeValue, len(s.results)+len(chunk))
copy(results, s.results)
copy(results[len(s.results):], chunk)
s.results = results
}
func (s *arrayStream) Count() int64 {
return s.count
}
func (s *arrayStream) Error() error {
return s.err
}
// WithError keeps exiting error if already present. err can be nil.
func (s *arrayStream) WithError(err error) {
if s.err == nil {
s.err = err
}
}
// ** QUERY STREAM
func (s *queryStream) HasNext() bool {
return s.internalStream.HasNext()
}
func (s *queryStream) Next() map[string]*dynamodb.AttributeValue {
next := s.internalStream.Next()
for !s.internalStream.HasNext() && len(s.queries) > 0 {
if len(s.nextPageToken) == 0 {
s.queries = s.queries[1:]
}
if len(s.queries) > 0 {
s.populateNextChunk()
}
}
return next
}
func (s *queryStream) populateNextChunk() {
query := *s.queries[0]
query.ExclusiveStartKey = s.nextPageToken
result, err := s.db.Query(&query)
if err != nil {
s.internalStream.WithError(err)
return
}
s.nextPageToken = result.LastEvaluatedKey
s.internalStream.AppendNextChunk(result.Items)
}
func (s *queryStream) Count() int64 {
return s.internalStream.count
}
func (s *queryStream) Error() error {
return s.internalStream.Error()
}
// ** GET STREAM
func (s *getStream) HasNext() bool {
return s.internalStream.HasNext()
}
func (s *getStream) Next() map[string]*dynamodb.AttributeValue {
next := s.internalStream.Next()
for !s.internalStream.HasNext() && (len(s.buffer) > 0 || len(s.keys) > 0) {
s.populateNextChunk()
}
return next
}
func (s *getStream) Count() int64 {
return s.internalStream.Count()
}
func (s *getStream) Error() error {
return s.internalStream.Error()
}
func (s *getStream) populateNextChunk() {
end := cap(s.buffer) - len(s.buffer)
if end > len(s.keys) {
end = len(s.keys)
}
if end > 0 {
s.buffer = append(s.buffer, s.keys[:end]...)
s.keys = s.keys[end:]
}
if len(s.buffer) == 0 {
return
}
result, err := s.db.BatchGetItem(&dynamodb.BatchGetItemInput{
RequestItems: map[string]*dynamodb.KeysAndAttributes{
s.table: {
Keys: s.buffer,
ProjectionExpression: s.projectionExpression,
},
},
})
if err != nil {
s.internalStream.WithError(err)
return
}
s.buffer = s.buffer[:0]
if table, ok := result.UnprocessedKeys[s.table]; ok && len(table.Keys) > 0 {
s.buffer = append(s.buffer, table.Keys...)
}
s.internalStream.AppendNextChunk(result.Responses[s.table])
}