|
| 1 | +/* |
| 2 | + * SizeStatisticsGroupingContinuation.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.provider.foundationdb.cursors; |
| 22 | + |
| 23 | +import com.apple.foundationdb.KeyValue; |
| 24 | +import com.apple.foundationdb.record.RecordCursorContinuation; |
| 25 | +import com.apple.foundationdb.record.RecordCursorEndContinuation; |
| 26 | +import com.apple.foundationdb.record.RecordCursorProto; |
| 27 | +import com.apple.foundationdb.record.RecordCursorResult; |
| 28 | +import com.apple.foundationdb.tuple.Tuple; |
| 29 | +import com.google.protobuf.ByteString; |
| 30 | + |
| 31 | +import javax.annotation.Nonnull; |
| 32 | +import javax.annotation.Nullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * A continuation for the {@link SizeStatisticsGroupingCursor}. |
| 36 | + */ |
| 37 | +class SizeStatisticsGroupingContinuation implements RecordCursorContinuation { |
| 38 | + /** |
| 39 | + * A continuation for the case where we have one last result to send, after which the cursor would be done. |
| 40 | + */ |
| 41 | + public static final SizeStatisticsGroupingContinuation LAST_RESULT_CONTINUATION = new SizeStatisticsGroupingContinuation(); |
| 42 | + |
| 43 | + /** Whether the continuation is a marker for the last result being sent before the cursor is done. */ |
| 44 | + private final boolean lastResultContinuation; |
| 45 | + @Nullable |
| 46 | + private RecordCursorContinuation innerContinuation; |
| 47 | + @Nullable |
| 48 | + private SizeStatisticsResults partialResults; |
| 49 | + private Tuple currentGroupingKey; |
| 50 | + @Nullable |
| 51 | + private byte[] cachedBytes; |
| 52 | + @Nullable |
| 53 | + private ByteString cachedByteString; |
| 54 | + |
| 55 | + /** |
| 56 | + * The inner cursor is done, and we sent the last result, cannot continue afterward. |
| 57 | + */ |
| 58 | + private SizeStatisticsGroupingContinuation() { |
| 59 | + lastResultContinuation = true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The inner cursor still has results, we can continue (e.g. group break). |
| 64 | + */ |
| 65 | + SizeStatisticsGroupingContinuation(@Nonnull RecordCursorResult<KeyValue> currentKvResult, |
| 66 | + @Nonnull SizeStatisticsResults partialResults, |
| 67 | + @Nonnull Tuple currentGroupingKey) { |
| 68 | + lastResultContinuation = false; |
| 69 | + this.innerContinuation = currentKvResult.getContinuation(); |
| 70 | + this.partialResults = partialResults.copy(); //cache an immutable snapshot of the partial aggregate state |
| 71 | + this.currentGroupingKey = currentGroupingKey; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Whether the given protobuf is the same as the "LAST_RESULT_CONTINUATION". |
| 76 | + * @param statsContinuation the incoming protobuf |
| 77 | + * @return TRUE if the protobuf marks a continuation with last result |
| 78 | + */ |
| 79 | + public static boolean isLastResultContinuation(final RecordCursorProto.SizeStatisticsGroupingContinuation statsContinuation) { |
| 80 | + // Default to false if unset |
| 81 | + return (statsContinuation.getLastResultContinuation()); |
| 82 | + } |
| 83 | + |
| 84 | + public boolean isLastResultContinuation() { |
| 85 | + return lastResultContinuation; |
| 86 | + } |
| 87 | + |
| 88 | + @Nullable |
| 89 | + @Override |
| 90 | + public byte[] toBytes() { |
| 91 | + // form bytes exactly once |
| 92 | + if (this.cachedBytes == null) { |
| 93 | + this.cachedBytes = toByteString().toByteArray(); |
| 94 | + } |
| 95 | + return cachedBytes; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @Nonnull |
| 100 | + public ByteString toByteString() { |
| 101 | + if (cachedByteString == null) { |
| 102 | + final RecordCursorProto.SizeStatisticsGroupingContinuation.Builder builder = RecordCursorProto.SizeStatisticsGroupingContinuation.newBuilder(); |
| 103 | + builder.setLastResultContinuation(lastResultContinuation); |
| 104 | + if (innerContinuation != null) { |
| 105 | + builder.setUnderlyingContinuation(innerContinuation.toByteString()); |
| 106 | + } |
| 107 | + if (partialResults != null) { |
| 108 | + builder.setPartialResults(partialResults.toProto()); |
| 109 | + } |
| 110 | + if (currentGroupingKey != null) { |
| 111 | + builder.setCurrentGroupingKey(ByteString.copyFrom(currentGroupingKey.pack())); |
| 112 | + } |
| 113 | + cachedByteString = builder.build().toByteString(); |
| 114 | + } |
| 115 | + return cachedByteString; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Whether this continuation is the end of the stream. |
| 120 | + * This is always false, since the {@link RecordCursorEndContinuation#END} would be used once the cursor is exhausted. |
| 121 | + * @return TRUE if the cursor is exhausted |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public boolean isEnd() { |
| 125 | + return false; |
| 126 | + } |
| 127 | +} |
0 commit comments