Search in sources :

Example 1 with QueryParseException

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

the class ReviewerRecommender method baseRankingForEmptyQuery.

private Map<Account.Id, MutableDouble> baseRankingForEmptyQuery(double baseWeight) throws OrmException {
    // Get the user's last 25 changes, check approvals
    try {
        List<ChangeData> result = internalChangeQuery.setLimit(25).setRequestedFields(ImmutableSet.of(ChangeField.REVIEWER.getName())).query(changeQueryBuilder.owner("self"));
        Map<Account.Id, MutableDouble> suggestions = new HashMap<>();
        for (ChangeData cd : result) {
            for (PatchSetApproval approval : cd.currentApprovals()) {
                Account.Id id = approval.getAccountId();
                if (suggestions.containsKey(id)) {
                    suggestions.get(id).add(baseWeight);
                } else {
                    suggestions.put(id, new MutableDouble(baseWeight));
                }
            }
        }
        return suggestions;
    } catch (QueryParseException e) {
        // Unhandled, because owner:self will never provoke a QueryParseException
        log.error("Exception while suggesting reviewers", e);
        return ImmutableMap.of();
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 2 with QueryParseException

use of com.google.gerrit.server.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 {
    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 3 with QueryParseException

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

the class QueryGroups method apply.

@Override
public List<GroupInfo> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, OrmException {
    if (Strings.isNullOrEmpty(query)) {
        throw new BadRequestException("missing query field");
    }
    GroupIndex searchIndex = indexes.getSearchIndex();
    if (searchIndex == null) {
        throw new MethodNotAllowedException("no group index");
    }
    if (start != 0) {
        queryProcessor.setStart(start);
    }
    if (limit != 0) {
        queryProcessor.setLimit(limit);
    }
    try {
        QueryResult<AccountGroup> result = queryProcessor.query(queryBuilder.parse(query));
        List<AccountGroup> groups = result.entities();
        ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
        json.addOptions(options);
        for (AccountGroup group : groups) {
            groupInfos.add(json.format(GroupDescriptions.forAccountGroup(group)));
        }
        if (!groupInfos.isEmpty() && result.more()) {
            groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
        }
        return groupInfos;
    } catch (QueryParseException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 4 with QueryParseException

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

the class FuzzyTopicPredicate method match.

@Override
public boolean match(final ChangeData cd) throws OrmException {
    Change change = cd.change();
    if (change == null) {
        return false;
    }
    String t = change.getTopic();
    if (t == null) {
        return false;
    }
    try {
        Predicate<ChangeData> thisId = new LegacyChangeIdPredicate(cd.getId());
        Iterable<ChangeData> results = index.getSource(and(thisId, this), IndexedChangeQuery.oneResult()).read();
        return !Iterables.isEmpty(results);
    } catch (QueryParseException e) {
        throw new OrmException(e);
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 5 with QueryParseException

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

the class IsWatchedByPredicate method filters.

protected static List<Predicate<ChangeData>> filters(ChangeQueryBuilder.Arguments args, boolean checkIsVisible) throws QueryParseException {
    List<Predicate<ChangeData>> r = new ArrayList<>();
    ChangeQueryBuilder builder = new ChangeQueryBuilder(args);
    for (ProjectWatchKey w : getWatches(args)) {
        Predicate<ChangeData> f = null;
        if (w.filter() != null) {
            try {
                f = builder.parse(w.filter());
                if (QueryBuilder.find(f, IsWatchedByPredicate.class) != null) {
                    // another user is filtering on. :-)
                    continue;
                }
            } catch (QueryParseException e) {
                continue;
            }
        }
        Predicate<ChangeData> p;
        if (w.project().equals(args.allProjectsName)) {
            p = null;
        } else {
            p = builder.project(w.project().get());
        }
        if (p != null && f != null) {
            r.add(and(p, f));
        } else if (p != null) {
            r.add(p);
        } else if (f != null) {
            r.add(f);
        } else {
            r.add(builder.status_open());
        }
    }
    if (r.isEmpty()) {
        return none();
    } else if (checkIsVisible) {
        return ImmutableList.of(or(r), builder.is_visible());
    } else {
        return ImmutableList.of(or(r));
    }
}
Also used : ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) ArrayList(java.util.ArrayList) AndPredicate(com.google.gerrit.server.query.AndPredicate) Predicate(com.google.gerrit.server.query.Predicate) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Aggregations

QueryParseException (com.google.gerrit.server.query.QueryParseException)19 OrmException (com.google.gwtorm.server.OrmException)7 Account (com.google.gerrit.reviewdb.client.Account)6 Predicate (com.google.gerrit.server.query.Predicate)6 IOException (java.io.IOException)5 Repository (org.eclipse.jgit.lib.Repository)5 Change (com.google.gerrit.reviewdb.client.Change)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)4 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)3 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)3 VersionedAccountDestinations (com.google.gerrit.server.account.VersionedAccountDestinations)3 VersionedAccountQueries (com.google.gerrit.server.account.VersionedAccountQueries)3 ChangeData (com.google.gerrit.server.query.change.ChangeData)3 Map (java.util.Map)3 Collectors.toList (java.util.stream.Collectors.toList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2