-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
182 lines (150 loc) · 4.19 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
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
package ensign
import (
"context"
"io"
api "github.com/rotationalio/go-ensign/api/v1beta1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// QueryCursor exposes event results from an EnSQL query with familiar database cursor
// semantics. Note that the cursor is not thread safe and should only be used from a
// single thread.
type QueryCursor struct {
stream api.Ensign_EnSQLClient
result *Event
}
// NewQueryCursor creates a new query cursor that reads from the specified stream.
func NewQueryCursor(stream api.Ensign_EnSQLClient) (cursor *QueryCursor, err error) {
cursor = &QueryCursor{
stream: stream,
}
// Fetch the first event to catch any errors.
if cursor.result, err = cursor.FetchOne(); err != nil {
return nil, err
}
return cursor, nil
}
// read fetches the next event from the stream and returns the previous result to the
// caller. If there are no more events then a nil event is returned.
func (c *QueryCursor) read() (event *Event, err error) {
if c.stream == nil {
return nil, ErrCursorClosed
}
// If there's a cached result then return it
if c.result != nil {
event = c.result
c.result = nil
return event, nil
}
// Read the next event and cache it
var wrapper *api.EventWrapper
if wrapper, err = c.stream.Recv(); err != nil {
if streamClosed(err) {
c.Close()
return nil, nil
}
return nil, err
}
// Convert the event into an API event
event = &Event{}
if err = event.fromPB(wrapper, query); err != nil {
c.Close()
return nil, err
}
return event, nil
}
// FetchOne returns the next query result. If there are no more results then nil is
// returned.
func (i *QueryCursor) FetchOne() (event *Event, err error) {
if event, err = i.read(); err != nil {
return nil, err
}
if event == nil {
return nil, ErrNoRows
}
return event, nil
}
// FetchMany returns the next n query results. If there are less than n results
// remaining then all the remaining results are returned.
func (i *QueryCursor) FetchMany(n int) (events []*Event, err error) {
events = make([]*Event, 0, n)
for len(events) < n {
var event *Event
if event, err = i.read(); err != nil {
return nil, err
}
if event == nil {
break
}
events = append(events, event)
}
if len(events) == 0 {
return nil, ErrNoRows
}
return events, nil
}
// FetchAll returns all events from the query stream. If there are no more events then
// an empty slice is returned.
func (i *QueryCursor) FetchAll() (events []*Event, err error) {
events = make([]*Event, 0)
for {
var event *Event
if event, err = i.read(); err != nil {
return nil, err
}
if event == nil {
break
}
events = append(events, event)
}
if len(events) == 0 {
return nil, ErrNoRows
}
return events, nil
}
// Close the cursor, which closes the underlying stream.
func (i *QueryCursor) Close() (err error) {
if i.stream == nil {
return nil
}
if err = i.stream.CloseSend(); err != nil {
i.stream = nil
return err
}
i.stream = nil
return nil
}
// EnSQL executes a query against Ensign and returns a cursor that can be used to fetch
// the event results. This RPC always returns a finite number of results. After all
// results have been returned the cursor will return nil. In order to retrieve events
// in a more streaming fashion, the Subscribe RPC should be used with a query option.
func (c *Client) EnSQL(ctx context.Context, query *api.Query) (cursor *QueryCursor, err error) {
if query.Query == "" {
return nil, ErrEmptyQuery
}
// Create the stream by sending the query request to the server.
var stream api.Ensign_EnSQLClient
if stream, err = c.api.EnSQL(ctx, query, c.copts...); err != nil {
return nil, err
}
return NewQueryCursor(stream)
}
// Explain returns the query plan for the specified query, including the expected
// number of results and errors that might be returned.
func (c *Client) Explain(ctx context.Context, query *api.Query) (plan *api.QueryExplanation, err error) {
if query.Query == "" {
return nil, ErrEmptyQuery
}
return c.api.Explain(ctx, query, c.copts...)
}
func streamClosed(err error) bool {
if err == io.EOF {
return true
}
if serr, ok := status.FromError(err); ok {
if serr.Code() == codes.Canceled {
return true
}
}
return false
}