dimas-b commented on code in PR #1938:
URL: https://github.com/apache/polaris/pull/1938#discussion_r2168018610


##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/Page.java:
##########
@@ -18,25 +18,99 @@
  */
 package org.apache.polaris.core.persistence.pagination;
 
+import static java.util.Spliterators.iterator;
+
+import jakarta.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
- * An immutable page of items plus their paging cursor. The {@link PageToken} 
here can be used to
- * continue the listing operation that generated the `items`.
+ * An immutable page of items plus the next-page token value, if there are 
more items. The {@link
+ * #encodedResponseToken()} here can be used to continue the listing operation 
that generated the
+ * `items`.
  */
 public class Page<T> {
-  public final PageToken pageToken;
-  public final List<T> items;
+  private final PageToken request;
+  private final List<T> items;
+  @Nullable private final Token nextToken;
 
-  public Page(PageToken pageToken, List<T> items) {
-    this.pageToken = pageToken;
+  private Page(PageToken request, @Nullable Token nextToken, List<T> items) {
+    this.request = request;
+    this.nextToken = nextToken;
     this.items = items;
   }
 
   /**
-   * Used to wrap a {@link List<T>} of items into a {@link Page <T>} when 
there are no more pages
+   * Builds a complete response page for the full list of relevant items. No 
subsequence pages of
+   * related data exist.
    */
   public static <T> Page<T> fromItems(List<T> items) {
-    return new Page<>(new DonePageToken(), items);
+    return new Page<>(PageToken.readEverything(), null, items);
+  }
+
+  /**
+   * Produces a response page by consuming the number of items from the 
provided stream according to
+   * the {@code request} parameter. Source items can be converted to a 
different type by providing a
+   * {@code mapper} function. The page token for the response will be produced 
from the request data
+   * combined with the pointer to the next page of data provided by the {@code 
dataPointer}
+   * function.
+   *
+   * @param request defines pagination parameters that were uses to produce 
this page of data.
+   * @param items stream of source data
+   * @param mapper converter from source data types to response data types.
+   * @param tokenBuilder determines the {@link Token} used to start the next 
page of data given the
+   *     last item from the previous page. The output of this function will be 
available from {@link
+   *     PageToken#value()} associated with the request for the next page.
+   */
+  public static <R, T> Page<R> mapped(
+      PageToken request, Stream<T> items, Function<T, R> mapper, Function<T, 
Token> tokenBuilder) {
+    List<R> data;
+    T last = null;
+    if (!request.paginationRequested()) {
+      // short-cut for "no pagination"
+      data = items.map(mapper).collect(Collectors.toList());
+    } else {
+      data = new ArrayList<>(request.pageSize().orElse(10));
+
+      Iterator<T> it = iterator(items.spliterator());
+      int limit = request.pageSize().orElse(Integer.MAX_VALUE);
+      while (it.hasNext() && data.size() < limit) {
+        last = it.next();
+        data.add(mapper.apply(last));
+      }
+
+      // Signal "no more data" if the number of items is less than the 
requested page size or if
+      // there is no more data.
+      if (data.size() < limit || !it.hasNext()) {

Review Comment:
   Good point! I wonder why it does not break any tests, though :thinking: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to