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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations