Add QueryProcessor methods to search by Predicate
Change-Id: I3e524fafc1e471308e9bc9b2d72c11fc75752647
This commit is contained in:
@@ -135,7 +135,7 @@ public class QueryChanges implements RestReadView<TopLevelResource> {
|
|||||||
private List<List<ChangeInfo>> query0() throws OrmException,
|
private List<List<ChangeInfo>> query0() throws OrmException,
|
||||||
QueryParseException {
|
QueryParseException {
|
||||||
int cnt = queries.size();
|
int cnt = queries.size();
|
||||||
List<QueryResult> results = imp.queryChanges(queries);
|
List<QueryResult> results = imp.queryByStrings(queries);
|
||||||
List<List<ChangeInfo>> res = json.addOptions(options)
|
List<List<ChangeInfo>> res = json.addOptions(options)
|
||||||
.formatQueryResults(results);
|
.formatQueryResults(results);
|
||||||
for (int n = 0; n < cnt; n++) {
|
for (int n = 0; n < cnt; n++) {
|
||||||
|
@@ -170,36 +170,72 @@ public class QueryProcessor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Query for changes that match the query string.
|
* Query for changes that match the query string.
|
||||||
* <p>
|
*
|
||||||
* If a limit was specified using {@link #setLimit(int)} this method may
|
* @see #queryChanges(List)
|
||||||
* return up to {@code limit + 1} results, allowing the caller to determine if
|
* @param queryString the query string to parse.
|
||||||
* there are more than {@code limit} matches and suggest to its own caller
|
* @return results of the query.
|
||||||
* that the query could be retried with {@link #setStart(int)}.
|
|
||||||
*/
|
*/
|
||||||
public QueryResult queryChanges(String queryString)
|
public QueryResult queryByString(String queryString)
|
||||||
throws OrmException, QueryParseException {
|
throws OrmException, QueryParseException {
|
||||||
return queryChanges(ImmutableList.of(queryString)).get(0);
|
return queryByStrings(ImmutableList.of(queryString)).get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query for changes that match the query string.
|
* Perform multiple queries over a list of query strings.
|
||||||
|
*
|
||||||
|
* @see #queryChanges(List)
|
||||||
|
* @param queryStrings the query strings to parse.
|
||||||
|
* @return results of the queries, one list per input query.
|
||||||
|
*/
|
||||||
|
public List<QueryResult> queryByStrings(List<String> queryStrings)
|
||||||
|
throws OrmException, QueryParseException {
|
||||||
|
List<Predicate<ChangeData>> queries = new ArrayList<>(queryStrings.size());
|
||||||
|
for (String qs : queryStrings) {
|
||||||
|
queries.add(queryBuilder.parse(qs));
|
||||||
|
}
|
||||||
|
return queryChanges(queries);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query for changes that match a structured query.
|
||||||
|
*
|
||||||
|
* @see #queryChanges(List)
|
||||||
|
* @param query the query.
|
||||||
|
* @return results of the query.
|
||||||
|
*/
|
||||||
|
public QueryResult queryChanges(Predicate<ChangeData> query)
|
||||||
|
throws OrmException, QueryParseException {
|
||||||
|
return queryChanges(ImmutableList.of(query)).get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Perform multiple queries over a list of query strings.
|
||||||
* <p>
|
* <p>
|
||||||
* If a limit was specified using {@link #setLimit(int)} this method may
|
* If a limit was specified using {@link #setLimit(int)} this method may
|
||||||
* return up to {@code limit + 1} results, allowing the caller to determine if
|
* return up to {@code limit + 1} results, allowing the caller to determine if
|
||||||
* there are more than {@code limit} matches and suggest to its own caller
|
* there are more than {@code limit} matches and suggest to its own caller
|
||||||
* that the query could be retried with {@link #setStart(int)}.
|
* that the query could be retried with {@link #setStart(int)}.
|
||||||
|
*
|
||||||
|
* @param queries the queries.
|
||||||
|
* @return results of the queries, one list per input query.
|
||||||
*/
|
*/
|
||||||
public List<QueryResult> queryChanges(List<String> queries)
|
public List<QueryResult> queryChanges(List<Predicate<ChangeData>> queries)
|
||||||
throws OrmException, QueryParseException {
|
throws OrmException, QueryParseException {
|
||||||
final Predicate<ChangeData> visibleToMe = queryBuilder.is_visible();
|
return queryChanges(null, queries);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<QueryResult> queryChanges(List<String> queryStrings,
|
||||||
|
List<Predicate<ChangeData>> queries)
|
||||||
|
throws OrmException, QueryParseException {
|
||||||
|
Predicate<ChangeData> visibleToMe = queryBuilder.is_visible();
|
||||||
int cnt = queries.size();
|
int cnt = queries.size();
|
||||||
|
|
||||||
// Parse and rewrite all queries.
|
// Parse and rewrite all queries.
|
||||||
List<Integer> limits = new ArrayList<>(cnt);
|
List<Integer> limits = new ArrayList<>(cnt);
|
||||||
List<Predicate<ChangeData>> predicates = new ArrayList<>(cnt);
|
List<Predicate<ChangeData>> predicates = new ArrayList<>(cnt);
|
||||||
List<ChangeDataSource> sources = new ArrayList<>(cnt);
|
List<ChangeDataSource> sources = new ArrayList<>(cnt);
|
||||||
for (String query : queries) {
|
for (Predicate<ChangeData> q : queries) {
|
||||||
Predicate<ChangeData> q = parseQuery(query, visibleToMe);
|
q = Predicate.and(q, visibleToMe);
|
||||||
int limit = getEffectiveLimit(q);
|
int limit = getEffectiveLimit(q);
|
||||||
limits.add(limit);
|
limits.add(limit);
|
||||||
|
|
||||||
@@ -231,7 +267,7 @@ public class QueryProcessor {
|
|||||||
List<QueryResult> out = new ArrayList<>(cnt);
|
List<QueryResult> out = new ArrayList<>(cnt);
|
||||||
for (int i = 0; i < cnt; i++) {
|
for (int i = 0; i < cnt; i++) {
|
||||||
out.add(QueryResult.create(
|
out.add(QueryResult.create(
|
||||||
queries.get(i),
|
queryStrings != null ? queryStrings.get(i) : null,
|
||||||
predicates.get(i),
|
predicates.get(i),
|
||||||
limits.get(i),
|
limits.get(i),
|
||||||
matches.get(i).toList()));
|
matches.get(i).toList()));
|
||||||
@@ -255,7 +291,7 @@ public class QueryProcessor {
|
|||||||
final QueryStatsAttribute stats = new QueryStatsAttribute();
|
final QueryStatsAttribute stats = new QueryStatsAttribute();
|
||||||
stats.runTimeMilliseconds = TimeUtil.nowMs();
|
stats.runTimeMilliseconds = TimeUtil.nowMs();
|
||||||
|
|
||||||
QueryResult results = queryChanges(queryString);
|
QueryResult results = queryByString(queryString);
|
||||||
ChangeAttribute c = null;
|
ChangeAttribute c = null;
|
||||||
for (ChangeData d : results.changes()) {
|
for (ChangeData d : results.changes()) {
|
||||||
ChangeControl cc = d.changeControl().forUser(user);
|
ChangeControl cc = d.changeControl().forUser(user);
|
||||||
@@ -376,11 +412,6 @@ public class QueryProcessor {
|
|||||||
return Ordering.natural().min(possibleLimits);
|
return Ordering.natural().min(possibleLimits);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate<ChangeData> parseQuery(String queryString,
|
|
||||||
Predicate<ChangeData> visibleToMe) throws QueryParseException {
|
|
||||||
return Predicate.and(queryBuilder.parse(queryString), visibleToMe);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void show(Object data) {
|
private void show(Object data) {
|
||||||
switch (outputFormat) {
|
switch (outputFormat) {
|
||||||
default:
|
default:
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
package com.google.gerrit.server.query.change;
|
package com.google.gerrit.server.query.change;
|
||||||
|
|
||||||
import com.google.auto.value.AutoValue;
|
import com.google.auto.value.AutoValue;
|
||||||
|
import com.google.gerrit.common.Nullable;
|
||||||
import com.google.gerrit.server.query.Predicate;
|
import com.google.gerrit.server.query.Predicate;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -22,8 +23,8 @@ import java.util.List;
|
|||||||
/** Results of a query over changes. */
|
/** Results of a query over changes. */
|
||||||
@AutoValue
|
@AutoValue
|
||||||
public abstract class QueryResult {
|
public abstract class QueryResult {
|
||||||
static QueryResult create(String query, Predicate<ChangeData> predicate,
|
static QueryResult create(@Nullable String query,
|
||||||
int limit, List<ChangeData> changes) {
|
Predicate<ChangeData> predicate, int limit, List<ChangeData> changes) {
|
||||||
boolean moreChanges;
|
boolean moreChanges;
|
||||||
if (changes.size() > limit) {
|
if (changes.size() > limit) {
|
||||||
moreChanges = true;
|
moreChanges = true;
|
||||||
@@ -34,8 +35,11 @@ public abstract class QueryResult {
|
|||||||
return new AutoValue_QueryResult(query, predicate, changes, moreChanges);
|
return new AutoValue_QueryResult(query, predicate, changes, moreChanges);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the original query string. */
|
/**
|
||||||
public abstract String query();
|
* @return the original query string, or null if the query was created
|
||||||
|
* programmatically.
|
||||||
|
*/
|
||||||
|
@Nullable public abstract String query();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the predicate after all rewriting and other modification by the
|
* @return the predicate after all rewriting and other modification by the
|
||||||
|
Reference in New Issue
Block a user