-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateDemo.java
This file contains 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,45 @@ | ||
/* | ||
* 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 java.util.Objects; | ||
|
||
public class AggregateDemo { | ||
|
||
public static void Demo1_CountOfDocumentsInACollection(Firestore db) throws Exception { | ||
Query query = db.collection("games").document("halo").collection("players"); | ||
AggregateQuerySnapshot snapshot = query.count().get().get(); | ||
assertEqual(snapshot.getCount(), 5_000_000); | ||
} | ||
|
||
public static void Demo2_LimitNumberOfDocumentsScannedWithLimit(Firestore db) throws Exception { | ||
// Limit the work / documents scanned by restricting underlying query. | ||
Query query = db.collection("games").document("halo").collection("players").limit(1000); | ||
AggregateQuerySnapshot snapshot = query.count().get().get(); | ||
assertEqual(snapshot.getCount(), 1000); | ||
} | ||
|
||
private static void assertEqual(Long num1, Long num2) { | ||
if (!Objects.equals(num1, num2)) { | ||
throw new AssertionError("num1!=num2"); | ||
} | ||
} | ||
|
||
private static void assertEqual(Long num1, int num2) { | ||
assertEqual(num1, Long.valueOf(num2)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateQuery.java
This file contains 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,35 @@ | ||
/* | ||
* 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 javax.annotation.Nonnull; | ||
|
||
public interface AggregateQuery { | ||
|
||
@Nonnull | ||
Query getQuery(); | ||
|
||
@Nonnull | ||
ApiFuture<AggregateQuerySnapshot> get(); | ||
|
||
@Override | ||
int hashCode(); | ||
|
||
@Override | ||
boolean equals(Object obj); | ||
} |
41 changes: 41 additions & 0 deletions
41
google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateQuerySnapshot.java
This file contains 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,41 @@ | ||
/* | ||
* 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.cloud.Timestamp; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public interface AggregateQuerySnapshot { | ||
|
||
@Nonnull | ||
AggregateQuery getQuery(); | ||
|
||
@Nonnull | ||
Timestamp getReadTime(); | ||
|
||
@Nullable | ||
Long getCount(); | ||
|
||
@Override | ||
boolean equals(Object obj); | ||
|
||
@Override | ||
int hashCode(); | ||
|
||
} |
This file contains 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 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