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");
}
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");
}
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;
}
}
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);
}
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());
}
Aggregations