-
Notifications
You must be signed in to change notification settings - Fork 155
/
query.go
206 lines (170 loc) · 4.94 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package query
import (
"context"
"sort"
"github.com/influxdata/platform"
)
// QueryService represents a service for performing queries.
type QueryService interface {
// Query submits a query spec for execution returning a results iterator.
Query(ctx context.Context, orgID platform.ID, query *Spec) (ResultIterator, error)
// Query submits a query string for execution returning a results iterator.
QueryWithCompile(ctx context.Context, orgID platform.ID, query string) (ResultIterator, error)
}
// ResultIterator allows iterating through all results
type ResultIterator interface {
// More indicates if there are more results.
// More must be called until it returns false in order to free all resources.
More() bool
// Next returns the next result.
// If More is false, Next panics.
Next() Result
// Cancel discards the remaining results.
// If not all results are going to be read, Cancel must be called to free resources.
Cancel()
// Err reports the first error encountered.
Err() error
}
// AsyncQueryService represents a service for performing queries where the results are delivered asynchronously.
type AsyncQueryService interface {
// Query submits a query for execution returning immediately.
// The spec must not be modified while the query is still active.
// Done must be called on any returned Query objects.
Query(ctx context.Context, orgID platform.ID, query *Spec) (Query, error)
// QueryWithCompile submits a query for execution returning immediately.
// The query string will be compiled before submitting for execution.
// Done must be called on returned Query objects.
QueryWithCompile(ctx context.Context, orgID platform.ID, query string) (Query, error)
}
// Query represents an active query.
type Query interface {
// Spec returns the spec used to execute this query.
// Spec must not be modified.
Spec() *Spec
// Ready returns a channel that will deliver the query results.
// Its possible that the channel is closed before any results arrive,
// in which case the query should be inspected for an error using Err().
Ready() <-chan map[string]Result
// Done must always be called to free resources.
Done()
// Cancel will stop the query execution.
// Done must still be called to free resources.
Cancel()
// Err reports any error the query may have encountered.
Err() error
}
// QueryServiceBridge implements the QueryService interface while consuming the AsyncQueryService interface.
type QueryServiceBridge struct {
AsyncQueryService AsyncQueryService
}
func (b QueryServiceBridge) Query(ctx context.Context, orgID platform.ID, spec *Spec) (ResultIterator, error) {
query, err := b.AsyncQueryService.Query(ctx, orgID, spec)
if err != nil {
return nil, err
}
return newResultIterator(query), nil
}
func (b QueryServiceBridge) QueryWithCompile(ctx context.Context, orgID platform.ID, queryStr string) (ResultIterator, error) {
query, err := b.AsyncQueryService.QueryWithCompile(ctx, orgID, queryStr)
if err != nil {
return nil, err
}
return newResultIterator(query), nil
}
// resultIterator implements a ResultIterator while consuming a Query
type resultIterator struct {
query Query
cancel chan struct{}
ready bool
results *MapResultIterator
}
func newResultIterator(q Query) *resultIterator {
return &resultIterator{
query: q,
cancel: make(chan struct{}),
}
}
func (r *resultIterator) More() bool {
if !r.ready {
select {
case <-r.cancel:
goto DONE
case results, ok := <-r.query.Ready():
if !ok {
goto DONE
}
r.ready = true
r.results = NewMapResultIterator(results)
}
}
if r.results.More() {
return true
}
DONE:
r.query.Done()
return false
}
func (r *resultIterator) Next() Result {
return r.results.Next()
}
func (r *resultIterator) Cancel() {
select {
case <-r.cancel:
default:
close(r.cancel)
}
r.query.Cancel()
}
func (r *resultIterator) Err() error {
return r.query.Err()
}
type MapResultIterator struct {
results map[string]Result
order []string
}
func NewMapResultIterator(results map[string]Result) *MapResultIterator {
order := make([]string, 0, len(results))
for k := range results {
order = append(order, k)
}
sort.Strings(order)
return &MapResultIterator{
results: results,
order: order,
}
}
func (r *MapResultIterator) More() bool {
return len(r.order) > 0
}
func (r *MapResultIterator) Next() Result {
next := r.order[0]
r.order = r.order[1:]
return r.results[next]
}
func (r *MapResultIterator) Cancel() {
}
func (r *MapResultIterator) Err() error {
return nil
}
type SliceResultIterator struct {
results []Result
}
func NewSliceResultIterator(results []Result) *SliceResultIterator {
return &SliceResultIterator{
results: results,
}
}
func (r *SliceResultIterator) More() bool {
return len(r.results) > 0
}
func (r *SliceResultIterator) Next() Result {
next := r.results[0]
r.results = r.results[1:]
return next
}
func (r *SliceResultIterator) Cancel() {
r.results = nil
}
func (r *SliceResultIterator) Err() error {
return nil
}