Skip to content

Add FirestoreClientProvider #6050

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
merged 66 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
830d631
dbToken Persistence
tom-andersen May 8, 2024
37b97be
Copyright
tom-andersen May 8, 2024
d9cf32e
Remove user from globals cache.
tom-andersen May 14, 2024
88919eb
Merge branch 'master' into tomandersen/dbToken
tom-andersen May 14, 2024
a1ff6d6
Merge branch 'master' into tomandersen/dbToken
tom-andersen May 17, 2024
297d2c4
Fix
tom-andersen May 17, 2024
09def37
Merge branch 'master' into tomandersen/dbToken
tom-andersen May 17, 2024
e80b843
Merge branch 'master' into tomandersen/dbToken
tom-andersen May 22, 2024
cb08356
Fix from review comments
tom-andersen May 27, 2024
f79a0a2
Merge branch 'master' into tomandersen/dbToken
tom-andersen May 27, 2024
686e19c
Rename
tom-andersen Jun 5, 2024
b5f66a0
Merge branch 'master' into tomandersen/dbToken
tom-andersen Jun 5, 2024
be8097f
AbstractStream refactor
tom-andersen Jun 5, 2024
b61a395
AbstractStream refactor
tom-andersen Jun 5, 2024
0d25982
Whitespace
tom-andersen Jun 5, 2024
6d0f288
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 5, 2024
f2c309f
Merge branch 'master' into tomandersen/dbToken
tom-andersen Jun 11, 2024
906acc5
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 11, 2024
b9f339a
Merge branch 'master' into tomandersen/dbToken
tom-andersen Jun 12, 2024
c65331b
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 12, 2024
01f424c
Merge branch 'main' into tomandersen/dbToken
tom-andersen Jun 14, 2024
2c4d4d1
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 14, 2024
3113171
Refactor ComponentProvider
tom-andersen Jun 14, 2024
a3be1e6
Remove dead code
tom-andersen Jun 17, 2024
c61a40c
Fix
tom-andersen Jun 17, 2024
93fd42f
Merge branch 'tomandersen/streamRefactor' into tomandersen/componentP…
tom-andersen Jun 17, 2024
4808d45
Comments and cleanup
tom-andersen Jun 17, 2024
103f775
Merge remote-tracking branch 'origin/tomandersen/componentProviderRef…
tom-andersen Jun 17, 2024
5d8d687
Merge branch 'main' into tomandersen/dbToken
tom-andersen Jun 17, 2024
0beca8a
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 17, 2024
22a97f3
Merge branch 'tomandersen/streamRefactor' into tomandersen/componentP…
tom-andersen Jun 17, 2024
b749f5c
Whitespace
tom-andersen Jun 17, 2024
89991ef
Merge remote-tracking branch 'origin/tomandersen/componentProviderRef…
tom-andersen Jun 17, 2024
29b2ef9
gRPC integration test of write handshake
tom-andersen Jun 17, 2024
fbc7ad9
PR feedback
tom-andersen Jun 17, 2024
90db84e
Merge branch 'tomandersen/dbToken' into tomandersen/streamRefactor
tom-andersen Jun 17, 2024
2b09c74
Merge branch 'tomandersen/streamRefactor' into tomandersen/componentP…
tom-andersen Jun 17, 2024
6f48bc3
Merge branch 'tomandersen/componentProviderRefactor' into tomandersen…
tom-andersen Jun 17, 2024
3aff190
Merge branch 'main' into tomandersen/streamRefactor
tom-andersen Jun 18, 2024
2699116
Merge branch 'tomandersen/streamRefactor' into tomandersen/componentP…
tom-andersen Jun 19, 2024
1cbaac1
Merge branch 'tomandersen/componentProviderRefactor' into tomandersen…
tom-andersen Jun 19, 2024
5e3f9f5
FirestoreClientProvider
tom-andersen Jun 19, 2024
7a8b052
Fix
tom-andersen Jun 21, 2024
66676d4
Merge remote-tracking branch 'origin/tomandersen/componentProviderRef…
tom-andersen Jun 21, 2024
ec7c414
Merge branch 'tomandersen/componentProviderRefactor' into tomandersen…
tom-andersen Jun 21, 2024
b797dd5
Merge branch 'tomandersen/handshakeTest' into tomandersen/firestoreCl…
tom-andersen Jun 21, 2024
f44a728
Merge remote-tracking branch 'origin/main' into tomandersen/firestore…
tom-andersen Jul 4, 2024
fae9bc0
Fix after merge
tom-andersen Jul 4, 2024
74bce13
Merge remote-tracking branch 'origin/main' into tomandersen/firestore…
tom-andersen Jul 4, 2024
0783dcd
Whitespace
tom-andersen Jul 4, 2024
63e2ec2
Fix
tom-andersen Jul 4, 2024
f2945bf
Merge remote-tracking branch 'origin/main' into tomandersen/firestore…
tom-andersen Jul 17, 2024
44540c4
Pretty
tom-andersen Jul 17, 2024
272cf53
Fix
tom-andersen Jul 17, 2024
1163a7b
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 18, 2024
285e6fd
Fixes from code review.
tom-andersen Jul 22, 2024
b1c88e2
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 22, 2024
f989c0e
Remove dead code.
tom-andersen Jul 22, 2024
0d35529
Fix according to review
tom-andersen Jul 22, 2024
1aa54b3
Add comments
tom-andersen Jul 26, 2024
4ea521e
Merge remote-tracking branch 'origin/main' into tomandersen/firestore…
tom-andersen Jul 26, 2024
0637848
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 29, 2024
f61fade
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 30, 2024
00f5a2a
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 30, 2024
00d6e22
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Jul 30, 2024
dd7c5e6
Merge branch 'main' into tomandersen/firestoreClientProvider
tom-andersen Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static FirebaseFirestore newFirebaseFirestore(
String persistenceKey,
CredentialsProvider<User> authProvider,
CredentialsProvider<String> appCheckProvider,
AsyncQueue asyncQueue,
Function<FirebaseFirestoreSettings, ComponentProvider> componentProviderFactory,
FirebaseApp firebaseApp,
FirebaseFirestore.InstanceRegistry instanceRegistry) {
Expand All @@ -43,14 +42,13 @@ public static FirebaseFirestore newFirebaseFirestore(
persistenceKey,
authProvider,
appCheckProvider,
asyncQueue,
componentProviderFactory,
firebaseApp,
instanceRegistry,
null);
}

public static AsyncQueue getAsyncQueue(FirebaseFirestore firestore) {
return firestore.getAsyncQueue();
return firestore.clientProvider.getAsyncQueue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public void testMultipleInequalityReadFromCacheWhenOffline() {
assertEquals(2L, snapshot1.size());
assertFalse(snapshot1.getMetadata().isFromCache());

waitFor(collection.firestore.getClient().disableNetwork());
waitFor(collection.firestore.disableNetwork());

QuerySnapshot snapshot2 = waitFor(query.get());
assertEquals(2L, snapshot2.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.google.firebase.firestore.FirebaseFirestoreException.Code;
import com.google.firebase.firestore.Query.Direction;
import com.google.firebase.firestore.auth.User;
import com.google.firebase.firestore.core.FirestoreClient;
import com.google.firebase.firestore.model.DatabaseId;
import com.google.firebase.firestore.testutil.EventAccumulator;
import com.google.firebase.firestore.testutil.IntegrationTestUtil;
Expand Down Expand Up @@ -1110,7 +1111,7 @@ public void testRestartFirestoreLeadsToNewInstance() {
assertSame(instance, sameInstance);
waitFor(instance.document("abc/123").set(Collections.singletonMap("field", 100L)));

instance.terminate();
waitFor(instance.terminate());
FirebaseFirestore newInstance = FirebaseFirestore.getInstance(app);
newInstance.setFirestoreSettings(newTestSettings());

Expand All @@ -1132,7 +1133,7 @@ public void testAppDeleteLeadsToFirestoreTerminate() {

app.delete();

assertTrue(instance.getClient().isTerminated());
assertTrue(instance.callClient(FirestoreClient::isTerminated));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ public void testQueriesFireFromCacheWhenOffline() {
assertFalse(querySnapshot.getMetadata().isFromCache());

// offline event with fromCache=true
waitFor(collection.firestore.getClient().disableNetwork());
waitFor(collection.firestore.disableNetwork());
querySnapshot = accum.await();
assertTrue(querySnapshot.getMetadata().isFromCache());

// back online event with fromCache=false
waitFor(collection.firestore.getClient().enableNetwork());
waitFor(collection.firestore.enableNetwork());
querySnapshot = accum.await();
assertFalse(querySnapshot.getMetadata().isFromCache());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void testServerTimestampsCanReturnPreviousValueOfDifferentType() {
@Test
public void testServerTimestampsCanRetainPreviousValueThroughConsecutiveUpdates() {
writeInitialData();
waitFor(docRef.getFirestore().getClient().disableNetwork());
waitFor(docRef.getFirestore().disableNetwork());
accumulator.awaitRemoteEvent();

docRef.update("a", FieldValue.serverTimestamp());
Expand All @@ -226,7 +226,7 @@ public void testServerTimestampsCanRetainPreviousValueThroughConsecutiveUpdates(
localSnapshot = accumulator.awaitLocalEvent();
assertEquals(42L, localSnapshot.get("a", ServerTimestampBehavior.PREVIOUS));

waitFor(docRef.getFirestore().getClient().enableNetwork());
waitFor(docRef.getFirestore().enableNetwork());

DocumentSnapshot remoteSnapshot = accumulator.awaitRemoteEvent();
assertThat(remoteSnapshot.get("a")).isInstanceOf(Timestamp.class);
Expand All @@ -235,7 +235,7 @@ public void testServerTimestampsCanRetainPreviousValueThroughConsecutiveUpdates(
@Test
public void testServerTimestampsUsesPreviousValueFromLocalMutation() {
writeInitialData();
waitFor(docRef.getFirestore().getClient().disableNetwork());
waitFor(docRef.getFirestore().disableNetwork());
accumulator.awaitRemoteEvent();

docRef.update("a", FieldValue.serverTimestamp());
Expand All @@ -249,7 +249,7 @@ public void testServerTimestampsUsesPreviousValueFromLocalMutation() {
localSnapshot = accumulator.awaitLocalEvent();
assertEquals(1337L, localSnapshot.get("a", ServerTimestampBehavior.PREVIOUS));

waitFor(docRef.getFirestore().getClient().enableNetwork());
waitFor(docRef.getFirestore().enableNetwork());

DocumentSnapshot remoteSnapshot = accumulator.awaitRemoteEvent();
assertThat(remoteSnapshot.get("a")).isInstanceOf(Timestamp.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public void testIncrementTransactionally() {
AtomicInteger started = new AtomicInteger(0);

FirebaseFirestore firestore = testFirestore();
firestore.getAsyncQueue().skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
AccessHelper.getAsyncQueue(firestore).skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
DocumentReference doc = firestore.collection("counters").document();
waitFor(doc.set(map("count", 5.0)));

Expand Down Expand Up @@ -437,7 +437,7 @@ public void testUpdateTransactionally() {
AtomicInteger counter = new AtomicInteger(0);

FirebaseFirestore firestore = testFirestore();
firestore.getAsyncQueue().skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
AccessHelper.getAsyncQueue(firestore).skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
DocumentReference doc = firestore.collection("counters").document();
waitFor(doc.set(map("count", 5.0, "other", "yes")));

Expand Down Expand Up @@ -532,7 +532,7 @@ public void testUpdatePOJOTransactionally() {
AtomicInteger started = new AtomicInteger(0);

FirebaseFirestore firestore = testFirestore();
firestore.getAsyncQueue().skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
AccessHelper.getAsyncQueue(firestore).skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
DocumentReference doc = firestore.collection("counters").document();
waitFor(doc.set(new POJO(5.0, "no", "clean")));

Expand Down Expand Up @@ -601,7 +601,7 @@ public void testRetriesWhenDocumentThatWasReadWithoutBeingWrittenChanges() {
@Test
public void testReadingADocTwiceWithDifferentVersions() {
FirebaseFirestore firestore = testFirestore();
firestore.getAsyncQueue().skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
AccessHelper.getAsyncQueue(firestore).skipDelaysForTimerId(TimerId.RETRY_TRANSACTION);
DocumentReference doc = firestore.collection("counters").document();
waitFor(doc.set(map("count", 15.0)));
AtomicInteger counter = new AtomicInteger(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void queriesCannotBeSortedByAnUncommittedServerTimestamp() {
CollectionReference collection = testCollection();

// Ensure the server timestamp stays uncommitted for the first half of the test
waitFor(collection.firestore.getClient().disableNetwork());
waitFor(collection.firestore.disableNetwork());

TaskCompletionSource<Void> offlineCallbackDone = new TaskCompletionSource<>();
TaskCompletionSource<Void> onlineCallbackDone = new TaskCompletionSource<>();
Expand Down Expand Up @@ -497,7 +497,7 @@ public void queriesCannotBeSortedByAnUncommittedServerTimestamp() {
document.set(map("timestamp", FieldValue.serverTimestamp()));
waitFor(offlineCallbackDone.getTask());

waitFor(collection.firestore.getClient().enableNetwork());
waitFor(collection.firestore.enableNetwork());
waitFor(onlineCallbackDone.getTask());

listenerRegistration.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ public static FirebaseFirestore testFirestore(
persistenceKey,
MockCredentialsProvider.instance(),
new EmptyAppCheckTokenProvider(),
asyncQueue,
ComponentProvider::defaultFactory,
/* firebaseApp= */ null,
/* instanceRegistry= */ (dbId) -> {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public Task<AggregateQuerySnapshot> get(@NonNull AggregateSource source) {
TaskCompletionSource<AggregateQuerySnapshot> tcs = new TaskCompletionSource<>();
query
.firestore
.getClient()
.runAggregateQuery(query.query, aggregateFieldList)
.callClient(client -> client.runAggregateQuery(query.query, aggregateFieldList))
.continueWith(
Executors.DIRECT_EXECUTOR,
(task) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.firebase.firestore.core.ActivityScope;
import com.google.firebase.firestore.core.AsyncEventListener;
import com.google.firebase.firestore.core.EventManager.ListenOptions;
import com.google.firebase.firestore.core.ListenerRegistrationImpl;
import com.google.firebase.firestore.core.QueryListener;
import com.google.firebase.firestore.core.UserData.ParsedSetData;
import com.google.firebase.firestore.core.UserData.ParsedUpdateData;
Expand All @@ -38,11 +37,12 @@
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.mutation.DeleteMutation;
import com.google.firebase.firestore.model.mutation.Mutation;
import com.google.firebase.firestore.model.mutation.Precondition;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.Executors;
import com.google.firebase.firestore.util.Util;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -165,9 +165,9 @@ public Task<Void> set(@NonNull Object data, @NonNull SetOptions options) {
options.isMerge()
? firestore.getUserDataReader().parseMergeData(data, options.getFieldMask())
: firestore.getUserDataReader().parseSetData(data);
List<Mutation> mutations = singletonList(parsed.toMutation(key, Precondition.NONE));
return firestore
.getClient()
.write(Collections.singletonList(parsed.toMutation(key, Precondition.NONE)))
.callClient(client -> client.write(mutations))
.continueWith(Executors.DIRECT_EXECUTOR, voidErrorTransformer());
}

Expand Down Expand Up @@ -229,9 +229,9 @@ public Task<Void> update(
}

private Task<Void> update(@NonNull ParsedUpdateData parsedData) {
List<Mutation> mutations = singletonList(parsedData.toMutation(key, Precondition.exists(true)));
return firestore
.getClient()
.write(Collections.singletonList(parsedData.toMutation(key, Precondition.exists(true))))
.callClient(client -> client.write(mutations))
.continueWith(Executors.DIRECT_EXECUTOR, voidErrorTransformer());
}

Expand All @@ -242,9 +242,9 @@ private Task<Void> update(@NonNull ParsedUpdateData parsedData) {
*/
@NonNull
public Task<Void> delete() {
List<Mutation> mutations = singletonList(new DeleteMutation(key, Precondition.NONE));
return firestore
.getClient()
.write(singletonList(new DeleteMutation(key, Precondition.NONE)))
.callClient(client -> client.write(mutations))
.continueWith(Executors.DIRECT_EXECUTOR, voidErrorTransformer());
}

Expand Down Expand Up @@ -274,8 +274,7 @@ public Task<DocumentSnapshot> get() {
public Task<DocumentSnapshot> get(@NonNull Source source) {
if (source == Source.CACHE) {
return firestore
.getClient()
.getDocumentFromLocalCache(key)
.callClient(client -> client.getDocumentFromLocalCache(key))
.continueWith(
Executors.DIRECT_EXECUTOR,
(Task<Document> task) -> {
Expand Down Expand Up @@ -531,11 +530,17 @@ private ListenerRegistration addSnapshotListenerInternal(
new AsyncEventListener<>(userExecutor, viewListener);

com.google.firebase.firestore.core.Query query = asQuery();
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);

return ActivityScope.bind(
activity,
new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
return firestore.callClient(
client -> {
QueryListener queryListener = client.listen(query, options, asyncListener);
return ActivityScope.bind(
activity,
() -> {
asyncListener.mute();
client.stopListening(queryListener);
});
});
}

@Override
Expand Down
Loading
Loading