Make LimitPredicate generic
This way it can be used for queries on any index. Change-Id: I1dfda31021aacf344e28439271293956d25fa507 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -23,14 +23,15 @@ import com.google.gerrit.server.index.IndexPredicate;
|
||||
import com.google.gerrit.server.index.IndexRewriter;
|
||||
import com.google.gerrit.server.index.QueryOptions;
|
||||
import com.google.gerrit.server.query.AndPredicate;
|
||||
import com.google.gerrit.server.query.LimitPredicate;
|
||||
import com.google.gerrit.server.query.NotPredicate;
|
||||
import com.google.gerrit.server.query.OrPredicate;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gerrit.server.query.change.AndSource;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
|
||||
import com.google.gerrit.server.query.change.ChangeStatusPredicate;
|
||||
import com.google.gerrit.server.query.change.LimitPredicate;
|
||||
import com.google.gerrit.server.query.change.OrSource;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
@@ -174,7 +175,7 @@ public class ChangeIndexRewriter implements IndexRewriter<ChangeData> {
|
||||
// Replace any limits with the limit provided by the caller. The caller
|
||||
// should have already searched the predicate tree for limit predicates
|
||||
// and included that in their limit computation.
|
||||
return new LimitPredicate(opts.limit());
|
||||
return new LimitPredicate<>(ChangeQueryBuilder.FIELD_LIMIT, opts.limit());
|
||||
} else if (!isRewritePossible(in)) {
|
||||
return null; // magic to indicate "in" cannot be rewritten
|
||||
}
|
||||
|
||||
@@ -12,31 +12,24 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.query.change;
|
||||
package com.google.gerrit.server.query;
|
||||
|
||||
import static com.google.gerrit.server.query.change.ChangeQueryBuilder.FIELD_LIMIT;
|
||||
|
||||
import com.google.gerrit.server.query.IntPredicate;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryBuilder;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
|
||||
public class LimitPredicate extends IntPredicate<ChangeData> {
|
||||
public class LimitPredicate<T> extends IntPredicate<T> {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Integer getLimit(Predicate<ChangeData> p) {
|
||||
IntPredicate<?> ip = QueryBuilder.find(p, IntPredicate.class, FIELD_LIMIT);
|
||||
public static Integer getLimit(String fieldName, Predicate<?> p) {
|
||||
IntPredicate<?> ip = QueryBuilder.find(p, IntPredicate.class, fieldName);
|
||||
return ip != null ? ip.intValue() : null;
|
||||
}
|
||||
|
||||
public LimitPredicate(int limit) throws QueryParseException {
|
||||
super(ChangeQueryBuilder.FIELD_LIMIT, limit);
|
||||
public LimitPredicate(String fieldName, int limit) throws QueryParseException {
|
||||
super(fieldName, limit);
|
||||
if (limit <= 0) {
|
||||
throw new QueryParseException("limit must be positive: " + limit);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean match(ChangeData object) {
|
||||
public boolean match(T object) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ import com.google.gerrit.server.patch.PatchListCache;
|
||||
import com.google.gerrit.server.project.ChangeControl;
|
||||
import com.google.gerrit.server.project.ListChildProjects;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
import com.google.gerrit.server.query.LimitPredicate;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryBuilder;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
@@ -843,7 +844,8 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
|
||||
|
||||
@Operator
|
||||
public Predicate<ChangeData> limit(String limit) throws QueryParseException {
|
||||
return new LimitPredicate(Integer.parseInt(limit));
|
||||
return new LimitPredicate<>(ChangeQueryBuilder.FIELD_LIMIT,
|
||||
Integer.parseInt(limit));
|
||||
}
|
||||
|
||||
@Operator
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package com.google.gerrit.server.query.change;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.gerrit.server.query.change.ChangeQueryBuilder.FIELD_LIMIT;
|
||||
import static com.google.gerrit.server.query.change.ChangeStatusPredicate.open;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
@@ -36,6 +37,7 @@ import com.google.gerrit.server.index.change.ChangeIndexRewriter;
|
||||
import com.google.gerrit.server.index.change.IndexedChangeQuery;
|
||||
import com.google.gerrit.server.notedb.ChangeNotes;
|
||||
import com.google.gerrit.server.project.ChangeControl;
|
||||
import com.google.gerrit.server.query.LimitPredicate;
|
||||
import com.google.gerrit.server.query.Predicate;
|
||||
import com.google.gerrit.server.query.QueryParseException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
@@ -245,7 +247,7 @@ public class QueryProcessor {
|
||||
if (limitFromCaller > 0) {
|
||||
possibleLimits.add(limitFromCaller);
|
||||
}
|
||||
Integer limitFromPredicate = LimitPredicate.getLimit(p);
|
||||
Integer limitFromPredicate = LimitPredicate.getLimit(FIELD_LIMIT, p);
|
||||
if (limitFromPredicate != null) {
|
||||
possibleLimits.add(limitFromPredicate);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user