Search in sources :

Example 6 with PropertyKey

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

the class PropertyKeyApiTest method testAddPropertyKeyWithUserData.

@Test
public void testAddPropertyKeyWithUserData() {
    PropertyKey age = schema().propertyKey("age").userdata("min", 0).userdata("max", 100).build();
    PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
    propertyKeyWithTask = propertyKeyAPI.create(age);
    Assert.assertEquals(0L, propertyKeyWithTask.taskId());
    age = propertyKeyWithTask.propertyKey();
    Assert.assertEquals(3, age.userdata().size());
    Assert.assertEquals(0, age.userdata().get("min"));
    Assert.assertEquals(100, age.userdata().get("max"));
    String time = (String) age.userdata().get("~create_time");
    Date createTime = DateUtil.parse(time);
    Assert.assertTrue(createTime.before(DateUtil.now()));
    PropertyKey id = schema().propertyKey("id").userdata("length", 15).userdata("length", 18).build();
    propertyKeyWithTask = propertyKeyAPI.create(id);
    Assert.assertEquals(0L, propertyKeyWithTask.taskId());
    id = propertyKeyWithTask.propertyKey();
    // The same key user data will be overwritten
    Assert.assertEquals(2, id.userdata().size());
    Assert.assertEquals(18, id.userdata().get("length"));
    time = (String) id.userdata().get("~create_time");
    createTime = DateUtil.parse(time);
    Assert.assertTrue(createTime.before(DateUtil.now()));
    PropertyKey sex = schema().propertyKey("sex").userdata("range", ImmutableList.of("male", "female")).build();
    propertyKeyWithTask = propertyKeyAPI.create(sex);
    Assert.assertEquals(0L, propertyKeyWithTask.taskId());
    sex = propertyKeyWithTask.propertyKey();
    Assert.assertEquals(2, sex.userdata().size());
    Assert.assertEquals(ImmutableList.of("male", "female"), sex.userdata().get("range"));
    time = (String) sex.userdata().get("~create_time");
    createTime = DateUtil.parse(time);
    Assert.assertTrue(createTime.before(DateUtil.now()));
}
Also used : PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Date(java.util.Date) Test(org.junit.Test)

Example 7 with PropertyKey

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

the class GraphService method fillProperties.

private void fillProperties(int connId, SchemaLabelEntity schema, GraphElement element, Map<String, Object> properties) {
    HugeClient client = this.client(connId);
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        String key = entry.getKey();
        Object rawValue = entry.getValue();
        // Skip nullable property
        if (schema.getNullableProps().contains(key)) {
            if (rawValue instanceof String && StringUtils.isEmpty((String) rawValue)) {
                continue;
            }
        }
        PropertyKeyEntity pkEntity = this.pkService.get(key, connId);
        PropertyKey propertyKey = PropertyKeyService.convert(pkEntity, client);
        assert propertyKey != null;
        Object value;
        try {
            // DataTypeUtil.convert in loader need param InputSource
            FileSource source = new FileSource();
            ListFormat listFormat = new ListFormat("", "", ",");
            source.listFormat(listFormat);
            value = DataTypeUtil.convert(rawValue, propertyKey, source);
        } catch (IllegalArgumentException e) {
            throw new ExternalException("graph.property.convert.failed", e, key, rawValue);
        }
        element.property(key, value);
    }
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) FileSource(com.baidu.hugegraph.loader.source.file.FileSource) ListFormat(com.baidu.hugegraph.loader.source.file.ListFormat) ExternalException(com.baidu.hugegraph.exception.ExternalException) Map(java.util.Map) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) PropertyKeyEntity(com.baidu.hugegraph.entity.schema.PropertyKeyEntity)

Example 8 with PropertyKey

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

the class EdgeLabelService method reuse.

public void reuse(ConflictDetail detail, int connId) {
    Ex.check(!detail.hasConflict(), "schema.cannot-reuse-conflict");
    HugeClient client = this.client(connId);
    List<PropertyKey> propertyKeys = this.pkService.filter(detail, client);
    if (!propertyKeys.isEmpty()) {
        try {
            this.pkService.addBatch(propertyKeys, client);
        } catch (Exception e) {
            throw new ExternalException("schema.propertykey.reuse.failed", e);
        }
    }
    List<VertexLabel> vertexLabels = this.vlService.filter(detail, client);
    if (!vertexLabels.isEmpty()) {
        try {
            this.vlService.addBatch(vertexLabels, client);
        } catch (Exception e) {
            this.pkService.removeBatch(propertyKeys, client);
            throw new ExternalException("schema.vertexlabel.reuse.failed", e);
        }
    }
    List<EdgeLabel> edgeLabels = this.filter(detail, client);
    if (!edgeLabels.isEmpty()) {
        try {
            this.addBatch(edgeLabels, client);
        } catch (Exception e) {
            this.vlService.removeBatch(vertexLabels, client);
            this.pkService.removeBatch(propertyKeys, client);
            throw new ExternalException("schema.edgelabel.reuse.failed", e);
        }
    }
    List<IndexLabel> indexLabels = this.piService.filter(detail, client);
    if (!indexLabels.isEmpty()) {
        try {
            this.piService.addBatch(indexLabels, client);
        } catch (Exception e) {
            this.removeBatch(edgeLabels, client);
            this.vlService.removeBatch(vertexLabels, client);
            this.pkService.removeBatch(propertyKeys, client);
            throw new ExternalException("schema.propertyindex.reuse.failed", e);
        }
    }
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) IndexLabel(com.baidu.hugegraph.structure.schema.IndexLabel) EdgeLabel(com.baidu.hugegraph.structure.schema.EdgeLabel) ExternalException(com.baidu.hugegraph.exception.ExternalException) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException)

Example 9 with PropertyKey

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

the class PropertyKeyService method reuse.

public void reuse(ConflictDetail detail, int connId) {
    // Assume that the conflict detail is valid
    Ex.check(!detail.hasConflict(), "schema.cannot-reuse-conflict");
    HugeClient client = this.client(connId);
    List<PropertyKey> propertyKeys = this.filter(detail, client);
    if (propertyKeys.isEmpty()) {
        return;
    }
    try {
        this.addBatch(propertyKeys, client);
    } catch (Exception e) {
        throw new ExternalException("schema.propertykey.reuse.failed", e);
    }
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) ExternalException(com.baidu.hugegraph.exception.ExternalException) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException)

Example 10 with PropertyKey

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

the class PropertyKeyAPI method create.

public PropertyKey.PropertyKeyWithTask create(PropertyKey propertyKey) {
    Object pkey = this.checkCreateOrUpdate(propertyKey);
    RestResult result = this.client.post(this.path(), pkey);
    if (this.client.apiVersionLt("0.65")) {
        return new PropertyKey.PropertyKeyWithTask(result.readObject(PropertyKey.class), 0L);
    }
    return result.readObject(PropertyKey.PropertyKeyWithTask.class);
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey)

Aggregations

PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)40 Test (org.junit.Test)27 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)7 HugeClient (com.baidu.hugegraph.driver.HugeClient)6 RestResult (com.baidu.hugegraph.rest.RestResult)6 ExternalException (com.baidu.hugegraph.exception.ExternalException)4 Date (java.util.Date)4 ServerException (com.baidu.hugegraph.exception.ServerException)3 PropertyKeyEntity (com.baidu.hugegraph.entity.schema.PropertyKeyEntity)2 Vertex (com.baidu.hugegraph.structure.graph.Vertex)2 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)2 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)2 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)1 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)1 InputSource (com.baidu.hugegraph.loader.source.InputSource)1 FileSource (com.baidu.hugegraph.loader.source.file.FileSource)1 ListFormat (com.baidu.hugegraph.loader.source.file.ListFormat)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)1 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)1