roryqi commented on code in PR #10671: URL: https://github.com/apache/gravitino/pull/10671#discussion_r3296506508
########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergPaginationHelper.java: ########## @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.iceberg.service.rest; + +import com.google.common.base.Preconditions; +import java.util.List; +import javax.annotation.Nullable; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.rest.responses.ListNamespacesResponse; +import org.apache.iceberg.rest.responses.ListTablesResponse; + +/** + * Utility for applying offset-based pagination to Iceberg list responses. + * + * <p><b>Known limitations:</b> + * + * <ul> + * <li>This is in-memory pagination: the full item list is materialized from the underlying + * catalog on every page request, so server-side memory usage is unchanged. Paging through N + * items is O(N²) total work. True server-push-down pagination depends on catalog + * implementations adding native support. + * <li>Offset-based page tokens assume a stable, deterministic ordering across calls. Concurrent + * create/drop operations between pages can cause items to be skipped or duplicated. + * <li>When {@code pageToken} is provided without {@code pageSize}, the response returns all items + * from that offset onward with no {@code nextPageToken}. + * </ul> + */ +class IcebergPaginationHelper { + + private IcebergPaginationHelper() {} + + /** + * Paginate a {@link ListNamespacesResponse}. + * + * @param response the full (unpaginated) response + * @param pageToken offset-based page token, or {@code null} for the first page + * @param pageSize maximum items per page, or {@code null} for no limit + * @return a new response containing only the requested page + */ + static ListNamespacesResponse paginateNamespaces( + ListNamespacesResponse response, @Nullable String pageToken, @Nullable Integer pageSize) { + if (pageSize == null && (pageToken == null || pageToken.isEmpty())) { + return response; + } + List<Namespace> all = response.namespaces(); + int offset = parsePageToken(pageToken); + int limit = pageSize != null ? pageSize : all.size(); + Preconditions.checkArgument(limit > 0, "pageSize must be positive, got: %s", limit); + if (offset >= all.size()) { + return ListNamespacesResponse.builder().build(); + } + int end = Math.min(offset + limit, all.size()); + List<Namespace> page = all.subList(offset, end); + ListNamespacesResponse.Builder builder = ListNamespacesResponse.builder().addAll(page); + if (end < all.size()) { + builder.nextPageToken(String.valueOf(end)); + } + return builder.build(); + } + + /** + * Paginate a {@link ListTablesResponse}. Works for both table and view list responses. + * + * @param response the full (unpaginated) response + * @param pageToken offset-based page token, or {@code null} for the first page + * @param pageSize maximum items per page, or {@code null} for no limit + * @return a new response containing only the requested page + */ + static ListTablesResponse paginateTables( Review Comment: paginateTables and paginateNamespaces has similar logic using the different constructor method. Maybe you can extract a internal method like ``` private T paginate(T response, Optional<String>, Optional<Integer> pageSize, T -> create(List<S> objects) ``` So we can reuse the duplicated logic. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
