Search in sources :

Example 1 with GraphProperty

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

the class VertexTypeMapper method parseFromVertexType.

public static VertexTypeMapper parseFromVertexType(GraphVertex graphVertex) {
    VertexTypeMapper vertexTypeMapper = new VertexTypeMapper();
    vertexTypeMapper.setId(graphVertex.getLabelId());
    vertexTypeMapper.setLabel(graphVertex.getLabel());
    vertexTypeMapper.setType(TypeEnum.VERTEX.toString());
    PrimaryKeyConstraint primaryKeyConstraint = graphVertex.getPrimaryKeyConstraint();
    VertexIndexMapper vertexIndexMapper = new VertexIndexMapper();
    vertexIndexMapper.setName("primary_key");
    vertexIndexMapper.setIndexType("PRIMARY_KEY");
    vertexIndexMapper.setPropertyNames(primaryKeyConstraint.getPrimaryKeyList());
    vertexTypeMapper.setIndexes(Lists.newArrayList(vertexIndexMapper));
    List<GraphPropertyMapper> propertyMapperList = Lists.newArrayList();
    for (GraphProperty graphProperty : graphVertex.getPropertyList()) {
        propertyMapperList.add(GraphPropertyMapper.parseFromGrapyProperty(graphProperty));
    }
    vertexTypeMapper.setPropertyDefList(propertyMapperList);
    vertexTypeMapper.setVersionId(graphVertex.getVersionId());
    vertexTypeMapper.setTableId(graphVertex.getTableId());
    return vertexTypeMapper;
}
Also used : GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) PrimaryKeyConstraint(com.alibaba.maxgraph.compiler.api.schema.PrimaryKeyConstraint)

Example 2 with GraphProperty

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

the class DefaultGraphSchema method getPropertyList.

@Override
public Map<GraphElement, GraphProperty> getPropertyList(String propName) {
    Map<GraphElement, GraphProperty> elementPropertyList = Maps.newHashMap();
    vertexList.forEach((key, value) -> {
        for (GraphProperty property : value.getPropertyList()) {
            if (StringUtils.equals(property.getName(), propName)) {
                elementPropertyList.put(value, property);
            }
        }
    });
    edgeList.forEach((key, value) -> {
        for (GraphProperty property : value.getPropertyList()) {
            if (StringUtils.equals(property.getName(), propName)) {
                elementPropertyList.put(value, property);
            }
        }
    });
    return elementPropertyList;
}
Also used : GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement)

Example 3 with GraphProperty

use of com.alibaba.maxgraph.compiler.api.schema.GraphProperty 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 4 with GraphProperty

use of com.alibaba.maxgraph.compiler.api.schema.GraphProperty 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 5 with GraphProperty

use of com.alibaba.maxgraph.compiler.api.schema.GraphProperty 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)

Aggregations

GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)11 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)3 GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)3 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)2 PrimaryKeyConstraint (com.alibaba.maxgraph.compiler.api.schema.PrimaryKeyConstraint)2 PropertyValue (com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)2 HashMap (java.util.HashMap)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 PropertyDefNotFoundException (com.alibaba.maxgraph.compiler.api.exception.PropertyDefNotFoundException)1 DataType (com.alibaba.maxgraph.compiler.api.schema.DataType)1 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1