|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.data.v2.stub.readrows; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.cloud.bigtable.data.v2.models.Filters; |
| 20 | + |
| 21 | +/** |
| 22 | + * A paginator for fetching extremely large rows from Bigtable chunk by chunk to avoid the 256MB gRPC size limit. |
| 23 | + * It yields Filters that chunk the row by cell limits and handles limit halving if FAILED_PRECONDITION occurs. |
| 24 | + */ |
| 25 | +@InternalApi("For internal usage only") |
| 26 | +public class LargeRowPaginator { |
| 27 | + private int currentLimit; |
| 28 | + private int currentOffset; |
| 29 | + private boolean hasMore; |
| 30 | + |
| 31 | + public LargeRowPaginator(int initialLimit) { |
| 32 | + this.currentLimit = initialLimit; |
| 33 | + this.currentOffset = 0; |
| 34 | + this.hasMore = true; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Yields the filter required to fetch the next chunk of cells for the large row. |
| 39 | + */ |
| 40 | + public Filters.Filter getNextFilter() { |
| 41 | + Filters.ChainFilter chain = Filters.FILTERS.chain(); |
| 42 | + if (currentOffset > 0) { |
| 43 | + chain.filter(Filters.FILTERS.offset().cellsPerRow(currentOffset)); |
| 44 | + } |
| 45 | + chain.filter(Filters.FILTERS.limit().cellsPerRow(currentLimit)); |
| 46 | + return chain; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Advances the internal offset. Call this after a successful Bigtable API call. |
| 51 | + * @param cellsReadInLastChunk The number of cells returned in the last chunk. |
| 52 | + * @return true if there are potentially more cells to fetch. |
| 53 | + */ |
| 54 | + public boolean advance(int cellsReadInLastChunk) { |
| 55 | + this.currentOffset += cellsReadInLastChunk; |
| 56 | + |
| 57 | + // If we read fewer cells than requested, we've hit the end of the row. |
| 58 | + if (cellsReadInLastChunk < currentLimit) { |
| 59 | + this.hasMore = false; |
| 60 | + } |
| 61 | + return this.hasMore; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Call this if the Bigtable API call fails with a FAILED_PRECONDITION due to size limits. |
| 66 | + * It reduces the batch size to fetch a smaller chunk on the next attempt. |
| 67 | + */ |
| 68 | + public void halveLimit() { |
| 69 | + this.currentLimit /= 2; |
| 70 | + if (this.currentLimit == 0) { |
| 71 | + throw new RuntimeException("Cannot divide limit further. A single cell might be too large."); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public boolean hasNext() { |
| 76 | + return this.hasMore; |
| 77 | + } |
| 78 | +} |
0 commit comments