Search in sources :

Example 6 with QueryParseException

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

the class QueryGroups method apply.

@Override
public Response<List<GroupInfo>> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, PermissionBackendException {
    if (Strings.isNullOrEmpty(query)) {
        throw new BadRequestException("missing query field");
    }
    GroupQueryProcessor queryProcessor = queryProcessorProvider.get();
    if (queryProcessor.isDisabled()) {
        throw new MethodNotAllowedException("query disabled");
    }
    if (start != 0) {
        queryProcessor.setStart(start);
    }
    if (limit != 0) {
        queryProcessor.setUserProvidedLimit(limit);
    }
    try {
        QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query));
        List<InternalGroup> groups = result.entities();
        ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
        json.addOptions(options);
        for (InternalGroup group : groups) {
            groupInfos.add(json.format(new InternalGroupDescription(group)));
        }
        if (!groupInfos.isEmpty() && result.more()) {
            groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
        }
        return Response.ok(groupInfos);
    } catch (QueryParseException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) GroupQueryProcessor(com.google.gerrit.server.query.group.GroupQueryProcessor) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InternalGroup(com.google.gerrit.entities.InternalGroup) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 7 with QueryParseException

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

the class QueryChanges method query.

private List<List<ChangeInfo>> query() throws QueryParseException, PermissionBackendException {
    ChangeQueryProcessor queryProcessor = queryProcessorProvider.get();
    if (queryProcessor.isDisabled()) {
        throw new QueryParseException("query disabled");
    }
    if (limit != null) {
        queryProcessor.setUserProvidedLimit(limit);
    }
    if (start != null) {
        queryProcessor.setStart(start);
    }
    if (noLimit != null) {
        queryProcessor.setNoLimit(noLimit);
    }
    if (skipVisibility != null) {
        queryProcessor.enforceVisibility(!skipVisibility);
    }
    dynamicBeans.forEach((p, b) -> queryProcessor.setDynamicBean(p, b));
    if (queries == null || queries.isEmpty()) {
        queries = Collections.singletonList("status:open");
    } else if (queries.size() > 10) {
        // users from submitting too much to the server in a single call.
        throw new QueryParseException("limit of 10 queries");
    }
    int cnt = queries.size();
    List<QueryResult<ChangeData>> results = queryProcessor.query(qb.parse(queries));
    List<List<ChangeInfo>> res = json.create(options, queryProcessor.getInfosFactory()).format(results);
    for (int n = 0; n < cnt; n++) {
        List<ChangeInfo> info = res.get(n);
        if (results.get(n).more() && !info.isEmpty()) {
            Iterables.getLast(info)._moreChanges = true;
        }
    }
    return res;
}
Also used : QueryResult(com.google.gerrit.index.query.QueryResult) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ChangeQueryProcessor(com.google.gerrit.server.query.change.ChangeQueryProcessor) ArrayList(java.util.ArrayList) List(java.util.List) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 8 with QueryParseException

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

the class ChangeIndexRewriterTest method unsupportedIndexOperator.

@Test
public void unsupportedIndexOperator() throws Exception {
    Predicate<ChangeData> in = parse("status:merged file:a");
    assertThat(rewrite(in)).isEqualTo(query(in));
    indexes.setSearchIndex(new FakeChangeIndex(FakeChangeIndex.V1));
    QueryParseException thrown = assertThrows(QueryParseException.class, () -> rewrite(in));
    assertThat(thrown).hasMessageThat().contains("Unsupported index predicate: file:a");
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.index.query.QueryParseException) Test(org.junit.Test)

Example 9 with QueryParseException

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

the class ChangeIndexRewriterTest method tooManyTerms.

@Test
public void tooManyTerms() throws Exception {
    String q = "file:a OR file:b OR file:c";
    Predicate<ChangeData> in = parse(q);
    assertEquals(query(in), rewrite(in));
    QueryParseException thrown = assertThrows(QueryParseException.class, () -> rewrite(parse(q + " OR file:d")));
    assertThat(thrown).hasMessageThat().contains("too many terms in query");
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData) QueryParseException(com.google.gerrit.index.query.QueryParseException) Test(org.junit.Test)

Example 10 with QueryParseException

use of com.google.gerrit.index.query.QueryParseException 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

QueryParseException (com.google.gerrit.index.query.QueryParseException)30 Account (com.google.gerrit.entities.Account)7 StorageException (com.google.gerrit.exceptions.StorageException)6 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 ArrayList (java.util.ArrayList)6 Predicate (com.google.gerrit.index.query.Predicate)5 AccountState (com.google.gerrit.server.account.AccountState)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 IOException (java.io.IOException)5 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)5 Repository (org.eclipse.jgit.lib.Repository)4 FluentLogger (com.google.common.flogger.FluentLogger)3 GroupReference (com.google.gerrit.entities.GroupReference)3 Project (com.google.gerrit.entities.Project)3 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)3 LimitPredicate (com.google.gerrit.index.query.LimitPredicate)3 Inject (com.google.inject.Inject)3 Provider (com.google.inject.Provider)3 Collections (java.util.Collections)3 List (java.util.List)3