Search in sources :

Example 6 with EdgeKind

use of com.alibaba.maxgraph.sdkcommon.schema.EdgeKind 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)

Example 7 with EdgeKind

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

the class DdlExecutorTest method testExecutor.

@Test
void testExecutor() throws InvalidProtocolBufferException {
    PropertyValue defaultValue = new PropertyValue(DataType.INT, ByteBuffer.allocate(Integer.BYTES).putInt(1).array());
    PropertyDef propertyDef = new PropertyDef(1, 1, "p1", DataType.INT, defaultValue, true, "property_1");
    TypeDef vertexTypeDef = TypeDef.newBuilder().setLabel("vertex1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.VERTEX).build();
    TypeDef edgeTypeDef = TypeDef.newBuilder().setLabel("edge1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.EDGE).build();
    EdgeKind edgeKind = EdgeKind.newBuilder().setEdgeLabel("edge1").setDstVertexLabel("vertex1").setSrcVertexLabel("vertex1").build();
    GraphDef graphDef = GraphDef.newBuilder().build();
    DdlExecutors ddlExecutors = new DdlExecutors();
    DdlRequestBatch.Builder requestBatchBuilder = DdlRequestBatch.newBuilder();
    requestBatchBuilder.addDdlRequest(new CreateVertexTypeRequest(vertexTypeDef));
    requestBatchBuilder.addDdlRequest(new CreateEdgeTypeRequest(edgeTypeDef));
    requestBatchBuilder.addDdlRequest(new AddEdgeKindRequest(edgeKind));
    DdlRequestBatch ddlRequestBatch = requestBatchBuilder.build();
    // Test serde
    ByteString bytes = ddlRequestBatch.toProto().toByteString();
    assertEquals(DdlRequestBatch.parseProto(DdlRequestBatchPb.parseFrom(bytes)), ddlRequestBatch);
    DdlResult ddlResult = ddlExecutors.executeDdlRequestBatch(ddlRequestBatch, graphDef, 1);
    LabelId vertexLabelId = new LabelId(1);
    LabelId edgeLabelId = new LabelId(2);
    EdgeKind edgeKindWithId = EdgeKind.newBuilder(edgeKind).setEdgeLabelId(edgeLabelId).setSrcVertexLabelId(vertexLabelId).setDstVertexLabelId(vertexLabelId).build();
    TypeDef vertexTypeWithId = TypeDef.newBuilder(vertexTypeDef).setLabelId(vertexLabelId).build();
    TypeDef edgeTypeWithId = TypeDef.newBuilder(edgeTypeDef).setLabelId(edgeLabelId).build();
    graphDef = GraphDef.newBuilder(graphDef).setVersion(3).setLabelIdx(2).setPropertyIdx(1).putPropertyNameToId("p1", 1).addTypeDef(vertexTypeWithId).putVertexTableId(vertexTypeWithId.getTypeLabelId(), 1L).addTypeDef(edgeTypeWithId).addEdgeKind(edgeKindWithId).putEdgeTableId(edgeKindWithId, 2L).setTableIdx(2L).build();
    assertEquals(graphDef, ddlResult.getGraphDef());
    List<Operation> ddlOperations = ddlResult.getDdlOperations();
    assertEquals(ddlOperations.size(), 3);
    assertEquals(ddlOperations.get(0).toBlob().toProto(), new CreateVertexTypeOperation(0, 1L, vertexTypeWithId, 1L).toBlob().toProto());
    assertEquals(ddlOperations.get(1).toBlob().toProto(), new CreateEdgeTypeOperation(0, 2L, edgeTypeWithId).toBlob().toProto());
    assertEquals(ddlOperations.get(2).toBlob().toProto(), new AddEdgeKindOperation(0, 3L, edgeKindWithId, 2L).toBlob().toProto());
    assertEquals(GraphDef.parseProto(GraphDefPb.parseFrom(graphDef.toProto().toByteString())), graphDef);
    requestBatchBuilder = DdlRequestBatch.newBuilder();
    requestBatchBuilder.addDdlRequest(new RemoveEdgeKindRequest(edgeKind));
    requestBatchBuilder.addDdlRequest(new DropEdgeTypeRequest(edgeTypeDef.getLabel()));
    requestBatchBuilder.addDdlRequest(new DropVertexTypeRequest(vertexTypeDef.getLabel()));
    ddlResult = ddlExecutors.executeDdlRequestBatch(requestBatchBuilder.build(), graphDef, 1);
    graphDef = GraphDef.newBuilder(graphDef).setVersion(6).removeEdgeKind(edgeKindWithId).removeTypeDef(edgeTypeDef.getLabel()).removeTypeDef(vertexTypeDef.getLabel()).clearUnusedPropertyName(Collections.emptySet()).build();
    assertEquals(graphDef, ddlResult.getGraphDef());
    ddlOperations = ddlResult.getDdlOperations();
    assertEquals(ddlOperations.size(), 3);
    assertEquals(ddlOperations.get(0).toBlob().toProto(), new RemoveEdgeKindOperation(0, 4L, edgeKindWithId).toBlob().toProto());
    assertEquals(ddlOperations.get(1).toBlob().toProto(), new DropEdgeTypeOperation(0, 5L, edgeLabelId).toBlob().toProto());
    assertEquals(ddlOperations.get(2).toBlob().toProto(), new DropVertexTypeOperation(0, 6L, vertexLabelId).toBlob().toProto());
}
Also used : PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) ByteString(com.google.protobuf.ByteString) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) CreateVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateVertexTypeOperation) CreateEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateEdgeTypeOperation) DropVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropVertexTypeOperation) Operation(com.alibaba.graphscope.groot.operation.Operation) DropEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropEdgeTypeOperation) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) RemoveEdgeKindRequest(com.alibaba.graphscope.groot.schema.request.RemoveEdgeKindRequest) DropVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropVertexTypeOperation) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) CreateVertexTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateVertexTypeOperation) AddEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation) DropVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.DropVertexTypeRequest) DdlRequestBatch(com.alibaba.graphscope.groot.schema.request.DdlRequestBatch) CreateEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.CreateEdgeTypeOperation) RemoveEdgeKindOperation(com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation) AddEdgeKindRequest(com.alibaba.graphscope.groot.schema.request.AddEdgeKindRequest) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) DdlExecutors(com.alibaba.graphscope.groot.schema.ddl.DdlExecutors) DropEdgeTypeOperation(com.alibaba.graphscope.groot.operation.ddl.DropEdgeTypeOperation) CreateVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) DropEdgeTypeRequest(com.alibaba.graphscope.groot.schema.request.DropEdgeTypeRequest) CreateEdgeTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateEdgeTypeRequest) DdlResult(com.alibaba.graphscope.groot.schema.ddl.DdlResult) Test(org.junit.jupiter.api.Test)

Example 8 with EdgeKind

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

the class ClientDdlService method toGraphDefPb.

private GraphDefPb toGraphDefPb(GraphDef graphDef) {
    GraphDefPb.Builder builder = GraphDefPb.newBuilder();
    builder.setVersion(graphDef.getVersion());
    builder.setKey("");
    builder.setGraphType(GraphTypePb.PERSISTENT_STORE);
    builder.setDirected(true);
    for (TypeDef typeDef : graphDef.getIdToType().values()) {
        builder.addTypeDefs(toTypeDefPb(typeDef));
    }
    for (Set<EdgeKind> edgeKinds : graphDef.getIdToKinds().values()) {
        for (EdgeKind edgeKind : edgeKinds) {
            EdgeKindPb edgeKindPb = EdgeKindPb.newBuilder().setEdgeLabel(edgeKind.getEdgeLabel()).setEdgeLabelId(LabelIdPb.newBuilder().setId(edgeKind.getEdgeLabelId().getId())).setSrcVertexLabel(edgeKind.getSrcVertexLabel()).setSrcVertexLabelId(LabelIdPb.newBuilder().setId(edgeKind.getSrcVertexLabelId().getId())).setDstVertexLabel(edgeKind.getDstVertexLabel()).setDstVertexLabelId(LabelIdPb.newBuilder().setId(edgeKind.getSrcVertexLabelId().getId())).build();
            builder.addEdgeKinds(edgeKindPb);
        }
    }
    MaxGraphInfoPb maxGraphInfoPb = MaxGraphInfoPb.newBuilder().setLastLabelId(graphDef.getLabelIdx()).setLastTableId(graphDef.getTableIdx()).setLastPropertyId(graphDef.getPropertyIdx()).build();
    builder.setExtension(Any.pack(maxGraphInfoPb));
    return builder.build();
}
Also used : TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind)

Example 9 with EdgeKind

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

the class ClientService method dropSchema.

@Override
public void dropSchema(DropSchemaRequest request, StreamObserver<DropSchemaResponse> responseObserver) {
    try {
        DdlRequestBatch.Builder ddlBatchBuilder = DdlRequestBatch.newBuilder();
        GraphDef graphDef = this.snapshotCache.getSnapshotWithSchema().getGraphDef();
        for (GraphEdge graphEdge : graphDef.getEdgeList()) {
            String label = graphEdge.getLabel();
            for (EdgeRelation relation : graphEdge.getRelationList()) {
                String sourceLabel = relation.getSource().getLabel();
                String destLabel = relation.getTarget().getLabel();
                EdgeKind edgeKind = EdgeKind.newBuilder().setEdgeLabel(label).setSrcVertexLabel(sourceLabel).setDstVertexLabel(destLabel).build();
                RemoveEdgeKindRequest removeEdgeKindRequest = new RemoveEdgeKindRequest(edgeKind);
                ddlBatchBuilder.addDdlRequest(removeEdgeKindRequest);
            }
            DropEdgeTypeRequest dropEdgeTypeRequest = new DropEdgeTypeRequest(label);
            ddlBatchBuilder.addDdlRequest(dropEdgeTypeRequest);
        }
        for (GraphVertex graphVertex : graphDef.getVertexList()) {
            DropVertexTypeRequest dropVertexTypeRequest = new DropVertexTypeRequest(graphVertex.getLabel());
            ddlBatchBuilder.addDdlRequest(dropVertexTypeRequest);
        }
        long snapshotId = this.batchDdlClient.batchDdl(ddlBatchBuilder.build());
        this.snapshotCache.addListener(snapshotId, () -> {
            responseObserver.onNext(DropSchemaResponse.newBuilder().setGraphDef(this.snapshotCache.getSnapshotWithSchema().getGraphDef().toProto()).build());
            responseObserver.onCompleted();
        });
    } catch (Exception e) {
        logger.error("drop schema commit failed", e);
        responseObserver.onError(Status.INTERNAL.withDescription(e.getMessage()).asRuntimeException());
    }
}
Also used : EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef)

Example 10 with EdgeKind

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

the class GraphWriter method addDeleteEdgeOperation.

private void addDeleteEdgeOperation(OperationBatch.Builder batchBuilder, GraphSchema schema, DataRecord dataRecord) {
    EdgeId edgeId;
    EdgeKind edgeKind;
    EdgeTarget edgeTarget = dataRecord.getEdgeTarget();
    if (edgeTarget != null) {
        edgeId = edgeTarget.getEdgeId();
        edgeKind = edgeTarget.getEdgeKind();
    } else {
        EdgeRecordKey edgeRecordKey = dataRecord.getEdgeRecordKey();
        VertexRecordKey srcVertexRecordKey = edgeRecordKey.getSrcVertexRecordKey();
        VertexRecordKey dstVertexRecordKey = edgeRecordKey.getDstVertexRecordKey();
        String label = edgeRecordKey.getLabel();
        GraphElement edgeDef = schema.getElement(label);
        GraphElement srcVertexDef = schema.getElement(srcVertexRecordKey.getLabel());
        GraphElement dstVertexDef = schema.getElement(dstVertexRecordKey.getLabel());
        int labelId = edgeDef.getLabelId();
        Map<Integer, PropertyValue> srcVertexPkVals = parseRawProperties(srcVertexDef, srcVertexRecordKey.getProperties());
        long srcVertexHashId = getHashId(srcVertexDef.getLabelId(), srcVertexPkVals, srcVertexDef);
        Map<Integer, PropertyValue> dstVertexPkVals = parseRawProperties(dstVertexDef, dstVertexRecordKey.getProperties());
        long dstVertexHashId = getHashId(dstVertexDef.getLabelId(), dstVertexPkVals, dstVertexDef);
        long edgeInnerId = edgeRecordKey.getEdgeInnerId();
        edgeId = new EdgeId(new VertexId(srcVertexHashId), new VertexId(dstVertexHashId), edgeInnerId);
        edgeKind = EdgeKind.newBuilder().setEdgeLabelId(new LabelId(labelId)).setSrcVertexLabelId(new LabelId(srcVertexDef.getLabelId())).setDstVertexLabelId(new LabelId(dstVertexDef.getLabelId())).build();
    }
    batchBuilder.addOperation(new DeleteEdgeOperation(edgeId, edgeKind, true));
    batchBuilder.addOperation(new DeleteEdgeOperation(edgeId, edgeKind, false));
}
Also used : EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) VertexId(com.alibaba.graphscope.groot.operation.VertexId) EdgeRecordKey(com.alibaba.maxgraph.sdkcommon.common.EdgeRecordKey) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VertexRecordKey(com.alibaba.maxgraph.sdkcommon.common.VertexRecordKey) EdgeId(com.alibaba.graphscope.groot.operation.EdgeId) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId)

Aggregations

EdgeKind (com.alibaba.maxgraph.sdkcommon.schema.EdgeKind)12 LabelId (com.alibaba.maxgraph.sdkcommon.schema.LabelId)10 GraphDef (com.alibaba.maxgraph.sdkcommon.schema.GraphDef)7 Operation (com.alibaba.graphscope.groot.operation.Operation)6 ByteString (com.google.protobuf.ByteString)6 DdlException (com.alibaba.graphscope.groot.schema.request.DdlException)5 TypeDef (com.alibaba.maxgraph.sdkcommon.schema.TypeDef)5 ArrayList (java.util.ArrayList)5 EdgeId (com.alibaba.graphscope.groot.operation.EdgeId)4 VertexId (com.alibaba.graphscope.groot.operation.VertexId)4 PropertyValue (com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)4 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)3 EdgeRecordKey (com.alibaba.maxgraph.sdkcommon.common.EdgeRecordKey)3 VertexRecordKey (com.alibaba.maxgraph.sdkcommon.common.VertexRecordKey)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AddEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.AddEdgeKindOperation)2 RemoveEdgeKindOperation (com.alibaba.graphscope.groot.operation.ddl.RemoveEdgeKindOperation)2 DataLoadTargetPb (com.alibaba.maxgraph.proto.DataLoadTargetPb)2 EdgeKindPb (com.alibaba.maxgraph.proto.groot.EdgeKindPb)2 DataLoadTarget (com.alibaba.maxgraph.sdkcommon.common.DataLoadTarget)2