/
local_group_by_sorted.go
87 lines (71 loc) · 2.07 KB
/
local_group_by_sorted.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
package instruction
import (
"fmt"
"io"
"github.com/chrislusf/gleam/pb"
"github.com/chrislusf/gleam/util"
)
func init() {
InstructionRunner.Register(func(m *pb.Instruction) Instruction {
if m.GetLocalGroupBySorted() != nil {
return NewLocalGroupBySorted(
toInts(m.GetLocalGroupBySorted().GetIndexes()),
)
}
return nil
})
}
type LocalGroupBySorted struct {
indexes []int
}
func NewLocalGroupBySorted(indexes []int) *LocalGroupBySorted {
return &LocalGroupBySorted{indexes}
}
func (b *LocalGroupBySorted) Name(prefix string) string {
return prefix + ".LocalGroupBySorted"
}
func (b *LocalGroupBySorted) Function() func(readers []io.Reader, writers []io.Writer, stats *pb.InstructionStat) error {
return func(readers []io.Reader, writers []io.Writer, stats *pb.InstructionStat) error {
return DoLocalGroupBySorted(readers[0], writers[0], b.indexes, stats)
}
}
func (b *LocalGroupBySorted) SerializeToCommand() *pb.Instruction {
return &pb.Instruction{
LocalGroupBySorted: &pb.Instruction_LocalGroupBySorted{
Indexes: getIndexes(b.indexes),
},
}
}
func (b *LocalGroupBySorted) GetMemoryCostInMB(partitionSize int64) int64 {
return 5
}
func DoLocalGroupBySorted(reader io.Reader, writer io.Writer,
indexes []int, stats *pb.InstructionStat) error {
var prev util.Row
err := util.ProcessRow(reader, indexes, func(row *util.Row) error {
// write prev row if key is different
stats.InputCounter++
if prev.K != nil && util.Compare(row.K, prev.K) != 0 {
if err := prev.WriteTo(writer); err != nil {
return fmt.Errorf("Sort>Failed to write: %v", err)
}
stats.OutputCounter++
prev.T, prev.K, prev.V = row.T, row.K, []interface{}{row.V}
} else if prev.K == nil {
prev.T, prev.K, prev.V = row.T, row.K, []interface{}{row.V}
} else {
prev.T = max(prev.T, row.T)
prev.V = append(prev.V, row.V)
}
return nil
})
if err != nil {
fmt.Printf("LocalGroupBySorted>Failed:%v\n", err)
return err
}
if err := prev.WriteTo(writer); err != nil {
return fmt.Errorf("Sort>Failed to write: %v", err)
}
stats.OutputCounter++
return nil
}