use of com.alibaba.maxgraph.compiler.tree.NodeType in project GraphScope by alibaba.
the class CostDataStatistics method computeStepCount.
/**
* Compute step count with given node and node label
*
* @param inputStatistics The given input statistics
* @param treeNode The given tree node
* @param currNodeLabel The given current node label
* @return The result step count
*/
private NodeStatistics computeStepCount(NodeStatistics inputStatistics, TreeNode treeNode, NodeLabelList currNodeLabel) {
GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
NodeStatistics nodeStatistics = new NodeStatistics(schema);
if (null == inputStatistics) {
if (currNodeLabel.isUnknownFlag()) {
for (Map.Entry<String, Double> entry : vertexCountList.entrySet()) {
nodeStatistics.addVertexCount(entry.getKey(), entry.getValue());
}
} else {
SourceTreeNode sourceTreeNode = (SourceTreeNode) treeNode;
Object[] ids = sourceTreeNode.getIds();
Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
for (String vertexLabel : vertexLabelList) {
double vertexCount = null == ids ? vertexCountList.getOrDefault(vertexLabel, INIT_VERTEX_COUNT) : (ids.length * 1.0 / vertexLabelList.size());
nodeStatistics.addVertexCount(vertexLabel, vertexCount);
}
Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
for (String edgeLabel : currNodeLabel.getEdgeLabelList()) {
double edgeCount = null == ids ? edgeCountList.getOrDefault(edgeLabel, INIT_EDGE_COUNT) : (ids.length * 1.0 / edgeLabelList.size());
nodeStatistics.addEdgeCount(edgeLabel, edgeCount);
}
}
} else {
if (treeNode instanceof VertexTreeNode) {
VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
Direction direction = vertexTreeNode.getDirection();
Set<String> edgeLabelList;
if (null == vertexTreeNode.getEdgeLabels()) {
edgeLabelList = Sets.newHashSet();
} else {
edgeLabelList = Sets.newHashSet(vertexTreeNode.getEdgeLabels());
}
switch(direction) {
case OUT:
{
nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
break;
}
case IN:
{
nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
break;
}
case BOTH:
{
nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
break;
}
}
} else if (treeNode instanceof EdgeTreeNode) {
Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
Direction direction = ((EdgeTreeNode) treeNode).getDirection();
switch(direction) {
case OUT:
{
nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
break;
}
case IN:
{
nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
break;
}
case BOTH:
{
nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
break;
}
}
} else if (treeNode instanceof EdgeVertexTreeNode) {
Map<String, Double> edgeCountList = inputStatistics.getEdgeCountList();
Direction direction = ((EdgeVertexTreeNode) treeNode).getDirection();
edgeCountList.keySet().stream().map(v -> {
try {
return schema.getElement(v);
} catch (Exception ignored) {
return null;
}
}).filter(v -> null != v && v instanceof GraphEdge).map(v -> (GraphEdge) v).forEach(v -> {
v.getRelationList().forEach(vv -> {
double currEdgeCount = edgeCountList.get(v.getLabel());
if (direction == Direction.OUT || direction == Direction.BOTH) {
List<String> resultVertexLabelList = Lists.newArrayList();
if (currNodeLabel.getVertexLabelList().contains(vv.getSource().getLabel())) {
resultVertexLabelList.add(vv.getSource().getLabel());
}
double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
for (String vertexLabel : resultVertexLabelList) {
nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
}
}
if (direction == Direction.IN || direction == Direction.BOTH) {
List<String> resultVertexLabelList = Lists.newArrayList();
if (currNodeLabel.getVertexLabelList().contains(vv.getTarget().getLabel())) {
resultVertexLabelList.add(vv.getTarget().getLabel());
}
double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
for (String vertexLabel : resultVertexLabelList) {
nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
}
}
});
});
} else if (treeNode instanceof EdgeOtherVertexTreeNode) {
Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
double avgVertexCount = inputStatistics.totalCount() / vertexLabelList.size();
vertexLabelList.forEach(v -> nodeStatistics.addVertexCount(v, avgVertexCount));
} else {
NodeType nodeType = treeNode.getNodeType();
if (NodeType.MAP == nodeType) {
nodeStatistics.addElementCount(inputStatistics.totalCount());
} else if (NodeType.FILTER == nodeType) {
nodeStatistics.merge(inputStatistics, FILTER_RATIO);
} else if (NodeType.FLATMAP == nodeType) {
nodeStatistics.addElementCount(inputStatistics.totalCount() * FLATMAP_RATIO);
} else if (NodeType.AGGREGATE == nodeType) {
nodeStatistics.addElementCount(1);
} else {
nodeStatistics.merge(inputStatistics);
}
}
}
return nodeStatistics;
}
Aggregations