Search in sources :

Example 1 with GraphAttachable

use of com.baidu.hugegraph.structure.constant.GraphAttachable in project incubator-hugegraph-toolchain by apache.

the class ResultSet method get.

public Result get(int index) {
    if (index >= this.data.size()) {
        return null;
    }
    Object object = this.data().get(index);
    if (object == null) {
        return null;
    }
    Class<?> clazz = this.parseResultClass(object);
    // Primitive type
    if (clazz.equals(object.getClass())) {
        return new Result(object);
    }
    try {
        String rawValue = MAPPER.writeValueAsString(object);
        object = MAPPER.readValue(rawValue, clazz);
        if (object instanceof GraphAttachable) {
            ((GraphAttachable) object).attachManager(graphManager);
        }
        return new Result(object);
    } catch (Exception e) {
        throw new SerializeException("Failed to deserialize: %s", e, object);
    }
}
Also used : GraphAttachable(com.baidu.hugegraph.structure.constant.GraphAttachable) SerializeException(com.baidu.hugegraph.rest.SerializeException) SerializeException(com.baidu.hugegraph.rest.SerializeException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with GraphAttachable

use of com.baidu.hugegraph.structure.constant.GraphAttachable in project incubator-hugegraph-toolchain by apache.

the class GremlinApiTest method testAttachedManager.

@Test
public void testAttachedManager() {
    GremlinRequest request = new GremlinRequest("g.V()");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    Iterator<Result> results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Vertex.class, object.getClass());
        Vertex vertex = (Vertex) object;
        Assert.assertNotNull(Whitebox.getInternalState(vertex, "manager"));
    }
    request = new GremlinRequest("g.E()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Edge.class, object.getClass());
        Edge edge = (Edge) object;
        Assert.assertNotNull(Whitebox.getInternalState(edge, "manager"));
    }
    request = new GremlinRequest("g.V().outE().path()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Path.class, object.getClass());
        Path path = (Path) object;
        Assert.assertNotNull(path.objects());
        for (Object pathObject : path.objects()) {
            Assert.assertTrue(pathObject instanceof GraphAttachable);
            Assert.assertNotNull(Whitebox.getInternalState(pathObject, "manager"));
        }
        Assert.assertNull(path.crosspoint());
    }
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) GraphAttachable(com.baidu.hugegraph.structure.constant.GraphAttachable) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) Test(org.junit.Test)

Aggregations

GraphAttachable (com.baidu.hugegraph.structure.constant.GraphAttachable)2 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)1 SerializeException (com.baidu.hugegraph.rest.SerializeException)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1 Path (com.baidu.hugegraph.structure.graph.Path)1 Vertex (com.baidu.hugegraph.structure.graph.Vertex)1 Result (com.baidu.hugegraph.structure.gremlin.Result)1 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)1 NoSuchElementException (java.util.NoSuchElementException)1 Test (org.junit.Test)1