Search in sources :

Example 1 with GraphDef

use of com.alibaba.maxgraph.sdkcommon.schema.GraphDef in project GraphScope by alibaba.

the class AbstractCreateTypeExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    TypeDefPb typeDefPb = TypeDefPb.parseFrom(ddlBlob);
    TypeDef typeDef = TypeDef.parseProto(typeDefPb);
    long version = graphDef.getSchemaVersion();
    String label = typeDef.getLabel();
    if (!label.matches(NAME_REGEX)) {
        throw new DdlException("illegal label name [" + label + "]");
    }
    if (graphDef.hasLabel(label)) {
        throw new DdlException("label [" + label + "] already exists, schema version [" + version + "]");
    }
    if (typeDef.getTypeEnum() == TypeEnum.VERTEX) {
        if (this instanceof CreateEdgeTypeExecutor) {
            throw new DdlException("Expect edge type but got vertex type");
        }
        if (typeDef.getPkIdxs().size() == 0) {
            throw new DdlException("Vertex type must define primary key. label [" + label + "]");
        }
    } else if (this instanceof CreateVertexTypeExecutor) {
        throw new DdlException("Expect vertex type but got edge type");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    TypeDef.Builder typeDefBuilder = TypeDef.newBuilder(typeDef);
    int propertyIdx = graphDef.getPropertyIdx();
    Map<String, Integer> propertyNameToId = graphDef.getPropertyNameToId();
    List<PropertyDef> inputPropertiesInfo = typeDef.getProperties();
    List<PropertyDef> propertyDefs = new ArrayList<>(inputPropertiesInfo.size());
    for (PropertyDef property : inputPropertiesInfo) {
        String propertyName = property.getName();
        if (!propertyName.matches(NAME_REGEX)) {
            throw new DdlException("illegal property name [" + propertyName + "]");
        }
        Integer propertyId = propertyNameToId.get(propertyName);
        if (propertyId == null) {
            propertyIdx++;
            propertyId = propertyIdx;
            graphDefBuilder.putPropertyNameToId(propertyName, propertyId);
            graphDefBuilder.setPropertyIdx(propertyIdx);
        }
        propertyDefs.add(PropertyDef.newBuilder(property).setId(propertyId).setInnerId(propertyId).build());
    }
    typeDefBuilder.setPropertyDefs(propertyDefs);
    int labelIdx = graphDef.getLabelIdx() + 1;
    LabelId labelId = new LabelId(labelIdx);
    typeDefBuilder.setLabelId(labelId);
    TypeDef newTypeDef = typeDefBuilder.build();
    version++;
    graphDefBuilder.setVersion(version);
    graphDefBuilder.addTypeDef(newTypeDef);
    graphDefBuilder.setLabelIdx(labelIdx);
    if (typeDef.getTypeEnum() == TypeEnum.VERTEX) {
        long tableIdx = graphDef.getTableIdx();
        tableIdx++;
        graphDefBuilder.putVertexTableId(labelId, tableIdx);
        graphDefBuilder.setTableIdx(tableIdx);
    }
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        Operation operation = makeOperation(i, version, newTypeDef, newGraphDef);
        operations.add(operation);
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Operation(com.alibaba.graphscope.groot.operation.Operation) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) TypeDefPb(com.alibaba.maxgraph.proto.groot.TypeDefPb)

Example 2 with GraphDef

use of com.alibaba.maxgraph.sdkcommon.schema.GraphDef in project GraphScope by alibaba.

the class AbstractDropTypeExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    TypeDefPb typeDefPb = TypeDefPb.parseFrom(ddlBlob);
    TypeDef typeDef = TypeDef.parseProto(typeDefPb);
    long version = graphDef.getSchemaVersion();
    String label = typeDef.getLabel();
    if (!graphDef.hasLabel(label)) {
        throw new DdlException("label [" + label + "] not exists, schema version [" + version + "]");
    }
    LabelId labelId = graphDef.getLabelId(label);
    Set<EdgeKind> edgeKindSet = graphDef.getIdToKinds().get(labelId);
    if (edgeKindSet != null && edgeKindSet.size() > 0) {
        throw new DdlException("cannot drop label [" + label + "], since it has related edgeKinds");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    version++;
    graphDefBuilder.setVersion(version);
    graphDefBuilder.removeTypeDef(label);
    Set<String> remainPropertyNames = new HashSet<>();
    for (TypeDef remainTypeDef : graphDefBuilder.getAllTypeDefs()) {
        for (PropertyDef property : remainTypeDef.getProperties()) {
            remainPropertyNames.add(property.getName());
        }
    }
    graphDefBuilder.clearUnusedPropertyName(remainPropertyNames);
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        operations.add(makeOperation(i, version, labelId));
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Operation(com.alibaba.graphscope.groot.operation.Operation) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) TypeDefPb(com.alibaba.maxgraph.proto.groot.TypeDefPb) HashSet(java.util.HashSet)

Example 3 with GraphDef

use of com.alibaba.maxgraph.sdkcommon.schema.GraphDef in project GraphScope by alibaba.

the class AddEdgeKindExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    EdgeKindPb edgeKindPb = EdgeKindPb.parseFrom(ddlBlob);
    EdgeKind edgeKind = EdgeKind.parseProto(edgeKindPb);
    long version = graphDef.getSchemaVersion();
    EdgeKind.Builder edgeKindBuilder = EdgeKind.newBuilder(edgeKind);
    String edgeLabel = edgeKind.getEdgeLabel();
    LabelId edgeLabelId = graphDef.getLabelId(edgeLabel);
    if (edgeLabelId == null) {
        throw new DdlException("invalid edgeLabel [" + edgeLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setEdgeLabelId(edgeLabelId);
    String srcVertexLabel = edgeKind.getSrcVertexLabel();
    LabelId srcVertexLabelId = graphDef.getLabelId(srcVertexLabel);
    if (srcVertexLabelId == null) {
        throw new DdlException("invalid srcVertexLabel [" + srcVertexLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setSrcVertexLabelId(srcVertexLabelId);
    String dstVertexLabel = edgeKind.getDstVertexLabel();
    LabelId dstVertexLabelId = graphDef.getLabelId(dstVertexLabel);
    if (dstVertexLabelId == null) {
        throw new DdlException("invalid dstVertexLabel [" + dstVertexLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setDstVertexLabelId(dstVertexLabelId);
    EdgeKind newEdgeKind = edgeKindBuilder.build();
    if (graphDef.hasEdgeKind(newEdgeKind)) {
        throw new DdlException("edgeKind [" + newEdgeKind + "] already exists, schema version [" + version + "]");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    version++;
    graphDefBuilder.setVersion(version);
    graphDefBuilder.addEdgeKind(newEdgeKind);
    long tableIdx = graphDef.getTableIdx();
    tableIdx++;
    graphDefBuilder.putEdgeTableId(newEdgeKind, tableIdx);
    graphDefBuilder.setTableIdx(tableIdx);
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        operations.add(new AddEdgeKindOperation(i, version, newEdgeKind, tableIdx));
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) EdgeKindPb(com.alibaba.maxgraph.proto.groot.EdgeKindPb) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Operation(com.alibaba.graphscope.groot.operation.Operation) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId)

Example 4 with GraphDef

use of com.alibaba.maxgraph.sdkcommon.schema.GraphDef in project GraphScope by alibaba.

the class PrepareDataLoadExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    DataLoadTargetPb dataLoadTargetPb = DataLoadTargetPb.parseFrom(ddlBlob);
    DataLoadTarget dataLoadTarget = DataLoadTarget.parseProto(dataLoadTargetPb);
    String label = dataLoadTarget.getLabel();
    String srcLabel = dataLoadTarget.getSrcLabel();
    String dstLabel = dataLoadTarget.getDstLabel();
    long version = graphDef.getSchemaVersion();
    if (!graphDef.hasLabel(label)) {
        throw new DdlException("label [" + label + "] not exists, schema version [" + version + "]");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    TypeDef typeDef = graphDef.getTypeDef(label);
    long tableIdx = graphDef.getTableIdx();
    tableIdx++;
    DataLoadTarget.Builder targetBuilder = DataLoadTarget.newBuilder(dataLoadTarget);
    if (srcLabel == null || srcLabel.isEmpty()) {
        // Vertex type
        if (typeDef.getTypeEnum() != TypeEnum.VERTEX) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], label is not a vertex");
        }
        graphDefBuilder.putVertexTableId(typeDef.getTypeLabelId(), tableIdx);
        targetBuilder.setLabelId(typeDef.getLabelId());
    } else {
        // Edge kind
        if (typeDef.getTypeEnum() != TypeEnum.EDGE) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], label is not an edge");
        }
        EdgeKind.Builder edgeKindBuilder = EdgeKind.newBuilder();
        LabelId edgeLabelId = graphDef.getLabelId(label);
        if (edgeLabelId == null) {
            throw new DdlException("invalid edgeLabel [" + label + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setEdgeLabelId(edgeLabelId);
        targetBuilder.setLabelId(edgeLabelId.getId());
        LabelId srcVertexLabelId = graphDef.getLabelId(srcLabel);
        if (srcVertexLabelId == null) {
            throw new DdlException("invalid srcVertexLabel [" + srcLabel + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setSrcVertexLabelId(srcVertexLabelId);
        targetBuilder.setSrcLabelId(srcVertexLabelId.getId());
        LabelId dstVertexLabelId = graphDef.getLabelId(dstLabel);
        if (dstVertexLabelId == null) {
            throw new DdlException("invalid dstVertexLabel [" + dstLabel + "], schema version [" + version + "]");
        }
        edgeKindBuilder.setDstVertexLabelId(dstVertexLabelId);
        targetBuilder.setDstLabelId(dstVertexLabelId.getId());
        EdgeKind edgeKind = edgeKindBuilder.build();
        if (!graphDef.hasEdgeKind(edgeKind)) {
            throw new DdlException("invalid data load target [" + dataLoadTarget + "], edgeKind not exists");
        }
        graphDefBuilder.putEdgeTableId(edgeKind, tableIdx);
    }
    version++;
    graphDefBuilder.setTableIdx(tableIdx);
    graphDefBuilder.setVersion(version);
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        operations.add(new PrepareDataLoadOperation(i, version, targetBuilder.build(), tableIdx));
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Operation(com.alibaba.graphscope.groot.operation.Operation) PrepareDataLoadOperation(com.alibaba.graphscope.groot.operation.ddl.PrepareDataLoadOperation) DataLoadTargetPb(com.alibaba.maxgraph.proto.DataLoadTargetPb) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) DataLoadTarget(com.alibaba.maxgraph.sdkcommon.common.DataLoadTarget) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) PrepareDataLoadOperation(com.alibaba.graphscope.groot.operation.ddl.PrepareDataLoadOperation)

Example 5 with GraphDef

use of com.alibaba.maxgraph.sdkcommon.schema.GraphDef in project GraphScope by alibaba.

the class RemoveEdgeKindExecutor method execute.

@Override
public DdlResult execute(ByteString ddlBlob, GraphDef graphDef, int partitionCount) throws InvalidProtocolBufferException {
    EdgeKindPb edgeKindPb = EdgeKindPb.parseFrom(ddlBlob);
    EdgeKind edgeKind = EdgeKind.parseProto(edgeKindPb);
    long version = graphDef.getSchemaVersion();
    EdgeKind.Builder edgeKindBuilder = EdgeKind.newBuilder(edgeKind);
    String edgeLabel = edgeKind.getEdgeLabel();
    LabelId edgeLabelId = graphDef.getLabelId(edgeLabel);
    if (edgeLabelId == null) {
        throw new DdlException("invalid edgeLabel [" + edgeLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setEdgeLabelId(edgeLabelId);
    String srcVertexLabel = edgeKind.getSrcVertexLabel();
    LabelId srcVertexLabelId = graphDef.getLabelId(srcVertexLabel);
    if (srcVertexLabelId == null) {
        throw new DdlException("invalid srcVertexLabel [" + srcVertexLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setSrcVertexLabelId(srcVertexLabelId);
    String dstVertexLabel = edgeKind.getDstVertexLabel();
    LabelId dstVertexLabelId = graphDef.getLabelId(dstVertexLabel);
    if (dstVertexLabelId == null) {
        throw new DdlException("invalid dstVertexLabel [" + dstVertexLabel + "], schema version [" + version + "]");
    }
    edgeKindBuilder.setDstVertexLabelId(dstVertexLabelId);
    EdgeKind edgeKindToRemove = edgeKindBuilder.build();
    if (!graphDef.hasEdgeKind(edgeKindToRemove)) {
        throw new DdlException("edgeKind [" + edgeKind + "] not exists, schema version [" + version + "]");
    }
    GraphDef.Builder graphDefBuilder = GraphDef.newBuilder(graphDef);
    version++;
    graphDefBuilder.setVersion(version);
    graphDefBuilder.removeEdgeKind(edgeKindToRemove);
    GraphDef newGraphDef = graphDefBuilder.build();
    List<Operation> operations = new ArrayList<>(partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        operations.add(new RemoveEdgeKindOperation(i, version, edgeKindToRemove));
    }
    return new DdlResult(newGraphDef, operations);
}
Also used : DdlException(com.alibaba.graphscope.groot.schema.request.DdlException) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) EdgeKindPb(com.alibaba.maxgraph.proto.groot.EdgeKindPb) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) Operation(com.alibaba.graphscope.groot.operation.Operation) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId)

Aggregations

GraphDef (com.alibaba.maxgraph.sdkcommon.schema.GraphDef)25 Operation (com.alibaba.graphscope.groot.operation.Operation)9 Test (org.junit.jupiter.api.Test)9 ByteString (com.google.protobuf.ByteString)8 ArrayList (java.util.ArrayList)8 EdgeKind (com.alibaba.maxgraph.sdkcommon.schema.EdgeKind)7 LabelId (com.alibaba.maxgraph.sdkcommon.schema.LabelId)7 DdlException (com.alibaba.graphscope.groot.schema.request.DdlException)6 TypeDef (com.alibaba.maxgraph.sdkcommon.schema.TypeDef)6 PropertyDef (com.alibaba.maxgraph.sdkcommon.schema.PropertyDef)4 SnapshotCache (com.alibaba.graphscope.groot.SnapshotCache)3 SchemaManager (com.alibaba.graphscope.groot.coordinator.SchemaManager)3 CompletionCallback (com.alibaba.graphscope.groot.CompletionCallback)2 SnapshotListener (com.alibaba.graphscope.groot.SnapshotListener)2 MetaService (com.alibaba.graphscope.groot.meta.MetaService)2 BatchId (com.alibaba.graphscope.groot.operation.BatchId)2 AddEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation)2 RemoveEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation)2 DdlExecutors (com.alibaba.graphscope.groot.schema.ddl.DdlExecutors)2 DdlResult (com.alibaba.graphscope.groot.schema.ddl.DdlResult)2