use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method edgesOfVertex.
private Map<String, Edge> edgesOfVertex(TypedResult result, Map<Object, Vertex> vertices, HugeClient client) {
final HugeConfig config = this.config;
int batchSize = config.get(HubbleOptions.GREMLIN_BATCH_QUERY_IDS);
int edgeLimit = config.get(HubbleOptions.GREMLIN_EDGES_TOTAL_LIMIT);
int degreeLimit = config.get(HubbleOptions.GREMLIN_VERTEX_DEGREE_LIMIT);
Set<Object> vertexIds = vertices.keySet();
Map<String, Edge> edges = new HashMap<>(vertexIds.size());
Iterables.partition(vertexIds, batchSize).forEach(batch -> {
List<String> escapedIds = batch.stream().map(GremlinUtil::escapeId).collect(Collectors.toList());
String ids = StringUtils.join(escapedIds, ",");
// Any better way to find two vertices has linked?
String gremlin;
if (result.getType().isPath()) {
// If result type is path, the vertices count not too much in theory
gremlin = String.format("g.V(%s).bothE().local(limit(%s)).dedup()", ids, degreeLimit);
} else {
gremlin = String.format("g.V(%s).bothE().dedup().limit(%s)", ids, edgeLimit);
}
ResultSet resultSet = client.gremlin().gremlin(gremlin).execute();
// The edges count for per vertex
Map<Object, Integer> degrees = new HashMap<>(resultSet.size());
for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
Edge edge = iter.next().getEdge();
Object source = edge.sourceId();
Object target = edge.targetId();
// only add the interconnected edges of the found vertices
if (!vertexIds.contains(source) || !vertexIds.contains(target)) {
continue;
}
edges.put(edge.id(), edge);
if (edges.size() >= edgeLimit) {
break;
}
int deg = degrees.compute(source, (k, v) -> v == null ? 1 : v + 1);
if (deg >= degreeLimit) {
break;
}
deg = degrees.compute(target, (k, v) -> v == null ? 1 : v + 1);
if (deg >= degreeLimit) {
break;
}
}
});
return edges;
}
use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.
the class RestResultTest method testReadGremlinVertices.
@Test
public void testReadGremlinVertices() {
String json = "{" + "\"requestId\": \"b0fd8ead-333f-43ac-97b0-4d78784726ae\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Beijing\",\"Wuhan\",\"Beijing\"]," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}," + "{" + "\"id\": \"software:lop\"," + "\"label\": \"software\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"price\": 328," + "\"name\": \"lop\"," + "\"lang\": [\"java\",\"python\",\"c++\"]" + "}" + "}," + "{" + "\"id\": \"person:peter\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Shanghai\"]," + "\"name\": \"peter\"," + "\"age\": 35" + "}" + "}" + "]," + "\"meta\": {}" + "}" + "}";
Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
RestResult restResult = new RestResult(this.mockResponse);
Assert.assertEquals(200, restResult.status());
Assert.assertNull(restResult.headers());
Response response = restResult.readObject(Response.class);
response.graphManager(graph());
Assert.assertEquals("b0fd8ead-333f-43ac-97b0-4d78784726ae", response.requestId());
Assert.assertEquals(200, response.status().code());
Vertex marko = new Vertex("person");
marko.id("person:marko");
marko.property("name", "marko");
marko.property("city", ImmutableList.of("Beijing", "Wuhan", "Beijing"));
marko.property("age", 29);
Vertex lop = new Vertex("software");
lop.id("software:lop");
lop.property("name", "lop");
lop.property("lang", ImmutableList.of("java", "python", "c++"));
lop.property("price", 328);
Vertex peter = new Vertex("person");
peter.id("person:peter");
peter.property("name", "peter");
peter.property("city", ImmutableList.of("Shanghai"));
peter.property("age", 35);
List<Vertex> vertices = new ArrayList<>(3);
vertices.add(peter);
vertices.add(marko);
vertices.add(lop);
Iterator<Result> results = response.result().iterator();
while (results.hasNext()) {
Result result = results.next();
Assert.assertEquals(Vertex.class, result.getObject().getClass());
Vertex vertex = result.getVertex();
Assert.assertTrue(Utils.contains(vertices, vertex));
}
}
use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.
the class EdgeTest method testQueryByPagingAndFiltering.
@Test
public void testQueryByPagingAndFiltering() {
SchemaManager schema = schema();
schema.propertyKey("no").asText().create();
schema.propertyKey("location").asText().create();
schema.propertyKey("callType").asText().create();
schema.propertyKey("calltime").asDate().create();
schema.propertyKey("duration").asInt().create();
schema.vertexLabel("phone").properties("no").primaryKeys("no").enableLabelIndex(false).create();
schema.edgeLabel("call").multiTimes().properties("location", "callType", "duration", "calltime").sourceLabel("phone").targetLabel("phone").sortKeys("location", "callType", "duration", "calltime").create();
Vertex v1 = graph().addVertex(T.label, "phone", "no", "13812345678");
Vertex v2 = graph().addVertex(T.label, "phone", "no", "13866668888");
Vertex v10086 = graph().addVertex(T.label, "phone", "no", "10086");
v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-1 23:00:00");
v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-2 12:00:01");
v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-3 12:08:02");
v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 8, "calltime", "2017-5-3 22:22:03");
v1.addEdge("call", v2, "location", "Beijing", "callType", "fun", "duration", 10, "calltime", "2017-5-4 20:33:04");
v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 12, "calltime", "2017-5-2 15:30:05");
v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 14, "calltime", "2017-5-3 14:56:06");
v2.addEdge("call", v10086, "location", "Nanjing", "callType", "fun", "duration", 15, "calltime", "2017-5-3 17:28:07");
ResultSet resultSet = gremlin().gremlin("g.V(vid).outE('call')" + ".has('location', 'Beijing')" + ".has('callType', 'work')" + ".has('duration', 3)" + ".has('calltime', " + "P.between('2017-5-2', " + "'2017-5-4'))" + ".toList()").binding("vid", v1.id()).execute();
Iterator<Result> results = resultSet.iterator();
Assert.assertEquals(2, Iterators.size(results));
Assert.assertThrows(ServerException.class, () -> {
// no location
gremlin().gremlin("g.V(vid).outE('call').has('callType', 'work')" + ".has('duration', 3).has('calltime', " + "P.between('2017-5-2', '2017-5-4'))" + ".has('~page', '')").binding("vid", v1.id()).execute();
}, e -> {
Assert.assertContains("Can't query by paging and filtering", e.getMessage());
});
}
use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testPrimitiveObject.
@Test
public void testPrimitiveObject() {
GremlinRequest request = new GremlinRequest("1 + 2");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(1, resultSet.size());
Iterator<Result> results = resultSet.iterator();
while (results.hasNext()) {
Result result = results.next();
Object object = result.getObject();
Assert.assertEquals(Integer.class, object.getClass());
Assert.assertEquals(3, object);
}
}
use of com.baidu.hugegraph.structure.gremlin.Result 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());
}
}
Aggregations