|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | + * http://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 | + |
| 17 | +package com.google.cloud.firestore; |
| 18 | + |
| 19 | +import com.google.api.core.ApiFuture; |
| 20 | +import com.google.api.core.InternalExtensionOnly; |
| 21 | +import com.google.api.core.SettableApiFuture; |
| 22 | +import com.google.api.gax.rpc.ResponseObserver; |
| 23 | +import com.google.api.gax.rpc.ServerStreamingCallable; |
| 24 | +import com.google.api.gax.rpc.StreamController; |
| 25 | +import com.google.cloud.Timestamp; |
| 26 | +import com.google.firestore.v1.RunAggregationQueryRequest; |
| 27 | +import com.google.firestore.v1.RunAggregationQueryResponse; |
| 28 | +import com.google.firestore.v1.RunQueryRequest; |
| 29 | +import com.google.firestore.v1.StructuredAggregationQuery; |
| 30 | +import com.google.firestore.v1.Value; |
| 31 | +import com.google.protobuf.ByteString; |
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 33 | +import javax.annotation.Nonnull; |
| 34 | +import javax.annotation.Nullable; |
| 35 | + |
| 36 | +// TODO(count) Make this class public |
| 37 | +@InternalExtensionOnly |
| 38 | +class AggregateQuery { |
| 39 | + |
| 40 | + /** |
| 41 | + * The "alias" to specify in the {@link RunAggregationQueryRequest} proto when running a count |
| 42 | + * query. The actual value is not meaningful, but will be used to get the count out of the {@link |
| 43 | + * RunAggregationQueryResponse}. |
| 44 | + */ |
| 45 | + private static final String ALIAS_COUNT = "count"; |
| 46 | + |
| 47 | + @Nonnull private final Query query; |
| 48 | + |
| 49 | + AggregateQuery(@Nonnull Query query) { |
| 50 | + this.query = query; |
| 51 | + } |
| 52 | + |
| 53 | + @Nonnull |
| 54 | + public Query getQuery() { |
| 55 | + return query; |
| 56 | + } |
| 57 | + |
| 58 | + @Nonnull |
| 59 | + public ApiFuture<AggregateQuerySnapshot> get() { |
| 60 | + return get(null); |
| 61 | + } |
| 62 | + |
| 63 | + @Nonnull |
| 64 | + ApiFuture<AggregateQuerySnapshot> get(@Nullable final ByteString transactionId) { |
| 65 | + RunAggregationQueryRequest request = toProto(transactionId); |
| 66 | + AggregateQueryResponseObserver responseObserver = new AggregateQueryResponseObserver(); |
| 67 | + ServerStreamingCallable<RunAggregationQueryRequest, RunAggregationQueryResponse> callable = |
| 68 | + query.rpcContext.getClient().runAggregationQueryCallable(); |
| 69 | + |
| 70 | + query.rpcContext.streamRequest(request, responseObserver, callable); |
| 71 | + |
| 72 | + return responseObserver.getFuture(); |
| 73 | + } |
| 74 | + |
| 75 | + private final class AggregateQueryResponseObserver |
| 76 | + implements ResponseObserver<RunAggregationQueryResponse> { |
| 77 | + |
| 78 | + private final SettableApiFuture<AggregateQuerySnapshot> future = SettableApiFuture.create(); |
| 79 | + private final AtomicBoolean isFutureNotified = new AtomicBoolean(false); |
| 80 | + private StreamController streamController; |
| 81 | + |
| 82 | + SettableApiFuture<AggregateQuerySnapshot> getFuture() { |
| 83 | + return future; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onStart(StreamController streamController) { |
| 88 | + this.streamController = streamController; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onResponse(RunAggregationQueryResponse response) { |
| 93 | + if (!isFutureNotified.compareAndSet(false, true)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + Timestamp readTime = Timestamp.fromProto(response.getReadTime()); |
| 98 | + Value value = response.getResult().getAggregateFieldsMap().get(ALIAS_COUNT); |
| 99 | + if (value == null) { |
| 100 | + throw new IllegalArgumentException( |
| 101 | + "RunAggregationQueryResponse is missing required alias: " + ALIAS_COUNT); |
| 102 | + } else if (value.getValueTypeCase() != Value.ValueTypeCase.INTEGER_VALUE) { |
| 103 | + throw new IllegalArgumentException( |
| 104 | + "RunAggregationQueryResponse alias " |
| 105 | + + ALIAS_COUNT |
| 106 | + + " has incorrect type: " |
| 107 | + + value.getValueTypeCase()); |
| 108 | + } |
| 109 | + long count = value.getIntegerValue(); |
| 110 | + |
| 111 | + future.set(new AggregateQuerySnapshot(AggregateQuery.this, readTime, count)); |
| 112 | + |
| 113 | + // Close the stream to avoid it dangling, since we're not expecting any more responses. |
| 114 | + streamController.cancel(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onError(Throwable throwable) { |
| 119 | + if (!isFutureNotified.compareAndSet(false, true)) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + future.setException(throwable); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onComplete() {} |
| 128 | + } |
| 129 | + |
| 130 | + @Nonnull |
| 131 | + public RunAggregationQueryRequest toProto() { |
| 132 | + return toProto(null); |
| 133 | + } |
| 134 | + |
| 135 | + @Nonnull |
| 136 | + RunAggregationQueryRequest toProto(@Nullable final ByteString transactionId) { |
| 137 | + RunQueryRequest runQueryRequest = query.toProto(); |
| 138 | + |
| 139 | + RunAggregationQueryRequest.Builder request = RunAggregationQueryRequest.newBuilder(); |
| 140 | + request.setParent(runQueryRequest.getParent()); |
| 141 | + if (transactionId != null) { |
| 142 | + request.setTransaction(transactionId); |
| 143 | + } |
| 144 | + |
| 145 | + StructuredAggregationQuery.Builder structuredAggregationQuery = |
| 146 | + request.getStructuredAggregationQueryBuilder(); |
| 147 | + structuredAggregationQuery.setStructuredQuery(runQueryRequest.getStructuredQuery()); |
| 148 | + |
| 149 | + StructuredAggregationQuery.Aggregation.Builder aggregation = |
| 150 | + StructuredAggregationQuery.Aggregation.newBuilder(); |
| 151 | + aggregation.setCount(StructuredAggregationQuery.Aggregation.Count.getDefaultInstance()); |
| 152 | + aggregation.setAlias(ALIAS_COUNT); |
| 153 | + structuredAggregationQuery.addAggregations(aggregation); |
| 154 | + |
| 155 | + return request.build(); |
| 156 | + } |
| 157 | + |
| 158 | + @Nonnull |
| 159 | + public static AggregateQuery fromProto(Firestore firestore, RunAggregationQueryRequest proto) { |
| 160 | + RunQueryRequest runQueryRequest = |
| 161 | + RunQueryRequest.newBuilder() |
| 162 | + .setParent(proto.getParent()) |
| 163 | + .setStructuredQuery(proto.getStructuredAggregationQuery().getStructuredQuery()) |
| 164 | + .build(); |
| 165 | + Query query = Query.fromProto(firestore, runQueryRequest); |
| 166 | + return new AggregateQuery(query); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public int hashCode() { |
| 171 | + return query.hashCode(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public boolean equals(Object obj) { |
| 176 | + if (obj == this) { |
| 177 | + return true; |
| 178 | + } else if (!(obj instanceof AggregateQuery)) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + AggregateQuery other = (AggregateQuery) obj; |
| 182 | + return query.equals(other.query); |
| 183 | + } |
| 184 | +} |
0 commit comments