Skip to content

Commit a683e2f

Browse files
committed
address comments #1
1 parent 9d8841e commit a683e2f

File tree

9 files changed

+22
-28
lines changed

9 files changed

+22
-28
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ public void testWaitForPendingWritesResolves() {
11171117
assertTrue(!pendingWrite.isComplete());
11181118
assertTrue(!awaitsPendingWrites2.isComplete());
11191119

1120-
waitFor(firestore.enableNetwork());
1121-
waitFor(pendingWrite);
1120+
firestore.enableNetwork();
11221121
waitFor(awaitsPendingWrites2);
11231122
assertTrue(awaitsPendingWrites2.isComplete() && awaitsPendingWrites2.isSuccessful());
11241123
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,10 @@ Task<Void> shutdown() {
363363
}
364364

365365
/**
366-
* Wait for server acknowledgement for all pending writes existing at the time of calling this
367-
* method.
366+
* Wait until all pending writes existed at the time of calling are sent to the backend.
368367
*
369-
* <p>Both acceptance and rejection count as server acknowledgement.
370-
*
371-
* @return A {@link Task} which resolves when all pending writes are acknowledged by the server.
368+
* @return A {@link Task} which resolves when all pending writes are sent to the backend. If there
369+
* is a Firebase user change, the return {@link Task} will resolve to an exception.
372370
*/
373371
Task<Void> waitForPendingWrites() {
374372
return client.waitForPendingWrites();

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

-6
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,6 @@ public <TResult> Task<TResult> transaction(
230230
*/
231231
public Task<Void> waitForPendingWrites() {
232232
this.verifyNotShutdown();
233-
if (!remoteStore.canUseNetwork()) {
234-
Logger.warn(
235-
LOG_TAG,
236-
"Network is disabled, the Task created to wait for all writes getting"
237-
+ " acknowledged by server will not complete until network is enabled.");
238-
}
239233

240234
final TaskCompletionSource<Void> source = new TaskCompletionSource<>();
241235
asyncQueue.enqueueAndForget(() -> syncEngine.registerPendingWritesTask(source));

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public void handleSuccessfulWrite(MutationBatchResult mutationBatchResult) {
413413
// they consistently happen before listen events.
414414
notifyUser(mutationBatchResult.getBatch().getBatchId(), /*status=*/ null);
415415

416-
resolveTasksAwaitingForPendingWritesIfAny(mutationBatchResult.getBatch().getBatchId());
416+
resolvePendingWriteTasks(mutationBatchResult.getBatch().getBatchId());
417417

418418
ImmutableSortedMap<DocumentKey, MaybeDocument> changes =
419419
localStore.acknowledgeBatch(mutationBatchResult);
@@ -435,16 +435,22 @@ public void handleRejectedWrite(int batchId, Status status) {
435435
// they consistently happen before listen events.
436436
notifyUser(batchId, status);
437437

438-
resolveTasksAwaitingForPendingWritesIfAny(batchId);
438+
resolvePendingWriteTasks(batchId);
439439

440440
emitNewSnapsAndNotifyLocalStore(changes, /*remoteEvent=*/ null);
441441
}
442442

443443
/**
444-
* Takes a snapshot of current local mutation queue, and register a user task which will resolve
445-
* when all those mutations are either accepted or rejected by the server.
444+
* Takes a snapshot of current mutation queue, and register a user task which will resolve when
445+
* all those mutations are either accepted or rejected by the server.
446446
*/
447447
public void registerPendingWritesTask(TaskCompletionSource<Void> userTask) {
448+
if (!remoteStore.canUseNetwork()) {
449+
Logger.debug(
450+
TAG,
451+
"The network is disabled. The task returned by 'awaitPendingWrites()' will not complete until the network is enabled.");
452+
}
453+
448454
int largestPendingBatchId = localStore.getHighestUnacknowledgedBatchId();
449455

450456
if (largestPendingBatchId == 0) {
@@ -461,7 +467,7 @@ public void registerPendingWritesTask(TaskCompletionSource<Void> userTask) {
461467
}
462468

463469
/** Resolves tasks waiting for this batch id to get acknowledged by server, if there is any. */
464-
private void resolveTasksAwaitingForPendingWritesIfAny(int batchId) {
470+
private void resolvePendingWriteTasks(int batchId) {
465471
if (pendingWritesCallbacks.containsKey(batchId)) {
466472
for (TaskCompletionSource<Void> task : pendingWritesCallbacks.get(batchId)) {
467473
task.setResult(null);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public ImmutableSortedMap<DocumentKey, MaybeDocument> rejectBatch(int batchId) {
287287
* Returns 0 if the queue is empty.
288288
*/
289289
public int getHighestUnacknowledgedBatchId() {
290-
return mutationQueue.getLargestUnacknowledgedBatchId();
290+
return mutationQueue.getHighestUnacknowledgedBatchId();
291291
}
292292

293293
/** Returns the last recorded stream token for the current user. */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
188188
}
189189

190190
@Override
191-
public int getLargestUnacknowledgedBatchId() {
192-
return queue.size() == 0 ? 0 : nextBatchId - 1;
191+
public int getHighestUnacknowledgedBatchId() {
192+
return queue.isEmpty() ? 0 : 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
@@ -77,7 +77,7 @@ MutationBatch addMutationBatch(
7777
* @return The largest (latest) batch id in mutation queue for the current user that is pending
7878
* server response, 0 if the queue is empty.
7979
*/
80-
int getLargestUnacknowledgedBatchId();
80+
int getHighestUnacknowledgedBatchId();
8181

8282
/** Returns all mutation batches in the mutation queue. */
8383
// TODO: PERF: Current consumer only needs mutated keys; if we can provide that

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,8 @@ public MutationBatch getNextMutationBatchAfterBatchId(int batchId) {
250250
}
251251

252252
@Override
253-
public int getLargestUnacknowledgedBatchId() {
254-
if (isEmpty()) {
255-
return 0;
256-
}
257-
return db.query("SELECT MAX(batch_id) FROM mutations " + "WHERE uid = ?")
253+
public int getHighestUnacknowledgedBatchId() {
254+
return db.query("SELECT IFNULL(MAX(batch_id), 0) FROM mutations WHERE uid = ?")
258255
.binding(uid)
259256
.firstValue(row -> row.getInt(0));
260257
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ public void testHandlesPatchMutationWithTransformThenRemoteEvent() {
11451145
}
11461146

11471147
@Test
1148-
public void testGetHighestUnacknowledgedBatchIdReturnsExpectedResult() {
1148+
public void testGetHighestUnacknowledgedBatchId() {
11491149
assertEquals(0, localStore.getHighestUnacknowledgedBatchId());
11501150

11511151
writeMutation(setMutation("foo/bar", map("abc", 123)));

0 commit comments

Comments
 (0)