-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathr8.go
167 lines (147 loc) · 3.34 KB
/
r8.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
// r8: add some parallelism (but back to non-optimized r1 version)
//
// ~213ms for 10M rows (4.71x as fast as r1)
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
type r8Stats struct {
min, max, sum float64
count int64
}
func r8(inputPath string, output io.Writer) error {
parts, err := splitFile(inputPath, maxGoroutines)
if err != nil {
return err
}
resultsCh := make(chan map[string]r8Stats)
for _, part := range parts {
go r8ProcessPart(inputPath, part.offset, part.size, resultsCh)
}
totals := make(map[string]r8Stats)
for i := 0; i < len(parts); i++ {
result := <-resultsCh
for station, s := range result {
ts, ok := totals[station]
if !ok {
totals[station] = r8Stats{
min: s.min,
max: s.max,
sum: s.sum,
count: s.count,
}
continue
}
ts.min = min(ts.min, s.min)
ts.max = max(ts.max, s.max)
ts.sum += s.sum
ts.count += s.count
totals[station] = ts
}
}
stations := make([]string, 0, len(totals))
for station := range totals {
stations = append(stations, station)
}
sort.Strings(stations)
fmt.Fprint(output, "{")
for i, station := range stations {
if i > 0 {
fmt.Fprint(output, ", ")
}
s := totals[station]
mean := s.sum / float64(s.count)
fmt.Fprintf(output, "%s=%.1f/%.1f/%.1f", station, s.min, mean, s.max)
}
fmt.Fprint(output, "}\n")
return nil
}
func r8ProcessPart(inputPath string, fileOffset, fileSize int64, resultsCh chan map[string]r8Stats) {
file, err := os.Open(inputPath)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Seek(fileOffset, io.SeekStart)
if err != nil {
panic(err)
}
f := io.LimitedReader{R: file, N: fileSize}
stationStats := make(map[string]r8Stats)
scanner := bufio.NewScanner(&f)
for scanner.Scan() {
line := scanner.Text()
station, tempStr, hasSemi := strings.Cut(line, ";")
if !hasSemi {
continue
}
temp, err := strconv.ParseFloat(tempStr, 64)
if err != nil {
panic(err)
}
s, ok := stationStats[station]
if !ok {
s.min = temp
s.max = temp
s.sum = temp
s.count = 1
} else {
s.min = min(s.min, temp)
s.max = max(s.max, temp)
s.sum += temp
s.count++
}
stationStats[station] = s
}
resultsCh <- stationStats
}
type part struct {
offset, size int64
}
func splitFile(inputPath string, numParts int) ([]part, error) {
const maxLineLength = 100
f, err := os.Open(inputPath)
if err != nil {
return nil, err
}
st, err := f.Stat()
if err != nil {
return nil, err
}
size := st.Size()
splitSize := size / int64(numParts)
buf := make([]byte, maxLineLength)
parts := make([]part, 0, numParts)
offset := int64(0)
for i := 0; i < numParts; i++ {
if i == numParts-1 {
if offset < size {
parts = append(parts, part{offset, size - offset})
}
break
}
seekOffset := max(offset+splitSize-maxLineLength, 0)
_, err := f.Seek(seekOffset, io.SeekStart)
if err != nil {
return nil, err
}
n, _ := io.ReadFull(f, buf)
chunk := buf[:n]
newline := bytes.LastIndexByte(chunk, '\n')
if newline < 0 {
return nil, fmt.Errorf("newline not found at offset %d", offset+splitSize-maxLineLength)
}
remaining := len(chunk) - newline - 1
nextOffset := seekOffset + int64(len(chunk)) - int64(remaining)
parts = append(parts, part{offset, nextOffset - offset})
offset = nextOffset
}
return parts, nil
}