Search in sources :

Example 1 with NodeType

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;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SchemaFetcher(com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) Sets(com.google.common.collect.Sets) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Lists(com.google.common.collect.Lists) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) Set(java.util.Set) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Direction(org.apache.tinkerpop.gremlin.structure.Direction) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)1 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)1 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)1 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)1 SchemaFetcher (com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher)1 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)1 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)1 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)1 NodeType (com.alibaba.maxgraph.compiler.tree.NodeType)1 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)1 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)1 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1