Search in sources :

Example 1 with EdgeKind

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

the class MaxGraphImpl method addEdge.

@Override
public Edge addEdge(String label, Vertex src, Vertex dst, Map<String, Object> properties) {
    GraphSchema schema = getSchema();
    int edgeLabelId = schema.getElement(label).getLabelId();
    EdgeKind edgeKind = EdgeKind.newBuilder().setEdgeLabelId(new LabelId(edgeLabelId)).setSrcVertexLabelId(new LabelId(src.id.typeId())).setDstVertexLabelId(new LabelId(dst.id.typeId())).build();
    long innerId = ++startEdgeInnerId;
    EdgeId edgeId = new EdgeId(new VertexId(src.id.id()), new VertexId(dst.id.id()), innerId);
    EdgeTarget edgeTarget = new EdgeTarget(edgeKind, edgeId);
    DataRecord dataRecord = new DataRecord(edgeTarget, properties);
    WriteRequest writeRequest = new WriteRequest(OperationType.OVERWRITE_EDGE, dataRecord);
    graphWriter.writeBatch(getClass().getCanonicalName(), this.writeSession, Arrays.asList(writeRequest));
    return null;
}
Also used : EdgeKind(com.alibaba.maxgraph.sdkcommon.schema.EdgeKind) EdgeId(com.alibaba.graphscope.groot.operation.EdgeId) VertexId(com.alibaba.graphscope.groot.operation.VertexId) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 2 with EdgeKind

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

the class GraphWriter method addOverwriteEdgeOperation.

private void addOverwriteEdgeOperation(OperationBatch.Builder batchBuilder, GraphSchema schema, DataRecord dataRecord) {
    EdgeId edgeId;
    EdgeKind edgeKind;
    GraphElement edgeDef;
    EdgeTarget edgeTarget = dataRecord.getEdgeTarget();
    Map<String, Object> properties = dataRecord.getProperties();
    if (edgeTarget != null) {
        edgeId = edgeTarget.getEdgeId();
        edgeKind = edgeTarget.getEdgeKind();
        edgeDef = schema.getElement(edgeKind.getEdgeLabelId().getId());
    } else {
        EdgeRecordKey edgeRecordKey = dataRecord.getEdgeRecordKey();
        VertexRecordKey srcVertexRecordKey = edgeRecordKey.getSrcVertexRecordKey();
        VertexRecordKey dstVertexRecordKey = edgeRecordKey.getDstVertexRecordKey();
        String label = edgeRecordKey.getLabel();
        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 = this.edgeIdGenerator.getNextId();
        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();
    }
    Map<Integer, PropertyValue> propertyVals = parseRawProperties(edgeDef, properties);
    batchBuilder.addOperation(new OverwriteEdgeOperation(edgeId, edgeKind, propertyVals, true));
    batchBuilder.addOperation(new OverwriteEdgeOperation(edgeId, edgeKind, propertyVals, 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)

Example 3 with EdgeKind

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

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

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

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