/
DescendantTreeAggregator.java
89 lines (79 loc) · 3.03 KB
/
DescendantTreeAggregator.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
package com.insightfullogic.honest_profiler.core.aggregation.aggregator;
import static java.util.stream.Collector.of;
import static java.util.stream.Collectors.groupingBy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collector;
import com.insightfullogic.honest_profiler.core.aggregation.AggregationProfile;
import com.insightfullogic.honest_profiler.core.aggregation.FqmnLink;
import com.insightfullogic.honest_profiler.core.aggregation.result.straight.Entry;
import com.insightfullogic.honest_profiler.core.aggregation.result.straight.Node;
import com.insightfullogic.honest_profiler.core.aggregation.result.straight.Tree;
import com.insightfullogic.honest_profiler.core.profiles.lean.LeanNode;
/**
* Aggregator which takes an {@link Entry} and aggregates the descendants into a {@link Tree}.
*/
public class DescendantTreeAggregator implements SubAggregator<Entry, Node>
{
// Aggregator Implementation
/**
* @see SubAggregator#aggregate(Object, LeanNode)
*/
@Override
public Tree aggregate(AggregationProfile source, Entry input)
{
Node root = new Node(input);
List<Node> list = new ArrayList<>();
list.add(root);
Tree result = new Tree(source, list);
Set<String> processed = new HashSet<>();
addDescendants(source, root, result, processed);
return result;
}
private void addDescendants(AggregationProfile source, Node parent, Tree tree,
Set<String> processed)
{
FqmnLink fqmnLink = source.getFqmnLinks().get(parent.getKey());
Map<String, Node> callers = fqmnLink.getChildren().values().stream()
.flatMap(set -> set.stream()).filter(
node -> node != null
&& !node.isThreadNode()
&& !processed.contains(source.getSource().getFqmn(node)))
.collect(
groupingBy(
node -> source.getSource().getFqmn(node),
getCollector(source, tree, processed)));
parent.addAll(callers);
}
private Collector<LeanNode, Node, Node> getCollector(AggregationProfile source, Tree tree,
Set<String> processed)
{
return of(
// Supplier
() ->
{
Node node = new Node(tree);
node.setReference(source.getGlobalData());
return node;
},
// Accumulator
(accumulator, node) ->
{
if (node.isThreadNode())
{
return;
}
processed.add(source.getSource().getFqmn(node));
accumulator.add(source.getSource().getFqmn(node), node);
if (accumulator.getChildren().isEmpty())
{
addDescendants(source, accumulator, tree, processed); // Recursion here !
}
},
// Combiner
(e1, e2) -> e1.combine(e2));
}
}