Skip to content

Commit 434ebfe

Browse files
committed
COUNT API added
1 parent 0fe063e commit 434ebfe

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import androidx.annotation.NonNull;
18+
19+
final class AggregateDemo {
20+
21+
private AggregateDemo() {
22+
}
23+
24+
public void Demo0_NormalQuery(FirebaseFirestore db) {
25+
CollectionReference query = db.collection("games/halo/players");
26+
QuerySnapshot snapshot = query.get().getResult();
27+
assertEqual(snapshot.size(), 5_000_000);
28+
}
29+
30+
public void Demo1_CountOfDocumentsInACollection(FirebaseFirestore db) {
31+
AggregateQuery countQuery = db.collection("games/halo/players").count();
32+
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
33+
assertEqual(snapshot.getCount(), 5_000_000);
34+
}
35+
36+
public void Demo2_CountOfDocumentsInACollectionWithFilter(FirebaseFirestore db) {
37+
Query query = db.collection("games/halo/players").where(Filter.equalTo("online", true));
38+
AggregateQuery countQuery = query.count();
39+
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
40+
assertEqual(snapshot.getCount(), 2000);
41+
}
42+
43+
public void Demo2_CountOfDocumentsInACollectionWithLimit(FirebaseFirestore db) {
44+
Query query = db.collection("games/halo/players").limit(9000);
45+
AggregateQuery countQuery = query.count();
46+
AggregateQuerySnapshot snapshot = countQuery.get(AggregateSource.SERVER_DIRECT).getResult();
47+
assertEqual(snapshot.getCount(), 9000);
48+
}
49+
50+
private static void assertEqual(Object o1, Object o2) {}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import androidx.annotation.NonNull;
18+
import com.google.android.gms.tasks.Task;
19+
20+
public class AggregateQuery {
21+
22+
AggregateQuery() {}
23+
24+
@NonNull
25+
public Query getQuery() {
26+
throw new RuntimeException("not implemented");
27+
}
28+
29+
@NonNull
30+
public Task<AggregateQuerySnapshot> get(@NonNull AggregateSource source) {
31+
throw new RuntimeException("not implemented");
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
throw new RuntimeException("not implemented");
37+
}
38+
39+
@Override
40+
public boolean equals(Object obj) {
41+
throw new RuntimeException("not implemented");
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import androidx.annotation.Nullable;
18+
import javax.annotation.Nonnull;
19+
20+
public class AggregateQuerySnapshot {
21+
22+
AggregateQuerySnapshot() {}
23+
24+
@Nonnull
25+
public AggregateQuery getQuery() {
26+
throw new RuntimeException("not implemented");
27+
}
28+
29+
@Nullable
30+
public Long getCount() {
31+
throw new RuntimeException("not implemented");
32+
}
33+
34+
@Override
35+
public boolean equals(@Nullable Object obj) {
36+
throw new RuntimeException("not implemented");
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
throw new RuntimeException("not implemented");
42+
}
43+
44+
@Override
45+
public String toString() {
46+
throw new RuntimeException("not implemented");
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
public enum AggregateSource {
18+
SERVER_DIRECT,
19+
}

firebase-firestore/src/main/java/com/google/firebase/firestore/Query.java

+5
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ private void validateHasExplicitOrderByForLimitToLast() {
12231223
}
12241224
}
12251225

1226+
@NonNull
1227+
public AggregateQuery count() {
1228+
throw new RuntimeException("not implemented");
1229+
}
1230+
12261231
@Override
12271232
public boolean equals(Object o) {
12281233
if (this == o) {

0 commit comments

Comments
 (0)