use of com.baidu.hugegraph.backend.query.QueryResults in project incubator-hugegraph by apache.
the class GraphTransaction method queryNumber.
@Override
public Number queryNumber(Query query) {
E.checkArgument(!this.hasUpdate(), "It's not allowed to query number when " + "there are uncommitted records.");
if (!(query instanceof ConditionQuery)) {
return super.queryNumber(query);
}
Aggregate aggregate = query.aggregateNotNull();
QueryList<Number> queries = this.optimizeQueries(query, q -> {
boolean indexQuery = q.getClass() == IdQuery.class;
OptimizedType optimized = ((ConditionQuery) query).optimized();
Number result;
if (!indexQuery) {
result = super.queryNumber(q);
} else {
E.checkArgument(aggregate.func() == AggregateFunc.COUNT, "The %s operator on index is not supported now", aggregate.func().string());
if (this.optimizeAggrByIndex && optimized == OptimizedType.INDEX) {
// The ids size means results count (assume no left index)
result = q.idsSize();
} else {
assert optimized == OptimizedType.INDEX_FILTER || optimized == OptimizedType.INDEX;
assert q.resultType().isVertex() || q.resultType().isEdge();
result = IteratorUtils.count(q.resultType().isVertex() ? this.queryVertices(q) : this.queryEdges(q));
}
}
return new QueryResults<>(IteratorUtils.of(result), q);
});
QueryResults<Number> results = queries.empty() ? QueryResults.empty() : queries.fetch(this.pageSize);
return aggregate.reduce(results.iterator());
}
use of com.baidu.hugegraph.backend.query.QueryResults in project incubator-hugegraph by apache.
the class AbstractTransaction method query.
@Watched(prefix = "tx")
public QueryResults<BackendEntry> query(Query query) {
LOG.debug("Transaction query: {}", query);
/*
* NOTE: it's dangerous if an IdQuery/ConditionQuery is empty
* check if the query is empty and its class is not the Query itself
*/
if (query.empty() && !query.getClass().equals(Query.class)) {
throw new BackendException("Query without any id or condition");
}
Query squery = this.serializer.writeQuery(query);
// Do rate limit if needed
RateLimiter rateLimiter = this.graph.readRateLimiter();
if (rateLimiter != null && query.resultType().isGraph()) {
double time = rateLimiter.acquire(1);
if (time > 0) {
LOG.debug("Waited for {}s to query", time);
}
BackendEntryIterator.checkInterrupted();
}
this.beforeRead();
try {
this.injectOlapPkIfNeeded(squery);
return new QueryResults<>(this.store.query(squery), query);
} finally {
// TODO: not complete the iteration currently
this.afterRead();
}
}
Aggregations