Search in sources :

Example 1 with LimitPredicate

use of com.google.gerrit.index.query.LimitPredicate in project gerrit by GerritCodeReview.

the class ChangeIndexRewriter method rewriteImpl.

/**
 * Rewrite a single predicate subtree.
 *
 * @param in predicate to rewrite.
 * @param index index whose schema determines which fields are indexed.
 * @param opts other query options.
 * @param leafTerms number of leaf index query terms encountered so far.
 * @return {@code null} if no part of this subtree can be queried in the index directly. {@code
 *     in} if this subtree and all its children can be queried directly in the index. Otherwise, a
 *     predicate that is semantically equivalent, with some of its subtrees wrapped to query the
 *     index directly.
 * @throws QueryParseException if the underlying index implementation does not support this
 *     predicate.
 */
private Predicate<ChangeData> rewriteImpl(Predicate<ChangeData> in, ChangeIndex index, QueryOptions opts, MutableInteger leafTerms) throws QueryParseException {
    in = IsSubmittablePredicate.rewrite(in);
    if (isIndexPredicate(in, index)) {
        if (++leafTerms.value > config.maxTerms()) {
            throw new TooManyTermsInQueryException();
        }
        return in;
    } else if (in instanceof LimitPredicate) {
        // and included that in their limit computation.
        return new LimitPredicate<>(ChangeQueryBuilder.FIELD_LIMIT, opts.limit());
    } else if (!isRewritePossible(in)) {
        if (in instanceof IndexPredicate) {
            throw new QueryParseException("Unsupported index predicate: " + in.toString());
        }
        // magic to indicate "in" cannot be rewritten
        return null;
    }
    int n = in.getChildCount();
    BitSet isIndexed = new BitSet(n);
    BitSet notIndexed = new BitSet(n);
    BitSet rewritten = new BitSet(n);
    BitSet changeSource = new BitSet(n);
    List<Predicate<ChangeData>> newChildren = Lists.newArrayListWithCapacity(n);
    for (int i = 0; i < n; i++) {
        Predicate<ChangeData> c = in.getChild(i);
        Predicate<ChangeData> nc = rewriteImpl(c, index, opts, leafTerms);
        if (isSameInstance(nc, c)) {
            isIndexed.set(i);
            newChildren.add(c);
        } else if (nc == null) /* cannot rewrite c */
        {
            notIndexed.set(i);
            newChildren.add(c);
        } else {
            if (nc instanceof ChangeDataSource) {
                changeSource.set(i);
            }
            rewritten.set(i);
            newChildren.add(nc);
        }
    }
    if (isIndexed.cardinality() == n) {
        // All children are indexed, leave as-is for parent.
        return in;
    } else if (notIndexed.cardinality() == n) {
        // Can't rewrite any children, so cannot rewrite in.
        return null;
    } else if (rewritten.cardinality() == n) {
        // All children were rewritten.
        if (changeSource.cardinality() == n) {
            return copy(in, newChildren);
        }
        return in.copy(newChildren);
    }
    return partitionChildren(in, newChildren, isIndexed, index, opts);
}
Also used : ChangeDataSource(com.google.gerrit.server.query.change.ChangeDataSource) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) IndexPredicate(com.google.gerrit.index.query.IndexPredicate) BitSet(java.util.BitSet) LimitPredicate(com.google.gerrit.index.query.LimitPredicate) ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.index.query.QueryParseException) IndexPredicate(com.google.gerrit.index.query.IndexPredicate) Predicate(com.google.gerrit.index.query.Predicate) OrPredicate(com.google.gerrit.index.query.OrPredicate) AndPredicate(com.google.gerrit.index.query.AndPredicate) NotPredicate(com.google.gerrit.index.query.NotPredicate) IsSubmittablePredicate(com.google.gerrit.server.query.change.IsSubmittablePredicate) ChangeStatusPredicate(com.google.gerrit.server.query.change.ChangeStatusPredicate) LimitPredicate(com.google.gerrit.index.query.LimitPredicate)

Aggregations

AndPredicate (com.google.gerrit.index.query.AndPredicate)1 IndexPredicate (com.google.gerrit.index.query.IndexPredicate)1 LimitPredicate (com.google.gerrit.index.query.LimitPredicate)1 NotPredicate (com.google.gerrit.index.query.NotPredicate)1 OrPredicate (com.google.gerrit.index.query.OrPredicate)1 Predicate (com.google.gerrit.index.query.Predicate)1 QueryParseException (com.google.gerrit.index.query.QueryParseException)1 TooManyTermsInQueryException (com.google.gerrit.index.query.TooManyTermsInQueryException)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 ChangeDataSource (com.google.gerrit.server.query.change.ChangeDataSource)1 ChangeStatusPredicate (com.google.gerrit.server.query.change.ChangeStatusPredicate)1 IsSubmittablePredicate (com.google.gerrit.server.query.change.IsSubmittablePredicate)1 BitSet (java.util.BitSet)1