generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
71 lines (60 loc) · 1.49 KB
/
query.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
package ddbds
import (
"context"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/ipfs/go-datastore/query"
)
// queryIterator queries a DynamoDB table/index for a query.
// Queries cannot be performed in parallel, they are paginated sequentially.
type queryIterator struct {
ddbClient *dynamodb.DynamoDB
cancel context.CancelFunc
ddbQuery *dynamodb.QueryInput
keysOnly bool
resultChan chan query.Result
}
func newQueryIterator(ddbClient *dynamodb.DynamoDB, ddbQuery *dynamodb.QueryInput, keysOnly bool) *queryIterator {
qi := &queryIterator{
ddbClient: ddbClient,
ddbQuery: ddbQuery,
keysOnly: keysOnly,
resultChan: make(chan query.Result),
}
return qi
}
func (q *queryIterator) start(ctx context.Context) {
ctx, q.cancel = context.WithCancel(ctx)
go func() {
defer close(q.resultChan)
defer q.cancel()
err := q.ddbClient.QueryPagesWithContext(ctx, q.ddbQuery, func(page *dynamodb.QueryOutput, morePages bool) bool {
for _, itemMap := range page.Items {
result := itemMapToQueryResult(itemMap, q.keysOnly)
select {
case <-ctx.Done():
return false
case q.resultChan <- result:
}
}
return true
})
if err != nil {
if ctx.Err() != nil {
return
}
select {
case <-ctx.Done():
return
case q.resultChan <- query.Result{Error: err}:
}
}
}()
}
func (q *queryIterator) Next() (query.Result, bool) {
res, ok := <-q.resultChan
return res, ok
}
func (q *queryIterator) Close() error {
q.cancel()
return nil
}