/
BoggleDriver.java
293 lines (226 loc) · 9.68 KB
/
BoggleDriver.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
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.compress.SnappyCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class BoggleDriver extends Configured implements Tool {
private static final Logger logger = Logger.getLogger("Boggle");
/** The parameter name for the minimum word size to output */
public static final String MINIMUM_WORD_SIZE_PARAM = "minimumwordsize";
/** The default value for the minimum word size to output */
public static final int MINIMUM_WORD_SIZE_DEFAULT = 3;
/** The parameter name for the bloom filter location */
public static final String BLOOM_PARAM = "bloompath";
/** The parameter name for the dictionary location */
public static final String DICTIONARY_PARAM = "dictionarypath";
/** The parameter name for the roll to be serialized */
public static final String ROLL_PARAM = "roll";
/** The parameter name for the roll version */
public static final String ROLL_VERSION_PARAM = "rollversion";
/** The parameter name for the roll version */
public static final int ROLL_VERSION_DEFAULT = BoggleRoll.NEW_VERSION;
/** The parameter name for the minimum word size to output */
public static final String MAX_ITERATIONS_PARAM = "maxiterations";
/** The default value for the minimum word size to output */
public static final int MAX_ITERATIONS_DEFAULT = 15;
/** The parameter name to use a BloomFilter */
public static final String ENABLE_BLOOM_PARAM = "bloom";
/** The default value for using the BloomFilter */
public static final boolean ENABLE_BLOOM_DEFAULT = true;
@Override
public int run(String[] args) throws Exception {
if (args.length != 4) {
System.out.println("Usage: BoggleDriver <bloomfile> <dictionary> <input dir> <output dir>");
return -1;
}
String bloomPath = args[0];
String dictionary = args[1];
String input = args[2];
String output = args[3];
// Change Log4J to only output the map and reducer data
// This helps view what's happening with the data easier
// Comment this out to get all log data
Logger.getLogger("Boggle").setLevel(Level.INFO);
Logger.getRootLogger().setLevel(Level.ERROR);
Configuration configuration = getConf();
FileSystem fileSystem = FileSystem.get(configuration);
if (!fileSystem.exists(new Path(bloomPath))) {
// Verify that Bloom file exists
System.out.println("Could not find bloom file");
return -1;
}
if (fileSystem.exists(new Path(output))) {
// Verify that output does not exist
System.out.println("Output file already exists");
return -1;
}
configuration.set(BLOOM_PARAM, bloomPath);
configuration.set(DICTIONARY_PARAM, dictionary);
configuration.setBooleanIfUnset(ENABLE_BLOOM_PARAM, ENABLE_BLOOM_DEFAULT);
BoggleRoll roll = BoggleRoll.createRoll(configuration.getInt(ROLL_VERSION_PARAM, ROLL_VERSION_DEFAULT));
configuration.set(ROLL_PARAM, roll.serialize());
int iteration = traverseGraph(input, configuration, fileSystem, roll);
boolean success = findWords(input, output, configuration, iteration, roll);
return success ? 0 : 1;
}
/**
* Traverses the graph until all possible words are found
*
* @param input
* The input directory
* @param configuration
* The configuration object
* @param fileSystem
* The filesystem object
* @param roll
* The Boggle roll to process
* @return The number of iterations it took to traverse the graph
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
private int traverseGraph(String input, Configuration configuration, FileSystem fileSystem, BoggleRoll roll)
throws IOException, InterruptedException, ClassNotFoundException {
int iteration = 0;
int maxiterations = configuration.getInt(MAX_ITERATIONS_PARAM, MAX_ITERATIONS_DEFAULT);
writeRollFile(input, fileSystem, configuration, roll, iteration);
long previousWordCount = 0;
long bloomSavings = 0;
logger.info("Starting graph traversal.");
// Traverse the graph until it is completely traversed
do {
Job job = new Job(configuration);
job.setJarByClass(BoggleDriver.class);
job.setJobName("Boggle Graph Iteration " + iteration);
FileInputFormat.setInputPaths(job, getPath(input, iteration));
FileOutputFormat.setOutputPath(job, getPath(input, iteration + 1));
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
FileOutputFormat.setOutputCompressorClass(job, SnappyCodec.class);
SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);
job.setNumReduceTasks(0);
job.setMapperClass(BoggleMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(RollGraphWritable.class);
boolean success = job.waitForCompletion(true);
if (!success) {
throw new RuntimeException("Job did not return sucessfully. Check the logs for info.");
}
long currentWordCount = job.getCounters().findCounter("boggle", "words").getValue();
bloomSavings += job.getCounters().findCounter("boggle", "bloom").getValue();
logger.info("Traversed graph for " + iteration + " iterations. Found " + currentWordCount
+ " potential words. Bloom prevented " + bloomSavings + " traversals so far.");
// Check to see if the entire graph has been traversed, the entire roll has been iterated,
// or the maximum number of iterations have happened.
if (currentWordCount == previousWordCount
|| iteration == (roll.rollSize * roll.rollSize) || iteration == maxiterations) {
logger.info("Finished traversing graph after " + iteration + " iterations. Found " + currentWordCount
+ " potential words. Bloom prevented " + bloomSavings + " traversals.");
break;
}
previousWordCount = currentWordCount;
iteration++;
} while (true);
return iteration;
}
/**
* Takes the traversed graph and finds the actual words in the Boggle Roll
*
* @param input
* The input directory
* @param output
* The output directory
* @param configuration
* The configuration object
* @param iteration
* The number of iterations it took to traverse the graph
* @param roll
* The Boggle roll
* @return If the job was successful
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
private boolean findWords(String input, String output, Configuration configuration, int iteration, BoggleRoll roll)
throws IOException, InterruptedException, ClassNotFoundException {
Job job = new Job(configuration);
job.setJarByClass(BoggleDriver.class);
job.setJobName("Boggle Graph Final");
FileInputFormat.setInputPaths(job, getPath(input, iteration));
FileOutputFormat.setOutputPath(job, new Path(output));
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setNumReduceTasks(1);
job.setMapperClass(BoggleWordMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(RollGraphWritable.class);
boolean success = job.waitForCompletion(true);
long finalWordCount = job.getCounters().findCounter("boggle", "finalwords").getValue();
logger.info("Finished traversing graph after " + iteration + " iterations. Found " + finalWordCount
+ " total words. With roll:\n" + roll.toString());
return success;
}
/**
* Writes out the Boggle roll to a file as an adjacency matrix
*
* @param input
* The place to write the roll to
* @param fileSystem
* The filesystem object
* @param roll
* The Boggle roll to write out
* @param iteration
* The iteration for the input
* @throws IOException
*/
private void writeRollFile(String input, FileSystem fileSystem, Configuration configuration, BoggleRoll roll,
int iteration) throws IOException {
Path parent = getPath(input, iteration);
fileSystem.mkdirs(parent);
SequenceFile.Writer writer = null;
for (int i = 0; i < roll.rollCharacters.length; i++) {
writer = SequenceFile.createWriter(fileSystem, configuration, new Path(parent, i + ".txt"),
Text.class, RollGraphWritable.class);
for (int j = 0; j < roll.rollCharacters[i].length; j++) {
ArrayList<Node> nodes = new ArrayList<Node>();
nodes.add(new Node(i, j));
RollGraphWritable graphWritable = new RollGraphWritable(nodes, false);
Text text = new Text(roll.rollCharacters[i][j]);
// Mimic the adjacency matrix written by the mapper to start things off
writer.append(text, graphWritable);
}
IOUtils.closeStream(writer);
}
IOUtils.closeStream(writer);
}
/**
* Gets the path based on the iteration
*
* @param input
* The base input directory
* @param iteration
* The iteration number
* @return The path for the iteration
*/
private Path getPath(String input, int iteration) {
return new Path(input + "-" + iteration);
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new Configuration(), new BoggleDriver(), args);
System.exit(exitCode);
}
}