Search in sources :

Example 6 with PropertyValue

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

the class GraphWriter method addOverwriteVertexOperation.

private void addOverwriteVertexOperation(OperationBatch.Builder batchBuilder, GraphSchema schema, DataRecord dataRecord) {
    VertexRecordKey vertexRecordKey = dataRecord.getVertexRecordKey();
    Map<String, Object> properties = dataRecord.getProperties();
    String label = vertexRecordKey.getLabel();
    GraphElement vertexDef = schema.getElement(label);
    int labelId = vertexDef.getLabelId();
    Map<Integer, PropertyValue> pkVals = parseRawProperties(vertexDef, vertexRecordKey.getProperties());
    Map<Integer, PropertyValue> propertyVals = parseRawProperties(vertexDef, properties);
    propertyVals.putAll(pkVals);
    long hashId = getHashId(labelId, propertyVals, vertexDef);
    batchBuilder.addOperation(new OverwriteVertexOperation(new VertexId(hashId), new LabelId(labelId), propertyVals));
}
Also used : PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) VertexId(com.alibaba.graphscope.groot.operation.VertexId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VertexRecordKey(com.alibaba.maxgraph.sdkcommon.common.VertexRecordKey) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) LabelId(com.alibaba.maxgraph.sdkcommon.schema.LabelId)

Example 7 with PropertyValue

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

the class SchemaManagerTest method testSchemaManager.

@Test
void testSchemaManager() throws IOException, InterruptedException {
    SnapshotManager mockSnapshotManager = mock(SnapshotManager.class);
    doAnswer(invocationOnMock -> {
        SnapshotListener listener = invocationOnMock.getArgument(1);
        listener.onSnapshotAvailable();
        return null;
    }).when(mockSnapshotManager).addSnapshotListener(anyLong(), any());
    when(mockSnapshotManager.increaseWriteSnapshotId()).thenReturn(1L);
    when(mockSnapshotManager.getCurrentWriteSnapshotId()).thenReturn(1L);
    DdlExecutors ddlExecutors = new DdlExecutors();
    DdlWriter mockDdlWriter = mock(DdlWriter.class);
    when(mockDdlWriter.writeOperations(anyString(), any())).thenReturn(new BatchId(1L));
    MetaService mockMetaService = mock(MetaService.class);
    GraphDefFetcher mockGraphDefFetcher = mock(GraphDefFetcher.class);
    GraphDef initialGraphDef = GraphDef.newBuilder().build();
    when(mockGraphDefFetcher.fetchGraphDef()).thenReturn(initialGraphDef);
    SchemaManager schemaManager = new SchemaManager(mockSnapshotManager, ddlExecutors, mockDdlWriter, mockMetaService, mockGraphDefFetcher);
    schemaManager.start();
    assertEquals(initialGraphDef, schemaManager.getGraphDef());
    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 typeDef = TypeDef.newBuilder().setLabel("vertex1").addPropertyDef(propertyDef).setTypeEnum(TypeEnum.VERTEX).build();
    DdlRequestBatch ddlRequestBatch = DdlRequestBatch.newBuilder().addDdlRequest(new CreateVertexTypeRequest(typeDef)).build();
    CountDownLatch latch = new CountDownLatch(1);
    schemaManager.submitBatchDdl("requestId", "sessionId", ddlRequestBatch, new CompletionCallback<Long>() {

        @Override
        public void onCompleted(Long res) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable t) {
        }
    });
    assertTrue(latch.await(5L, TimeUnit.SECONDS));
    schemaManager.stop();
}
Also used : MetaService(com.alibaba.graphscope.groot.meta.MetaService) PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) BatchId(com.alibaba.graphscope.groot.operation.BatchId) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) SchemaManager(com.alibaba.graphscope.groot.coordinator.SchemaManager) CountDownLatch(java.util.concurrent.CountDownLatch) GraphDefFetcher(com.alibaba.graphscope.groot.coordinator.GraphDefFetcher) GraphDef(com.alibaba.maxgraph.sdkcommon.schema.GraphDef) SnapshotListener(com.alibaba.graphscope.groot.SnapshotListener) DdlWriter(com.alibaba.graphscope.groot.coordinator.DdlWriter) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) DdlExecutors(com.alibaba.graphscope.groot.schema.ddl.DdlExecutors) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) CreateVertexTypeRequest(com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest) DdlRequestBatch(com.alibaba.graphscope.groot.schema.request.DdlRequestBatch) SnapshotManager(com.alibaba.graphscope.groot.coordinator.SnapshotManager) Test(org.junit.jupiter.api.Test)

Example 8 with PropertyValue

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

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

the class ClientDdlService method toPropertyDefPb.

private PropertyDefPb toPropertyDefPb(PropertyDef propertyDef) {
    PropertyDefPb.Builder builder = PropertyDefPb.newBuilder();
    builder.setId(propertyDef.getId());
    builder.setInnerId(propertyDef.getId());
    builder.setName(propertyDef.getName());
    builder.setDataType(DataTypePb.valueOf(propertyDef.getDataType().name()));
    builder.setPk(propertyDef.isPartOfPrimaryKey());
    builder.setComment(propertyDef.getComment());
    PropertyValue propertyValue = propertyDef.getDefaultPropertyValue();
    builder.setDefaultValue(PropertyValuePb.newBuilder().setDataType(DataTypePb.valueOf(propertyValue.getDataType().name())).setVal(ByteString.copyFrom(propertyValue.getValBytes())).build());
    return builder.build();
}
Also used : PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)

Example 10 with PropertyValue

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

the class ClientService method loadJsonSchema.

@Override
public void loadJsonSchema(LoadJsonSchemaRequest request, StreamObserver<LoadJsonSchemaResponse> responseObserver) {
    try {
        String schemaJson = request.getSchemaJson();
        GraphSchema graphSchema = GraphSchemaMapper.parseFromJson(schemaJson).toGraphSchema();
        DdlRequestBatch.Builder ddlBatchBuilder = DdlRequestBatch.newBuilder();
        for (GraphVertex graphVertex : graphSchema.getVertexList()) {
            String label = graphVertex.getLabel();
            List<String> primaryKeyList = graphVertex.getPrimaryKeyConstraint().getPrimaryKeyList();
            List<GraphProperty> propertyList = graphVertex.getPropertyList();
            TypeDef.Builder typeDefBuilder = TypeDef.newBuilder();
            typeDefBuilder.setLabel(label);
            typeDefBuilder.setTypeEnum(TypeEnum.VERTEX);
            Set<String> primaryKeys = new HashSet<>(primaryKeyList);
            for (GraphProperty graphProperty : propertyList) {
                PropertyDef.Builder propertyBuilder = PropertyDef.newBuilder();
                String propertyName = graphProperty.getName();
                DataType dataType = graphProperty.getDataType();
                propertyBuilder.setName(propertyName).setDataType(dataType).setComment(graphProperty.getComment());
                if (primaryKeys.contains(propertyName)) {
                    propertyBuilder.setPk(true);
                }
                if (graphProperty.hasDefaultValue()) {
                    Object defaultValue = graphProperty.getDefaultValue();
                    propertyBuilder.setDefaultValue(new PropertyValue(dataType, defaultValue));
                }
                typeDefBuilder.addPropertyDef(propertyBuilder.build());
            }
            CreateVertexTypeRequest ddlRequest = new CreateVertexTypeRequest(typeDefBuilder.build());
            ddlBatchBuilder.addDdlRequest(ddlRequest);
        }
        for (GraphEdge graphEdge : graphSchema.getEdgeList()) {
            String label = graphEdge.getLabel();
            List<GraphProperty> propertyList = graphEdge.getPropertyList();
            List<EdgeRelation> relationList = graphEdge.getRelationList();
            TypeDef.Builder typeDefBuilder = TypeDef.newBuilder();
            typeDefBuilder.setLabel(label);
            typeDefBuilder.setTypeEnum(TypeEnum.EDGE);
            for (GraphProperty graphProperty : propertyList) {
                PropertyDef.Builder propertyBuilder = PropertyDef.newBuilder();
                String propertyName = graphProperty.getName();
                DataType dataType = graphProperty.getDataType();
                propertyBuilder.setName(propertyName).setDataType(dataType).setComment(graphProperty.getComment());
                if (graphProperty.hasDefaultValue()) {
                    Object defaultValue = graphProperty.getDefaultValue();
                    propertyBuilder.setDefaultValue(new PropertyValue(dataType, defaultValue));
                }
                typeDefBuilder.addPropertyDef(propertyBuilder.build());
            }
            CreateEdgeTypeRequest createEdgeTypeRequest = new CreateEdgeTypeRequest(typeDefBuilder.build());
            ddlBatchBuilder.addDdlRequest(createEdgeTypeRequest);
            for (EdgeRelation edgeRelation : relationList) {
                AddEdgeKindRequest addEdgeKindRequest = new AddEdgeKindRequest(EdgeKind.newBuilder().setEdgeLabel(label).setSrcVertexLabel(edgeRelation.getSource().getLabel()).setDstVertexLabel(edgeRelation.getTarget().getLabel()).build());
                ddlBatchBuilder.addDdlRequest(addEdgeKindRequest);
            }
        }
        long snapshotId = this.batchDdlClient.batchDdl(ddlBatchBuilder.build());
        this.snapshotCache.addListener(snapshotId, () -> {
            responseObserver.onNext(LoadJsonSchemaResponse.newBuilder().setGraphDef(this.snapshotCache.getSnapshotWithSchema().getGraphDef().toProto()).build());
            responseObserver.onCompleted();
        });
    } catch (Exception e) {
        logger.error("commit failed", e);
        responseObserver.onError(Status.INTERNAL.withDescription(e.getMessage()).asRuntimeException());
    }
}
Also used : PropertyDef(com.alibaba.maxgraph.sdkcommon.schema.PropertyDef) TypeDef(com.alibaba.maxgraph.sdkcommon.schema.TypeDef) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)

Aggregations

PropertyValue (com.alibaba.maxgraph.sdkcommon.schema.PropertyValue)17 LabelId (com.alibaba.maxgraph.sdkcommon.schema.LabelId)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 VertexId (com.alibaba.graphscope.groot.operation.VertexId)6 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)6 VertexRecordKey (com.alibaba.maxgraph.sdkcommon.common.VertexRecordKey)6 EdgeKind (com.alibaba.maxgraph.sdkcommon.schema.EdgeKind)4 PropertyDef (com.alibaba.maxgraph.sdkcommon.schema.PropertyDef)4 TypeDef (com.alibaba.maxgraph.sdkcommon.schema.TypeDef)4 EdgeId (com.alibaba.graphscope.groot.operation.EdgeId)3 EdgeRecordKey (com.alibaba.maxgraph.sdkcommon.common.EdgeRecordKey)3 DdlExecutors (com.alibaba.graphscope.groot.schema.ddl.DdlExecutors)2 CreateVertexTypeRequest (com.alibaba.graphscope.groot.schema.request.CreateVertexTypeRequest)2 DdlRequestBatch (com.alibaba.graphscope.groot.schema.request.DdlRequestBatch)2 PropertyDefNotFoundException (com.alibaba.maxgraph.compiler.api.exception.PropertyDefNotFoundException)2 GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)2 GraphDef (com.alibaba.maxgraph.sdkcommon.schema.GraphDef)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 SnapshotListener (com.alibaba.graphscope.groot.SnapshotListener)1