diff options
Diffstat (limited to 'java/com/google/gerrit/index/query/PaginatingSource.java')
-rw-r--r-- | java/com/google/gerrit/index/query/PaginatingSource.java | 81 |
1 files changed, 50 insertions, 31 deletions
diff --git a/java/com/google/gerrit/index/query/PaginatingSource.java b/java/com/google/gerrit/index/query/PaginatingSource.java index 4eceb319e7..8847005629 100644 --- a/java/com/google/gerrit/index/query/PaginatingSource.java +++ b/java/com/google/gerrit/index/query/PaginatingSource.java @@ -55,47 +55,66 @@ public class PaginatingSource<T> implements DataSource<T> { List<T> r = new ArrayList<>(); T last = null; int pageResultSize = 0; + boolean skipped = false; for (T data : buffer(resultSet)) { if (!isMatchable() || match(data)) { r.add(data); + } else { + skipped = true; } last = data; pageResultSize++; } - if (last != null - && source instanceof Paginated - // TODO: this fix is only for the stable branches and the real refactoring would be to - // restore the logic - // for the filtering in AndSource (L58 - 64) as per - // https://gerrit-review.googlesource.com/c/gerrit/+/345634/9 - && !indexConfig.paginationType().equals(PaginationType.NONE)) { - // Restart source and continue if we have not filled the - // full limit the caller wants. - // - @SuppressWarnings("unchecked") - Paginated<T> p = (Paginated<T>) source; - QueryOptions opts = p.getOptions(); - final int limit = opts.limit(); - int pageSize = opts.pageSize(); - int pageSizeMultiplier = opts.pageSizeMultiplier(); - Object searchAfter = resultSet.searchAfter(); - int nextStart = pageResultSize; - while (pageResultSize == pageSize && r.size() <= limit) { // get 1 more than the limit - pageSize = getNextPageSize(pageSize, pageSizeMultiplier); - ResultSet<T> next = - indexConfig.paginationType().equals(PaginationType.SEARCH_AFTER) - ? p.restart(searchAfter, pageSize) - : p.restart(nextStart, pageSize); - pageResultSize = 0; - for (T data : buffer(next)) { - if (match(data)) { - r.add(data); + if (last != null && source instanceof Paginated) { + // TODO: this fix is only for the stable branches and the real refactoring would be to + // restore the logic + // for the filtering in AndSource (L58 - 64) as per + // https://gerrit-review.googlesource.com/c/gerrit/+/345634/9 + if (!indexConfig.paginationType().equals(PaginationType.NONE)) { + // Restart source and continue if we have not filled the + // full limit the caller wants. + // + @SuppressWarnings("unchecked") + Paginated<T> p = (Paginated<T>) source; + QueryOptions opts = p.getOptions(); + final int limit = opts.limit(); + int pageSize = opts.pageSize(); + int pageSizeMultiplier = opts.pageSizeMultiplier(); + Object searchAfter = resultSet.searchAfter(); + int nextStart = pageResultSize; + while (pageResultSize == pageSize && r.size() <= limit) { // get 1 more than the limit + pageSize = getNextPageSize(pageSize, pageSizeMultiplier); + ResultSet<T> next = + indexConfig.paginationType().equals(PaginationType.SEARCH_AFTER) + ? p.restart(searchAfter, pageSize) + : p.restart(nextStart, pageSize); + pageResultSize = 0; + for (T data : buffer(next)) { + if (match(data)) { + r.add(data); + } + pageResultSize++; + } + nextStart += pageResultSize; + searchAfter = next.searchAfter(); + } + } else { + @SuppressWarnings("unchecked") + Paginated<T> p = (Paginated<T>) source; + while (skipped && r.size() < p.getOptions().limit() + start) { + skipped = false; + ResultSet<T> next = p.restart(pageResultSize); + + for (T data : buffer(next)) { + if (match(data)) { + r.add(data); + } else { + skipped = true; + } + pageResultSize++; } - pageResultSize++; } - nextStart += pageResultSize; - searchAfter = next.searchAfter(); } } |