Search in sources :

Example 21 with VertexLabel

use of com.baidu.hugegraph.structure.schema.VertexLabel in project incubator-hugegraph-toolchain by apache.

the class VertexLabelService method convert.

private static VertexLabel convert(VertexLabelUpdateEntity entity, HugeClient client) {
    if (entity == null) {
        return null;
    }
    Set<String> properties = new HashSet<>();
    if (entity.getAppendProperties() != null) {
        entity.getAppendProperties().forEach(p -> {
            properties.add(p.getName());
        });
    }
    VertexLabel.Builder builder;
    builder = client.schema().vertexLabel(entity.getName()).properties(toStringArray(properties)).nullableKeys(toStringArray(properties));
    VertexLabel vertexLabel = builder.build();
    Map<String, Object> userdata = vertexLabel.userdata();
    /*
         * The style requires the front end to pass in the full amount
         * TODO: use builder or setter, now use builder throw exception
         * "Can't access builder which is completed"
         */
    VertexLabelStyle style = entity.getStyle();
    if (style != null) {
        userdata.put(USER_KEY_STYLE, JsonUtil.toJson(style));
    }
    return vertexLabel;
}
Also used : VertexLabelStyle(com.baidu.hugegraph.entity.schema.VertexLabelStyle) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) HashSet(java.util.HashSet)

Example 22 with VertexLabel

use of com.baidu.hugegraph.structure.schema.VertexLabel in project incubator-hugegraph-toolchain by apache.

the class RestoreManager method initPrimaryKeyVLs.

private void initPrimaryKeyVLs() {
    if (this.primaryKeyVLs != null) {
        return;
    }
    this.primaryKeyVLs = new HashMap<>();
    List<VertexLabel> vertexLabels = this.client.schema().getVertexLabels();
    for (VertexLabel vl : vertexLabels) {
        if (vl.idStrategy() == IdStrategy.PRIMARY_KEY) {
            this.primaryKeyVLs.put(vl.name(), vl.id());
        }
    }
}
Also used : VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel)

Example 23 with VertexLabel

use of com.baidu.hugegraph.structure.schema.VertexLabel in project incubator-hugegraph-toolchain by apache.

the class VertexLabelAPI method checkCreateOrUpdate.

@Override
protected Object checkCreateOrUpdate(SchemaElement schemaElement) {
    VertexLabel vertexLabel = (VertexLabel) schemaElement;
    if (vertexLabel.idStrategy().isCustomizeUuid()) {
        this.client.checkApiVersion("0.46", "customize UUID strategy");
    }
    Object vl = vertexLabel;
    if (this.client.apiVersionLt("0.54")) {
        E.checkArgument(vertexLabel.ttl() == 0L && vertexLabel.ttlStartTime() == null, "Not support ttl until api version 0.54");
        vl = vertexLabel.switchV53();
    }
    return vl;
}
Also used : VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel)

Example 24 with VertexLabel

use of com.baidu.hugegraph.structure.schema.VertexLabel in project incubator-hugegraph-toolchain by apache.

the class ParseTaskBuilder method buildTask.

private ParseTask buildTask(ElementBuilder builder, List<Line> lines) {
    final LoadMetrics metrics = this.context.summary().metrics(this.struct);
    final int batchSize = this.context.options().batchSize;
    final ElementMapping mapping = builder.mapping();
    final boolean needRemoveId = builder instanceof VertexBuilder && ((VertexLabel) builder.schemaLabel()).idStrategy().isPrimaryKey();
    return new ParseTask(mapping, () -> {
        List<List<Record>> batches = new ArrayList<>();
        // One batch record
        List<Record> records = new ArrayList<>(batchSize);
        int count = 0;
        for (Line line : lines) {
            try {
                // NOTE: don't remove entry in keyValues
                @SuppressWarnings("unchecked") List<GraphElement> elements = builder.build(line.names(), line.values());
                E.checkState(elements.size() <= batchSize, "The number of columns in a line cannot " + "exceed the size of a batch, but got %s > %s", elements.size(), batchSize);
                // Prevent batch size from exceeding limit
                if (records.size() + elements.size() > batchSize) {
                    LOG.debug("Create a new batch for {}", mapping);
                    // Add current batch and create a new batch
                    batches.add(records);
                    records = new ArrayList<>(batchSize);
                }
                for (GraphElement element : elements) {
                    if (needRemoveId) {
                        ((Vertex) element).id(null);
                    }
                    records.add(new Record(line.rawLine(), element));
                    count++;
                }
            } catch (IllegalArgumentException e) {
                metrics.increaseParseFailure(mapping);
                ParseException pe = new ParseException(line.rawLine(), e);
                this.handleParseFailure(mapping, pe);
            }
        }
        if (!records.isEmpty()) {
            batches.add(records);
        }
        metrics.plusParseSuccess(mapping, count);
        return batches;
    });
}
Also used : ElementMapping(com.baidu.hugegraph.loader.mapping.ElementMapping) Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) LoadMetrics(com.baidu.hugegraph.loader.metrics.LoadMetrics) Line(com.baidu.hugegraph.loader.reader.line.Line) VertexBuilder(com.baidu.hugegraph.loader.builder.VertexBuilder) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) List(java.util.List) Record(com.baidu.hugegraph.loader.builder.Record) ParseException(com.baidu.hugegraph.loader.exception.ParseException)

Example 25 with VertexLabel

use of com.baidu.hugegraph.structure.schema.VertexLabel in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadVertexLabels.

@Test
public void testReadVertexLabels() {
    String json = "{\"vertexlabels\": [" + "{" + "\"id\": 1," + "\"primary_keys\": [\"name\"]," + "\"index_labels\": []," + "\"name\": \"software\"," + "\"id_strategy\": \"PRIMARY_KEY\"," + "\"properties\": [\"price\", \"name\", \"lang\"]" + "}," + "{" + "\"id\": 2," + "\"primary_keys\": []," + "\"index_labels\": []," + "\"name\": \"person\"," + "\"id_strategy\": \"CUSTOMIZE_STRING\"," + "\"properties\": [\"city\", \"name\", \"age\"]" + "}" + "]}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());
    List<VertexLabel> vertexLabels = result.readList("vertexlabels", VertexLabel.class);
    Assert.assertEquals(2, vertexLabels.size());
    VertexLabel vertexLabel1 = vertexLabels.get(0);
    VertexLabel vertexLabel2 = vertexLabels.get(1);
    Assert.assertEquals("software", vertexLabel1.name());
    Assert.assertEquals(IdStrategy.PRIMARY_KEY, vertexLabel1.idStrategy());
    Assert.assertEquals(ImmutableList.of("name"), vertexLabel1.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("price", "name", "lang"), vertexLabel1.properties());
    Assert.assertEquals("person", vertexLabel2.name());
    Assert.assertEquals(IdStrategy.CUSTOMIZE_STRING, vertexLabel2.idStrategy());
    Assert.assertEquals(Collections.emptyList(), vertexLabel2.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("city", "name", "age"), vertexLabel2.properties());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) Test(org.junit.Test)

Aggregations

VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)46 Test (org.junit.Test)31 HugeClient (com.baidu.hugegraph.driver.HugeClient)7 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)7 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)6 ExternalException (com.baidu.hugegraph.exception.ExternalException)5 ServerException (com.baidu.hugegraph.exception.ServerException)5 Edge (com.baidu.hugegraph.structure.graph.Edge)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 RestResult (com.baidu.hugegraph.rest.RestResult)3 Vertex (com.baidu.hugegraph.structure.graph.Vertex)3 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)3 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)2 VertexLabelEntity (com.baidu.hugegraph.entity.schema.VertexLabelEntity)1 VertexLabelStyle (com.baidu.hugegraph.entity.schema.VertexLabelStyle)1 Record (com.baidu.hugegraph.loader.builder.Record)1 VertexBuilder (com.baidu.hugegraph.loader.builder.VertexBuilder)1 ParseException (com.baidu.hugegraph.loader.exception.ParseException)1 ElementMapping (com.baidu.hugegraph.loader.mapping.ElementMapping)1