Search in sources :

Example 26 with PropertyKey

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

the class PropertyKeyTest method testAddPropertyKeyWithUserData.

@Test
public void testAddPropertyKeyWithUserData() {
    SchemaManager schema = schema();
    PropertyKey age = schema.propertyKey("age").userdata("min", 0).userdata("max", 100).create();
    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).create();
    // 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")).create();
    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 : SchemaManager(com.baidu.hugegraph.driver.SchemaManager) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Date(java.util.Date) Test(org.junit.Test)

Example 27 with PropertyKey

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

the class VertexApiTest method testOlapPropertyWrite.

@Test
public void testOlapPropertyWrite() {
    List<Vertex> vertices = super.create100PersonBatch();
    List<Object> ids = vertexAPI.create(vertices);
    // Create olap property key
    PropertyKey pagerank = schema().propertyKey("pagerank").asDouble().writeType(WriteType.OLAP_RANGE).build();
    PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
    propertyKeyWithTask = propertyKeyAPI.create(pagerank);
    long taskId1 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId1);
    PropertyKey wcc = schema().propertyKey("wcc").asText().writeType(WriteType.OLAP_SECONDARY).build();
    propertyKeyWithTask = propertyKeyAPI.create(wcc);
    long taskId2 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId2);
    PropertyKey none = schema().propertyKey("none").asText().writeType(WriteType.OLAP_COMMON).build();
    propertyKeyWithTask = propertyKeyAPI.create(none);
    long taskId3 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Add olap properties
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("pagerank", 0.1D * i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("wcc", "wcc" + i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("none", "none" + i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    // Query vertices by id before set graph read mode to 'ALL'
    for (int i = 0; i < 100; i++) {
        Vertex person = vertexAPI.get(ids.get(i));
        Assert.assertEquals("person", person.label());
        Map<String, Object> props = ImmutableMap.of("name", "Person-" + i, "city", "Beijing", "age", 30);
        Assert.assertEquals(props, person.properties());
    }
    // Set graph read mode to 'ALL'
    graphsAPI.readMode("hugegraph", GraphReadMode.ALL);
    // Query vertices by id after set graph read mode to 'ALL'
    for (int i = 0; i < 100; i++) {
        Vertex person = vertexAPI.get(ids.get(i));
        Assert.assertEquals("person", person.label());
        Map<String, Object> props = ImmutableMap.<String, Object>builder().put("name", "Person-" + i).put("city", "Beijing").put("age", 30).put("pagerank", 0.1D * i).put("wcc", "wcc" + i).put("none", "none" + i).build();
        Assert.assertEquals(props, person.properties());
    }
    // Query vertices by olap properties
    GremlinRequest request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(50, resultSet.size());
    request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(2, resultSet.size());
    // Clear olap property key
    propertyKeyWithTask = propertyKeyAPI.clear(pagerank);
    taskId1 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId1);
    propertyKeyWithTask = propertyKeyAPI.clear(wcc);
    taskId2 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId2);
    propertyKeyWithTask = propertyKeyAPI.clear(none);
    taskId3 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Query after clear olap property key
    request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
    request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
    // Delete olap property key
    taskId1 = propertyKeyAPI.delete(pagerank.name());
    Assert.assertNotEquals(0L, taskId1);
    taskId2 = propertyKeyAPI.delete(wcc.name());
    Assert.assertNotEquals(0L, taskId2);
    taskId3 = propertyKeyAPI.delete(none.name());
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Query after delete olap property key
    Assert.assertThrows(ServerException.class, () -> {
        gremlin().execute(new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))"));
    }, e -> {
        Assert.assertContains("Undefined property key: 'pagerank'", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        gremlin().execute(new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))"));
    }, e -> {
        Assert.assertContains("Undefined property key: 'wcc'", e.getMessage());
    });
    // Resume graph read mode to 'OLTP_ONLY'
    graphsAPI.readMode("hugegraph", GraphReadMode.OLTP_ONLY);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Test(org.junit.Test)

Example 28 with PropertyKey

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

the class PropertyKeyApiTest method testCreateWithDataType.

@Test
public void testCreateWithDataType() {
    PropertyKey propertyKey = schema().propertyKey("name").dataType(DataType.LONG).valueSingle().build();
    PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
    propertyKeyWithTask = propertyKeyAPI.create(propertyKey);
    Assert.assertEquals(0L, propertyKeyWithTask.taskId());
    propertyKey = propertyKeyWithTask.propertyKey();
    Assert.assertEquals("name", propertyKey.name());
    Assert.assertEquals(DataType.LONG, propertyKey.dataType());
    Assert.assertEquals(Cardinality.SINGLE, propertyKey.cardinality());
}
Also used : PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Test(org.junit.Test)

Example 29 with PropertyKey

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

the class PropertyKeyApiTest method testCreateWithInvalidName.

@Test
public void testCreateWithInvalidName() {
    Utils.assertResponseError(400, () -> {
        propertyKeyAPI.create(new PropertyKey(""));
    });
    Utils.assertResponseError(400, () -> {
        propertyKeyAPI.create(new PropertyKey(" "));
    });
    Utils.assertResponseError(400, () -> {
        propertyKeyAPI.create(new PropertyKey("    "));
    });
}
Also used : PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Test(org.junit.Test)

Example 30 with PropertyKey

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

the class PropertyKeyApiTest method testCreateExistedPropertyKey.

@Test
public void testCreateExistedPropertyKey() {
    PropertyKey propertyKey = new PropertyKey("name");
    propertyKeyAPI.create(propertyKey);
    Utils.assertResponseError(400, () -> {
        propertyKeyAPI.create(new PropertyKey("name"));
    });
}
Also used : PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Test(org.junit.Test)

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