diff options
Diffstat (limited to 'java/com/google/gerrit/index/QueryOptions.java')
-rw-r--r-- | java/com/google/gerrit/index/QueryOptions.java | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/java/com/google/gerrit/index/QueryOptions.java b/java/com/google/gerrit/index/QueryOptions.java index 0401dab067..91c8d1a5e5 100644 --- a/java/com/google/gerrit/index/QueryOptions.java +++ b/java/com/google/gerrit/index/QueryOptions.java @@ -19,15 +19,52 @@ import static com.google.common.base.Preconditions.checkArgument; import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Ints; +import com.google.gerrit.common.Nullable; import java.util.Set; import java.util.function.Function; @AutoValue public abstract class QueryOptions { public static QueryOptions create(IndexConfig config, int start, int limit, Set<String> fields) { + return create(config, start, null, limit, config.pageSizeMultiplier(), limit, fields); + } + + public static QueryOptions create( + IndexConfig config, int start, int pageSize, int limit, Set<String> fields) { + return create(config, start, null, pageSize, config.pageSizeMultiplier(), limit, fields); + } + + public static QueryOptions create( + IndexConfig config, + int start, + int pageSize, + int pageSizeMultiplier, + int limit, + Set<String> fields) { + return create(config, start, null, pageSize, pageSizeMultiplier, limit, fields); + } + + public static QueryOptions create( + IndexConfig config, + int start, + Object searchAfter, + int pageSize, + int pageSizeMultiplier, + int limit, + Set<String> fields) { checkArgument(start >= 0, "start must be nonnegative: %s", start); checkArgument(limit > 0, "limit must be positive: %s", limit); - return new AutoValue_QueryOptions(config, start, limit, ImmutableSet.copyOf(fields)); + if (searchAfter != null) { + checkArgument(start == 0, "start must be 0 when searchAfter is specified: %s", start); + } + return new AutoValue_QueryOptions( + config, + start, + searchAfter, + pageSize, + pageSizeMultiplier, + limit, + ImmutableSet.copyOf(fields)); } public QueryOptions convertForBackend() { @@ -36,26 +73,56 @@ public abstract class QueryOptions { int backendLimit = config().maxLimit(); int limit = Ints.saturatedCast((long) limit() + start()); limit = Math.min(limit, backendLimit); - return create(config(), 0, limit, fields()); + int pageSize = Math.min(Ints.saturatedCast((long) pageSize() + start()), backendLimit); + return create(config(), 0, null, pageSize, pageSizeMultiplier(), limit, fields()); } public abstract IndexConfig config(); public abstract int start(); + @Nullable + public abstract Object searchAfter(); + + public abstract int pageSize(); + + public abstract int pageSizeMultiplier(); + public abstract int limit(); public abstract ImmutableSet<String> fields(); + public QueryOptions withPageSize(int pageSize) { + return create( + config(), start(), searchAfter(), pageSize, pageSizeMultiplier(), limit(), fields()); + } + public QueryOptions withLimit(int newLimit) { - return create(config(), start(), newLimit, fields()); + return create( + config(), start(), searchAfter(), pageSize(), pageSizeMultiplier(), newLimit, fields()); } public QueryOptions withStart(int newStart) { - return create(config(), newStart, limit(), fields()); + return create( + config(), newStart, searchAfter(), pageSize(), pageSizeMultiplier(), limit(), fields()); + } + + public QueryOptions withSearchAfter(Object newSearchAfter) { + // Index search-after APIs don't use 'start', so set it to 0 to be safe. ElasticSearch for + // example, expects it to be 0 when using search-after APIs. + return create( + config(), start(), newSearchAfter, pageSize(), pageSizeMultiplier(), limit(), fields()) + .withStart(0); } public QueryOptions filterFields(Function<QueryOptions, Set<String>> filter) { - return create(config(), start(), limit(), filter.apply(this)); + return create( + config(), + start(), + searchAfter(), + pageSize(), + pageSizeMultiplier(), + limit(), + filter.apply(this)); } } |