Search in sources :

Example 76 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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 77 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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 78 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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 (isSameInstance(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.index.query.IndexPredicate) MutableInteger(org.eclipse.jgit.util.MutableInteger) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 79 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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 80 with ChangeData

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

the class ImpersonationIT method voteOnBehalfOf.

@Test
public void voteOnBehalfOf() throws Exception {
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    RevisionApi revision = gApi.changes().id(r.getChangeId()).current();
    ReviewInput in = ReviewInput.recommend();
    in.onBehalfOf = user.id().toString();
    in.message = "Message on behalf of";
    revision.review(in);
    PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
    assertThat(psa.patchSetId().get()).isEqualTo(1);
    assertThat(psa.label()).isEqualTo("Code-Review");
    assertThat(psa.accountId()).isEqualTo(user.id());
    assertThat(psa.value()).isEqualTo(1);
    assertThat(psa.realAccountId()).isEqualTo(admin.id());
    ChangeData cd = r.getChange();
    ChangeMessage m = Iterables.getLast(cmUtil.byChange(cd.notes()));
    assertThat(m.getMessage()).endsWith(in.message);
    assertThat(m.getAuthor()).isEqualTo(user.id());
    assertThat(m.getRealAuthor()).isEqualTo(admin.id());
}
Also used : RevisionApi(com.google.gerrit.extensions.api.changes.RevisionApi) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ChangeData (com.google.gerrit.server.query.change.ChangeData)208 Test (org.junit.Test)75 Change (com.google.gerrit.entities.Change)58 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)57 RevCommit (org.eclipse.jgit.revwalk.RevCommit)53 ObjectId (org.eclipse.jgit.lib.ObjectId)45 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)33 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)27 PatchSet (com.google.gerrit.entities.PatchSet)26 StorageException (com.google.gerrit.exceptions.StorageException)25 Inject (com.google.inject.Inject)25 HashMap (java.util.HashMap)25 Map (java.util.Map)24 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)23 List (java.util.List)23 Project (com.google.gerrit.entities.Project)21 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)21 OrmException (com.google.gwtorm.server.OrmException)20 Repository (org.eclipse.jgit.lib.Repository)20