use of com.facebook.buck.graph.AcyclicDepthFirstPostOrderTraversal.CycleException in project buck by facebook.
the class TargetGraphHashing method hashTargetGraph.
/**
* Given a {@link TargetGraph} and any number of root nodes to traverse,
* returns a map of {@code (BuildTarget, HashCode)} pairs for all root
* build targets and their dependencies.
*/
public ImmutableMap<BuildTarget, HashCode> hashTargetGraph() throws CycleException {
try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("ShowTargetHashes"))) {
AcyclicDepthFirstPostOrderTraversal<TargetNode<?, ?>> traversal = new AcyclicDepthFirstPostOrderTraversal<>(node -> targetGraph.getAll(node.getDeps()).iterator());
final Map<BuildTarget, ForkJoinTask<HashCode>> buildTargetHashes = new HashMap<>();
Queue<ForkJoinTask<HashCode>> tasksToSchedule = new ArrayDeque<>();
// Start all the node tasks, bottom up
for (final TargetNode<?, ?> node : traversal.traverse(roots)) {
HashNodeTask task = new HashNodeTask(node, buildTargetHashes);
buildTargetHashes.put(node.getBuildTarget(), task);
tasksToSchedule.add(task);
}
// Execute tasks in parallel
ForkJoinPool pool = new ForkJoinPool(numThreads);
for (ForkJoinTask<HashCode> task : tasksToSchedule) {
pool.execute(task);
}
// Wait for all scheduled tasks to complete
return ImmutableMap.copyOf(Maps.transformEntries(buildTargetHashes, (key, value) -> value.join()));
}
}
Aggregations