-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
54 lines (51 loc) · 1.16 KB
/
run.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
package tuna
import (
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// Run applies an against a stream. It will display the current progression at
// every multiple of checkpoint.
func Run(stream Stream, agg Agg, sink Sink, checkpoint uint) error {
// Run the exature aggs over the stream
var (
n uint
t0 = time.Now()
p = message.NewPrinter(language.English)
)
for row := range stream {
n++
// Check there is no error
if row.Err != nil {
return row.Err
}
// Update the Agg
if err := agg.Update(row.Row); err != nil {
return err
}
// Display the current progress if a checkpoint is reached
if checkpoint > 0 && n%checkpoint == 0 {
t := time.Since(t0)
p.Printf(
"\r%s -- %d rows -- %.0f rows/second",
fmtDuration(t),
n,
float64(n)/t.Seconds(),
)
}
}
// If there is a sink then the data has to be written
if sink != nil {
if err := sink.Write(agg.Collect()); err != nil {
return err
}
}
// If agg is a SequentialGroupBy then the last group hasn't been written
// down yet
if sgb, ok := agg.(*SequentialGroupBy); ok {
if err := sgb.Flush(); err != nil {
return err
}
}
return nil
}