-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Count queries (not available for use yet) #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
05f352d
Update protos from preview branch of https://github.com/googleapis/go…
dconeybe 396c22a
update clirr-ignored-differences.xml
dconeybe 70a61ad
fix copyrights in files exported from googleapis-gen
dconeybe 1ae9901
Firestore[Stub]Settings.java: add back newHttpJsonBuilder() and frien…
dconeybe 741c0fa
format code by running mvn com.coveo:fmt-maven-plugin:format
dconeybe 0ded2c0
Merge remote-tracking branch 'origin/main' into count
dconeybe 80189c1
feat: Query.count() implementation, part 1 (#1026)
dconeybe 066718c
Merge remote-tracking branch 'origin/main' into count
dconeybe 23a1557
feat: Transaction.get(AggregateQuery) added (#1029)
dconeybe f3ce77a
Merge remote-tracking branch 'origin/main' into count
dconeybe 8e22716
Fix integration tests to use the default FirestoreOptions
dconeybe b7ec1a1
Merge remote-tracking branch 'origin/main' into count
dconeybe b227ea9
Reduce visibility of COUNT queries until it's ready to be released
dconeybe 08b46b8
ITQueryCountTest.java: fix import of Preconditions from the correct p…
dconeybe 862a8c9
AggregateQuerySnapshot.java: change return value of getCount() to `lo…
dconeybe ddcaf21
AggregateQuery.java: close the stream after receiving the first result
dconeybe 77936cd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 4b8d8f6
LocalFirestoreHelper.java: make sure to call onStart() on the Respons…
dconeybe c963f7b
AggregateQuery.java: simplify the logic to avoid repeated completion …
dconeybe 16af34a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 0d61d13
Convert ITAggregateQueryTest to a unit test
dconeybe 8b8da7a
AggregateQuery.java: improve error messages if bad responses are rece…
dconeybe e8a2f61
QueryCountTest.java unit test added
dconeybe ef39a25
Merge remote-tracking branch 'origin/main' into count
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.firestore; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.InternalExtensionOnly; | ||
| import com.google.api.core.SettableApiFuture; | ||
| import com.google.api.gax.rpc.ResponseObserver; | ||
| import com.google.api.gax.rpc.ServerStreamingCallable; | ||
| import com.google.api.gax.rpc.StreamController; | ||
| import com.google.cloud.Timestamp; | ||
| import com.google.firestore.v1.RunAggregationQueryRequest; | ||
| import com.google.firestore.v1.RunAggregationQueryResponse; | ||
| import com.google.firestore.v1.RunQueryRequest; | ||
| import com.google.firestore.v1.StructuredAggregationQuery; | ||
| import com.google.firestore.v1.Value; | ||
| import com.google.protobuf.ByteString; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| // TODO(count) Make this class public | ||
| @InternalExtensionOnly | ||
| class AggregateQuery { | ||
|
|
||
| /** | ||
| * The "alias" to specify in the {@link RunAggregationQueryRequest} proto when running a count | ||
| * query. The actual value is not meaningful, but will be used to get the count out of the {@link | ||
| * RunAggregationQueryResponse}. | ||
| */ | ||
| private static final String ALIAS_COUNT = "count"; | ||
|
|
||
| @Nonnull private final Query query; | ||
|
|
||
| AggregateQuery(@Nonnull Query query) { | ||
| this.query = query; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Query getQuery() { | ||
| return query; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public ApiFuture<AggregateQuerySnapshot> get() { | ||
| return get(null); | ||
| } | ||
|
|
||
| @Nonnull | ||
| ApiFuture<AggregateQuerySnapshot> get(@Nullable final ByteString transactionId) { | ||
| RunAggregationQueryRequest request = toProto(transactionId); | ||
| AggregateQueryResponseObserver responseObserver = new AggregateQueryResponseObserver(); | ||
| ServerStreamingCallable<RunAggregationQueryRequest, RunAggregationQueryResponse> callable = | ||
| query.rpcContext.getClient().runAggregationQueryCallable(); | ||
|
|
||
| query.rpcContext.streamRequest(request, responseObserver, callable); | ||
|
|
||
| return responseObserver.getFuture(); | ||
| } | ||
|
|
||
| private final class AggregateQueryResponseObserver | ||
| implements ResponseObserver<RunAggregationQueryResponse> { | ||
|
|
||
| private final SettableApiFuture<AggregateQuerySnapshot> future = SettableApiFuture.create(); | ||
| private final AtomicBoolean isFutureNotified = new AtomicBoolean(false); | ||
| private StreamController streamController; | ||
|
|
||
| SettableApiFuture<AggregateQuerySnapshot> getFuture() { | ||
| return future; | ||
| } | ||
|
|
||
| @Override | ||
| public void onStart(StreamController streamController) { | ||
| this.streamController = streamController; | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(RunAggregationQueryResponse response) { | ||
| if (!isFutureNotified.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
|
|
||
| Timestamp readTime = Timestamp.fromProto(response.getReadTime()); | ||
| Value value = response.getResult().getAggregateFieldsMap().get(ALIAS_COUNT); | ||
| if (value == null) { | ||
| throw new IllegalArgumentException( | ||
| "RunAggregationQueryResponse is missing required alias: " + ALIAS_COUNT); | ||
| } else if (value.getValueTypeCase() != Value.ValueTypeCase.INTEGER_VALUE) { | ||
| throw new IllegalArgumentException( | ||
| "RunAggregationQueryResponse alias " | ||
| + ALIAS_COUNT | ||
| + " has incorrect type: " | ||
| + value.getValueTypeCase()); | ||
| } | ||
| long count = value.getIntegerValue(); | ||
|
|
||
| future.set(new AggregateQuerySnapshot(AggregateQuery.this, readTime, count)); | ||
|
|
||
| // Close the stream to avoid it dangling, since we're not expecting any more responses. | ||
| streamController.cancel(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
| if (!isFutureNotified.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
|
|
||
| future.setException(throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() {} | ||
| } | ||
|
|
||
| @Nonnull | ||
| public RunAggregationQueryRequest toProto() { | ||
| return toProto(null); | ||
| } | ||
|
|
||
| @Nonnull | ||
| RunAggregationQueryRequest toProto(@Nullable final ByteString transactionId) { | ||
| RunQueryRequest runQueryRequest = query.toProto(); | ||
|
|
||
| RunAggregationQueryRequest.Builder request = RunAggregationQueryRequest.newBuilder(); | ||
| request.setParent(runQueryRequest.getParent()); | ||
| if (transactionId != null) { | ||
| request.setTransaction(transactionId); | ||
| } | ||
|
|
||
| StructuredAggregationQuery.Builder structuredAggregationQuery = | ||
| request.getStructuredAggregationQueryBuilder(); | ||
| structuredAggregationQuery.setStructuredQuery(runQueryRequest.getStructuredQuery()); | ||
|
|
||
| StructuredAggregationQuery.Aggregation.Builder aggregation = | ||
| StructuredAggregationQuery.Aggregation.newBuilder(); | ||
| aggregation.setCount(StructuredAggregationQuery.Aggregation.Count.getDefaultInstance()); | ||
| aggregation.setAlias(ALIAS_COUNT); | ||
| structuredAggregationQuery.addAggregations(aggregation); | ||
|
|
||
| return request.build(); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public static AggregateQuery fromProto(Firestore firestore, RunAggregationQueryRequest proto) { | ||
| RunQueryRequest runQueryRequest = | ||
| RunQueryRequest.newBuilder() | ||
| .setParent(proto.getParent()) | ||
| .setStructuredQuery(proto.getStructuredAggregationQuery().getStructuredQuery()) | ||
| .build(); | ||
| Query query = Query.fromProto(firestore, runQueryRequest); | ||
| return new AggregateQuery(query); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return query.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) { | ||
| return true; | ||
| } else if (!(obj instanceof AggregateQuery)) { | ||
| return false; | ||
| } | ||
| AggregateQuery other = (AggregateQuery) obj; | ||
| return query.equals(other.query); | ||
| } | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateQuerySnapshot.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.firestore; | ||
|
|
||
| import com.google.api.core.InternalExtensionOnly; | ||
| import com.google.cloud.Timestamp; | ||
| import java.util.Objects; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| // TODO(count) Make this class public | ||
| @InternalExtensionOnly | ||
| class AggregateQuerySnapshot { | ||
|
|
||
| @Nonnull private final AggregateQuery query; | ||
| @Nonnull private final Timestamp readTime; | ||
| private final long count; | ||
|
|
||
| AggregateQuerySnapshot(@Nonnull AggregateQuery query, @Nonnull Timestamp readTime, long count) { | ||
| this.query = query; | ||
| this.readTime = readTime; | ||
| this.count = count; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public AggregateQuery getQuery() { | ||
| return query; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Timestamp getReadTime() { | ||
| return readTime; | ||
| } | ||
|
|
||
| public long getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) { | ||
| return true; | ||
| } else if (!(obj instanceof AggregateQuerySnapshot)) { | ||
| return false; | ||
| } | ||
|
|
||
| AggregateQuerySnapshot other = (AggregateQuerySnapshot) obj; | ||
| return query.equals(other.query) && readTime.equals(other.readTime) && count == other.count; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(query, readTime, count); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1846,6 +1846,12 @@ private boolean isRetryableError(Throwable throwable) { | |
| return false; | ||
| } | ||
|
|
||
| // TODO(count) Make this method public | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need a comment for the method. Please do as part of the next PR. |
||
| @Nonnull | ||
| AggregateQuery count() { | ||
| return new AggregateQuery(this); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if this Query is equal to the provided object. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be good to add a comment that we expect only one response and why. Please do as part of the next PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: #1041