-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaginator.go
53 lines (44 loc) · 1.06 KB
/
paginator.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
package dynamoql
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
type QueryPaginator struct {
client *dynamodb.Client
query dynamodb.QueryInput
lastEvalKey PageToken
scannedPages uint32
itemCount int32
}
func NewQueryPaginator(pageSize int32, c *dynamodb.Client, q dynamodb.QueryInput) *QueryPaginator {
if pageSize > 0 {
q.Limit = &pageSize
}
return &QueryPaginator{
client: c,
query: q,
}
}
func (p QueryPaginator) NextPageToken() PageToken {
return p.lastEvalKey
}
func (p QueryPaginator) Next() bool {
return p.lastEvalKey.String() != "" || p.scannedPages == 0
}
func (p QueryPaginator) ScannedPages() uint32 {
return p.scannedPages
}
func (p QueryPaginator) Count() int32 {
return p.itemCount
}
func (p *QueryPaginator) GetPage(ctx context.Context) (*dynamodb.QueryOutput, error) {
p.query.ExclusiveStartKey = p.lastEvalKey
out, err := p.client.Query(ctx, &p.query)
if err != nil {
return nil, err
}
p.lastEvalKey = out.LastEvaluatedKey
p.scannedPages++
p.itemCount += out.Count
return out, err
}