-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_iter.go
80 lines (70 loc) · 1.85 KB
/
query_iter.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
package grpcds
import (
"io"
"sync"
"time"
pb "github.com/guseggert/go-ds-grpc/proto"
"github.com/ipfs/go-datastore/query"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/status"
)
// queryIterator wraps a gRPC stream of query results with methods that can be used for query.ResultsFromIterator
type queryIterator struct {
stream pb.Datastore_QueryClient
closedMut sync.RWMutex
closed bool
}
func (q *queryIterator) Next() (query.Result, bool) {
// mimic channel behavior, which is what consumers expect
if q.isClosed() {
return query.Result{}, false
}
select {
case <-q.stream.Context().Done():
q.Close()
// note that it's possible to race here and end up sending this twice
// but that's okay, reading from closed channel yields default value repeatedly
return query.Result{}, false
default:
}
queryResult, err := q.stream.Recv()
if err == io.EOF {
q.Close()
return query.Result{}, false
}
if err != nil {
q.Close()
return query.Result{Error: GRPCToDSError(err)}, true
}
result := query.Result{
Entry: query.Entry{
Key: queryResult.Key,
Value: queryResult.Value,
Expiration: time.Unix(int64(queryResult.Expiration), 0),
Size: int(queryResult.Size),
},
}
if queryResult.Error != nil {
// unpack the status protobuf and apply error transformation
sp := &spb.Status{}
err = queryResult.Error.UnmarshalTo(sp)
if err != nil {
// the problem is about message contents, so no reason to stop
return query.Result{Error: GRPCToDSError(err)}, true
}
s := status.FromProto(sp)
result.Error = GRPCToDSError(s.Err())
}
return result, true
}
func (q *queryIterator) isClosed() bool {
q.closedMut.RLock()
defer q.closedMut.RUnlock()
return q.closed
}
func (q *queryIterator) Close() error {
q.closedMut.Lock()
q.closed = true
q.closedMut.Unlock()
return nil
}