Search in sources :

Example 11 with QueryParseException

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

the class QueryBuilder method fullTextQuery.

private Query fullTextQuery(IndexPredicate<V> p) throws QueryParseException {
    String value = p.getValue();
    if (value == null) {
        throw new QueryParseException("Full-text search over empty string not supported");
    }
    Query query = queryBuilder.createPhraseQuery(p.getField().getName(), value);
    if (query == null) {
        throw new QueryParseException("Cannot create full-text query with value: " + value);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 12 with QueryParseException

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

the class QueryChanges method query.

private List<List<ChangeInfo>> query() throws OrmException, QueryParseException {
    if (imp.isDisabled()) {
        throw new QueryParseException("query disabled");
    }
    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 = imp.query(qb.parse(queries));
    boolean requireLazyLoad = containsAnyOf(options, ImmutableSet.of(DETAILED_LABELS, LABELS)) && !qb.getArgs().getSchema().hasField(ChangeField.STORED_SUBMIT_RECORD_LENIENT);
    ChangeJson cjson = json.create(options);
    cjson.setPluginDefinedAttributesFactory(this.imp);
    List<List<ChangeInfo>> res = cjson.lazyLoad(requireLazyLoad || containsAnyOf(options, ChangeJson.REQUIRE_LAZY_LOAD)).formatQueryResults(results);
    for (int n = 0; n < cnt; n++) {
        List<ChangeInfo> info = res.get(n);
        if (results.get(n).more()) {
            info.get(info.size() - 1)._moreChanges = true;
        }
    }
    return res;
}
Also used : QueryResult(com.google.gerrit.server.query.QueryResult) ChangeJson(com.google.gerrit.server.change.ChangeJson) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ArrayList(java.util.ArrayList) List(java.util.List) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 13 with QueryParseException

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

the class OutputStreamQuery method query.

public void query(String queryString) throws IOException {
    out = new //
    PrintWriter(new //
    BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)));
    try {
        if (queryProcessor.isDisabled()) {
            ErrorMessage m = new ErrorMessage();
            m.message = "query disabled";
            show(m);
            return;
        }
        try {
            final QueryStatsAttribute stats = new QueryStatsAttribute();
            stats.runTimeMilliseconds = TimeUtil.nowMs();
            Map<Project.NameKey, Repository> repos = new HashMap<>();
            Map<Project.NameKey, RevWalk> revWalks = new HashMap<>();
            QueryResult<ChangeData> results = queryProcessor.query(queryBuilder.parse(queryString));
            try {
                for (ChangeData d : results.entities()) {
                    show(buildChangeAttribute(d, repos, revWalks));
                }
            } finally {
                closeAll(revWalks.values(), repos.values());
            }
            stats.rowCount = results.entities().size();
            stats.moreChanges = results.more();
            stats.runTimeMilliseconds = TimeUtil.nowMs() - stats.runTimeMilliseconds;
            show(stats);
        } catch (OrmException err) {
            log.error("Cannot execute query: " + queryString, err);
            ErrorMessage m = new ErrorMessage();
            m.message = "cannot query database";
            show(m);
        } catch (QueryParseException e) {
            ErrorMessage m = new ErrorMessage();
            m.message = e.getMessage();
            show(m);
        }
    } finally {
        try {
            out.flush();
        } finally {
            out = null;
        }
    }
}
Also used : HashMap(java.util.HashMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BufferedWriter(java.io.BufferedWriter) QueryParseException(com.google.gerrit.server.query.QueryParseException) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) QueryStatsAttribute(com.google.gerrit.server.data.QueryStatsAttribute) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with QueryParseException

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

the class ProjectWatch method getWatchers.

/** Returns all watchers that are relevant */
public final Watchers getWatchers(NotifyType type, boolean includeWatchersFromNotifyConfig) throws OrmException {
    Watchers matching = new Watchers();
    Set<Account.Id> projectWatchers = new HashSet<>();
    for (AccountState a : args.accountQueryProvider.get().byWatchedProject(project)) {
        Account.Id accountId = a.getAccount().getId();
        for (Map.Entry<ProjectWatchKey, Set<NotifyType>> e : a.getProjectWatches().entrySet()) {
            if (project.equals(e.getKey().project()) && add(matching, accountId, e.getKey(), e.getValue(), type)) {
                // We only want to prevent matching All-Projects if this filter hits
                projectWatchers.add(accountId);
            }
        }
    }
    for (AccountState a : args.accountQueryProvider.get().byWatchedProject(args.allProjectsName)) {
        for (Map.Entry<ProjectWatchKey, Set<NotifyType>> e : a.getProjectWatches().entrySet()) {
            if (args.allProjectsName.equals(e.getKey().project())) {
                Account.Id accountId = a.getAccount().getId();
                if (!projectWatchers.contains(accountId)) {
                    add(matching, accountId, e.getKey(), e.getValue(), type);
                }
            }
        }
    }
    if (!includeWatchersFromNotifyConfig) {
        return matching;
    }
    for (ProjectState state : projectState.tree()) {
        for (NotifyConfig nc : state.getConfig().getNotifyConfigs()) {
            if (nc.isNotify(type)) {
                try {
                    add(matching, nc);
                } catch (QueryParseException e) {
                    log.warn("Project {} has invalid notify {} filter \"{}\": {}", state.getProject().getName(), nc.getName(), nc.getFilter(), e.getMessage());
                }
            }
        }
    }
    return matching;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashSet(java.util.HashSet) Set(java.util.Set) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.server.query.QueryParseException) ProjectState(com.google.gerrit.server.project.ProjectState) NotifyConfig(com.google.gerrit.server.git.NotifyConfig) Map(java.util.Map) HashSet(java.util.HashSet)

Example 15 with QueryParseException

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

the class QueryAccounts method apply.

@Override
public List<AccountInfo> apply(TopLevelResource rsrc) throws OrmException, BadRequestException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(query)) {
        throw new BadRequestException("missing query field");
    }
    if (suggest && (!suggestConfig || query.length() < suggestFrom)) {
        return Collections.emptyList();
    }
    Set<FillOptions> fillOptions = EnumSet.of(FillOptions.ID);
    if (options.contains(ListAccountsOption.DETAILS)) {
        fillOptions.addAll(AccountLoader.DETAILED_OPTIONS);
    }
    if (options.contains(ListAccountsOption.ALL_EMAILS)) {
        fillOptions.add(FillOptions.EMAIL);
        fillOptions.add(FillOptions.SECONDARY_EMAILS);
    }
    if (suggest) {
        fillOptions.addAll(AccountLoader.DETAILED_OPTIONS);
        fillOptions.add(FillOptions.EMAIL);
        fillOptions.add(FillOptions.SECONDARY_EMAILS);
    }
    accountLoader = accountLoaderFactory.create(fillOptions);
    if (queryProcessor.isDisabled()) {
        throw new MethodNotAllowedException("query disabled");
    }
    if (start != null) {
        queryProcessor.setStart(start);
    }
    Map<Account.Id, AccountInfo> matches = new LinkedHashMap<>();
    try {
        Predicate<AccountState> queryPred;
        if (suggest) {
            queryPred = queryBuilder.defaultQuery(query);
            queryProcessor.setLimit(suggestLimit);
        } else {
            queryPred = queryBuilder.parse(query);
        }
        QueryResult<AccountState> result = queryProcessor.query(queryPred);
        for (AccountState accountState : result.entities()) {
            Account.Id id = accountState.getAccount().getId();
            matches.put(id, accountLoader.get(id));
        }
        accountLoader.fill();
        List<AccountInfo> sorted = AccountInfoComparator.ORDER_NULLS_LAST.sortedCopy(matches.values());
        if (!sorted.isEmpty() && result.more()) {
            sorted.get(sorted.size() - 1)._moreAccounts = true;
        }
        return sorted;
    } catch (QueryParseException e) {
        if (suggest) {
            return ImmutableList.of();
        }
        throw new BadRequestException(e.getMessage());
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) LinkedHashMap(java.util.LinkedHashMap) QueryParseException(com.google.gerrit.server.query.QueryParseException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) FillOptions(com.google.gerrit.server.account.AccountDirectory.FillOptions) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

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