Skip to content

Commit aafbec4

Browse files
Only advance the limbo free snapshot if query is synced (#699)
1 parent febdeab commit aafbec4

File tree

11 files changed

+41
-46
lines changed

11 files changed

+41
-46
lines changed

firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/TestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static QuerySnapshot querySnapshot(
117117
documentChanges,
118118
isFromCache,
119119
mutatedKeys,
120-
/* hasLimboDocuments= */ false,
120+
/* synced= */ false,
121121
/* didSyncStateChange= */ true,
122122
/* excludesMetadataChanges= */ false);
123123
return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void onViewSnapshot(ViewSnapshot newSnapshot) {
8080
documentChanges,
8181
newSnapshot.isFromCache(),
8282
newSnapshot.getMutatedKeys(),
83-
newSnapshot.hasLimboDocuments(),
83+
newSnapshot.isSynced(),
8484
newSnapshot.didSyncStateChange(),
8585
/* excludesMetadataChanges= */ true);
8686
}
@@ -159,7 +159,7 @@ private void raiseInitialEvent(ViewSnapshot snapshot) {
159159
snapshot.getDocuments(),
160160
snapshot.getMutatedKeys(),
161161
snapshot.isFromCache(),
162-
snapshot.hasLimboDocuments(),
162+
snapshot.isSynced(),
163163
snapshot.excludesMetadataChanges());
164164
raisedInitialEvent = true;
165165
listener.onEvent(snapshot, null);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ public ViewChange applyChanges(DocumentChanges docChanges, TargetChange targetCh
296296
});
297297
applyTargetChange(targetChange);
298298
List<LimboDocumentChange> limboDocumentChanges = updateLimboDocuments();
299-
boolean hasLimboDocuments = !(limboDocuments.size() == 0);
300-
boolean synced = !hasLimboDocuments && current;
299+
boolean synced = limboDocuments.size() == 0 && current;
301300
SyncState newSyncState = synced ? SyncState.SYNCED : SyncState.LOCAL;
302301
boolean syncStatedChanged = newSyncState != syncState;
303302
syncState = newSyncState;
@@ -312,7 +311,7 @@ public ViewChange applyChanges(DocumentChanges docChanges, TargetChange targetCh
312311
viewChanges,
313312
fromCache,
314313
docChanges.mutatedKeys,
315-
hasLimboDocuments,
314+
synced,
316315
syncStatedChanged,
317316
/* excludesMetadataChanges= */ false);
318317
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public enum SyncState {
3737
private final List<DocumentViewChange> changes;
3838
private final boolean isFromCache;
3939
private final ImmutableSortedSet<DocumentKey> mutatedKeys;
40-
private final boolean hasLimboDocuments;
40+
private final boolean synced;
4141
private final boolean didSyncStateChange;
4242
private boolean excludesMetadataChanges;
4343

@@ -48,7 +48,7 @@ public ViewSnapshot(
4848
List<DocumentViewChange> changes,
4949
boolean isFromCache,
5050
ImmutableSortedSet<DocumentKey> mutatedKeys,
51-
boolean hasLimboDocuments,
51+
boolean synced,
5252
boolean didSyncStateChange,
5353
boolean excludesMetadataChanges) {
5454
this.query = query;
@@ -57,7 +57,7 @@ public ViewSnapshot(
5757
this.changes = changes;
5858
this.isFromCache = isFromCache;
5959
this.mutatedKeys = mutatedKeys;
60-
this.hasLimboDocuments = hasLimboDocuments;
60+
this.synced = synced;
6161
this.didSyncStateChange = didSyncStateChange;
6262
this.excludesMetadataChanges = excludesMetadataChanges;
6363
}
@@ -68,7 +68,7 @@ public static ViewSnapshot fromInitialDocuments(
6868
DocumentSet documents,
6969
ImmutableSortedSet<DocumentKey> mutatedKeys,
7070
boolean fromCache,
71-
boolean hasLimboDocuments,
71+
boolean synced,
7272
boolean excludesMetadataChanges) {
7373
List<DocumentViewChange> viewChanges = new ArrayList<>();
7474
for (Document doc : documents) {
@@ -81,7 +81,7 @@ public static ViewSnapshot fromInitialDocuments(
8181
viewChanges,
8282
fromCache,
8383
mutatedKeys,
84-
hasLimboDocuments,
84+
synced,
8585
/* didSyncStateChange= */ true,
8686
excludesMetadataChanges);
8787
}
@@ -90,8 +90,8 @@ public Query getQuery() {
9090
return query;
9191
}
9292

93-
public boolean hasLimboDocuments() {
94-
return hasLimboDocuments;
93+
public boolean isSynced() {
94+
return synced;
9595
}
9696

9797
public DocumentSet getDocuments() {
@@ -140,7 +140,7 @@ public final boolean equals(Object o) {
140140
if (isFromCache != that.isFromCache) {
141141
return false;
142142
}
143-
if (hasLimboDocuments != that.hasLimboDocuments) {
143+
if (synced != that.synced) {
144144
return false;
145145
}
146146
if (didSyncStateChange != that.didSyncStateChange) {
@@ -172,7 +172,7 @@ public int hashCode() {
172172
result = 31 * result + changes.hashCode();
173173
result = 31 * result + mutatedKeys.hashCode();
174174
result = 31 * result + (isFromCache ? 1 : 0);
175-
result = 31 * result + (hasLimboDocuments ? 1 : 0);
175+
result = 31 * result + (synced ? 1 : 0);
176176
result = 31 * result + (didSyncStateChange ? 1 : 0);
177177
result = 31 * result + (excludesMetadataChanges ? 1 : 0);
178178
return result;
@@ -192,8 +192,8 @@ public String toString() {
192192
+ isFromCache
193193
+ ", mutatedKeys="
194194
+ mutatedKeys.size()
195-
+ ", hasLimboDocuments="
196-
+ hasLimboDocuments
195+
+ ", synced="
196+
+ synced
197197
+ ", didSyncStateChange="
198198
+ didSyncStateChange
199199
+ ", excludesMetadataChanges="

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
491491
}
492492
localViewReferences.removeReferences(removed, targetId);
493493

494-
if (!viewChange.hasUnresolvedLimboDocuments()) {
494+
if (viewChange.isSynced()) {
495495
QueryData queryData = targetIds.get(targetId);
496496
hardAssert(
497497
queryData != null,

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ public static LocalViewChanges fromViewSnapshot(int targetId, ViewSnapshot snaps
4949
}
5050
}
5151

52-
return new LocalViewChanges(targetId, snapshot.hasLimboDocuments(), addedKeys, removedKeys);
52+
return new LocalViewChanges(targetId, snapshot.isSynced(), addedKeys, removedKeys);
5353
}
5454

5555
private final int targetId;
56-
private final boolean hasUnresolvedLimboDocuments;
56+
private final boolean synced;
5757

5858
private final ImmutableSortedSet<DocumentKey> added;
5959
private final ImmutableSortedSet<DocumentKey> removed;
6060

6161
public LocalViewChanges(
6262
int targetId,
63-
boolean hasUnresolvedLimboDocuments,
63+
boolean synced,
6464
ImmutableSortedSet<DocumentKey> added,
6565
ImmutableSortedSet<DocumentKey> removed) {
6666
this.targetId = targetId;
67-
this.hasUnresolvedLimboDocuments = hasUnresolvedLimboDocuments;
67+
this.synced = synced;
6868
this.added = added;
6969
this.removed = removed;
7070
}
@@ -73,12 +73,8 @@ public int getTargetId() {
7373
return targetId;
7474
}
7575

76-
/**
77-
* Returns whether there were any unresolved limbo documents in the corresponding view when this
78-
* change was computed.
79-
*/
80-
public boolean hasUnresolvedLimboDocuments() {
81-
return hasUnresolvedLimboDocuments;
76+
public boolean isSynced() {
77+
return synced;
8278
}
8379

8480
public ImmutableSortedSet<DocumentKey> getAdded() {

firebase-firestore/src/roboUtil/java/com/google/firebase/firestore/TestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static QuerySnapshot querySnapshot(
129129
documentChanges,
130130
isFromCache,
131131
mutatedKeys,
132-
/* hasLimboDocuments= */ false,
132+
/* synced= */ false,
133133
/* didSyncStateChange= */ true,
134134
/* excludesMetadataChanges= */ false);
135135
return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);

firebase-firestore/src/test/java/com/google/firebase/firestore/QuerySnapshotTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void testIncludeMetadataChanges() {
125125
documentChanges,
126126
/*isFromCache=*/ false,
127127
/*mutatedKeys=*/ keySet(),
128-
/*hasLimboDocuments=*/ true,
128+
/*isSynced=*/ true,
129129
/*didSyncStateChange=*/ true,
130130
/* excludesMetadataChanges= */ false);
131131

firebase-firestore/src/test/java/com/google/firebase/firestore/core/QueryListenerTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void testRaisesCollectionEvents() {
108108
asList(change1, change4),
109109
snap2.isFromCache(),
110110
snap2.getMutatedKeys(),
111-
/* hasLimboDocuments= */ false,
111+
/* synced= */ false,
112112
/* didSyncStateChange= */ true,
113113
/* excludesMetadataChanges= */ false);
114114
assertEquals(asList(snap2Prime), otherAccum);
@@ -266,7 +266,7 @@ public void testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChang
266266
asList(),
267267
snap4.isFromCache(),
268268
snap4.getMutatedKeys(),
269-
snap4.hasLimboDocuments(),
269+
snap4.isSynced(),
270270
snap4.didSyncStateChange(),
271271
/* excludeMetadataChanges= */ true); // This test excludes document metadata changes
272272

@@ -308,7 +308,7 @@ public void testMetadataOnlyDocumentChangesAreFilteredOut() {
308308
asList(change3),
309309
snap2.isFromCache(),
310310
snap2.getMutatedKeys(),
311-
snap2.hasLimboDocuments(),
311+
snap2.isSynced(),
312312
snap2.didSyncStateChange(),
313313
/* excludesMetadataChanges= */ true);
314314
assertEquals(
@@ -351,7 +351,7 @@ public void testWillWaitForSyncIfOnline() {
351351
asList(change1, change2),
352352
/* isFromCache= */ false,
353353
snap3.getMutatedKeys(),
354-
/* hasLimboDocuments= */ false,
354+
/* synced= */ true,
355355
/* didSyncStateChange= */ true,
356356
/* excludesMetadataChanges= */ true);
357357
assertEquals(asList(expectedSnapshot), events);
@@ -390,7 +390,7 @@ public void testWillRaiseInitialEventWhenGoingOffline() {
390390
asList(change1),
391391
/* isFromCache= */ true,
392392
snap1.getMutatedKeys(),
393-
snap1.hasLimboDocuments(),
393+
snap1.isSynced(),
394394
/* didSyncStateChange= */ true,
395395
/* excludesMetadataChanges= */ true);
396396
ViewSnapshot expectedSnapshot2 =
@@ -401,7 +401,7 @@ public void testWillRaiseInitialEventWhenGoingOffline() {
401401
asList(change2),
402402
/* isFromCache= */ true,
403403
snap2.getMutatedKeys(),
404-
snap2.hasLimboDocuments(),
404+
snap2.isSynced(),
405405
/* didSyncStateChange= */ false,
406406
/* excludesMetadataChanges= */ true);
407407
assertEquals(asList(expectedSnapshot1, expectedSnapshot2), events);
@@ -429,7 +429,7 @@ public void testWillRaiseInitialEventWhenGoingOfflineAndThereAreNoDocs() {
429429
asList(),
430430
/* isFromCache= */ true,
431431
snap1.getMutatedKeys(),
432-
snap1.hasLimboDocuments(),
432+
snap1.isSynced(),
433433
/* didSyncStateChange= */ true,
434434
/* excludesMetadataChanges= */ true);
435435
assertEquals(asList(expectedSnapshot), events);
@@ -456,7 +456,7 @@ public void testWillRaiseInitialEventWhenStartingOfflineAndThereAreNoDocs() {
456456
asList(),
457457
/* isFromCache= */ true,
458458
snap1.getMutatedKeys(),
459-
snap1.hasLimboDocuments(),
459+
snap1.isSynced(),
460460
/* didSyncStateChange= */ true,
461461
/* excludesMetadataChanges= */ true);
462462
assertEquals(asList(expectedSnapshot), events);
@@ -470,7 +470,7 @@ private ViewSnapshot applyExpectedMetadata(ViewSnapshot snap, MetadataChanges me
470470
snap.getChanges(),
471471
snap.isFromCache(),
472472
snap.getMutatedKeys(),
473-
snap.hasLimboDocuments(),
473+
snap.isSynced(),
474474
snap.didSyncStateChange(),
475475
MetadataChanges.EXCLUDE.equals(metadata));
476476
}

firebase-firestore/src/test/java/com/google/firebase/firestore/core/ViewSnapshotTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void testConstructor() {
4545
List<DocumentViewChange> changes =
4646
Arrays.asList(DocumentViewChange.create(Type.ADDED, doc("c/foo", 1, map())));
4747
ImmutableSortedSet<DocumentKey> mutatedKeys = keySet(key("c/foo"));
48-
boolean hasLimboDocuments = true;
48+
boolean synced = true;
4949
boolean fromCache = true;
5050
boolean hasPendingWrites = true;
5151
boolean syncStateChanges = true;
@@ -59,7 +59,7 @@ public void testConstructor() {
5959
changes,
6060
fromCache,
6161
mutatedKeys,
62-
hasLimboDocuments,
62+
synced,
6363
syncStateChanges,
6464
excludesMetadataChanges);
6565

@@ -69,7 +69,7 @@ public void testConstructor() {
6969
assertEquals(changes, snapshot.getChanges());
7070
assertEquals(fromCache, snapshot.isFromCache());
7171
assertEquals(mutatedKeys, snapshot.getMutatedKeys());
72-
assertEquals(hasLimboDocuments, snapshot.hasLimboDocuments());
72+
assertEquals(synced, snapshot.isSynced());
7373
assertEquals(hasPendingWrites, snapshot.hasPendingWrites());
7474
assertEquals(syncStateChanges, snapshot.didSyncStateChange());
7575
assertEquals(excludesMetadataChanges, snapshot.excludesMetadataChanges());

firebase-firestore/src/test/java/com/google/firebase/firestore/core/ViewTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public void testKeepsTrackOfLimboDocuments() {
302302
}
303303

304304
@Test
305-
public void testViewsWithLimboDocumentsAreMarkedAsSuch() {
305+
public void testViewsWithLimboDocumentsAreNotMarkedSynced() {
306306
Query query = messageQuery();
307307
View view = new View(query, DocumentKey.emptyKeySet());
308308
Document doc1 = doc("rooms/eros/messages/0", 0, map());
@@ -311,20 +311,20 @@ public void testViewsWithLimboDocumentsAreMarkedAsSuch() {
311311
// Doc1 is contained in the local view, but we are not yet CURRENT so it is expected that the
312312
// backend hasn't told us about all documents yet.
313313
ViewChange change = applyChanges(view, doc1);
314-
assertFalse(change.getSnapshot().hasLimboDocuments());
314+
assertFalse(change.getSnapshot().isSynced());
315315

316316
// Add doc2 to generate a snapshot. Doc1 is still missing.
317317
View.DocumentChanges viewDocChanges = view.computeDocChanges(docUpdates(doc2));
318318
change =
319319
view.applyChanges(
320320
viewDocChanges, targetChange(ByteString.EMPTY, true, asList(doc2), null, null));
321-
assertTrue(change.getSnapshot().hasLimboDocuments());
321+
assertFalse(change.getSnapshot().isSynced()); // We are CURRENT but doc1 is in limbo.
322322

323323
// Add doc1 to the backend's result set.
324324
change =
325325
view.applyChanges(
326326
viewDocChanges, targetChange(ByteString.EMPTY, true, asList(doc1), null, null));
327-
assertFalse(change.getSnapshot().hasLimboDocuments());
327+
assertTrue(change.getSnapshot().isSynced());
328328
}
329329

330330
@Test

0 commit comments

Comments
 (0)