-
Notifications
You must be signed in to change notification settings - Fork 11
/
readfile3.go
128 lines (117 loc) · 2.96 KB
/
readfile3.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"time"
)
func main() {
start := time.Now()
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
firstNamePat := regexp.MustCompile(", \\s*([^, ]+)")
names := make([]string, 0)
firstNames := make([]string, 0)
dates := make([]string, 0)
commonName := ""
commonCount := 0
scanner := bufio.NewScanner(file)
type entry struct {
firstName string
name string
date string
wg *sync.WaitGroup
}
entriesC := make(chan []entry)
wg := sync.WaitGroup{}
go func() {
for {
select {
case entries, ok := <-entriesC:
if ok {
for _, entry := range entries {
if entry.firstName != "" {
firstNames = append(firstNames, entry.firstName)
}
names = append(names, entry.name)
dates = append(dates, entry.date)
entry.wg.Done()
}
}
}
}
}()
linesChunkLen := 64 * 1024
lines := make([]string, 0, 0)
scanner.Scan()
for {
lines = append(lines, scanner.Text())
willScan := scanner.Scan()
if len(lines) == linesChunkLen || !willScan {
toProcess := lines
wg.Add(len(toProcess))
go func() {
entries := make([]entry, 0, len(toProcess))
for _, text := range toProcess {
// get all the names
entry := entry{wg: &wg}
split := strings.SplitN(text, "|", 9)
name := strings.TrimSpace(split[7])
entry.name = name
// extract first names
if matches := firstNamePat.FindAllStringSubmatch(name, 1); len(matches) > 0 {
entry.firstName = matches[0][1]
}
// extract dates
chars := strings.TrimSpace(split[4])[:6]
entry.date = chars[:4] + "-" + chars[4:6]
entries = append(entries, entry)
}
entriesC <- entries
}()
lines = make([]string, 0, linesChunkLen)
}
if !willScan {
break
}
}
wg.Wait()
close(entriesC)
// report c2: names at index
fmt.Printf("Name: %s at index: %v\n", names[0], 0)
fmt.Printf("Name: %s at index: %v\n", names[432], 432)
fmt.Printf("Name: %s at index: %v\n", names[43243], 43243)
fmt.Printf("Name time: %v\n", time.Since(start))
// report c1: total number of lines
fmt.Printf("Total file line count: %v\n", len(names))
fmt.Printf("Line count time: %v\n", time.Since(start))
// report c3: donation frequency
dateMap := make(map[string]int)
for _, date := range dates {
dateMap[date] += 1
}
for k, v := range dateMap {
fmt.Printf("Donations per month and year: %v and donation count: %v\n", k, v)
}
fmt.Printf("Donations time: %v\n", time.Since(start))
// report c4: most common firstName
nameMap := make(map[string]int)
nameCount := 0
for _, name := range firstNames {
nameCount = nameMap[name] + 1
nameMap[name] = nameCount
if nameCount > commonCount {
commonName = name
commonCount = nameCount
}
}
fmt.Printf("The most common first name is: %s and it occurs: %v times.\n", commonName, commonCount)
fmt.Printf("Most common name time: %v\n", time.Since(start))
}