Sandbox

Compare Revisions

Ignore whitespace Rev 4 → Rev 5

/Drafts/src/org/mapreduce/MapReduce.java
136,10 → 136,10
final List<MappedItem> mappedItems = new LinkedList<MappedItem>();
final MapReduceCallback<String, String> mapCallback = new MapReduceCallback<String, String>() {
final MapCallback<String, MappedItem> mapCallback = new MapCallback<String, MappedItem>() {
@Override
public synchronized void emit(String k, String v) {
mappedItems.add(new MappedItem(k, v));
public synchronized void mapDone(String file, List<MappedItem> results) {
mappedItems.addAll(results);
}
};
189,9 → 189,9
// REDUCE:
final MapReduceCallback<String, Map<String, Integer>> reduceCallback = new MapReduceCallback<String, Map<String, Integer>>() {
final ReduceCallback<String, String, Integer> reduceCallback = new ReduceCallback<String, String, Integer>() {
@Override
public synchronized void emit(String k, Map<String, Integer> v) {
public synchronized void reduceDone(String k, Map<String, Integer> v) {
output.put(k, v);
}
};
247,19 → 247,27
output.put(word, reducedList);
}
private static interface MapReduceCallback<K, V> {
public static interface MapCallback<E, V> {
public void emit(K k, V v);
public void mapDone(E key, List<V> values);
}
public static void map(String file, String contents, MapReduceCallback<String, String> callback) {
public static void map(String file, String contents, MapCallback<String, MappedItem> callback) {
String[] words = contents.trim().split("\\s+");
List<MappedItem> results = new ArrayList<MappedItem>(words.length);
for(String word: words) {
callback.emit(word, file);
results.add(new MappedItem(word, file));
}
callback.mapDone(file, results);
}
public static void reduce(String word, List<String> list, MapReduceCallback<String, Map<String, Integer>> callback) {
public static interface ReduceCallback<E, K, V> {
public void reduceDone(E e, Map<K,V> results);
}
public static void reduce(String word, List<String> list, ReduceCallback<String, String, Integer> callback) {
Map<String, Integer> reducedList = new HashMap<String, Integer>();
for(String file: list) {
Integer occurrences = reducedList.get(file);
269,7 → 277,7
reducedList.put(file, occurrences.intValue() + 1);
}
}
callback.emit(word, reducedList);
callback.reduceDone(word, reducedList);
}
private static class MappedItem {
289,5 → 297,10
public String getFile() {
return file;
}
@Override
public String toString() {
return "[\"" + word + "\",\"" + file + "\"]";
}
}
}