Search in sources :

Example 6 with GremlinRequest

use of com.baidu.hugegraph.api.gremlin.GremlinRequest in project incubator-hugegraph-toolchain by apache.

the class GremlinApiTest method testAsyncRemoveAllEdges.

@Test
public void testAsyncRemoveAllEdges() {
    GremlinRequest request = new GremlinRequest("g.E()");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    String gremlin = "g.E().drop()";
    request = new GremlinRequest(gremlin);
    long id = gremlin().executeAsTask(request);
    waitUntilTaskCompleted(id);
    request = new GremlinRequest("g.E()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
}
Also used : ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Test(org.junit.Test)

Example 7 with GremlinRequest

use of com.baidu.hugegraph.api.gremlin.GremlinRequest in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method executeAsyncTask.

public Long executeAsyncTask(int connId, GremlinQuery query) {
    HugeClient client = this.getClient(connId);
    log.debug("The async gremlin ==> {}", query.getContent());
    // Execute optimized gremlin query
    GremlinRequest request = new GremlinRequest(query.getContent());
    return client.gremlin().executeAsTask(request);
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest)

Example 8 with GremlinRequest

use of com.baidu.hugegraph.api.gremlin.GremlinRequest in project incubator-hugegraph-toolchain by apache.

the class TaskApiTest method testCancel.

@Test
public void testCancel() {
    schema().vertexLabel("man").useAutomaticId().ifNotExist().create();
    String groovy = "for (int i = 0; i < 10; i++) {" + "hugegraph.addVertex(T.label, 'man');" + "hugegraph.tx().commit();" + "}";
    // Insert 10 records in sync mode
    GremlinRequest request = new GremlinRequest(groovy);
    gremlin().execute(request);
    // Verify insertion takes effect
    groovy = "g.V()";
    request = new GremlinRequest(groovy);
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(10, resultSet.size());
    // Delete to prepare for insertion in async mode
    groovy = "g.V().drop()";
    request = new GremlinRequest(groovy);
    gremlin().execute(request);
    /*
         * The asyn task scripts need to be able to handle interrupts,
         * otherwise they cannot be cancelled
         */
    groovy = "for (int i = 0; i < 10; i++) {" + "hugegraph.addVertex(T.label, 'man');" + "hugegraph.tx().commit();" + "try {" + "sleep(1000);" + "} catch (InterruptedException e) {" + "break;" + "}" + "}";
    request = new GremlinRequest(groovy);
    long taskId = gremlin().executeAsTask(request);
    groovy = "g.V()";
    request = new GremlinRequest(groovy);
    // Wait async task running
    while (true) {
        resultSet = gremlin().execute(request);
        if (resultSet.size() > 0) {
            break;
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignored) {
            }
        }
    }
    // Cancel async task
    Task task = taskAPI.cancel(taskId);
    Assert.assertTrue(task.cancelling());
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
    // ignored
    }
    task = taskAPI.get(taskId);
    Assert.assertTrue(task.cancelled());
    resultSet = gremlin().execute(request);
    Assert.assertTrue(resultSet.size() < 10);
}
Also used : Task(com.baidu.hugegraph.structure.Task) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Test(org.junit.Test)

Example 9 with GremlinRequest

use of com.baidu.hugegraph.api.gremlin.GremlinRequest 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 10 with GremlinRequest

use of com.baidu.hugegraph.api.gremlin.GremlinRequest in project incubator-hugegraph-toolchain by apache.

the class GremlinApiTest method testAsyncRemoveAllVertices.

@Test
public void testAsyncRemoveAllVertices() {
    GremlinRequest request = new GremlinRequest("g.V()");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    String gremlin = "hugegraph.traversal().V().drop()";
    request = new GremlinRequest(gremlin);
    long id = gremlin().executeAsTask(request);
    waitUntilTaskCompleted(id);
    request = new GremlinRequest("g.V()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
}
Also used : ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Test(org.junit.Test)

Aggregations

GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)12 Test (org.junit.Test)11 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)9 Vertex (com.baidu.hugegraph.structure.graph.Vertex)2 Result (com.baidu.hugegraph.structure.gremlin.Result)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 Task (com.baidu.hugegraph.structure.Task)1 GraphAttachable (com.baidu.hugegraph.structure.constant.GraphAttachable)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1 Path (com.baidu.hugegraph.structure.graph.Path)1 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)1