-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy patharray_cursor.gen.go.tmpl
618 lines (529 loc) · 15 KB
/
array_cursor.gen.go.tmpl
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package reads
import (
"errors"
"fmt"
"math"
"github.com/influxdata/flux/interval"
"github.com/influxdata/flux/values"
errors2 "github.com/influxdata/influxdb/kit/platform/errors"
"github.com/influxdata/influxdb/tsdb/cursors"
)
const (
// MaxPointsPerBlock is the maximum number of points in an encoded
// block in a TSM file. It should match the value in the tsm1
// package, but we don't want to import it.
MaxPointsPerBlock = 1000
)
func newLimitArrayCursor(cur cursors.Cursor) cursors.Cursor {
switch cur := cur.(type) {
{{range .}}{{/* every type supports limit */}}
case cursors.{{.Name}}ArrayCursor:
return new{{.Name}}LimitArrayCursor(cur)
{{end}}
default:
panic(fmt.Sprintf("unreachable: %T", cur))
}
}
func newWindowFirstArrayCursor(cur cursors.Cursor, window interval.Window) cursors.Cursor {
if window.IsZero() {
return newLimitArrayCursor(cur)
}
switch cur := cur.(type) {
{{range .}}{{/* every type supports first */}}
case cursors.{{.Name}}ArrayCursor:
return new{{.Name}}WindowFirstArrayCursor(cur, window)
{{end}}
default:
panic(fmt.Sprintf("unreachable: %T", cur))
}
}
func newWindowLastArrayCursor(cur cursors.Cursor, window interval.Window) cursors.Cursor {
if window.IsZero() {
return newLimitArrayCursor(cur)
}
switch cur := cur.(type) {
{{range .}}{{/* every type supports last */}}
case cursors.{{.Name}}ArrayCursor:
return new{{.Name}}WindowLastArrayCursor(cur, window)
{{end}}
default:
panic(fmt.Sprintf("unreachable: %T", cur))
}
}
func newWindowCountArrayCursor(cur cursors.Cursor, window interval.Window) cursors.Cursor {
switch cur := cur.(type) {
{{range .}}{{/* every type supports count */}}
case cursors.{{.Name}}ArrayCursor:
return new{{.Name}}WindowCountArrayCursor(cur, window)
{{end}}
default:
panic(fmt.Sprintf("unreachable: %T", cur))
}
}
func newWindowSumArrayCursor(cur cursors.Cursor, window interval.Window) (cursors.Cursor, error) {
switch cur := cur.(type) {
{{range .}}
{{$Type := .Name}}
{{range .Aggs}}
{{if eq .Name "Sum"}}
case cursors.{{$Type}}ArrayCursor:
return new{{$Type}}WindowSumArrayCursor(cur, window), nil
{{end}}
{{end}}{{/* for each supported agg fn */}}
{{end}}{{/* for each field type */}}
default:
return nil, &errors2.Error{
Code: errors2.EInvalid,
Msg: fmt.Sprintf("unsupported input type for sum aggregate: %s", arrayCursorType(cur)),
}
}
}
func newWindowMinArrayCursor(cur cursors.Cursor, window interval.Window) cursors.Cursor {
switch cur := cur.(type) {
{{range .}}
{{$Type := .Name}}
{{range .Aggs}}
{{if eq .Name "Min"}}
case cursors.{{$Type}}ArrayCursor:
return new{{$Type}}WindowMinArrayCursor(cur, window)
{{end}}
{{end}}{{/* for each supported agg fn */}}
{{end}}{{/* for each field type */}}
default:
panic(fmt.Sprintf("unsupported for aggregate min: %T", cur))
}
}
func newWindowMaxArrayCursor(cur cursors.Cursor, window interval.Window) cursors.Cursor {
switch cur := cur.(type) {
{{range .}}
{{$Type := .Name}}
{{range .Aggs}}
{{if eq .Name "Max"}}
case cursors.{{$Type}}ArrayCursor:
return new{{$Type}}WindowMaxArrayCursor(cur, window)
{{end}}
{{end}}{{/* for each supported agg fn */}}
{{end}}{{/* for each field type */}}
default:
panic(fmt.Sprintf("unsupported for aggregate max: %T", cur))
}
}
func newWindowMeanArrayCursor(cur cursors.Cursor, window interval.Window) (cursors.Cursor, error) {
switch cur := cur.(type) {
{{range .}}
{{$Type := .Name}}
{{range .Aggs}}
{{if eq .Name "Mean"}}
case cursors.{{$Type}}ArrayCursor:
return new{{$Type}}WindowMeanArrayCursor(cur, window), nil
{{end}}
{{end}}{{/* for each supported agg fn */}}
{{end}}{{/* for each field type */}}
default:
return nil, &errors2.Error{
Code: errors2.EInvalid,
Msg: fmt.Sprintf("unsupported input type for mean aggregate: %s", arrayCursorType(cur)),
}
}
}
func newWindowMeanCountArrayCursor(cur cursors.Cursor, window interval.Window) (cursors.Cursor, error) {
switch cur := cur.(type) {
{{range .}}
{{$Type := .Name}}
{{range .Aggs}}
{{if eq .Name "MeanCount"}}
case cursors.{{$Type}}ArrayCursor:
return new{{$Type}}WindowMeanCountArrayCursor(cur, window), nil
{{end}}
{{end}}{{/* for each supported agg fn */}}
{{end}}{{/* for each field type */}}
default:
return nil, &errors2.Error{
Code: errors2.EInvalid,
Msg: fmt.Sprintf("unsupported input type for meancount aggregate: %s", arrayCursorType(cur)),
}
}
}
{{range .}}
{{$arrayType := print "*cursors." .Name "Array"}}
{{$type := print .name "ArrayFilterCursor"}}
{{$Type := print .Name "ArrayFilterCursor"}}
// ********************
// {{.Name}} Array Cursor
type {{$type}} struct {
cursors.{{.Name}}ArrayCursor
cond expression
m *singleValue
res {{$arrayType}}
tmp {{$arrayType}}
}
func new{{.Name}}FilterArrayCursor(cond expression) *{{$type}} {
return &{{$type}}{
cond: cond,
m: &singleValue{},
res: cursors.New{{.Name}}ArrayLen(MaxPointsPerBlock),
tmp: &cursors.{{.Name}}Array{},
}
}
func (c *{{$type}}) reset(cur cursors.{{.Name}}ArrayCursor) {
c.{{.Name}}ArrayCursor = cur
c.tmp.Timestamps, c.tmp.Values = nil, nil
}
func (c *{{$type}}) Stats() cursors.CursorStats { return c.{{.Name}}ArrayCursor.Stats() }
func (c *{{$type}}) Next() {{$arrayType}} {
pos := 0
c.res.Timestamps = c.res.Timestamps[:cap(c.res.Timestamps)]
c.res.Values = c.res.Values[:cap(c.res.Values)]
var a {{$arrayType}}
if c.tmp.Len() > 0 {
a = c.tmp
} else {
a = c.{{.Name}}ArrayCursor.Next()
}
LOOP:
for len(a.Timestamps) > 0 {
for i, v := range a.Values {
c.m.v = v
if c.cond.EvalBool(c.m) {
c.res.Timestamps[pos] = a.Timestamps[i]
c.res.Values[pos] = v
pos++
if pos >= MaxPointsPerBlock {
c.tmp.Timestamps = a.Timestamps[i+1:]
c.tmp.Values = a.Values[i+1:]
break LOOP
}
}
}
// Clear buffered timestamps & values if we make it through a cursor.
// The break above will skip this if a cursor is partially read.
c.tmp.Timestamps = nil
c.tmp.Values = nil
a = c.{{.Name}}ArrayCursor.Next()
}
c.res.Timestamps = c.res.Timestamps[:pos]
c.res.Values = c.res.Values[:pos]
return c.res
}
type {{.name}}MultiShardArrayCursor struct {
cursors.{{.Name}}ArrayCursor
cursorContext
filter *{{$type}}
}
func (c *{{.name}}MultiShardArrayCursor) reset(cur cursors.{{.Name}}ArrayCursor, itrs cursors.CursorIterators, cond expression) {
if cond != nil {
if c.filter == nil {
c.filter = new{{.Name}}FilterArrayCursor(cond)
} else {
c.filter.cond = cond
}
c.filter.reset(cur)
cur = c.filter
}
c.{{.Name}}ArrayCursor = cur
c.itrs = itrs
c.err = nil
}
func (c *{{.name}}MultiShardArrayCursor) Err() error { return c.err }
func (c *{{.name}}MultiShardArrayCursor) Stats() cursors.CursorStats {
return c.{{.Name}}ArrayCursor.Stats()
}
func (c *{{.name}}MultiShardArrayCursor) Next() {{$arrayType}} {
for {
a := c.{{.Name}}ArrayCursor.Next()
if a.Len() == 0 {
if c.nextArrayCursor() {
continue
}
}
return a
}
}
func (c *{{.name}}MultiShardArrayCursor) nextArrayCursor() bool {
if len(c.itrs) == 0 {
return false
}
c.{{.Name}}ArrayCursor.Close()
var itr cursors.CursorIterator
var cur cursors.Cursor
for cur == nil && len(c.itrs) > 0 {
itr, c.itrs = c.itrs[0], c.itrs[1:]
cur, _ = itr.Next(c.ctx, c.req)
}
var ok bool
if cur != nil {
var next cursors.{{.Name}}ArrayCursor
next, ok = cur.(cursors.{{.Name}}ArrayCursor)
if !ok {
cur.Close()
next = {{.Name}}EmptyArrayCursor
c.err = errors.New("expected {{.name}} cursor")
} else {
if c.filter != nil {
c.filter.reset(next)
next = c.filter
}
}
c.{{.Name}}ArrayCursor = next
} else {
c.{{.Name}}ArrayCursor = {{.Name}}EmptyArrayCursor
}
return ok
}
type {{.name}}LimitArrayCursor struct {
cursors.{{.Name}}ArrayCursor
res {{$arrayType}}
done bool
}
func new{{.Name}}LimitArrayCursor(cur cursors.{{.Name}}ArrayCursor) *{{.name}}LimitArrayCursor {
return &{{.name}}LimitArrayCursor{
{{.Name}}ArrayCursor: cur,
res: cursors.New{{.Name}}ArrayLen(1),
}
}
func (c *{{.name}}LimitArrayCursor) Stats() cursors.CursorStats { return c.{{.Name}}ArrayCursor.Stats() }
func (c *{{.name}}LimitArrayCursor) Next() {{$arrayType}} {
if c.done {
return &cursors.{{.Name}}Array{}
}
a := c.{{.Name}}ArrayCursor.Next()
if len(a.Timestamps) == 0 {
return a
}
c.done = true
c.res.Timestamps[0] = a.Timestamps[0]
c.res.Values[0] = a.Values[0]
return c.res
}
type {{.name}}WindowLastArrayCursor struct {
cursors.{{.Name}}ArrayCursor
windowEnd int64
res {{$arrayType}}
tmp {{$arrayType}}
window interval.Window
}
// Window array cursors assume that every != 0 && every != MaxInt64.
// Such a cursor will panic in the first case and possibly overflow in the second.
func new{{.Name}}WindowLastArrayCursor(cur cursors.{{.Name}}ArrayCursor, window interval.Window) *{{.name}}WindowLastArrayCursor {
return &{{.name}}WindowLastArrayCursor{
{{.Name}}ArrayCursor: cur,
windowEnd: math.MinInt64,
res: cursors.New{{.Name}}ArrayLen(MaxPointsPerBlock),
tmp: &cursors.{{.Name}}Array{},
window: window,
}
}
func (c *{{.name}}WindowLastArrayCursor) Stats() cursors.CursorStats {
return c.{{.Name}}ArrayCursor.Stats()
}
func (c *{{.name}}WindowLastArrayCursor) Next() *cursors.{{.Name}}Array {
cur := -1
NEXT:
var a *cursors.{{.Name}}Array
if c.tmp.Len() > 0 {
a = c.tmp
} else {
a = c.{{.Name}}ArrayCursor.Next()
}
if a.Len() == 0 {
c.res.Timestamps = c.res.Timestamps[:cur+1]
c.res.Values = c.res.Values[:cur+1]
return c.res
}
for i, t := range a.Timestamps {
if t >= c.windowEnd {
cur++
}
if cur == MaxPointsPerBlock {
c.tmp.Timestamps = a.Timestamps[i:]
c.tmp.Values = a.Values[i:]
return c.res
}
c.res.Timestamps[cur] = t
c.res.Values[cur] = a.Values[i]
c.windowEnd = int64(c.window.GetLatestBounds(values.Time(t)).Stop())
}
c.tmp.Timestamps = nil
c.tmp.Values = nil
goto NEXT
}
type {{.name}}WindowFirstArrayCursor struct {
cursors.{{.Name}}ArrayCursor
windowEnd int64
res {{$arrayType}}
tmp {{$arrayType}}
window interval.Window
}
// Window array cursors assume that every != 0 && every != MaxInt64.
// Such a cursor will panic in the first case and possibly overflow in the second.
func new{{.Name}}WindowFirstArrayCursor(cur cursors.{{.Name}}ArrayCursor, window interval.Window) *{{.name}}WindowFirstArrayCursor {
return &{{.name}}WindowFirstArrayCursor{
{{.Name}}ArrayCursor: cur,
windowEnd: math.MinInt64,
res: cursors.New{{.Name}}ArrayLen(MaxPointsPerBlock),
tmp: &cursors.{{.Name}}Array{},
window: window,
}
}
func (c *{{.name}}WindowFirstArrayCursor) Stats() cursors.CursorStats {
return c.{{.Name}}ArrayCursor.Stats()
}
func (c *{{.name}}WindowFirstArrayCursor) Next() *cursors.{{.Name}}Array {
c.res.Timestamps = c.res.Timestamps[:0]
c.res.Values = c.res.Values[:0]
NEXT:
var a *cursors.{{.Name}}Array
if c.tmp.Len() > 0 {
a = c.tmp
} else {
a = c.{{.Name}}ArrayCursor.Next()
}
if a.Len() == 0 {
return c.res
}
for i, t := range a.Timestamps {
if t < c.windowEnd {
continue
}
c.windowEnd = int64(c.window.GetLatestBounds(values.Time(t)).Stop())
c.res.Timestamps = append(c.res.Timestamps, t)
c.res.Values = append(c.res.Values, a.Values[i])
if c.res.Len() == MaxPointsPerBlock {
c.tmp.Timestamps = a.Timestamps[i+1:]
c.tmp.Values = a.Values[i+1:]
return c.res
}
}
c.tmp.Timestamps = nil
c.tmp.Values = nil
goto NEXT
}
{{/* create an aggregate cursor for each aggregate function supported by the type */}}
{{$Name := .Name}}
{{$name := .name}}
{{range .Aggs}}
{{$aggName := .Name}}
type {{$name}}Window{{$aggName}}ArrayCursor struct {
cursors.{{$Name}}ArrayCursor
res *cursors.{{.OutputTypeName}}Array
tmp {{$arrayType}}
window interval.Window
}
func new{{$Name}}Window{{$aggName}}ArrayCursor(cur cursors.{{$Name}}ArrayCursor, window interval.Window) *{{$name}}Window{{$aggName}}ArrayCursor {
resLen := MaxPointsPerBlock
if window.IsZero() {
resLen = 1
}
return &{{$name}}Window{{$aggName}}ArrayCursor{
{{$Name}}ArrayCursor: cur,
res: cursors.New{{.OutputTypeName}}ArrayLen(resLen),
tmp: &cursors.{{$Name}}Array{},
window: window,
}
}
func (c *{{$name}}Window{{$aggName}}ArrayCursor) Stats() cursors.CursorStats {
return c.{{$Name}}ArrayCursor.Stats()
}
func (c *{{$name}}Window{{$aggName}}ArrayCursor) Next() *cursors.{{.OutputTypeName}}Array {
pos := 0
c.res.Timestamps = c.res.Timestamps[:cap(c.res.Timestamps)]
{{if eq .OutputTypeName "MeanCount" }}
c.res.Values0 = c.res.Values0[:cap(c.res.Values0)]
c.res.Values1 = c.res.Values1[:cap(c.res.Values1)]
{{else}}
c.res.Values = c.res.Values[:cap(c.res.Values)]
{{end}}
var a *cursors.{{$Name}}Array
if c.tmp.Len() > 0 {
a = c.tmp
} else {
a = c.{{$Name}}ArrayCursor.Next()
}
if a.Len() == 0 {
return &cursors.{{.OutputTypeName}}Array{}
}
rowIdx := 0
{{.AccDecls}}
var windowEnd int64
if !c.window.IsZero() {
windowEnd = int64(c.window.GetLatestBounds(values.Time(a.Timestamps[rowIdx])).Stop())
} else {
windowEnd = math.MaxInt64
}
windowHasPoints := false
// enumerate windows
WINDOWS:
for {
for ; rowIdx < a.Len(); rowIdx++ {
ts := a.Timestamps[rowIdx]
if !c.window.IsZero() && ts >= windowEnd {
// new window detected, close the current window
// do not generate a point for empty windows
if windowHasPoints {
{{.AccEmit}}
pos++
if pos >= MaxPointsPerBlock {
// the output array is full,
// save the remaining points in the input array in tmp.
// they will be processed in the next call to Next()
c.tmp.Timestamps = a.Timestamps[rowIdx:]
c.tmp.Values = a.Values[rowIdx:]
break WINDOWS
}
}
// start the new window
{{.AccReset}}
windowEnd = int64(c.window.GetLatestBounds(values.Time(ts)).Stop())
windowHasPoints = false
continue WINDOWS
} else {
{{.Accumulate}}
windowHasPoints = true
}
}
// Clear buffered timestamps & values if we make it through a cursor.
// The break above will skip this if a cursor is partially read.
c.tmp.Timestamps = nil
c.tmp.Values = nil
// get the next chunk
a = c.{{$Name}}ArrayCursor.Next()
if a.Len() == 0 {
// write the final point
// do not generate a point for empty windows
if windowHasPoints {
{{.AccEmit}}
pos++
}
break WINDOWS
}
rowIdx = 0
}
c.res.Timestamps = c.res.Timestamps[:pos]
{{if eq .OutputTypeName "MeanCount" }}
c.res.Values0 = c.res.Values0[:pos]
c.res.Values1 = c.res.Values1[:pos]
{{else}}
c.res.Values = c.res.Values[:pos]
{{end}}
return c.res
}
{{end}}{{/* range .Aggs */}}
type {{.name}}EmptyArrayCursor struct {
res cursors.{{.Name}}Array
}
var {{.Name}}EmptyArrayCursor cursors.{{.Name}}ArrayCursor = &{{.name}}EmptyArrayCursor{}
func (c *{{.name}}EmptyArrayCursor) Err() error { return nil }
func (c *{{.name}}EmptyArrayCursor) Close() {}
func (c *{{.name}}EmptyArrayCursor) Stats() cursors.CursorStats { return cursors.CursorStats{} }
func (c *{{.name}}EmptyArrayCursor) Next() {{$arrayType}} { return &c.res }
{{end}}{{/* range . */}}
func arrayCursorType(cur cursors.Cursor) string {
switch cur.(type) {
{{range .}}
case cursors.{{.Name}}ArrayCursor:
return "{{.name}}"
{{end}}{{/* range . */}}
default:
return "unknown"
}
}