|
17 | 17 |
|
18 | 18 | import com.google.api.core.InternalApi; |
19 | 19 | import com.google.cloud.bigtable.data.v2.models.Filters; |
| 20 | +import javax.annotation.Nullable; |
20 | 21 |
|
21 | 22 | /** |
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. |
| 23 | + * A paginator for fetching large rows from Bigtable chunk by chunk to avoid the 256MB size limit. |
| 24 | + * It yields Filters that chunk the row by cell limits and handles limit halving if |
| 25 | + * FAILED_PRECONDITION occurs. |
24 | 26 | */ |
25 | 27 | @InternalApi("For internal usage only") |
26 | 28 | public class LargeRowPaginator { |
27 | | - private int currentLimit; |
28 | | - private int currentOffset; |
29 | | - private boolean hasMore; |
| 29 | + private int currentLimit; |
| 30 | + private int currentOffset; |
| 31 | + private boolean hasMore; |
| 32 | + @Nullable private final Filters.Filter baseFilter; |
30 | 33 |
|
31 | | - public LargeRowPaginator(int initialLimit) { |
32 | | - this.currentLimit = initialLimit; |
33 | | - this.currentOffset = 0; |
34 | | - this.hasMore = true; |
35 | | - } |
| 34 | + public LargeRowPaginator(int initialLimit, @Nullable Filters.Filter filter) { |
| 35 | + this.currentLimit = initialLimit; |
| 36 | + this.currentOffset = 0; |
| 37 | + this.hasMore = true; |
| 38 | + this.baseFilter = filter; |
| 39 | + } |
36 | 40 |
|
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; |
| 41 | + /** Yields the filter required to fetch the next chunk of cells for the large row. */ |
| 42 | + public Filters.Filter getNextFilter() { |
| 43 | + Filters.ChainFilter chain = Filters.FILTERS.chain(); |
| 44 | + if (baseFilter != null) { |
| 45 | + chain.filter(baseFilter); |
47 | 46 | } |
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; |
| 47 | + if (currentOffset > 0) { |
| 48 | + chain.filter(Filters.FILTERS.offset().cellsPerRow(currentOffset)); |
62 | 49 | } |
| 50 | + chain.filter(Filters.FILTERS.limit().cellsPerRow(currentLimit)); |
| 51 | + return chain; |
| 52 | + } |
63 | 53 |
|
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 | | - } |
| 54 | + /** |
| 55 | + * Advances the internal offset. Call this after a successful Bigtable API call. |
| 56 | + * |
| 57 | + * @param cellsReadInLastChunk The number of cells returned in the last chunk. |
| 58 | + * @return true if there are potentially more cells to fetch. |
| 59 | + */ |
| 60 | + public boolean advance(int cellsReadInLastChunk) { |
| 61 | + this.currentOffset += cellsReadInLastChunk; |
| 62 | + |
| 63 | + // If we read fewer cells than requested, we've hit the end of the row. |
| 64 | + if (cellsReadInLastChunk < currentLimit) { |
| 65 | + this.hasMore = false; |
73 | 66 | } |
| 67 | + return this.hasMore; |
| 68 | + } |
74 | 69 |
|
75 | | - public boolean hasNext() { |
76 | | - return this.hasMore; |
| 70 | + /** |
| 71 | + * Call this if the Bigtable API call fails with a FAILED_PRECONDITION due to size limits. It |
| 72 | + * reduces the batch size to fetch a smaller chunk on the next attempt. |
| 73 | + */ |
| 74 | + public void halveLimit() { |
| 75 | + this.currentLimit /= 2; |
| 76 | + if (this.currentLimit == 0) { |
| 77 | + throw new RuntimeException("Cannot divide limit further. A single cell might be too large."); |
77 | 78 | } |
| 79 | + } |
| 80 | + |
| 81 | + public boolean hasNext() { |
| 82 | + return this.hasMore; |
| 83 | + } |
78 | 84 | } |
0 commit comments