Search in sources :

Example 1 with IndexPredicate

use of com.google.gerrit.server.index.IndexPredicate 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 {
    if (isIndexPredicate(in, index)) {
        if (++leafTerms.value > config.maxTerms()) {
            throw new QueryParseException("too many terms in query");
        }
        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);
    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 (nc == c) {
            isIndexed.set(i);
            newChildren.add(c);
        } else if (nc == null) /* cannot rewrite c */
        {
            notIndexed.set(i);
            newChildren.add(c);
        } else {
            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.
        return in.copy(newChildren);
    }
    return partitionChildren(in, newChildren, isIndexed, index, opts);
}
Also used : IndexPredicate(com.google.gerrit.server.index.IndexPredicate) BitSet(java.util.BitSet) LimitPredicate(com.google.gerrit.server.query.LimitPredicate) ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.server.query.QueryParseException) OrPredicate(com.google.gerrit.server.query.OrPredicate) ChangeStatusPredicate(com.google.gerrit.server.query.change.ChangeStatusPredicate) Predicate(com.google.gerrit.server.query.Predicate) LimitPredicate(com.google.gerrit.server.query.LimitPredicate) NotPredicate(com.google.gerrit.server.query.NotPredicate) IndexPredicate(com.google.gerrit.server.index.IndexPredicate) AndPredicate(com.google.gerrit.server.query.AndPredicate)

Example 2 with IndexPredicate

use of com.google.gerrit.server.index.IndexPredicate in project gerrit by GerritCodeReview.

the class ChangeIndexRewriter method rewriteImpl.

private Predicate<ChangeData> rewriteImpl(Predicate<ChangeData> in, QueryOptions opts) throws QueryParseException {
    ChangeIndex index = indexes.getSearchIndex();
    MutableInteger leafTerms = new MutableInteger();
    Predicate<ChangeData> out = rewriteImpl(in, index, opts, leafTerms);
    if (in == out || out instanceof IndexPredicate) {
        return new IndexedChangeQuery(index, out, opts);
    } else if (out == null) /* cannot rewrite */
    {
        return in;
    } else {
        return out;
    }
}
Also used : IndexPredicate(com.google.gerrit.server.index.IndexPredicate) MutableInteger(org.eclipse.jgit.util.MutableInteger) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Aggregations

IndexPredicate (com.google.gerrit.server.index.IndexPredicate)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 AndPredicate (com.google.gerrit.server.query.AndPredicate)1 LimitPredicate (com.google.gerrit.server.query.LimitPredicate)1 NotPredicate (com.google.gerrit.server.query.NotPredicate)1 OrPredicate (com.google.gerrit.server.query.OrPredicate)1 Predicate (com.google.gerrit.server.query.Predicate)1 QueryParseException (com.google.gerrit.server.query.QueryParseException)1 ChangeStatusPredicate (com.google.gerrit.server.query.change.ChangeStatusPredicate)1 BitSet (java.util.BitSet)1 MutableInteger (org.eclipse.jgit.util.MutableInteger)1