-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapWorker.java
147 lines (139 loc) · 4.1 KB
/
MapWorker.java
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
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.StringTokenizer;
public class MapWorker extends Thread {
WorkPool wp, reduceWp;
//private HashMap<String, HashMap<String, Integer> > docHash;
public MapWorker(WorkPool workpool, WorkPool reduceWp) {
this.wp = workpool;
this.reduceWp = reduceWp;
}
/**
* Procesarea unei solutii partiale. Aceasta poate implica generarea unor
* noi solutii partiale care se adauga in workpool folosind putWork().
* @throws IOException
*/
void processPartialSolution(MapTask task) throws IOException {
//get string from file
HashMap<String, Integer> docHash = new HashMap<String, Integer>();
RandomAccessFile f = new RandomAccessFile(task.getDocumentName(), "r");
boolean end_of_file = false;
boolean begin_of_file = false;
if (task.getOffset() == 0) {
begin_of_file = true;
}
else {
f.seek(task.getOffset() - 1);
}
int dim = (int)task.getDim();
String add;
byte[] b = new byte[dim + 30];
try {
f.readFully(b);
}
catch (EOFException e) {
if (begin_of_file == false) {
f.read(b, 0, dim + 1);
}
else {
f.read(b, 0, dim);
}
end_of_file = true;
long len = f.length();
f.seek(task.getOffset() - 1);
b = new byte[(int)(len - task.getOffset())];
f.readFully(b);
}
String s = new String(b);
//System.out.println(s);
//if the task begins in the middle of a word, ignore it
if (task.getOffset() != 0)
{
boolean flag = false;
for (int i = 0; i < dim; i++) {
char c = s.charAt(i);
if (c == '_' || c == ';' || c == ':' || c == '?' || c == '~' ||
c == '\\' || c == '.' || c == ',' || c == '>' || c == '<' ||
c == '~' || c == '`' || c == '[' || c == ']' || c == '{' ||
c == '}' || c == '(' || c == ')' || c == '!' || c == '@' ||
c == '#' || c == '$' || c == '%' || c == '^' || c == '&' ||
c == '-' || c == '+' || c == '\'' || c == '*' || c == '|' ||
c == '\"' || c == '\t' || c == '\n' || c == ' ' ) {
s = s.substring(i + 1, s.length());
dim = dim - i;
flag = true;
break;
}
}
if (flag == false) {
f.close();
return;
}
}
//if the task ends in the middle of a word, add the entire word to the task
if (end_of_file == false || end_of_file == true)
{
for (int i = 0; i < s.length() - dim; i++)
{
//find out where the word ends; modify the task dimension accordingly
char c = s.charAt(dim + i - 1);
if (c == '_' || c == ';' || c == ':' || c == '?' || c == '~' ||
c == '\\' || c == '.' || c == ',' || c == '>' || c == '<' ||
c == '~' || c == '`' || c == '[' || c == ']' || c == '{' ||
c == '}' || c == '(' || c == ')' || c == '!' || c == '@' ||
c == '#' || c == '$' || c == '%' || c == '^' || c == '&' ||
c == '-' || c == '+' || c == '\'' || c == '*' || c == '|' ||
c == '\"' || c == '\t' || c == '\n' || c == ' ' )
{
task.setDim(dim + i);
dim = dim + i;
s = s.substring(0, dim);
//System.out.println(s);
break;
}
}
}
//System.out.println(s);
//add the words to the hashmap
//System.out.println(s);
StringTokenizer st = new StringTokenizer(s, "_;:/?~\\.,><~`[]{}()!@#$%^&-+'=*|\"\t\n ", false);
while (st.hasMoreTokens()) {
add = st.nextToken().toLowerCase();
if (docHash.containsKey(add))
{
docHash.put(add, docHash.get(add) + 1);
}
else
{
docHash.put(add, 1);
}
//System.out.println("Am adaugat " + add + ".");
}
reduceWp.putWork(new ReduceTask(task.getDocumentName(), docHash));
f.close();
}
public WorkPool getWp(){
return reduceWp;
}
@Override
public void run() {
//System.out.println("Thread-ul worker " + this.getName() + " a pornit...");
MapTask task;
while (true) {
task = (MapTask)wp.getWork();
if (task == null){
break;
}
try {
processPartialSolution(task);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Add the partial solutions to the Reduce workpool
//reduceWp.putWork(new ReduceTask(docHash));
}
}