use of com.baidu.hugegraph.entity.query.GraphView in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method expandVertex.
public GremlinResult expandVertex(int connId, AdjacentQuery query) {
HugeClient client = this.getClient(connId);
// Build gremlin query
String gremlin = this.buildGremlinQuery(connId, query);
log.debug("expand vertex gremlin ==> {}", gremlin);
// Execute gremlin query
ResultSet resultSet = this.executeGremlin(gremlin, client);
List<Vertex> vertices = new ArrayList<>(resultSet.size());
List<Edge> edges = new ArrayList<>(resultSet.size());
for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
Path path = iter.next().getPath();
List<Object> objects = path.objects();
assert objects.size() == 3;
Edge edge = (Edge) objects.get(1);
Vertex vertex = (Vertex) objects.get(2);
// Filter vertices and edges that existed in query
if (query.retainEdge(edge)) {
edges.add(edge);
}
if (query.retainVertex(vertex)) {
vertices.add(vertex);
}
}
// Build graph view
GraphView graphView = new GraphView(vertices, edges);
return GremlinResult.builder().type(Type.PATH).graphView(graphView).build();
}
use of com.baidu.hugegraph.entity.query.GraphView in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method executeQuery.
public GremlinResult executeQuery(int connId, GremlinQuery query) {
HugeClient client = this.getClient(connId);
log.debug("The original gremlin ==> {}", query.getContent());
String gremlin = this.optimize(query.getContent());
log.debug("The optimized gremlin ==> {}", gremlin);
// Execute gremlin query
ResultSet resultSet = this.executeGremlin(gremlin, client);
// Scan data, vote the result type
TypedResult typedResult = this.parseResults(resultSet);
// Build json view
JsonView jsonView = new JsonView(typedResult.getData());
// Build table view
TableView tableView = this.buildTableView(typedResult);
// Build graph view
GraphView graphView = this.buildGraphView(typedResult, client);
return GremlinResult.builder().type(typedResult.getType()).jsonView(jsonView).tableView(tableView).graphView(graphView).build();
}
use of com.baidu.hugegraph.entity.query.GraphView in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method buildGraphView.
private GraphView buildGraphView(TypedResult result, HugeClient client) {
List<Object> data = result.getData();
if (!result.getType().isGraph() || CollectionUtils.isEmpty(data)) {
return GraphView.EMPTY;
}
Map<Object, Vertex> vertices = new HashMap<>();
Map<String, Edge> edges = new HashMap<>();
for (Object object : data) {
if (object instanceof Vertex) {
Vertex vertex = (Vertex) object;
vertices.put(vertex.id(), vertex);
} else if (object instanceof Edge) {
Edge edge = (Edge) object;
edges.put(edge.id(), edge);
} else if (object instanceof Path) {
List<Object> elements = ((Path) object).objects();
for (Object element : elements) {
if (element instanceof Vertex) {
Vertex vertex = (Vertex) element;
vertices.put(vertex.id(), vertex);
} else if (element instanceof Edge) {
Edge edge = (Edge) element;
edges.put(edge.id(), edge);
} else {
return GraphView.EMPTY;
}
}
}
}
if (!edges.isEmpty()) {
if (vertices.isEmpty()) {
vertices = this.verticesOfEdge(result, edges, client);
} else {
// TODO: reduce the number of requests
vertices.putAll(this.verticesOfEdge(result, edges, client));
}
} else {
if (!vertices.isEmpty()) {
edges = this.edgesOfVertex(result, vertices, client);
}
}
if (!edges.isEmpty()) {
Ex.check(!vertices.isEmpty(), "gremlin.edges.linked-vertex.not-exist");
}
return new GraphView(vertices.values(), edges.values());
}
use of com.baidu.hugegraph.entity.query.GraphView in project incubator-hugegraph-toolchain by apache.
the class OltpAlgoService method shortestPath.
public GremlinResult shortestPath(int connId, ShortestPath body) {
HugeClient client = this.getClient(connId);
TraverserManager traverser = client.traverser();
Path result = traverser.shortestPath(body.getSource(), body.getTarget(), body.getDirection(), body.getLabel(), body.getMaxDepth(), body.getMaxDegree(), body.getSkipDegree(), body.getCapacity());
JsonView jsonView = new JsonView();
jsonView.setData(result.objects());
Date createTime = HubbleUtil.nowDate();
TableView tableView = this.buildPathTableView(result);
GraphView graphView = this.buildPathGraphView(result);
// Insert execute history
ExecuteStatus status = ExecuteStatus.SUCCESS;
ExecuteHistory history;
history = new ExecuteHistory(null, connId, 0L, ExecuteType.ALGORITHM, body.toString(), status, AsyncTaskStatus.UNKNOWN, -1L, createTime);
this.historyService.save(history);
return GremlinResult.builder().type(GremlinResult.Type.PATH).jsonView(jsonView).tableView(tableView).graphView(graphView).build();
}
use of com.baidu.hugegraph.entity.query.GraphView in project incubator-hugegraph-toolchain by apache.
the class OltpAlgoService method buildPathGraphView.
private GraphView buildPathGraphView(Path result) {
Map<Object, Vertex> vertices = new HashMap<>();
Map<String, Edge> edges = new HashMap<>();
List<Object> elements = result.objects();
for (Object element : elements) {
if (element instanceof Vertex) {
Vertex vertex = (Vertex) element;
vertices.put(vertex.id(), vertex);
} else if (element instanceof Edge) {
Edge edge = (Edge) element;
edges.put(edge.id(), edge);
} else {
return GraphView.EMPTY;
}
}
return new GraphView(vertices.values(), new ArrayList<>());
}
Aggregations