Skip to content

Commit 75a6515

Browse files
Simplify
1 parent bba4249 commit 75a6515

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ private int writeEntriesForCollectionGroup(
137137
Collection<FieldIndex> fieldIndexes = indexManager.getFieldIndexes(collectionGroup);
138138
IndexOffset existingOffset = getExistingOffset(fieldIndexes);
139139

140-
LocalDocumentsResult nextDocuments =
140+
LocalDocumentsResult nextBatch =
141141
localDocumentsView.getNextDocuments(
142142
collectionGroup, existingOffset, documentsRemainingUnderCap);
143+
indexManager.updateIndexEntries(nextBatch.getDocuments());
143144

144-
indexManager.updateIndexEntries(nextDocuments.getDocuments());
145-
IndexOffset newOffset = getNewOffset(existingOffset, nextDocuments);
145+
IndexOffset newOffset = getNewOffset(existingOffset, nextBatch);
146146
indexManager.updateCollectionGroup(collectionGroup, newOffset);
147147

148-
return nextDocuments.getDocuments().size();
148+
return nextBatch.getDocuments().size();
149149
}
150150

151151
private IndexOffset getNewOffset(IndexOffset existingOffset, LocalDocumentsResult lookupResult) {

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap;
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
1919

20+
import androidx.annotation.Nullable;
2021
import androidx.annotation.VisibleForTesting;
2122
import com.google.firebase.Timestamp;
2223
import com.google.firebase.database.collection.ImmutableSortedMap;
@@ -31,7 +32,6 @@
3132
import com.google.firebase.firestore.model.mutation.MutationBatch;
3233
import com.google.firebase.firestore.model.mutation.Overlay;
3334
import com.google.firebase.firestore.model.mutation.PatchMutation;
34-
import java.util.ArrayList;
3535
import java.util.Collections;
3636
import java.util.HashMap;
3737
import java.util.HashSet;
@@ -86,15 +86,10 @@ DocumentOverlayCache getDocumentOverlayCache() {
8686
*/
8787
Document getDocument(DocumentKey key) {
8888
Overlay overlay = documentOverlayCache.getOverlay(key);
89-
// Only read from remote document cache if overlay is a patch.
90-
MutableDocument document =
91-
(overlay == null || overlay.getMutation() instanceof PatchMutation)
92-
? remoteDocumentCache.get(key)
93-
: MutableDocument.newInvalidDocument(key);
89+
MutableDocument document = getBaseDocument(key, overlay);
9490
if (overlay != null) {
9591
overlay.getMutation().applyToLocalView(document, null, Timestamp.now());
9692
}
97-
9893
return document;
9994
}
10095

@@ -279,16 +274,13 @@ LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset
279274
: Collections.emptyMap();
280275

281276
int largestBatchId = -1;
282-
List<DocumentKey> mutatedKeys = new ArrayList<>();
283-
for (Overlay overlay : overlays.values()) {
284-
if (!docs.containsKey(overlay.getKey())) {
285-
mutatedKeys.add(overlay.getKey());
277+
if (count - docs.size() > 0) {
278+
for (Overlay overlay : overlays.values()) {
279+
if (!docs.containsKey(overlay.getKey())) {
280+
docs.put(overlay.getKey(), getBaseDocument(overlay.getKey(), overlay));
281+
}
282+
largestBatchId = Math.max(largestBatchId, overlay.getLargestBatchId());
286283
}
287-
largestBatchId = Math.max(largestBatchId, overlay.getLargestBatchId());
288-
}
289-
290-
if (!mutatedKeys.isEmpty()) {
291-
docs.putAll(remoteDocumentCache.getAll(mutatedKeys));
292284
}
293285

294286
ImmutableSortedMap<DocumentKey, Document> localDocs =
@@ -325,4 +317,11 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
325317
}
326318
return results;
327319
}
320+
321+
/** Provides a base document to apply `overlay`. */
322+
private MutableDocument getBaseDocument(DocumentKey key, @Nullable Overlay overlay) {
323+
return (overlay == null || overlay.getMutation() instanceof PatchMutation)
324+
? remoteDocumentCache.get(key)
325+
: MutableDocument.newInvalidDocument(key);
326+
}
328327
}

0 commit comments

Comments
 (0)