Skip to content

Commit 86e3abf

Browse files
committed
addressing comments #2
1 parent a683e2f commit 86e3abf

File tree

7 files changed

+14
-11
lines changed

7 files changed

+14
-11
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ public void testWaitForPendingWritesResolves() {
11101110
Task<Void> pendingWrite = documentReference.set(data);
11111111
Task<Void> awaitsPendingWrites2 = firestore.waitForPendingWrites();
11121112

1113-
// `awaitsPendingWrites1` is complete immediately because there is no pending writes at
1113+
// `awaitsPendingWrites1` completes immediately because there are no pending writes at
11141114
// the time it is created.
11151115
waitFor(awaitsPendingWrites1);
11161116
assertTrue(awaitsPendingWrites1.isComplete() && awaitsPendingWrites1.isSuccessful());

firebase-firestore/src/main/java/com/google/firebase/firestore/core/SyncEngine.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.firebase.firestore.model.NoDocument;
4141
import com.google.firebase.firestore.model.SnapshotVersion;
4242
import com.google.firebase.firestore.model.mutation.Mutation;
43+
import com.google.firebase.firestore.model.mutation.MutationBatch;
4344
import com.google.firebase.firestore.model.mutation.MutationBatchResult;
4445
import com.google.firebase.firestore.remote.Datastore;
4546
import com.google.firebase.firestore.remote.RemoteEvent;
@@ -448,12 +449,13 @@ public void registerPendingWritesTask(TaskCompletionSource<Void> userTask) {
448449
if (!remoteStore.canUseNetwork()) {
449450
Logger.debug(
450451
TAG,
451-
"The network is disabled. The task returned by 'awaitPendingWrites()' will not complete until the network is enabled.");
452+
"The network is disabled. The task returned by 'awaitPendingWrites()' will not"
453+
+ " complete until the network is enabled.");
452454
}
453455

454456
int largestPendingBatchId = localStore.getHighestUnacknowledgedBatchId();
455457

456-
if (largestPendingBatchId == 0) {
458+
if (largestPendingBatchId == MutationBatch.UNKNOWN) {
457459
// Complete the task right away if there is no pending writes at the moment.
458460
userTask.setResult(null);
459461
return;
@@ -623,10 +625,11 @@ public void handleCredentialChange(User user) {
623625
currentUser = user;
624626

625627
if (userChanged) {
628+
// Fails tasks waiting for pending writes requested by previous user.
629+
failOutstandingPendingWritesAwaitingTasks();
626630
// Notify local store and emit any resulting events from swapping out the mutation queue.
627631
ImmutableSortedMap<DocumentKey, MaybeDocument> changes = localStore.handleUserChange(user);
628632
emitNewSnapsAndNotifyLocalStore(changes, /*remoteEvent=*/ null);
629-
failOutstandingPendingWritesAwaitingTasks();
630633
}
631634

632635
// Notify remote store so it can restart its streams.

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> rejectBatch(int batchId) {
284284

285285
/**
286286
* Returns the largest (latest) batch id in mutation queue that is pending server response.
287-
* Returns 0 if the queue is empty.
287+
* Returns {@link MutationBatch#UNKNOWN} if the queue is empty.
288288
*/
289289
public int getHighestUnacknowledgedBatchId() {
290290
return mutationQueue.getHighestUnacknowledgedBatchId();

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryMutationQueue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
189189

190190
@Override
191191
public int getHighestUnacknowledgedBatchId() {
192-
return queue.isEmpty() ? 0 : nextBatchId - 1;
192+
return queue.isEmpty() ? MutationBatch.UNKNOWN : nextBatchId - 1;
193193
}
194194

195195
@Override

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MutationQueue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ MutationBatch addMutationBatch(
7575

7676
/**
7777
* @return The largest (latest) batch id in mutation queue for the current user that is pending
78-
* server response, 0 if the queue is empty.
78+
* server response, {@link MutationBatch#UNKNOWN} if the queue is empty.
7979
*/
8080
int getHighestUnacknowledgedBatchId();
8181

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteMutationQueue.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
251251

252252
@Override
253253
public int getHighestUnacknowledgedBatchId() {
254-
return db.query("SELECT IFNULL(MAX(batch_id), 0) FROM mutations WHERE uid = ?")
255-
.binding(uid)
254+
return db.query("SELECT IFNULL(MAX(batch_id), ?) FROM mutations WHERE uid = ?")
255+
.binding(MutationBatch.UNKNOWN, uid)
256256
.firstValue(row -> row.getInt(0));
257257
}
258258

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ public void testHandlesPatchMutationWithTransformThenRemoteEvent() {
11461146

11471147
@Test
11481148
public void testGetHighestUnacknowledgedBatchId() {
1149-
assertEquals(0, localStore.getHighestUnacknowledgedBatchId());
1149+
assertEquals(-1, localStore.getHighestUnacknowledgedBatchId());
11501150

11511151
writeMutation(setMutation("foo/bar", map("abc", 123)));
11521152
assertEquals(1, localStore.getHighestUnacknowledgedBatchId());
@@ -1158,6 +1158,6 @@ public void testGetHighestUnacknowledgedBatchId() {
11581158
assertEquals(2, localStore.getHighestUnacknowledgedBatchId());
11591159

11601160
rejectMutation();
1161-
assertEquals(0, localStore.getHighestUnacknowledgedBatchId());
1161+
assertEquals(-1, localStore.getHighestUnacknowledgedBatchId());
11621162
}
11631163
}

0 commit comments

Comments
 (0)