Search in sources :

Example 6 with GraphVertex

use of com.alibaba.maxgraph.compiler.api.schema.GraphVertex in project GraphScope by alibaba.

the class GraphSchemaMapper method parseFromSchema.

public static GraphSchemaMapper parseFromSchema(GraphSchema schema) {
    GraphSchemaMapper schemaMapper = new GraphSchemaMapper();
    schemaMapper.version = schema.getVersion();
    schemaMapper.types = Lists.newArrayList();
    for (GraphVertex graphVertex : schema.getVertexList()) {
        schemaMapper.types.add(VertexTypeMapper.parseFromVertexType(graphVertex));
    }
    for (GraphEdge graphEdge : schema.getEdgeList()) {
        schemaMapper.types.add(EdgeTypeMapper.parseFromEdgeType(graphEdge));
    }
    return schemaMapper;
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Example 7 with GraphVertex

use of com.alibaba.maxgraph.compiler.api.schema.GraphVertex in project GraphScope by alibaba.

the class SourceVertexTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    LogicalSubQueryPlan logicalQueryPlan = new LogicalSubQueryPlan(contextManager);
    logicalQueryPlan.setDelegateSourceFlag(false);
    Message.Value.Builder argumentBuilder = Message.Value.newBuilder();
    processLabelArgument(argumentBuilder, true);
    processIdArgument(argumentBuilder, true);
    Map<String, List<Integer>> primaryKeyLabelIdList = Maps.newHashMap();
    for (GraphVertex vertexType : this.schema.getVertexList()) {
        List<GraphProperty> primaryKeyList = vertexType.getPrimaryKeyList();
        if (null != primaryKeyList && primaryKeyList.size() == 1) {
            String propertyName = primaryKeyList.get(0).getName();
            List<Integer> labelIdList = primaryKeyLabelIdList.computeIfAbsent(propertyName, k -> Lists.newArrayList());
            labelIdList.add(vertexType.getLabelId());
        }
    }
    QueryFlowOuterClass.VertexPrimaryKeyListProto.Builder primaryKeyBuilder = QueryFlowOuterClass.VertexPrimaryKeyListProto.newBuilder();
    for (HasContainer container : this.hasContainerList) {
        String key = container.getKey();
        List<Integer> labelIdList = primaryKeyLabelIdList.get(key);
        if (null != labelIdList) {
            for (Integer labelId : labelIdList) {
                if (container.getBiPredicate() instanceof Compare && container.getBiPredicate() == Compare.eq) {
                    primaryKeyBuilder.addPrimaryKeys(QueryFlowOuterClass.VertexPrimaryKeyProto.newBuilder().setLabelId(labelId).setPrimaryKeyValue(container.getValue().toString()));
                } else if (container.getBiPredicate() instanceof Contains && container.getBiPredicate() == Contains.within) {
                    for (Object val : (Collection<Object>) container.getValue()) {
                        primaryKeyBuilder.addPrimaryKeys(QueryFlowOuterClass.VertexPrimaryKeyProto.newBuilder().setLabelId(labelId).setPrimaryKeyValue(val.toString()));
                    }
                }
            }
        }
    }
    argumentBuilder.setPayload(primaryKeyBuilder.build().toByteString()).setBoolFlag(isPartitionIdFlag());
    ProcessorSourceFunction processorSourceFunction = new ProcessorSourceFunction(getCountOperator(QueryFlowOuterClass.OperatorType.V), argumentBuilder, rangeLimit);
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    TreeNodeLabelManager treeNodeLabelManager = contextManager.getTreeNodeLabelManager();
    LogicalSourceVertex logicalSourceVertex = new LogicalSourceVertex(vertexIdManager.getId(), processorSourceFunction);
    logicalSourceVertex.getBeforeRequirementList().addAll(buildBeforeRequirementList(treeNodeLabelManager));
    logicalSourceVertex.getAfterRequirementList().addAll(buildAfterRequirementList(treeNodeLabelManager));
    getUsedLabelList().forEach(v -> processorSourceFunction.getUsedLabelList().add(treeNodeLabelManager.getLabelIndex(v)));
    logicalQueryPlan.addLogicalVertex(logicalSourceVertex);
    setFinishVertex(logicalQueryPlan.getOutputVertex(), treeNodeLabelManager);
    return logicalQueryPlan;
}
Also used : LogicalSourceVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceVertex) TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ProcessorSourceFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) List(java.util.List) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 8 with GraphVertex

use of com.alibaba.maxgraph.compiler.api.schema.GraphVertex in project GraphScope by alibaba.

the class DefaultGraphSchema method buildSchemaFromJson.

public static GraphSchema buildSchemaFromJson(String schemaJson) {
    JSONObject jsonObject = JSONObject.parseObject(schemaJson);
    Map<String, GraphVertex> vertexList = Maps.newHashMap();
    Map<String, GraphEdge> edgeList = Maps.newHashMap();
    Map<String, Integer> propNameToIdList = Maps.newHashMap();
    JSONArray typeList = jsonObject.getJSONArray("types");
    if (null != typeList) {
        int propId = 1;
        for (int i = 0; i < typeList.size(); i++) {
            JSONObject typeObject = typeList.getJSONObject(i);
            int labelId = typeObject.getInteger("id");
            String label = typeObject.getString("label");
            String type = typeObject.getString("type");
            Map<String, GraphProperty> namePropertyList = Maps.newHashMap();
            List<GraphProperty> propertyList = Lists.newArrayList();
            JSONArray propArray = typeObject.getJSONArray("propertyDefList");
            if (null != propArray) {
                for (int j = 0; j < propArray.size(); j++) {
                    JSONObject propObject = propArray.getJSONObject(j);
                    String propName = propObject.getString("name");
                    Integer currPropId = propObject.getInteger("id");
                    if (null == currPropId) {
                        currPropId = propId++;
                    }
                    String propDataTypeString = propObject.getString("data_type");
                    com.alibaba.maxgraph.sdkcommon.meta.DataType dataType;
                    if (StringUtils.startsWith(propDataTypeString, "LIST")) {
                        dataType = new com.alibaba.maxgraph.sdkcommon.meta.DataType(InternalDataType.LIST);
                        try {
                            dataType.setExpression(StringUtils.removeEnd(StringUtils.removeStart(propDataTypeString, "LIST<"), ">"));
                        } catch (MaxGraphException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        dataType = com.alibaba.maxgraph.sdkcommon.meta.DataType.valueOf(propDataTypeString);
                    }
                    GraphProperty property = new DefaultGraphProperty(currPropId, propName, DataType.parseFromDataType(dataType));
                    propertyList.add(property);
                    namePropertyList.put(propName, property);
                    propNameToIdList.put(propName, currPropId);
                }
            } else {
                logger.warn("There's no property def list in " + label);
            }
            if (StringUtils.equals(type, "VERTEX")) {
                List<GraphProperty> primaryPropertyList = Lists.newArrayList();
                JSONArray indexArray = typeObject.getJSONArray("indexes");
                if (indexArray != null) {
                    for (int k = 0; k < indexArray.size(); k++) {
                        JSONObject indexObject = indexArray.getJSONObject(k);
                        JSONArray priNameList = indexObject.getJSONArray("propertyNames");
                        for (int j = 0; j < priNameList.size(); j++) {
                            primaryPropertyList.add(namePropertyList.get(priNameList.getString(j)));
                        }
                    }
                }
                DefaultGraphVertex graphVertex = new DefaultGraphVertex(labelId, label, propertyList, primaryPropertyList);
                vertexList.put(label, graphVertex);
            } else {
                List<EdgeRelation> relationList = Lists.newArrayList();
                JSONArray relationArray = typeObject.getJSONArray("rawRelationShips");
                if (null != relationArray) {
                    for (int k = 0; k < relationArray.size(); k++) {
                        JSONObject relationObject = relationArray.getJSONObject(k);
                        String sourceLabel = relationObject.getString("srcVertexLabel");
                        String targetLabel = relationObject.getString("dstVertexLabel");
                        relationList.add(new DefaultEdgeRelation(vertexList.get(sourceLabel), vertexList.get(targetLabel)));
                    }
                } else {
                    logger.warn("There's no relation def in edge " + label);
                }
                DefaultGraphEdge graphEdge = new DefaultGraphEdge(labelId, label, propertyList, relationList);
                edgeList.put(label, graphEdge);
            }
        }
    } else {
        logger.error("Cant get types field in json[" + schemaJson + "]");
    }
    return new DefaultGraphSchema(vertexList, edgeList, propNameToIdList);
}
Also used : MaxGraphException(com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) JSONArray(com.alibaba.fastjson.JSONArray) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) JSONObject(com.alibaba.fastjson.JSONObject) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Example 9 with GraphVertex

use of com.alibaba.maxgraph.compiler.api.schema.GraphVertex in project GraphScope by alibaba.

the class TinkerMaxGraph method parseVertexProperties.

private Pair<String, Map<String, Object>> parseVertexProperties(Object... keyValues) {
    Map<String, Object> kvs = Utils.convertToMap(keyValues);
    Object labelObj = kvs.remove(T.label.toString());
    if (labelObj == null) {
        labelObj = Vertex.DEFAULT_LABEL;
    }
    final String label = labelObj.toString();
    GraphSchema schema = graph.getSchema();
    GraphElement graphElement = schema.getElement(label);
    if (!(graphElement instanceof GraphVertex)) {
        throw new IllegalArgumentException("Label " + label + " is not vertex");
    }
    List<GraphProperty> primaryKeyList = ((GraphVertex) graphElement).getPrimaryKeyList();
    if (kvs.isEmpty()) {
        for (GraphProperty property : primaryKeyList) {
            kvs.put(property.getName(), property.getDataType().getRandomValue());
        }
    } else {
        for (GraphProperty property : primaryKeyList) {
            if (!kvs.containsKey(property.getName())) {
                kvs.put(property.getName(), property.getDataType().getRandomValue());
            }
        }
    }
    return Pair.of(label, kvs);
}
Also used : GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 10 with GraphVertex

use of com.alibaba.maxgraph.compiler.api.schema.GraphVertex in project GraphScope by alibaba.

the class GraphSchemaMapper method toGraphSchema.

public GraphSchema toGraphSchema() {
    DefaultGraphSchema graphSchema = new DefaultGraphSchema();
    Map<String, GraphVertex> vertexTypeMap = Maps.newHashMap();
    for (SchemaElementMapper elementMapper : this.types) {
        if (elementMapper instanceof VertexTypeMapper) {
            GraphVertex graphVertex = ((VertexTypeMapper) elementMapper).toVertexType();
            graphSchema.createVertexType(graphVertex);
            vertexTypeMap.put(graphVertex.getLabel(), graphVertex);
        }
    }
    for (SchemaElementMapper elementMapper : this.types) {
        if (elementMapper instanceof EdgeTypeMapper) {
            GraphEdge graphEdge = ((EdgeTypeMapper) elementMapper).toEdgeType(vertexTypeMap);
            graphSchema.createEdgeType(graphEdge);
        }
    }
    return graphSchema;
}
Also used : GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) DefaultGraphSchema(com.alibaba.maxgraph.sdkcommon.schema.mapper.DefaultGraphSchema) VertexTypeMapper(com.alibaba.maxgraph.sdkcommon.schema.mapper.VertexTypeMapper) EdgeTypeMapper(com.alibaba.maxgraph.sdkcommon.schema.mapper.EdgeTypeMapper) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) SchemaElementMapper(com.alibaba.maxgraph.sdkcommon.schema.mapper.SchemaElementMapper)

Aggregations

GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)10 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)5 GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)3 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 DefaultGraphSchema (com.alibaba.graphscope.groot.schema.mapper.DefaultGraphSchema)1 EdgeTypeMapper (com.alibaba.graphscope.groot.schema.mapper.EdgeTypeMapper)1 SchemaElementMapper (com.alibaba.graphscope.groot.schema.mapper.SchemaElementMapper)1 VertexTypeMapper (com.alibaba.graphscope.groot.schema.mapper.VertexTypeMapper)1 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)1 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)1 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)1 LogicalSourceVertex (com.alibaba.maxgraph.compiler.logical.LogicalSourceVertex)1 LogicalSubQueryPlan (com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)1 VertexIdManager (com.alibaba.maxgraph.compiler.logical.VertexIdManager)1 ProcessorSourceFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction)1 TreeNodeLabelManager (com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager)1 MaxGraphException (com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException)1 DefaultGraphSchema (com.alibaba.maxgraph.sdkcommon.schema.mapper.DefaultGraphSchema)1 EdgeTypeMapper (com.alibaba.maxgraph.sdkcommon.schema.mapper.EdgeTypeMapper)1