package Dijkstra;
//TODO: Provide a filehandle for writing out the print statements to a file. Its very clumsy
// when the output prints to the console and its hard to read too.
import cgl.imr.base.Key;
import cgl.imr.base.MapOutputCollector;
import cgl.imr.base.MapTask;
import cgl.imr.base.SerializationException;
import cgl.imr.base.TwisterException;
import cgl.imr.base.Value;
import cgl.imr.base.impl.JobConf;
import cgl.imr.base.impl.MapperConf;
import cgl.imr.types.BytesValue;
import cgl.imr.types.StringKey;
public class AllPairsShortestPathMap implements MapTask {
public void map(MapOutputCollector collector, Key key, Value val)
throws TwisterException, NullPointerException {
try {
Graph subgraph = new Graph();
subgraph.fromBytes(val.getBytes());
for (Node thisNode : subgraph.getNodes().values()) {
int sumOfEdgeWeights = 0;
for (int id : thisNode.getEdges().keySet()) {
Node adjacentNode = new Node(id);
adjacentNode.setEdgeWeight(thisNode.getID(), thisNode.getEdgeWeight(id));
for (int v1 : thisNode.getEdges().keySet()) {
if (v1 != id) {
sumOfEdgeWeights = thisNode.getEdgeWeight(id)
+ thisNode.getEdgeWeight(v1);
adjacentNode.setEdgeWeight(v1, sumOfEdgeWeights);
}
}
collector.collect(new StringKey("" + adjacentNode.getID()),
new BytesValue(adjacentNode.getBytes()));
setNodeToNullForGarbageCollection(adjacentNode);
}
collector.collect(new StringKey("" + thisNode.getID()),
new BytesValue(thisNode.getBytes()));
}
} catch (SerializationException e) {
throw new TwisterException(e);
}
}
/**
* point this node to NULL, for easy
* garbage collecton. Else these nodes
* become dangling and consume up the heap space.
* @param adjacentNode
*/
private void setNodeToNullForGarbageCollection(Node adjacentNode) {
adjacentNode = null;
}
@Override
public void close() throws TwisterException {
}
@Override
public void configure(JobConf arg0, MapperConf arg1)
throws TwisterException {
}
}