-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (192 loc) · 4.25 KB
/
main.go
File metadata and controls
213 lines (192 loc) · 4.25 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
. "./utils"
"bufio"
"fmt"
"io"
"os"
"strconv"
"time"
//"strings"
)
var NUM_FILE int = 100
var NUM_TOP int = 100
var SIZE_BATCH int = 3900000
var memString []string
// Read the file, put the url into memory。
// handle batch operation when the size reaches SIZE_BATCH for preventing memory explosion
func ReadFile(filePath string, handle func([]string)) error {
f, err := os.Open(filePath)
defer f.Close()
if err != nil {
return err
}
buf := bufio.NewReader(f)
count := 0
memString = make([]string, 0)
for {
line, _, err := buf.ReadLine()
if count == SIZE_BATCH {
handle(memString)
memString = make([]string, 0)
count = 0
}
memString = append(memString, string(line))
if err != nil {
if err == io.EOF {
if len(memString) > 0 {
handle(memString)
memString = make([]string, 0)
count = 0
}
return nil
}
return err
}
count++
}
}
// Batch processing of in-memory data,
// Use the BKDRHash64 function to split the urls in the oversized file and put them into NUM_FILE files.
func setPartition(strs []string) {
fileMap := make(map[string][]string)
for _, str := range strs {
if str == "" {
continue
}
partition := "./tmp/" + strconv.Itoa(int(BKDRHash64(str))%NUM_FILE) + ".txt"
if _, ok := fileMap[partition]; ok {
fileMap[partition] = append(fileMap[partition], str)
} else {
fileMap[partition] = []string{str}
}
}
temp_dir := "./tmp"
_, err := os.Stat(temp_dir)
if err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(temp_dir, os.ModePerm)
if err != nil {
fmt.Printf("mkdir failed![%v]\n", err)
return
}
}
}
for k, vs := range fileMap {
f, err := os.OpenFile(k, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err.Error())
} else {
for _, v := range vs {
_, err = f.Write([]byte(v + "\n"))
}
}
f.Close()
}
}
// Calculate the Heap generated by the file
func getMinHeapFromFile(filePath string) (*MinHeap, error) {
FreqMap := make(map[string]int64)
var addToHashmap func([]string)
addToHashmap = func(keys []string) {
for _, key := range keys {
if _, ok := FreqMap[key]; ok {
FreqMap[key]++
} else {
if key != "" {
FreqMap[key] = 1
}
}
}
}
err := ReadFile(filePath, addToHashmap)
if err != nil {
return nil, err
}
heap := NewMinHeap()
for k, v := range FreqMap {
if heap.Length() < NUM_TOP {
heap.Insert(&Url{v, k})
continue
}
min, _ := heap.Min()
if min.Freq <= v {
heap.DeleteMin()
heap.Insert(&Url{v, k})
}
}
return heap, nil
}
// Merge two MinHeap with a Length NUM_TOP
// Because we only need Top K urls
func mergeTwoHeap(oldH, newH *MinHeap) *MinHeap {
if newH == nil || newH.Length() == 0 {
return oldH
}
for newH.Length() != 0 {
value, _ := newH.DeleteMin()
if oldH.Length() < NUM_TOP {
oldH.Insert(value)
continue
}
min, _ := oldH.Min()
if min.Freq <= value.Freq {
oldH.DeleteMin()
oldH.Insert(value)
}
}
return oldH
}
// Reduce all MinHeap
func reduce() *MinHeap {
heap := NewMinHeap()
for i := 0; i < NUM_FILE; i++ {
NextHeap, err := getMinHeapFromFile("./tmp/" + strconv.Itoa(i) + ".txt")
if err != nil {
continue
}
heap = mergeTwoHeap(heap, NextHeap)
}
return heap
}
// Output the contents of min_heap to a file
func heapToFile(heap *MinHeap) error {
f, err := os.OpenFile("./output.txt", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
defer f.Close()
if err != nil {
fmt.Println(err.Error())
return err
} else {
for heap.Length() != 0 {
item, _ := heap.DeleteMin()
_, err = f.Write([]byte("Frequence: " + strconv.FormatInt(item.Freq, 10) + " | Url: " + item.Addr + "\n"))
if err != nil {
fmt.Println(err.Error())
return err
}
}
return nil
}
}
func main() {
// err := GenerateUrlData("./Dataset.txt")
// if err != nil {
// fmt.Println(err.Error())
// return
// }
t2 := time.Now() // get current time
err := ReadFile("./Dataset.txt", setPartition)
if err != nil {
fmt.Println(err.Error())
return
}
heap := reduce()
err = heapToFile(heap)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("The top " + strconv.Itoa(NUM_TOP) + " results have been output to file \"./output.txt\"")
elapsed := time.Since(t2)
fmt.Println("App elapsed: ", elapsed)
}