eric-maynard commented on code in PR #273: URL: https://github.com/apache/polaris/pull/273#discussion_r1750386983
########## polaris-core/src/main/java/org/apache/polaris/core/catalog/PageToken.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.catalog; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.List; + +/** + * Represents a page token that can be used by operations like `listTables`. Clients that specify a + * `pageSize` (or a `pageToken`) may receive a `next-page-token` in the response, the content of + * which is a serialized PageToken. + * + * <p>By providing that in the next query's `pageToken`, the client can resume listing where they + * 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 class PageToken { + + public final int offset; + public final int pageSize; + + private static final String TOKEN_PREFIX = "polaris"; + private static final int TOKEN_START = 0; + private static final int DEFAULT_PAGE_SIZE = 1000; + + public static PageToken DONE = null; + + public PageToken(int offset, int pageSize) { + this.offset = offset; + this.pageSize = pageSize; + } + + /** Construct a PageToken from a plain limit */ + public static PageToken fromLimit(int limit) { + return new PageToken(TOKEN_START, limit); + } + + /** Construct a PageToken to read everything */ + public static PageToken readEverything() { + return new PageToken(TOKEN_START, Integer.MAX_VALUE); + } + + /** Deserialize a token string into a PageToken object */ + public static PageToken fromString(String tokenString) { + if (tokenString == null) { + return PageToken.readEverything(); + } else if (tokenString.isEmpty()) { + return PageToken.fromLimit(DEFAULT_PAGE_SIZE); + } else { + try { + String decoded = + new String(Base64.getDecoder().decode(tokenString), StandardCharsets.UTF_8); + String[] parts = decoded.split(":"); + + if (parts.length != 4 || !parts[0].equals(TOKEN_PREFIX)) { + throw new IllegalArgumentException("Invalid token format in token: " + tokenString); + } + + int offset = Integer.parseInt(parts[1]); + int pageSize = Integer.parseInt(parts[2]); + int checksum = Integer.parseInt(parts[3]); + PageToken token = new PageToken(offset, pageSize); + + if (token.hashCode() != checksum) { + throw new IllegalArgumentException("Invalid checksum for token: " + tokenString); + } else { + return token; + } + } catch (Exception e) { + throw new IllegalArgumentException("Failed to decode page token: " + tokenString, e); + } + } + } + + /** + * Builds a new page token to reflect new data that's been read. If the amount of data read is + * less than the pageSize, this will return `PageToken.DONE` (done) + */ + public PageToken updated(List<?> newData) { + if (newData == null || newData.isEmpty() || newData.size() < pageSize) { Review Comment: Ah, I see. I have added a new test case [here](https://github.com/apache/polaris/pull/273/files#diff-465cb011ce85cca2fdfcfa100ca31ae8e4300734597ce87cfed6b240e63658a0R1344) to hopefully clarify this behavior. It is true that a potentially wasteful listing operation could happen. So if there are 5000 entities and you use the default page size of 1000, then you will need to do **6** listing operations right now. Of course, this problem goes away if there are 4999 or 5001 entities. The reason is that we limit the metastore operation to retrieve only 1000 results, and so it has no way of knowing there wasn't actually any more data. This will depend on the metastore implementation of course, but the current eclipselink metastore will exhibit this issue. One potential fix would be to increase the limit to e.g. pageSize + 1 and then to be a little smarter about the token generated for the response. Do you think this is necessary? -- 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]
