Repository navigation
Expand file tree
/
Copy pathWorker.java
More file actions
178 lines (148 loc) · 6.12 KB
/
Copy pathWorker.java
File metadata and controls
178 lines (148 loc) · 6.12 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
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.*;
import java.util.*;
// Represents a parallel processing thread.
// Each worker contributes to all stages of the pipeline:
public class Worker extends Thread {
private final int id;
private final ObjectMapper mapper; // Jackson parser instance
public Worker(int id) {
this.id = id;
this.mapper = new ObjectMapper();
}
@Override
public void run() {
try {
// STAGE 1: Parallel Reading
executeParallelReading();
SharedData.barrier.await(); // Wait for all threads to finish reading
// STAGE 2: Centralized Processing (Thread 0 only)
// Removes duplicates and sorts the unified list to ensure determinism.
if (id == 0) {
processArticlesCentralized();
}
SharedData.barrier.await(); // Wait for processing to complete
// STAGE 3: Parallel Mapping
executeParallelMapping();
SharedData.barrier.await(); // Wait for mapping to finish
// STAGE 4: Final Output (Thread 0 only)
if (id == 0) {
Writer.finalizeAndWriteFiles();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Dynamic Load Balancing: Threads pull file indices from an atomic counter.
private void executeParallelReading() {
List<Article> threadBuffer = new LinkedList<>();
while (true) {
int fileId = SharedData.fileIndex.getAndIncrement();
if (fileId >= SharedData.inputJsonFiles.size()) {
break;
}
File file = new File(SharedData.inputJsonFiles.get(fileId));
if (file.exists()) {
parseJsonFile(file, threadBuffer);
}
}
// Merge local results into the shared list safely.
if (!threadBuffer.isEmpty()) {
SharedData.addRawArticles(threadBuffer);
}
}
private void parseJsonFile(File file, List<Article> buffer) {
try {
List<Article> parsed = mapper.readValue(file, new TypeReference<List<Article>>() {});
if (parsed != null) {
buffer.addAll(parsed);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// OPTIMIZATION: Uses HashMap to count occurrences.
// Filters out articles that appear more than once (strict duplicate removal).
private void processArticlesCentralized() {
Map<String, Integer> uuidCounts = new HashMap<>();
Map<String, Integer> titleCounts = new HashMap<>();
// 1. Count frequencies
for (Article a : SharedData.rawArticles) {
if (a.uuid != null) {
uuidCounts.put(a.uuid, uuidCounts.getOrDefault(a.uuid, 0) + 1);
}
if (a.title != null) {
titleCounts.put(a.title, titleCounts.getOrDefault(a.title, 0) + 1);
}
}
SharedData.validArticles = new ArrayList<>();
SharedData.duplicatesFound = 0;
// 2. Filter valid articles
for (Article a : SharedData.rawArticles) {
boolean isUniqueUuid = (a.uuid != null && uuidCounts.get(a.uuid) == 1);
boolean isUniqueTitle = (a.title != null && titleCounts.get(a.title) == 1);
if (isUniqueUuid && isUniqueTitle) {
SharedData.validArticles.add(a);
} else {
SharedData.duplicatesFound++;
}
}
// Final sorting for global file output.
Collections.sort(SharedData.validArticles, new Sort.SortByDate());
}
// Dynamic Load Balancing for processing articles.
private void executeParallelMapping() {
List<ResultInfo> catBuffer = new LinkedList<>();
List<ResultInfo> langBuffer = new LinkedList<>();
List<String> keywordBuffer = new LinkedList<>();
while (true) {
int idx = SharedData.articleIndex.getAndIncrement();
if (idx >= SharedData.validArticles.size()) break;
Article art = SharedData.validArticles.get(idx);
extractCategories(art, catBuffer);
extractLanguage(art, langBuffer);
extractKeywords(art, keywordBuffer);
}
// Merge local results into global aggregators.
if (!catBuffer.isEmpty()) SharedData.addGlobalCatPairs(catBuffer);
if (!langBuffer.isEmpty()) SharedData.addGlobalLangPairs(langBuffer);
if (!keywordBuffer.isEmpty()) SharedData.addGlobalKeywords(keywordBuffer);
}
private void extractCategories(Article art, List<ResultInfo> buffer) {
if (art.categories == null) return;
for (String cat : art.categories) {
if (SharedData.targetCategories.contains(cat)) {
String normalized = Utils.normalizeCategoryName(cat);
buffer.add(new ResultInfo(normalized, art.uuid));
}
}
}
private void extractLanguage(Article art, List<ResultInfo> buffer) {
if (art.language != null && SharedData.targetLanguages.contains(art.language)) {
buffer.add(new ResultInfo(art.language, art.uuid));
}
}
// Text processing for English articles.
private void extractKeywords(Article art, List<String> buffer) {
if (!"english".equals(art.language) || art.text == null) return;
List<String> uniqueInDoc = new LinkedList<>();
String content = art.text.toLowerCase(Locale.ROOT);
String[] tokens = content.split("\\s+");
for (String rawToken : tokens) {
String cleanToken = "";
// Keep only letters
for (char c : rawToken.toCharArray()) {
if (c >= 'a' && c <= 'z') {
cleanToken += c;
}
}
if (!cleanToken.isEmpty() && !SharedData.stopWords.contains(cleanToken)) {
if (!uniqueInDoc.contains(cleanToken)) {
uniqueInDoc.add(cleanToken);
}
}
}
buffer.addAll(uniqueInDoc);
}
}