|
20 | 20 | import com.google.api.gax.rpc.ApiExceptions; |
21 | 21 | import com.google.api.gax.rpc.ApiStreamObserver; |
22 | 22 | import com.google.cloud.firestore.v1.FirestoreClient; |
| 23 | +import com.google.common.base.Preconditions; |
23 | 24 | import com.google.firestore.v1.Cursor; |
24 | 25 | import com.google.firestore.v1.PartitionQueryRequest; |
25 | 26 | import io.opencensus.common.Scope; |
@@ -53,42 +54,51 @@ public class CollectionGroup extends Query { |
53 | 54 | */ |
54 | 55 | public void getPartitions( |
55 | 56 | long desiredPartitionCount, ApiStreamObserver<QueryPartition> observer) { |
| 57 | + Preconditions.checkArgument( |
| 58 | + desiredPartitionCount > 0, "Desired partition count must be one or greater"); |
| 59 | + |
56 | 60 | // Partition queries require explicit ordering by __name__. |
57 | 61 | Query queryWithDefaultOrder = orderBy(FieldPath.DOCUMENT_ID); |
58 | 62 |
|
59 | | - PartitionQueryRequest.Builder request = PartitionQueryRequest.newBuilder(); |
60 | | - request.setStructuredQuery(queryWithDefaultOrder.buildQuery()); |
61 | | - request.setParent(options.getParentPath().toString()); |
| 63 | + if (desiredPartitionCount == 1) { |
| 64 | + // Short circuit if the user only requested a single partition. |
| 65 | + observer.onNext(new QueryPartition(queryWithDefaultOrder, null, null)); |
| 66 | + } else { |
| 67 | + PartitionQueryRequest.Builder request = PartitionQueryRequest.newBuilder(); |
| 68 | + request.setStructuredQuery(queryWithDefaultOrder.buildQuery()); |
| 69 | + request.setParent(options.getParentPath().toString()); |
62 | 70 |
|
63 | | - // Since we are always returning an extra partition (with en empty endBefore cursor), we |
64 | | - // reduce the desired partition count by one. |
65 | | - request.setPartitionCount(desiredPartitionCount - 1); |
| 71 | + // Since we are always returning an extra partition (with en empty endBefore cursor), we |
| 72 | + // reduce the desired partition count by one. |
| 73 | + request.setPartitionCount(desiredPartitionCount - 1); |
66 | 74 |
|
67 | | - final FirestoreClient.PartitionQueryPagedResponse response; |
68 | | - final TraceUtil traceUtil = TraceUtil.getInstance(); |
69 | | - Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_PARTITIONQUERY); |
70 | | - try (Scope scope = traceUtil.getTracer().withSpan(span)) { |
71 | | - response = |
72 | | - ApiExceptions.callAndTranslateApiException( |
73 | | - rpcContext.sendRequest( |
74 | | - request.build(), rpcContext.getClient().partitionQueryPagedCallable())); |
75 | | - } catch (ApiException exception) { |
76 | | - span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage())); |
77 | | - throw FirestoreException.apiException(exception); |
78 | | - } finally { |
79 | | - span.end(TraceUtil.END_SPAN_OPTIONS); |
80 | | - } |
| 75 | + final FirestoreClient.PartitionQueryPagedResponse response; |
| 76 | + final TraceUtil traceUtil = TraceUtil.getInstance(); |
| 77 | + Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_PARTITIONQUERY); |
| 78 | + try (Scope scope = traceUtil.getTracer().withSpan(span)) { |
| 79 | + response = |
| 80 | + ApiExceptions.callAndTranslateApiException( |
| 81 | + rpcContext.sendRequest( |
| 82 | + request.build(), rpcContext.getClient().partitionQueryPagedCallable())); |
| 83 | + } catch (ApiException exception) { |
| 84 | + span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage())); |
| 85 | + throw FirestoreException.apiException(exception); |
| 86 | + } finally { |
| 87 | + span.end(TraceUtil.END_SPAN_OPTIONS); |
| 88 | + } |
81 | 89 |
|
82 | | - @Nullable Object[] lastCursor = null; |
83 | | - for (Cursor cursor : response.iterateAll()) { |
84 | | - Object[] decodedCursorValue = new Object[cursor.getValuesCount()]; |
85 | | - for (int i = 0; i < cursor.getValuesCount(); ++i) { |
86 | | - decodedCursorValue[i] = UserDataConverter.decodeValue(rpcContext, cursor.getValues(i)); |
| 90 | + @Nullable Object[] lastCursor = null; |
| 91 | + for (Cursor cursor : response.iterateAll()) { |
| 92 | + Object[] decodedCursorValue = new Object[cursor.getValuesCount()]; |
| 93 | + for (int i = 0; i < cursor.getValuesCount(); ++i) { |
| 94 | + decodedCursorValue[i] = UserDataConverter.decodeValue(rpcContext, cursor.getValues(i)); |
| 95 | + } |
| 96 | + observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, decodedCursorValue)); |
| 97 | + lastCursor = decodedCursorValue; |
87 | 98 | } |
88 | | - observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, decodedCursorValue)); |
89 | | - lastCursor = decodedCursorValue; |
| 99 | + observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, null)); |
90 | 100 | } |
91 | | - observer.onNext(new QueryPartition(queryWithDefaultOrder, lastCursor, null)); |
| 101 | + |
92 | 102 | observer.onCompleted(); |
93 | 103 | } |
94 | 104 | } |
0 commit comments