Search in sources :

Example 1 with QueryResults

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());
}
Also used : OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) QueryResults(com.baidu.hugegraph.backend.query.QueryResults)

Example 2 with QueryResults

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();
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) RateLimiter(com.google.common.util.concurrent.RateLimiter) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

QueryResults (com.baidu.hugegraph.backend.query.QueryResults)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)1 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)1 OptimizedType (com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType)1 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)1 Query (com.baidu.hugegraph.backend.query.Query)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1