-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
198 lines (171 loc) · 5.24 KB
/
MainClass.java
File metadata and controls
198 lines (171 loc) · 5.24 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class MainClass {
public static void merge(HashMap<String, HashMap<String, Integer>> central, HashMap<String, HashMap<String, Integer>> temp) {
for (String docName : temp.keySet()) {
if (central.containsKey(docName)) {
;
}
else {
central.put(docName, new HashMap<String, Integer>());
}
for (String key : temp.get(docName).keySet()) {
if (central.get(docName).containsKey(key)) {
central.get(docName).put(key, central.get(docName).get(key) + temp.get(docName).get(key));
}
else {
central.get(docName).put(key, temp.get(docName).get(key));
}
}
}
}
/*public static void addToWp(WorkPool wp1, WorkPool wp2) {
Task t = wp2.getWork();
if (t != null) {
wp1.putWork(t);
}
while (t != null) {
System.out.println(t.toString());
t = wp2.getWork();
wp1.putWork(t);
}
}*/
public static void main(String args[]) throws InterruptedException, IOException {
int threads = Integer.parseInt(args[0]);
FileInputStream in = new FileInputStream(new File(args[1]));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read the input file
int D = Integer.parseInt(br.readLine());
double X = Double.parseDouble(br.readLine());
int ND = Integer.parseInt(br.readLine());
//Declare documents
ArrayList<String> documents = new ArrayList<>();
for (int i = 0; i < ND; i++)
{
documents.add(br.readLine());
}
br.close();
//Map
//Declare workpool and workers
WorkPool mapWorkPool = new WorkPool(threads);
WorkPool reduceWorkPool = new WorkPool(threads);
WorkPool compareWorkPool = new WorkPool(threads);
ArrayList<MapWorker> mapWorkers;
mapWorkers = new ArrayList<>();
for (int i = 0; i < threads; i++)
{
mapWorkers.add(new MapWorker(mapWorkPool, reduceWorkPool));
}
//Add tasks to workpool
for (int i = 0; i < documents.size(); i++)
{
File f = new File(documents.get(i));
long fileSize = f.length();
//System.out.println("File size is " + fileSize);
long cursor = 0;
while (cursor < fileSize)
{
if (D < fileSize - cursor)
{
mapWorkPool.putWork(new MapTask(documents.get(i), cursor, D));
cursor += D;
}
else
{
mapWorkPool.putWork(new MapTask(documents.get(i), cursor, fileSize - cursor));
cursor = fileSize;
}
}
}
//Start map threads
for (int i = 0; i < threads; i++)
{
mapWorkers.get(i).start();
}
for (int i = 0; i < threads; i++)
{
mapWorkers.get(i).join();
}
System.out.println("Am terminat map");
for (int i = 0; i < threads; i++) {
reduceWorkPool.add(mapWorkers.get(i).getWp());
}
System.out.println("Am terminat map");
//declare reduce threads
ArrayList<ReduceWorker> reduceWorkers;
reduceWorkers = new ArrayList<>();
for (int i = 0; i < threads; i++)
{
reduceWorkers.add(new ReduceWorker(reduceWorkPool, compareWorkPool));
}
//reduce workpool is already populated
for (int i = 0; i < threads; i++) {
reduceWorkers.get(i).start();
}
for (int i = 0; i < threads; i++) {
reduceWorkers.get(i).join();
}
//add all the data into a central Hashmap
HashMap<String, HashMap<String, Integer>> centralHash = new HashMap<String, HashMap<String, Integer>>();
HashMap<String, HashMap<String, Integer>> tempHash = new HashMap<String, HashMap<String, Integer>>();
for (int i = 0; i < threads; i++) {
tempHash = reduceWorkers.get(i).getHash();
merge(centralHash, tempHash);
}
System.out.println("Am terminat reduce");
//declare compare threads
ArrayList<CompareWorker> compareWorkers;
compareWorkers = new ArrayList<>();
for (int i = 0; i < threads; i++)
{
compareWorkers.add(new CompareWorker(compareWorkPool));
}
//add to compareWorkPool
for (int i = 0; i < documents.size(); i++) {
for (int j = i+1; j < documents.size(); j++) {
compareWorkPool.putWork(new CompareTask(documents.get(i), centralHash.get(documents.get(i)),
documents.get(j), centralHash.get(documents.get(j)), X));
}
}
for (int i = 0; i < threads; i++) {
compareWorkers.get(i).start();
}
for (int i = 0; i < threads; i++) {
compareWorkers.get(i).join();
}
ArrayList<CompareResult> results = new ArrayList<CompareResult>();
for (int i = 0; i < threads; i++) {
results.addAll(compareWorkers.get(i).getResults());
}
Collections.sort(results, new SimComparator());
PrintWriter writer = new PrintWriter(args[2], "UTF-8");
DecimalFormat d = new DecimalFormat("0.0000");
for (int i = 0; i < results.size(); i++) {
writer.println(results.get(i).doc1 + ";" + results.get(i).doc2 + ";" + d.format(results.get(i).X).replace('.', ','));
}
writer.close();
}
}
class SimComparator implements Comparator<CompareResult>{
public int compare(CompareResult r1, CompareResult r2) {
if (r1.X > r2.X) {
return -1;
}
else if (r1.X < r2.X) {
return 1;
}
else {
return 0;
}
}
}