-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessingEngine.java
More file actions
139 lines (110 loc) · 4.69 KB
/
Copy pathProcessingEngine.java
File metadata and controls
139 lines (110 loc) · 4.69 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
package csc435.app;
import java.util.ArrayList;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
class IndexResult {
public double executionTime;
public long totalBytesRead;
public IndexResult(double executionTime, long totalBytesRead) {
this.executionTime = executionTime;
this.totalBytesRead = totalBytesRead;
}
}
class DocPathFreqPair {
public String documentPath;
public long wordFrequency;
public DocPathFreqPair(String documentPath, long wordFrequency) {
this.documentPath = documentPath;
this.wordFrequency = wordFrequency;
}
}
class SearchResult {
public double excutionTime;
public ArrayList<DocPathFreqPair> documentFrequencies;
public SearchResult(double executionTime, ArrayList<DocPathFreqPair> documentFrequencies) {
this.excutionTime = executionTime;
this.documentFrequencies = documentFrequencies;
}
}
public class ProcessingEngine {
// keep a reference to the index store
private IndexStore store;
// the number of worker threads to use during indexing
private int numWorkerThreads;
public ProcessingEngine(IndexStore store, int numWorkerThreads) {
this.store = store;
this.numWorkerThreads = numWorkerThreads;
}
public IndexResult indexFiles(String folderPath) {
IndexResult result = new IndexResult(0.0, 0);
long startTime = System.currentTimeMillis();
try {
List<Path> filePaths = Files.walk(Paths.get(folderPath))
.filter(Files::isRegularFile)
.collect(Collectors.toList());
ExecutorService executor = Executors.newFixedThreadPool(numWorkerThreads);
List<Future<Long>> futures = new ArrayList<>();
for (Path filePath : filePaths) {
futures.add(executor.submit(() -> indexFile(filePath)));
}
long totalBytesRead = 0;
for (Future<Long> future : futures) {
totalBytesRead += future.get();
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
long endTime = System.currentTimeMillis();
double executionTime = (endTime - startTime) / 1000.0;
result = new IndexResult(executionTime, totalBytesRead);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return result;
}
private long indexFile(Path filePath) throws IOException {
long documentNumber = store.putDocument(filePath.toString());
String content = new String(Files.readAllBytes(filePath));
HashMap<String, Long> wordFrequencies = extractWordFrequencies(content);
store.updateIndex(documentNumber, wordFrequencies);
return Files.size(filePath);
}
private HashMap<String, Long> extractWordFrequencies(String content) {
HashMap<String, Long> frequencies = new HashMap<>();
String[] words = content.split("\\W+");
for (String word : words) {
if (word.length() > 2 && word.matches("^[a-zA-Z0-9]+$")) {
frequencies.put(word.toLowerCase(), frequencies.getOrDefault(word.toLowerCase(), 0L) + 1);
}
}
return frequencies;
}
public SearchResult searchFiles(ArrayList<String> terms) {
SearchResult result = new SearchResult(0.0, new ArrayList<DocPathFreqPair>());
long startTime = System.currentTimeMillis();
HashMap<Long, Long> documentFrequencies = new HashMap<>();
for (String term : terms) {
ArrayList<DocFreqPair> pairs = store.lookupIndex(term.toLowerCase());
for (DocFreqPair pair : pairs) {
documentFrequencies.merge(pair.documentNumber, pair.wordFrequency, Long::sum);
}
}
List<Map.Entry<Long, Long>> sortedEntries = documentFrequencies.entrySet().stream()
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
.limit(10)
.collect(Collectors.toList());
ArrayList<DocPathFreqPair> topDocuments = new ArrayList<>();
for (Map.Entry<Long, Long> entry : sortedEntries) {
String documentPath = store.getDocument(entry.getKey());
topDocuments.add(new DocPathFreqPair(documentPath, entry.getValue()));
}
long endTime = System.currentTimeMillis();
double executionTime = (endTime - startTime) / 1000.0;
result = new SearchResult(executionTime, topDocuments);
return result;
}
}