/
main.go
164 lines (143 loc) · 3.01 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
)
// TSVScanner splits input line into chunks separated with TAB character
type TSVScanner struct {
orig []byte
rest []byte
cols int
curCol int
}
// NewTSVScanner constructor
func NewTSVScanner(colLimit int) *TSVScanner {
return &TSVScanner{
cols: colLimit,
}
}
// SetSource ...
func (s *TSVScanner) SetSource(line []byte) {
s.orig = line
s.rest = line
}
// GetField ...
func (s *TSVScanner) GetField() (field []byte, err error) {
pos := -1
for i, char := range s.rest {
if char == '\t' {
pos = i
break
}
}
if pos < 0 {
err = fmt.Errorf("Malformed line `%s`: not enough columns", string(s.orig))
return
}
field = s.rest[:pos]
s.rest = s.rest[pos+1:]
return
}
// Atoi "fast" parsing
func Atoi(s []byte) (int, bool) {
i := 0
var x int
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
return 0, false
}
x = x*10 + int(c) - '0'
}
return x, true
}
// Process processed that google's TSV file
func Process(r io.Reader, keyIndex, valIndex, colLimit int) (key string, val int, err error) {
counters := make(map[string]*int, 16384)
countValues := make([]int, 0, 16384)
indices := [2]int{keyIndex, valIndex}
if keyIndex > valIndex {
indices[0], indices[1] = indices[1], indices[0]
}
s := NewTSVScanner(colLimit)
reader := bufio.NewReaderSize(r, 512*1024)
scanner := bufio.NewScanner(reader)
var t1, t2 []byte
for scanner.Scan() {
s.SetSource(scanner.Bytes())
for i := 1; i < indices[0]; i++ {
_, err = s.GetField()
if err != nil {
return
}
}
t1, err = s.GetField()
if err != nil {
return
}
for i := indices[0] + 1; i < indices[1]; i++ {
_, err = s.GetField()
if err != nil {
return
}
}
t2, err = s.GetField()
if err != nil {
return
}
if keyIndex > valIndex {
t1, t2 = t2, t1
}
v, ok := Atoi(t2)
if !ok {
err = fmt.Errorf("Cannot parse value `%s` into integer in `%s`", string(t2), scanner.Text())
return
}
if val, ok := counters[string(t1)]; ok {
*val += v
} else {
countValues = append(countValues, v)
counters[string(t1)] = &(countValues[len(countValues)-1])
}
}
key = ""
val = -1
for k, v := range counters {
if *v > val {
key = k
val = *v
}
}
return
}
func main() {
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var keyIndex, valIndex, colLimit int
flag.IntVar(&keyIndex, "key", 2, "key column index (1, 2, 3, ...)")
flag.IntVar(&valIndex, "val", 3, "value column index (1, 2, 3, ...)")
flag.IntVar(&colLimit, "cols", 4, "how many columns to scan")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
inputFileName := flag.Arg(0)
file, err := os.Open(inputFileName)
if err != nil {
panic(err)
}
key, val, err := Process(file, keyIndex, valIndex, colLimit)
if err != nil {
panic(err)
}
fmt.Printf("Max key: %s, max val: %d\n", key, val)
}