-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (142 loc) · 3.23 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bufio"
"context"
"fmt"
"hash/crc32"
"os"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
)
type Result struct {
text string
}
const CHUNK_SIZE = 1024 * 1024
type StationData struct {
min, max, sum float64
count int
}
var STATIONS_MAP []sync.Map
var numShards = 128
func getShard(key string) int {
return int(crc32.ChecksumIEEE([]byte(key))) % numShards
}
func parseValues(chunk string) {
lines := strings.Split(chunk, "\n")
for _, line := range lines {
if line == "" {
continue
}
parts := strings.Split(line, ";")
if len(parts) != 2 {
continue
}
station := parts[0]
temp := parts[1]
if temp == "" {
continue
}
temperature, err := strconv.ParseFloat(temp, 64)
if err != nil {
continue
}
shard := getShard(station)
data, _ := STATIONS_MAP[shard].LoadOrStore(station, &StationData{min: temperature, max: temperature})
stationData := data.(*StationData)
if temperature < stationData.min {
stationData.min = temperature
}
if temperature > stationData.max {
stationData.max = temperature
}
stationData.sum += temperature
stationData.count++
}
}
func worker(ctx context.Context, wg *sync.WaitGroup, fileReadJobs <-chan []byte) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case chunk, ok := <-fileReadJobs:
if !ok {
return
}
chunkText := string(chunk)
parseValues(chunkText)
}
}
}
func readFile(fileReadJobs chan<- []byte) error {
file, err := os.Open("./measurements.txt")
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
chunk := make([]byte, CHUNK_SIZE)
for {
size, err := reader.Read(chunk)
if err != nil {
if err.Error() == "EOF" {
break
}
return err
}
fileReadJobs <- chunk[:size]
}
close(fileReadJobs)
return nil
}
func main() {
start := time.Now()
numCpu := runtime.NumCPU()
runtime.GOMAXPROCS(numCpu)
var wg sync.WaitGroup
goroutineCount := numCpu * 4
fileReadJobs := make(chan []byte, goroutineCount)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
STATIONS_MAP = make([]sync.Map, numShards)
for i := 0; i < goroutineCount; i++ {
wg.Add(1)
go worker(ctx, &wg, fileReadJobs)
}
go func() {
err := readFile(fileReadJobs)
if err != nil {
fmt.Println("Error: ", err)
cancel()
}
}()
wg.Wait()
file, err := os.Create("./output.txt")
if err != nil {
fmt.Println("Error: ", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
var contents []string
for shard := 0; shard < numShards; shard++ {
STATIONS_MAP[shard].Range(func(key, value interface{}) bool {
station := key.(string)
data := value.(*StationData)
avg := data.sum / float64(data.count)
contents = append(contents, fmt.Sprintf("%s;%.2f;%.2f;%.2f", station, data.min, data.max, avg))
return true
})
}
sort.Strings(contents)
for _, line := range contents {
writer.WriteString(line + "\n")
}
writer.Flush()
elapsed := time.Since(start)
fmt.Printf("Time elapsed: %s\n", elapsed)
}