Move query parsing out of QueryProcessor
The two queryByString methods were trivial, just parsing the input query string(s) into a predicate. Remove these methods and let callers use a ChangeQueryBuilder directly. This will eventually allow us to remove QueryBuilder as a dependency of QueryProcessor. Change-Id: Id9416719270ed1997d153ba9f5e6004df0b94a8a
This commit is contained in:
@@ -172,7 +172,7 @@ public abstract class QueryBuilder<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a user supplied query string into a predicate.
|
||||
* Parse a user-supplied query string into a predicate.
|
||||
*
|
||||
* @param query the query string.
|
||||
* @return predicate representing the user query.
|
||||
@@ -185,6 +185,27 @@ public abstract class QueryBuilder<T> {
|
||||
return toPredicate(QueryParser.parse(query));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse multiple user-supplied query strings into a list of predicates.
|
||||
*
|
||||
* @param queries the query strings.
|
||||
* @return predicates representing the user query, in the same order as the
|
||||
* input.
|
||||
* @throws QueryParseException one of the query strings is invalid and cannot
|
||||
* be parsed by this parser. This may be due to a syntax error, may be
|
||||
* due to an operator not being supported, or due to an invalid value
|
||||
* being passed to a recognized operator.
|
||||
*
|
||||
*/
|
||||
public List<Predicate<T>> parse(final List<String> queries)
|
||||
throws QueryParseException {
|
||||
List<Predicate<T>> predicates = new ArrayList<>(queries.size());
|
||||
for (String query : queries) {
|
||||
predicates.add(parse(query));
|
||||
}
|
||||
return predicates;
|
||||
}
|
||||
|
||||
private Predicate<T> toPredicate(final Tree r) throws QueryParseException,
|
||||
IllegalArgumentException {
|
||||
switch (r.getType()) {
|
||||
|
||||
@@ -30,9 +30,10 @@ public class InternalChangeQuery {
|
||||
private final ChangeQueryBuilder qb;
|
||||
|
||||
@Inject
|
||||
InternalChangeQuery(QueryProcessor queryProcessor) {
|
||||
InternalChangeQuery(QueryProcessor queryProcessor,
|
||||
ChangeQueryBuilder queryBuilder) {
|
||||
qp = queryProcessor;
|
||||
qb = qp.getQueryBuilder();
|
||||
qb = queryBuilder;
|
||||
}
|
||||
|
||||
public InternalChangeQuery setLimit(int n) {
|
||||
|
||||
@@ -63,6 +63,7 @@ public class OutputStreamQuery {
|
||||
TEXT, JSON
|
||||
}
|
||||
|
||||
private final ChangeQueryBuilder queryBuilder;
|
||||
private final QueryProcessor queryProcessor;
|
||||
private final EventFactory eventFactory;
|
||||
private final TrackingFooters trackingFooters;
|
||||
@@ -83,10 +84,13 @@ public class OutputStreamQuery {
|
||||
private PrintWriter out;
|
||||
|
||||
@Inject
|
||||
OutputStreamQuery(QueryProcessor queryProcessor,
|
||||
OutputStreamQuery(
|
||||
ChangeQueryBuilder queryBuilder,
|
||||
QueryProcessor queryProcessor,
|
||||
EventFactory eventFactory,
|
||||
TrackingFooters trackingFooters,
|
||||
CurrentUser user) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
this.queryProcessor = queryProcessor;
|
||||
this.eventFactory = eventFactory;
|
||||
this.trackingFooters = trackingFooters;
|
||||
@@ -174,7 +178,8 @@ public class OutputStreamQuery {
|
||||
final QueryStatsAttribute stats = new QueryStatsAttribute();
|
||||
stats.runTimeMilliseconds = TimeUtil.nowMs();
|
||||
|
||||
QueryResult results = queryProcessor.queryByString(queryString);
|
||||
QueryResult results =
|
||||
queryProcessor.queryChanges(queryBuilder.parse(queryString));
|
||||
ChangeAttribute c = null;
|
||||
for (ChangeData d : results.changes()) {
|
||||
ChangeControl cc = d.changeControl().forUser(user);
|
||||
|
||||
@@ -39,6 +39,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class QueryChanges implements RestReadView<TopLevelResource> {
|
||||
private final ChangeJson json;
|
||||
private final ChangeQueryBuilder qb;
|
||||
private final QueryProcessor imp;
|
||||
private final Provider<CurrentUser> user;
|
||||
private EnumSet<ListChangesOption> options;
|
||||
@@ -67,8 +68,12 @@ public class QueryChanges implements RestReadView<TopLevelResource> {
|
||||
}
|
||||
|
||||
@Inject
|
||||
QueryChanges(ChangeJson json, QueryProcessor qp, Provider<CurrentUser> user) {
|
||||
QueryChanges(ChangeJson json,
|
||||
ChangeQueryBuilder qb,
|
||||
QueryProcessor qp,
|
||||
Provider<CurrentUser> user) {
|
||||
this.json = json;
|
||||
this.qb = qb;
|
||||
this.imp = qp;
|
||||
this.user = user;
|
||||
|
||||
@@ -135,7 +140,7 @@ public class QueryChanges implements RestReadView<TopLevelResource> {
|
||||
private List<List<ChangeInfo>> query0() throws OrmException,
|
||||
QueryParseException {
|
||||
int cnt = queries.size();
|
||||
List<QueryResult> results = imp.queryByStrings(queries);
|
||||
List<QueryResult> results = imp.queryChanges(qb.parse(queries));
|
||||
List<List<ChangeInfo>> res = json.addOptions(options)
|
||||
.formatQueryResults(results);
|
||||
for (int n = 0; n < cnt; n++) {
|
||||
|
||||
@@ -49,10 +49,6 @@ public class QueryProcessor {
|
||||
this.userProvider = userProvider;
|
||||
}
|
||||
|
||||
public ChangeQueryBuilder getQueryBuilder() {
|
||||
return queryBuilder;
|
||||
}
|
||||
|
||||
public QueryProcessor enforceVisibility(boolean enforce) {
|
||||
enforceVisibility = enforce;
|
||||
return this;
|
||||
@@ -68,34 +64,6 @@ public class QueryProcessor {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query for changes that match the query string.
|
||||
*
|
||||
* @see #queryChanges(List)
|
||||
* @param queryString the query string to parse.
|
||||
* @return results of the query.
|
||||
*/
|
||||
public QueryResult queryByString(String queryString)
|
||||
throws OrmException, QueryParseException {
|
||||
return queryByStrings(ImmutableList.of(queryString)).get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user