snazy commented on code in PR #1838: URL: https://github.com/apache/polaris/pull/1838#discussion_r2137576089
########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageTokenUtil.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.polaris.core.persistence.pagination; + +import static com.google.api.client.util.Preconditions.checkArgument; + +import jakarta.annotation.Nullable; +import java.util.Optional; + +public final class PageTokenUtil { Review Comment: This can become package-protected. Only non-package reference is from the unused function IcebergCatalog.buildPageRequest ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageRequest.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.polaris.core.persistence.pagination; + +import static com.google.common.base.Preconditions.checkState; + +import jakarta.annotation.Nullable; + +/** A wrapper for pagination information passed in as part of a request. */ +public class PageRequest { + private final @Nullable String innerPageToken; + private final int pageSize; + + PageRequest(@Nullable String innerPageToken, int pageSize) { + this.innerPageToken = innerPageToken; + this.pageSize = pageSize; + } + + /** Represents a non-paginated request. */ + public static PageRequest readEverything() { + return new PageRequest(null, -1); + } + + /** Represents a request to start paginating with a particular page size. */ + public static PageRequest firstPage(int limit) { Review Comment: What's the general use case of this one? It's only used in some `drop*()` _implementation_. ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/Page.java: ########## @@ -18,25 +18,84 @@ */ 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`. */ public class Page<T> { - public final PageToken pageToken; - public final List<T> items; + private final PageRequest request; + private final PageToken nextToken; + private final List<T> items; - public Page(PageToken pageToken, List<T> items) { - this.pageToken = pageToken; + private Page(PageRequest request, PageToken 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 - */ - public static <T> Page<T> fromItems(List<T> items) { - return new Page<>(new DonePageToken(), items); + public static <T> Page<T> allItems(List<T> items) { + return new Page<>(PageRequest.readEverything(), () -> null, items); + } + + public static <T> Page<T> fromStream( Review Comment: This one is only called from tests. Maybe remove this one and rename `mapped` to `fromStream`? ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageToken.java: ########## @@ -30,70 +29,8 @@ * left off. If the client provides a `pageToken` or `pageSize` but `next-page-token` is null in the * response, that means there is no more data to read. */ -public abstract class PageToken { - - /** Build a new PageToken that reads everything */ - public static PageToken readEverything() { - return build(null, null); - } - - /** Build a new PageToken from an input String, without a specified page size */ - public static PageToken fromString(String token) { - return build(token, null); - } - - /** Build a new PageToken from a limit */ - public static PageToken fromLimit(Integer pageSize) { - return build(null, pageSize); - } - - /** Build a {@link PageToken} from the input string and page size */ - public static PageToken build(String token, Integer pageSize) { - if (token == null || token.isEmpty()) { - if (pageSize != null) { - return new LimitPageToken(pageSize); - } else { - return new ReadEverythingPageToken(); - } - } else { - // TODO implement, split out by the token's prefix - throw new IllegalArgumentException("Unrecognized page token: " + token); - } - } - +public abstract interface PageToken { Review Comment: ```suggestion public interface PageToken { ``` nit: superfluous keyword ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageToken.java: ########## @@ -18,8 +18,7 @@ */ package org.apache.polaris.core.persistence.pagination; -import java.util.List; -import java.util.Objects; +import jakarta.annotation.Nullable; /** * Represents a page token that can be used by operations like `listTables`. Clients that specify a Review Comment: Looks like the javadoc needs to be adapted. ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/EntityIdPageToken.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.polaris.core.persistence.pagination; + +import java.util.List; +import org.apache.polaris.core.entity.PolarisBaseEntity; + +public class EntityIdPageToken implements PageToken { + private final boolean hasMore; + private final boolean hasPrevious; + private final long entityId; + + private EntityIdPageToken(boolean hasMore, boolean hasPrevious, long entityId) { + this.hasMore = hasMore; + this.hasPrevious = hasPrevious; + this.entityId = entityId; + } + + public static EntityIdPageToken forPage(List<PolarisBaseEntity> data) { + if (data.isEmpty()) { + return forLastItem(null); + } + + return forLastItem(data.get(data.size() - 1)); + } + + public static EntityIdPageToken forLastItem(PolarisBaseEntity entity) { Review Comment: Shouldn't the parameter be `@Nonnull`, as the function name says `forLastItem`? ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/EntityIdPageToken.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.polaris.core.persistence.pagination; + +import java.util.List; +import org.apache.polaris.core.entity.PolarisBaseEntity; + +public class EntityIdPageToken implements PageToken { + private final boolean hasMore; + private final boolean hasPrevious; + private final long entityId; + + private EntityIdPageToken(boolean hasMore, boolean hasPrevious, long entityId) { + this.hasMore = hasMore; + this.hasPrevious = hasPrevious; + this.entityId = entityId; + } + + public static EntityIdPageToken forPage(List<PolarisBaseEntity> data) { + if (data.isEmpty()) { + return forLastItem(null); + } + + return forLastItem(data.get(data.size() - 1)); + } + + public static EntityIdPageToken forLastItem(PolarisBaseEntity entity) { + if (entity == null) { + return new EntityIdPageToken(false, false, 0); + } + return new EntityIdPageToken(true, true, entity.getId()); + } + + /** + * Extracts an {@link EntityIdPageToken} from a {@link PageRequest} assuming the request is a + * continuation of the API call that previously produced an {@link EntityIdPageToken}. + */ + public static EntityIdPageToken fromPageRequest(PageRequest pageRequest) { + String token = pageRequest.innerPageToken(); + if (token == null) { + return new EntityIdPageToken(false, false, 0); + } else { + return new EntityIdPageToken(false, true, Long.parseLong(token)); + } + } + + public long getId() { + return entityId; + } + + public boolean hasPrevious() { Review Comment: Not sure I understand the meaning of `hasPrevious`. Doesn't the presence of a page-token implicitly mean that there's something "before"? ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageTokenUtil.java: ########## @@ -0,0 +1,61 @@ +/* + * 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.polaris.core.persistence.pagination; + +import static com.google.api.client.util.Preconditions.checkArgument; + +import jakarta.annotation.Nullable; +import java.util.Optional; + +public final class PageTokenUtil { + + private PageTokenUtil() {} + + public static PageRequest decodePageRequest( + @Nullable String requestedPageToken, @Nullable Integer requestedPageSize) { + int pageSize; + String token = null; + if (requestedPageToken != null) { + int idx = requestedPageToken.indexOf(':'); Review Comment: I know it's not a new thing to this PR, but I don't understand why the size of the _previous_ page matters at all. AFAIK the page-size is included in every request. Feels a bit weird to have an "outer" and "inner" meaning of an otherwise opaque token. ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/EntityIdPageToken.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.polaris.core.persistence.pagination; + +import java.util.List; +import org.apache.polaris.core.entity.PolarisBaseEntity; + +public class EntityIdPageToken implements PageToken { + private final boolean hasMore; Review Comment: Never used ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/EntityIdPageToken.java: ########## @@ -0,0 +1,79 @@ +/* + * 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.polaris.core.persistence.pagination; + +import java.util.List; +import org.apache.polaris.core.entity.PolarisBaseEntity; + +public class EntityIdPageToken implements PageToken { + private final boolean hasMore; + private final boolean hasPrevious; + private final long entityId; + + private EntityIdPageToken(boolean hasMore, boolean hasPrevious, long entityId) { + this.hasMore = hasMore; + this.hasPrevious = hasPrevious; + this.entityId = entityId; + } + + public static EntityIdPageToken forPage(List<PolarisBaseEntity> data) { Review Comment: This one's unused ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageRequest.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.polaris.core.persistence.pagination; + +import static com.google.common.base.Preconditions.checkState; + +import jakarta.annotation.Nullable; + +/** A wrapper for pagination information passed in as part of a request. */ +public class PageRequest { + private final @Nullable String innerPageToken; + private final int pageSize; + + PageRequest(@Nullable String innerPageToken, int pageSize) { + this.innerPageToken = innerPageToken; + this.pageSize = pageSize; + } + + /** Represents a non-paginated request. */ + public static PageRequest readEverything() { + return new PageRequest(null, -1); + } + + /** Represents a request to start paginating with a particular page size. */ + public static PageRequest firstPage(int limit) { + return new PageRequest(null, limit); + } + + /** + * Reconstructs a page request from the previous API-level page token and an API-level new + * requested page size. + * + * @param previousPageToken page token from the {@link Page#encodedResponseToken() previous page} + * @param requestedPageSize optional page size for the next page. If not set, the page size of the + * previus page (encoded in the previous page token) will be reused. + */ + public static PageRequest nextPage( Review Comment: The function name and phrase 'from the previous API-level...` are a bit confusing: what's "next"? What's "previous"? Why's there no "current"? All it does is constructing a `PageRequest` from the two pagination-parameters. Why not call this function `constructFromParameters`? ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/pagination/PageRequest.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.polaris.core.persistence.pagination; + +import static com.google.common.base.Preconditions.checkState; + +import jakarta.annotation.Nullable; + +/** A wrapper for pagination information passed in as part of a request. */ +public class PageRequest { + private final @Nullable String innerPageToken; + private final int pageSize; + + PageRequest(@Nullable String innerPageToken, int pageSize) { + this.innerPageToken = innerPageToken; + this.pageSize = pageSize; + } + + /** Represents a non-paginated request. */ + public static PageRequest readEverything() { Review Comment: Maybe rename to `notPaged()`? -- 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]
