Skip to content

Commit ad0cffd

Browse files
committed
AggregateSource/GroupBySource added, and addSnapshotListener() -> listen() builder pattern
1 parent 98c8698 commit ad0cffd

File tree

8 files changed

+190
-160
lines changed

8 files changed

+190
-160
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 static com.google.firebase.firestore.AggregateField.count;
18+
19+
import android.app.Activity;
20+
import androidx.annotation.NonNull;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.locks.Condition;
23+
import java.util.concurrent.locks.Lock;
24+
import java.util.concurrent.locks.ReentrantLock;
25+
26+
final class AggregateDemo {
27+
28+
private final FirebaseFirestore db;
29+
30+
AggregateDemo(@NonNull FirebaseFirestore db) {
31+
this.db = db;
32+
}
33+
34+
void countBasicQuery() {
35+
AggregateQuery query = db.collection("users").count();
36+
AggregateQuerySnapshot snapshot = query.get(AggregateSource.SERVER_DIRECT).getResult();
37+
assertEqual(snapshot.get(count()), 50);
38+
}
39+
40+
void countLimitNumRowsScanned() {
41+
AggregateQuery query = db.collection("users").limit(11).count();
42+
AggregateQuerySnapshot snapshot = query.get(AggregateSource.SERVER_DIRECT).getResult();
43+
assertEqual(snapshot.get(count()), 11);
44+
}
45+
46+
void countUpTo() {
47+
AggregateQuery query = db.collection("users").aggregate(count().upTo(11));
48+
AggregateQuerySnapshot snapshot = query.get(AggregateSource.SERVER_DIRECT).getResult();
49+
assertEqual(snapshot.get(count()), 11);
50+
}
51+
52+
void countRealTimeUpdates() throws InterruptedException {
53+
AggregateQuery query = db.collection("users").aggregate(count());
54+
55+
Lock lock = new ReentrantLock();
56+
Condition condition = lock.newCondition();
57+
58+
ListenerRegistration registration =
59+
query
60+
.listen()
61+
.startDirectFromServer(
62+
(snapshot, error) -> {
63+
assertNull(error);
64+
assertNotNull(snapshot);
65+
assertEqual(snapshot.get(count()), 50);
66+
assertFalse(snapshot.getMetadata().isFromCache());
67+
assertFalse(snapshot.getMetadata().hasPendingWrites());
68+
lock.lock();
69+
try {
70+
condition.signalAll();
71+
} finally {
72+
lock.unlock();
73+
}
74+
});
75+
76+
lock.lock();
77+
try {
78+
condition.await();
79+
} finally {
80+
lock.unlock();
81+
}
82+
83+
registration.remove();
84+
}
85+
86+
private static void assertEqual(Object o1, Object o2) {}
87+
88+
private static void assertNull(Object o) {}
89+
90+
private static void assertFalse(Object o) {}
91+
92+
private static void assertNotNull(Object o) {
93+
if (o == null) {
94+
throw new NullPointerException();
95+
}
96+
}
97+
}

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,58 +34,48 @@ public Task<AggregateQuerySnapshot> get() {
3434
}
3535

3636
@NonNull
37-
public Task<AggregateQuerySnapshot> get(@NonNull Source source) {
37+
public Task<AggregateQuerySnapshot> get(@NonNull AggregateSource source) {
3838
throw new RuntimeException("not implemented");
3939
}
4040

4141
@NonNull
42-
public ListenerRegistration addSnapshotListener(
43-
@NonNull EventListener<AggregateQuerySnapshot> listener) {
42+
public ListenConfig listen() {
4443
throw new RuntimeException("not implemented");
4544
}
4645

47-
@NonNull
48-
public ListenerRegistration addSnapshotListener(
49-
@NonNull Executor executor, @NonNull EventListener<AggregateQuerySnapshot> listener) {
46+
@Override
47+
public int hashCode() {
5048
throw new RuntimeException("not implemented");
5149
}
5250

53-
@NonNull
54-
public ListenerRegistration addSnapshotListener(
55-
@NonNull Activity activity, @NonNull EventListener<AggregateQuerySnapshot> listener) {
51+
@Override
52+
public boolean equals(Object obj) {
5653
throw new RuntimeException("not implemented");
5754
}
5855

59-
@NonNull
60-
public ListenerRegistration addSnapshotListener(
61-
@NonNull MetadataChanges metadataChanges,
62-
@NonNull EventListener<AggregateQuerySnapshot> listener) {
63-
throw new RuntimeException("not implemented");
64-
}
56+
public static final class ListenConfig {
6557

66-
@NonNull
67-
public ListenerRegistration addSnapshotListener(
68-
@NonNull Executor executor,
69-
@NonNull MetadataChanges metadataChanges,
70-
@NonNull EventListener<AggregateQuerySnapshot> listener) {
71-
throw new RuntimeException("not implemented");
72-
}
58+
private ListenConfig() {}
7359

74-
@NonNull
75-
public ListenerRegistration addSnapshotListener(
76-
@NonNull Activity activity,
77-
@NonNull MetadataChanges metadataChanges,
78-
@NonNull EventListener<AggregateQuerySnapshot> listener) {
79-
throw new RuntimeException("not implemented");
80-
}
60+
@NonNull
61+
public ListenConfig executeCallbacksOn(@NonNull Executor executor) {
62+
throw new RuntimeException("not implemented");
63+
}
8164

82-
@Override
83-
public int hashCode() {
84-
throw new RuntimeException("not implemented");
85-
}
65+
@NonNull
66+
public ListenConfig scopeTo(@NonNull Activity executor) {
67+
throw new RuntimeException("not implemented");
68+
}
8669

87-
@Override
88-
public boolean equals(Object obj) {
89-
throw new RuntimeException("not implemented");
70+
@NonNull
71+
public ListenConfig includeMetadataOnlyChanges(boolean includeMetadataOnlyChanges) {
72+
throw new RuntimeException("not implemented");
73+
}
74+
75+
@NonNull
76+
public ListenerRegistration startDirectFromServer(
77+
@NonNull EventListener<AggregateQuerySnapshot> listener) {
78+
throw new RuntimeException("not implemented");
79+
}
9080
}
9181
}
Lines changed: 19 additions & 0 deletions
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/GroupByQuery.java

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,12 @@ public Task<GroupByQuerySnapshot> get() {
3535
}
3636

3737
@NonNull
38-
public Task<GroupByQuerySnapshot> get(@NonNull Source source) {
38+
public Task<GroupByQuerySnapshot> get(@NonNull GroupBySource source) {
3939
throw new RuntimeException("not implemented");
4040
}
4141

4242
@NonNull
43-
public ListenerRegistration addSnapshotListener(
44-
@NonNull EventListener<GroupByQuerySnapshot> listener) {
45-
throw new RuntimeException("not implemented");
46-
}
47-
48-
@NonNull
49-
public ListenerRegistration addSnapshotListener(
50-
@NonNull Executor executor, @NonNull EventListener<GroupByQuerySnapshot> listener) {
51-
throw new RuntimeException("not implemented");
52-
}
53-
54-
@NonNull
55-
public ListenerRegistration addSnapshotListener(
56-
@NonNull Activity activity, @NonNull EventListener<GroupByQuerySnapshot> listener) {
57-
throw new RuntimeException("not implemented");
58-
}
59-
60-
@NonNull
61-
public ListenerRegistration addSnapshotListener(
62-
@NonNull MetadataChanges metadataChanges,
63-
@NonNull EventListener<GroupByQuerySnapshot> listener) {
64-
throw new RuntimeException("not implemented");
65-
}
66-
67-
@NonNull
68-
public ListenerRegistration addSnapshotListener(
69-
@NonNull Executor executor,
70-
@NonNull MetadataChanges metadataChanges,
71-
@NonNull EventListener<GroupByQuerySnapshot> listener) {
72-
throw new RuntimeException("not implemented");
73-
}
74-
75-
@NonNull
76-
public ListenerRegistration addSnapshotListener(
77-
@NonNull Activity activity,
78-
@NonNull MetadataChanges metadataChanges,
79-
@NonNull EventListener<GroupByQuerySnapshot> listener) {
43+
public AggregateQuery.ListenConfig listen() {
8044
throw new RuntimeException("not implemented");
8145
}
8246

@@ -87,11 +51,6 @@ public GroupByQuery aggregate(@NonNull AggregateField... fields) {
8751
throw new RuntimeException("not implemented");
8852
}
8953

90-
@NonNull
91-
public GroupByQuery withLatencyCompensationMode(AggregateExecutionMode mode) {
92-
throw new RuntimeException("not implemented");
93-
}
94-
9554
@NonNull
9655
public GroupByQuery groupLimit(long maxGroups) {
9756
throw new RuntimeException("not implemented");
@@ -186,4 +145,31 @@ public int hashCode() {
186145
public boolean equals(Object obj) {
187146
throw new RuntimeException("not implemented");
188147
}
148+
149+
public static final class ListenConfig {
150+
151+
private ListenConfig() {}
152+
153+
@NonNull
154+
public AggregateQuery.ListenConfig executeCallbacksOn(@NonNull Executor executor) {
155+
throw new RuntimeException("not implemented");
156+
}
157+
158+
@NonNull
159+
public AggregateQuery.ListenConfig scopeTo(@NonNull Activity executor) {
160+
throw new RuntimeException("not implemented");
161+
}
162+
163+
@NonNull
164+
public AggregateQuery.ListenConfig includeMetadataOnlyChanges(
165+
boolean includeMetadataOnlyChanges) {
166+
throw new RuntimeException("not implemented");
167+
}
168+
169+
@NonNull
170+
public ListenerRegistration startDirectFromServer(
171+
@NonNull EventListener<AggregateQuerySnapshot> listener) {
172+
throw new RuntimeException("not implemented");
173+
}
174+
}
189175
}
Lines changed: 19 additions & 0 deletions
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 GroupBySource {
18+
SERVER_DIRECT,
19+
}

0 commit comments

Comments
 (0)