use of com.baidu.hugegraph.entity.query.GremlinResult.Type in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method parseResults.
private TypedResult parseResults(ResultSet resultSet) {
if (resultSet == null) {
return new TypedResult(Type.EMPTY, null);
}
Iterator<Result> iter = resultSet.iterator();
if (!iter.hasNext()) {
return new TypedResult(Type.EMPTY, null);
}
Map<Type, Integer> typeVotes = new HashMap<>();
List<Object> typedData = new ArrayList<>(resultSet.size());
while (iter.hasNext()) {
Result result = iter.next();
if (result == null) {
// NOTE: null value doesn't vote
continue;
}
Object object = result.getObject();
Type type;
if (object instanceof Vertex) {
type = Type.VERTEX;
} else if (object instanceof Edge) {
type = Type.EDGE;
} else if (object instanceof Path) {
type = Type.PATH;
} else {
type = Type.GENERAL;
}
typeVotes.compute(type, (k, v) -> v == null ? 1 : v + 1);
typedData.add(object);
}
Type type;
if (typeVotes.isEmpty()) {
type = Type.EMPTY;
} else {
// Find the key with max value
type = Collections.max(typeVotes.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
}
return new TypedResult(type, typedData);
}
use of com.baidu.hugegraph.entity.query.GremlinResult.Type 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;
}
Aggregations