-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrawlerConfiguration.java
344 lines (316 loc) · 10.8 KB
/
CrawlerConfiguration.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.udacity.webcrawler.json;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.time.Duration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* A data class that represents the configuration of a single web crawl.
*/
@JsonDeserialize(builder = CrawlerConfiguration.Builder.class)
public final class CrawlerConfiguration {
private final List<String> startPages;
private final List<Pattern> ignoredUrls;
private final List<Pattern> ignoredWords;
private final int parallelism;
private final String implementationOverride;
private final int maxDepth;
private final Duration timeout;
private final int popularWordCount;
private final String profileOutputPath;
private final String resultPath;
private CrawlerConfiguration(
List<String> startPages,
List<Pattern> ignoredUrls,
List<Pattern> ignoredWords,
int parallelism,
String implementationOverride,
int maxDepth,
Duration timeout,
int popularWordCount,
String profileOutputPath,
String resultPath) {
this.startPages = startPages;
this.ignoredUrls = ignoredUrls;
this.ignoredWords = ignoredWords;
this.parallelism = parallelism;
this.implementationOverride = implementationOverride;
this.maxDepth = maxDepth;
this.timeout = timeout;
this.popularWordCount = popularWordCount;
this.profileOutputPath = profileOutputPath;
this.resultPath = resultPath;
}
/**
* An unmodifiable {@link List} of URLs that define the starting points of the web crawl. It
* should not contain any duplicate URLs.
*/
public List<String> getStartPages() {
return startPages;
}
/**
* A {@link List} of regular expression {@link Pattern}s that determine which URLs, if any, the
* web crawler should not follow.
*/
public List<Pattern> getIgnoredUrls() {
return ignoredUrls;
}
/**
* A {@link List} of regular expression {@link Pattern}s that determine which words, if any, the
* web crawler should not be counted toward the popular word count returned from the crawl.
*/
public List<Pattern> getIgnoredWords() {
return ignoredWords;
}
/**
* The desired parallelism (e.g., number of CPU cores) that should be used for the web crawl. This
* setting is optional.
*
* <p>If set to 1, the legacy sequential crawler will be used (unless
* {@link #getImplementationOverride()} explicitly specifies otherwise). If set to a value less
* than 1, the crawler will default to using the number of available CPU cores on the system.
*/
public int getParallelism() {
return parallelism;
}
/**
* An explicit override for which web crawler implementation should be used for this crawl.
*
* <p>If set, it should be the fully qualified class name of a
* {@link com.udacity.webcrawler.WebCrawler} implementation, for example
* {@code "com.udacity.webcrawler.SequentialWebCrawler"}.
*
* <p>If unset or empty, the value of {@link #getParallelism()} setting is used to determine which
* crawler implementation to use.
*/
public String getImplementationOverride() {
return implementationOverride;
}
/**
* The maximum depth allowed for the crawl.
*
* <p>The "depth" of a crawl is the number of links the crawler has followed, starting from the
* starting URLs (see {@link #getStartPages()}).
*
* <p>
* <p>Example:
*
* <p>Suppose the max depth is 2, and suppose the starting page "A" links to the following web
* pages:
*
* <p><pre>
* A
* / | \
* B C D
* / \ \
* E F G
* </pre>
*
* <p>In this example, the crawler will visit at most A, B, C, and D. The crawler may visit
* <i>fewer</i> pages if it runs out of time before it can finish. See {@link #getTimeout()}.
*/
public int getMaxDepth() {
return maxDepth;
}
/**
* The maximum amount of time the crawler is allowed to run.
*
* <p>This is not a strict deadline, meaning the crawler does not have to drop everything and
* terminate immediately. However, Once this amount of time has been reached, the crawler will
* finish processing any HTML it has already downloaded, but it will not download any more pages
* or follow any more links.
*/
public Duration getTimeout() {
return timeout;
}
/**
* The number of popular words the crawler should record in its output.
*
* <p>See {@link com.udacity.webcrawler.json.CrawlResult#getWordCounts()} for more information.
*/
public int getPopularWordCount() {
return popularWordCount;
}
/**
* Path to the output file where performance data from this web crawl should be written.
*
* <p>If a file already exists at the path, performance information should be appended to the
* existing file.
*
* <p>If the path is empty, the data will be written to standard output.
*/
public String getProfileOutputPath() {
return profileOutputPath;
}
/**
* Path to the output file where the result data from this web crawl should be written.
*
* <p>If a file already exists at the path, the existing file should be replaced.
*
* <p>If the path is empty, the data will be written to standard output.
*
* <p>See {@link com.udacity.webcrawler.json.CrawlResult}.
*/
public String getResultPath() {
return resultPath;
}
/**
* A builder class to create {@link CrawlerConfiguration} instances.
*/
public static final class Builder {
private final Set<String> startPages = new LinkedHashSet<>();
private final Set<String> ignoredUrls = new LinkedHashSet<>();
private final Set<String> ignoredWords = new LinkedHashSet<>();
private int parallelism = -1;
private String implementationOverride = "";
private int maxDepth = 0;
private int timeoutSeconds = 1;
private int popularWordCount = 0;
private String profileOutputPath = "";
private String resultPath = "";
/**
* Adds a start page URL.
*
* <p>Does nothing if the given page has already been added. See {@link #getStartPages()}.
*/
@JsonProperty("startPages")
public Builder addStartPages(String... startPages) {
for (String startPage : startPages) {
this.startPages.add(Objects.requireNonNull(startPage));
}
return this;
}
/**
* Adds a regular expression pattern that defines URLs to ignore during the crawl.
*
* <p>Does nothing if the same pattern has already been added. See {@link #getIgnoredUrls()}.
*
* @param patterns one or more regular expressions that define a valid {@link Pattern}.
*/
@JsonProperty("ignoredURLs")
public Builder addIgnoredUrls(String... patterns) {
for (String pattern : patterns) {
ignoredUrls.add(Objects.requireNonNull(pattern));
}
return this;
}
/**
* Adds a regular expression pattern that defines words to ignore when computing the popular
* counts.
*
* <p>Does nothing if the same pattern has already been added. See {@link #getIgnoredWords()}.
*
* <p>See {@link com.udacity.webcrawler.json.CrawlResult#getWordCounts()} for more information
* about the popular word computation.
*
* @param patterns one or more regular expressions that define a valid {@link Pattern}.
*/
@JsonProperty("ignoredWords")
public Builder addIgnoredWords(String... patterns) {
for (String pattern : patterns) {
ignoredWords.add(Objects.requireNonNull(pattern));
}
return this;
}
/**
* Sets the desired parallelism of the crawl.
*
* <p>See {@link #getParallelism()}.
*/
@JsonProperty("parallelism")
public Builder setParallelism(int parallelism) {
this.parallelism = parallelism;
return this;
}
/**
* Overrides the {@link com.udacity.webcrawler.WebCrawler} implementation that should be used
* for the crawl.
*
* <p>See {@link #getImplementationOverride()}.
*/
@JsonProperty("implementationOverride")
public Builder setImplementationOverride(String implementationOverride) {
this.implementationOverride = Objects.requireNonNull(implementationOverride);
return this;
}
/**
* Sets the maximum depth of the crawl.
*
* <p>See {@link #getMaxDepth()}.
*/
@JsonProperty("maxDepth")
public Builder setMaxDepth(int maxDepth) {
this.maxDepth = maxDepth;
return this;
}
/**
* Sets the maximum amount of time allowed for the crawl, specified in seconds.
*
* <p>See {@link #getTimeout()}.
*/
@JsonProperty("timeoutSeconds")
public Builder setTimeoutSeconds(int seconds) {
this.timeoutSeconds = seconds;
return this;
}
/**
* Sets the number of most popular words that should be reported by the crawl.
*
* <p>See {@link #getPopularWordCount()}.
*/
@JsonProperty("popularWordCount")
public Builder setPopularWordCount(int popularWordCount) {
this.popularWordCount = popularWordCount;
return this;
}
/**
* Sets the path to the file where profiling data for this crawl should be written.
*
* <p>See {@link #getProfileOutputPath()}.
*/
@JsonProperty("profileOutputPath")
public Builder setProfileOutputPath(String profileOutputPath) {
this.profileOutputPath = Objects.requireNonNull(profileOutputPath);
return this;
}
/**
* Sets the path to the file where the result of this crawl should be written.
*
* <p>See {@link #getResultPath()}.
*/
@JsonProperty("resultPath")
public Builder setResultPath(String resultPath) {
this.resultPath = Objects.requireNonNull(resultPath);
return this;
}
/**
* Constructs a {@link CrawlerConfiguration} from this builder.
*/
public CrawlerConfiguration build() {
if (maxDepth < 0) {
throw new IllegalArgumentException("maxDepth cannot be negative");
}
if (timeoutSeconds <= 0) {
throw new IllegalArgumentException("timeoutSeconds must be positive");
}
if (popularWordCount < 0) {
throw new IllegalArgumentException("popularWordCount cannot be negative");
}
return new CrawlerConfiguration(
startPages.stream().collect(Collectors.toUnmodifiableList()),
ignoredUrls.stream().map(Pattern::compile).collect(Collectors.toUnmodifiableList()),
ignoredWords.stream().map(Pattern::compile).collect(Collectors.toUnmodifiableList()),
parallelism,
implementationOverride,
maxDepth,
Duration.ofSeconds(timeoutSeconds),
popularWordCount,
profileOutputPath,
resultPath);
}
}
}