use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.
the class QueryGroups method apply.
@Override
public Response<List<GroupInfo>> apply(TopLevelResource resource) throws BadRequestException, MethodNotAllowedException, PermissionBackendException {
if (Strings.isNullOrEmpty(query)) {
throw new BadRequestException("missing query field");
}
GroupQueryProcessor queryProcessor = queryProcessorProvider.get();
if (queryProcessor.isDisabled()) {
throw new MethodNotAllowedException("query disabled");
}
if (start != 0) {
queryProcessor.setStart(start);
}
if (limit != 0) {
queryProcessor.setUserProvidedLimit(limit);
}
try {
QueryResult<InternalGroup> result = queryProcessor.query(queryBuilder.parse(query));
List<InternalGroup> groups = result.entities();
ArrayList<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groups.size());
json.addOptions(options);
for (InternalGroup group : groups) {
groupInfos.add(json.format(new InternalGroupDescription(group)));
}
if (!groupInfos.isEmpty() && result.more()) {
groupInfos.get(groupInfos.size() - 1)._moreGroups = true;
}
return Response.ok(groupInfos);
} catch (QueryParseException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.gerrit.index.query.QueryParseException in project gerrit by GerritCodeReview.
the class QueryChanges method query.
private List<List<ChangeInfo>> query() throws QueryParseException, PermissionBackendException {
ChangeQueryProcessor queryProcessor = queryProcessorProvider.get();
if (queryProcessor.isDisabled()) {
throw new QueryParseException("query disabled");
}
if (limit != null) {
queryProcessor.setUserProvidedLimit(limit);
}
if (start != null) {
queryProcessor.setStart(start);
}
if (noLimit != null) {
queryProcessor.setNoLimit(noLimit);
}
if (skipVisibility != null) {
queryProcessor.enforceVisibility(!skipVisibility);
}
dynamicBeans.forEach((p, b) -> queryProcessor.setDynamicBean(p, b));
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 = queryProcessor.query(qb.parse(queries));
List<List<ChangeInfo>> res = json.create(options, queryProcessor.getInfosFactory()).format(results);
for (int n = 0; n < cnt; n++) {
List<ChangeInfo> info = res.get(n);
if (results.get(n).more() && !info.isEmpty()) {
Iterables.getLast(info)._moreChanges = true;
}
}
return res;
}
use of com.google.gerrit.index.query.QueryParseException 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.index.query.QueryParseException 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.index.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 {
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);
}
Aggregations