Search in sources :

Example 6 with Predicate

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

Example 7 with Predicate

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

the class ProjectsConsistencyChecker method executeQueryAndAutoCloseChanges.

private ImmutableList<ChangeInfo> executeQueryAndAutoCloseChanges(Predicate<ChangeData> basePredicate, Set<Change.Id> seenChanges, List<Predicate<ChangeData>> predicates, boolean fix, Map<Change.Key, ObjectId> changeIdToMergedSha1, List<ObjectId> mergedSha1s) {
    if (predicates.isEmpty()) {
        return ImmutableList.of();
    }
    try {
        List<ChangeData> queryResult = retryHelper.changeIndexQuery("projectsConsistencyCheckerQueryChanges", q -> q.setRequestedFields(ChangeField.CHANGE, ChangeField.PATCH_SET).query(and(basePredicate, or(predicates)))).call();
        // Result for this query that we want to return to the client.
        ImmutableList.Builder<ChangeInfo> autoCloseableChangesByBranch = ImmutableList.builder();
        for (ChangeData autoCloseableChange : queryResult) {
            // earlier queries.
            if (seenChanges.add(autoCloseableChange.getId())) {
                retryHelper.changeUpdate("projectsConsistencyCheckerAutoCloseChanges", () -> {
                    // Auto-close by change
                    if (changeIdToMergedSha1.containsKey(autoCloseableChange.change().getKey())) {
                        autoCloseableChangesByBranch.add(changeJson(fix, changeIdToMergedSha1.get(autoCloseableChange.change().getKey())).format(autoCloseableChange));
                        return null;
                    }
                    // Auto-close by commit
                    for (ObjectId patchSetSha1 : autoCloseableChange.patchSets().stream().map(PatchSet::commitId).collect(toSet())) {
                        if (mergedSha1s.contains(patchSetSha1)) {
                            autoCloseableChangesByBranch.add(changeJson(fix, patchSetSha1).format(autoCloseableChange));
                            break;
                        }
                    }
                    return null;
                }).call();
            }
        }
        return autoCloseableChangesByBranch.build();
    } catch (Exception e) {
        Throwables.throwIfUnchecked(e);
        throw new StorageException(e);
    }
}
Also used : DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ChangePredicates(com.google.gerrit.server.query.change.ChangePredicates) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CheckProjectResultInfo(com.google.gerrit.extensions.api.projects.CheckProjectResultInfo) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) RetryHelper(com.google.gerrit.server.update.RetryHelper) UrlFormatter(com.google.gerrit.server.config.UrlFormatter) Predicate.or(com.google.gerrit.index.query.Predicate.or) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) ChangeJson(com.google.gerrit.server.change.ChangeJson) Singleton(com.google.inject.Singleton) AutoCloseableChangesCheckResult(com.google.gerrit.extensions.api.projects.CheckProjectResultInfo.AutoCloseableChangesCheckResult) IndexConfig(com.google.gerrit.index.IndexConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FixInput(com.google.gerrit.extensions.api.changes.FixInput) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) AutoCloseableChangesCheckInput(com.google.gerrit.extensions.api.projects.CheckProjectInput.AutoCloseableChangesCheckInput) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) Predicate(com.google.gerrit.index.query.Predicate) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeStatusPredicate.open(com.google.gerrit.server.query.change.ChangeStatusPredicate.open) StorageException(com.google.gerrit.exceptions.StorageException) Predicate.and(com.google.gerrit.index.query.Predicate.and) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeField(com.google.gerrit.server.index.change.ChangeField) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Project(com.google.gerrit.entities.Project) CheckProjectInput(com.google.gerrit.extensions.api.projects.CheckProjectInput) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Repository(org.eclipse.jgit.lib.Repository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableList(com.google.common.collect.ImmutableList) PatchSet(com.google.gerrit.entities.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException)

Example 8 with Predicate

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

the class ProjectsConsistencyChecker method checkForAutoCloseableChanges.

private AutoCloseableChangesCheckResult checkForAutoCloseableChanges(Project.NameKey projectName, AutoCloseableChangesCheckInput input) throws IOException, RestApiException {
    AutoCloseableChangesCheckResult r = new AutoCloseableChangesCheckResult();
    if (Strings.isNullOrEmpty(input.branch)) {
        throw new BadRequestException("branch is required");
    }
    boolean fix = input.fix != null ? input.fix : false;
    if (input.maxCommits != null && input.maxCommits > AUTO_CLOSE_MAX_COMMITS_LIMIT) {
        throw new BadRequestException("max commits can at most be set to " + AUTO_CLOSE_MAX_COMMITS_LIMIT);
    }
    int maxCommits = input.maxCommits != null ? input.maxCommits : AUTO_CLOSE_MAX_COMMITS_LIMIT;
    // Result that we want to return to the client.
    List<ChangeInfo> autoCloseableChanges = new ArrayList<>();
    // Remember the change IDs of all changes that we already included into the result, so that we
    // can avoid including the same change twice.
    Set<Change.Id> seenChanges = new HashSet<>();
    try (Repository repo = repoManager.openRepository(projectName);
        RevWalk rw = new RevWalk(repo)) {
        String branch = RefNames.fullName(input.branch);
        Ref ref = repo.exactRef(branch);
        if (ref == null) {
            throw new UnprocessableEntityException(String.format("branch '%s' not found", input.branch));
        }
        rw.reset();
        rw.markStart(rw.parseCommit(ref.getObjectId()));
        rw.sort(RevSort.TOPO);
        rw.sort(RevSort.REVERSE);
        // Cache the SHA1's of all merged commits. We need this for knowing which commit merged the
        // change when auto-closing changes by commit.
        List<ObjectId> mergedSha1s = new ArrayList<>();
        // Cache the Change-Id to commit SHA1 mapping for all Change-Id's that we find in merged
        // commits. We need this for knowing which commit merged the change when auto-closing
        // changes by Change-Id.
        Map<Change.Key, ObjectId> changeIdToMergedSha1 = new HashMap<>();
        // Base predicate which is fixed for every change query.
        Predicate<ChangeData> basePredicate = and(ChangePredicates.project(projectName), ChangePredicates.ref(branch), open());
        int maxLeafPredicates = indexConfig.maxTerms() - basePredicate.getLeafCount();
        // List of predicates by which we want to find open changes for the branch. These predicates
        // will be combined with the 'or' operator.
        List<Predicate<ChangeData>> predicates = new ArrayList<>(maxLeafPredicates);
        RevCommit commit;
        int skippedCommits = 0;
        int walkedCommits = 0;
        while ((commit = rw.next()) != null) {
            if (input.skipCommits != null && skippedCommits < input.skipCommits) {
                skippedCommits++;
                continue;
            }
            if (walkedCommits >= maxCommits) {
                break;
            }
            walkedCommits++;
            ObjectId commitId = commit.copy();
            mergedSha1s.add(commitId);
            // Consider all Change-Id lines since this is what ReceiveCommits#autoCloseChanges does.
            List<String> changeIds = ChangeUtil.getChangeIdsFromFooter(commit, urlFormatter.get());
            // Number of predicates that we need to add for this commit, 1 per Change-Id plus one for
            // the commit.
            int newPredicatesCount = changeIds.size() + 1;
            // the query and start a new one.
            if (predicates.size() + newPredicatesCount > maxLeafPredicates) {
                autoCloseableChanges.addAll(executeQueryAndAutoCloseChanges(basePredicate, seenChanges, predicates, fix, changeIdToMergedSha1, mergedSha1s));
                mergedSha1s.clear();
                changeIdToMergedSha1.clear();
                predicates.clear();
                if (newPredicatesCount > maxLeafPredicates) {
                    // Whee, a single commit generates more than maxLeafPredicates predicates. Give up.
                    throw new ResourceConflictException(String.format("commit %s contains more Change-Ids than we can handle", commit.name()));
                }
            }
            changeIds.forEach(changeId -> {
                // It can happen that there are multiple merged commits with the same Change-Id
                // footer (e.g. if a change was cherry-picked to a stable branch stable branch which
                // then got merged back into master, or just by directly pushing several commits
                // with the same Change-Id). In this case it is hard to say which of the commits
                // should be used to auto-close an open change with the same Change-Id (and branch).
                // Possible approaches are:
                // 1. use the oldest commit with that Change-Id to auto-close the change
                // 2. use the newest commit with that Change-Id to auto-close the change
                // Possibility 1. has the disadvantage that the commit may have been merged before
                // the change was created in which case it is strange how it could auto-close the
                // change. Also this strategy would require to walk all commits since otherwise we
                // cannot be sure that we have seen the oldest commit with that Change-Id.
                // Possibility 2 has the disadvantage that it doesn't produce the same result as if
                // auto-closing on push would have worked, since on direct push the first commit with
                // a Change-Id of an open change would have closed that change. Also for this we
                // would need to consider all commits that are skipped.
                // Since both possibilities are not perfect and require extra effort we choose the
                // easiest approach, which is use the newest commit with that Change-Id that we have
                // seen (this means we ignore skipped commits). This should be okay since the
                // important thing for callers is that auto-closable changes are closed. Which of the
                // commits is used to auto-close a change if there are several candidates is of minor
                // importance and hence can be non-deterministic.
                Change.Key changeKey = Change.key(changeId);
                if (!changeIdToMergedSha1.containsKey(changeKey)) {
                    changeIdToMergedSha1.put(changeKey, commitId);
                }
                // Find changes that have a matching Change-Id.
                predicates.add(ChangePredicates.idPrefix(changeId));
            });
            // Find changes that have a matching commit.
            predicates.add(ChangePredicates.commitPrefix(commit.name()));
        }
        if (!predicates.isEmpty()) {
            // Execute the query with the remaining predicates that were collected.
            autoCloseableChanges.addAll(executeQueryAndAutoCloseChanges(basePredicate, seenChanges, predicates, fix, changeIdToMergedSha1, mergedSha1s));
        }
    }
    r.autoCloseableChanges = autoCloseableChanges;
    return r;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Predicate(com.google.gerrit.index.query.Predicate) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeData(com.google.gerrit.server.query.change.ChangeData) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ObjectId(org.eclipse.jgit.lib.ObjectId) AutoCloseableChangesCheckResult(com.google.gerrit.extensions.api.projects.CheckProjectResultInfo.AutoCloseableChangesCheckResult)

Example 9 with Predicate

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

the class ReviewersUtil method suggestAccounts.

private List<Account.Id> suggestAccounts(SuggestReviewers suggestReviewers) throws BadRequestException {
    try (Timer0.Context ctx = metrics.queryAccountsLatency.start()) {
        // For performance reasons we don't use AccountQueryProvider as it would always load the
        // complete account from the cache (or worse, from NoteDb) even though we only need the ID
        // which we can directly get from the returned results.
        Predicate<AccountState> pred = Predicate.and(AccountPredicates.isActive(), accountQueryBuilder.defaultQuery(suggestReviewers.getQuery()));
        logger.atFine().log("accounts index query: %s", pred);
        accountIndexRewriter.validateMaxTermsInQuery(pred);
        boolean useLegacyNumericFields = accountIndexes.getSearchIndex().getSchema().useLegacyNumericFields();
        FieldDef<AccountState, ?> idField = useLegacyNumericFields ? AccountField.ID : AccountField.ID_STR;
        ResultSet<FieldBundle> result = accountIndexes.getSearchIndex().getSource(pred, QueryOptions.create(indexConfig, 0, suggestReviewers.getLimit(), ImmutableSet.of(idField.getName()))).readRaw();
        List<Account.Id> matches = result.toList().stream().map(f -> fromIdField(f, useLegacyNumericFields)).collect(toList());
        logger.atFine().log("Matches: %s", matches);
        return matches;
    } catch (TooManyTermsInQueryException e) {
        throw new BadRequestException(e.getMessage());
    } catch (QueryParseException e) {
        logger.atWarning().withCause(e).log("Suggesting accounts failed, return empty result.");
        return ImmutableList.of();
    } catch (StorageException e) {
        if (e.getCause() instanceof TooManyTermsInQueryException) {
            throw new BadRequestException(e.getMessage());
        }
        if (e.getCause() instanceof QueryParseException) {
            return ImmutableList.of();
        }
        throw e;
    }
}
Also used : GroupBackend(com.google.gerrit.server.account.GroupBackend) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ReviewerModifier(com.google.gerrit.server.change.ReviewerModifier) EnumSet(java.util.EnumSet) FieldDef(com.google.gerrit.index.FieldDef) ImmutableSet(com.google.common.collect.ImmutableSet) GroupBaseInfo(com.google.gerrit.extensions.common.GroupBaseInfo) Timer0(com.google.gerrit.metrics.Timer0) Account(com.google.gerrit.entities.Account) FillOptions(com.google.gerrit.server.account.AccountDirectory.FillOptions) AccountQueryBuilder(com.google.gerrit.server.query.account.AccountQueryBuilder) Set(java.util.Set) AccountIndexCollection(com.google.gerrit.server.index.account.AccountIndexCollection) Sets(com.google.common.collect.Sets) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Url(com.google.gerrit.extensions.restapi.Url) MetricMaker(com.google.gerrit.metrics.MetricMaker) LazyArgs.lazy(com.google.common.flogger.LazyArgs.lazy) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) AccountLoader(com.google.gerrit.server.account.AccountLoader) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IndexConfig(com.google.gerrit.index.IndexConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) ResultSet(com.google.gerrit.index.query.ResultSet) AccountPredicates(com.google.gerrit.server.query.account.AccountPredicates) GroupMembers(com.google.gerrit.server.account.GroupMembers) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) QueryParseException(com.google.gerrit.index.query.QueryParseException) Description(com.google.gerrit.metrics.Description) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountIndexRewriter(com.google.gerrit.server.index.account.AccountIndexRewriter) Predicate(com.google.gerrit.index.query.Predicate) SuggestedReviewerInfo(com.google.gerrit.extensions.common.SuggestedReviewerInfo) AccountControl(com.google.gerrit.server.account.AccountControl) CurrentUser(com.google.gerrit.server.CurrentUser) AccountField(com.google.gerrit.server.index.account.AccountField) StorageException(com.google.gerrit.exceptions.StorageException) Units(com.google.gerrit.metrics.Description.Units) ProjectState(com.google.gerrit.server.project.ProjectState) QueryOptions(com.google.gerrit.index.QueryOptions) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Project(com.google.gerrit.entities.Project) AccountState(com.google.gerrit.server.account.AccountState) ServiceUserClassifier(com.google.gerrit.server.account.ServiceUserClassifier) Collections(java.util.Collections) TooManyTermsInQueryException(com.google.gerrit.index.query.TooManyTermsInQueryException) FieldBundle(com.google.gerrit.index.query.FieldBundle) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.index.query.QueryParseException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Timer0(com.google.gerrit.metrics.Timer0) StorageException(com.google.gerrit.exceptions.StorageException)

Example 10 with Predicate

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

the class CommitsCollection method canRead.

/**
 * Returns true if {@code commit} is visible to the caller.
 */
public boolean canRead(ProjectState state, Repository repo, RevCommit commit) throws IOException {
    Project.NameKey project = state.getNameKey();
    if (indexes.getSearchIndex() == null) {
        // as the commit might be a patchset of a not yet submitted change.
        return reachable.fromRefs(project, repo, commit, repo.getRefDatabase().getRefs());
    }
    // Check first if any patchset of any change references the commit in question. This is much
    // cheaper than ref visibility filtering and reachability computation.
    List<ChangeData> changes = retryHelper.changeIndexQuery("queryChangesByProjectCommitWithLimit1", q -> q.enforceVisibility(true).setLimit(1).byProjectCommit(project, commit)).call();
    if (!changes.isEmpty()) {
        return true;
    }
    // Maybe the commit was a merge commit of a change. Try to find promising candidates for
    // branches to check, by seeing if its parents were associated to changes.
    Predicate<ChangeData> pred = Predicate.and(ChangePredicates.project(project), Predicate.or(Arrays.stream(commit.getParents()).map(parent -> ChangePredicates.commitPrefix(parent.getId().getName())).collect(toImmutableList())));
    changes = retryHelper.changeIndexQuery("queryChangesByProjectCommit", q -> q.enforceVisibility(true).query(pred)).call();
    Set<Ref> branchesForCommitParents = new HashSet<>(changes.size());
    for (ChangeData cd : changes) {
        Ref ref = repo.exactRef(cd.change().getDest().branch());
        if (ref != null) {
            branchesForCommitParents.add(ref);
        }
    }
    if (reachable.fromRefs(project, repo, commit, branchesForCommitParents.stream().collect(Collectors.toList()))) {
        return true;
    }
    // If we have already checked change refs using the change index, spare any further checks for
    // changes.
    List<Ref> refs = repo.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, ImmutableSet.of(RefNames.REFS_CHANGES));
    return reachable.fromRefs(project, repo, commit, refs);
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Arrays(java.util.Arrays) RevCommit(org.eclipse.jgit.revwalk.RevCommit) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IdString(com.google.gerrit.extensions.restapi.IdString) Inject(com.google.inject.Inject) ChildCollection(com.google.gerrit.extensions.restapi.ChildCollection) CommitResource(com.google.gerrit.server.project.CommitResource) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) ChangePredicates(com.google.gerrit.server.query.change.ChangePredicates) HashSet(java.util.HashSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RefDatabase(org.eclipse.jgit.lib.RefDatabase) ImmutableList(com.google.common.collect.ImmutableList) RefNames(com.google.gerrit.entities.RefNames) RetryHelper(com.google.gerrit.server.update.RetryHelper) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Predicate(com.google.gerrit.index.query.Predicate) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) RestView(com.google.gerrit.extensions.restapi.RestView) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectResource(com.google.gerrit.server.project.ProjectResource) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Reachable(com.google.gerrit.server.project.Reachable) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) Ref(org.eclipse.jgit.lib.Ref) Project(com.google.gerrit.entities.Project) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) Project(com.google.gerrit.entities.Project) Ref(org.eclipse.jgit.lib.Ref) ChangeData(com.google.gerrit.server.query.change.ChangeData) HashSet(java.util.HashSet)

Aggregations

Predicate (com.google.gerrit.index.query.Predicate)14 LimitPredicate (com.google.gerrit.index.query.LimitPredicate)6 ImmutableList (com.google.common.collect.ImmutableList)5 Account (com.google.gerrit.entities.Account)5 Change (com.google.gerrit.entities.Change)5 Project (com.google.gerrit.entities.Project)5 QueryParseException (com.google.gerrit.index.query.QueryParseException)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 ArrayList (java.util.ArrayList)5 AccountGroup (com.google.gerrit.entities.AccountGroup)4 GroupReference (com.google.gerrit.entities.GroupReference)4 Inject (com.google.inject.Inject)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 Repository (org.eclipse.jgit.lib.Repository)4 GroupDescription (com.google.gerrit.entities.GroupDescription)3 RefNames (com.google.gerrit.entities.RefNames)3 StorageException (com.google.gerrit.exceptions.StorageException)3