|
17 | 17 | import static com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap;
|
18 | 18 | import static com.google.firebase.firestore.util.Assert.hardAssert;
|
19 | 19 |
|
| 20 | +import androidx.annotation.Nullable; |
20 | 21 | import androidx.annotation.VisibleForTesting;
|
21 | 22 | import com.google.firebase.Timestamp;
|
22 | 23 | import com.google.firebase.database.collection.ImmutableSortedMap;
|
|
31 | 32 | import com.google.firebase.firestore.model.mutation.MutationBatch;
|
32 | 33 | import com.google.firebase.firestore.model.mutation.Overlay;
|
33 | 34 | import com.google.firebase.firestore.model.mutation.PatchMutation;
|
34 |
| -import java.util.ArrayList; |
35 | 35 | import java.util.Collections;
|
36 | 36 | import java.util.HashMap;
|
37 | 37 | import java.util.HashSet;
|
@@ -86,15 +86,10 @@ DocumentOverlayCache getDocumentOverlayCache() {
|
86 | 86 | */
|
87 | 87 | Document getDocument(DocumentKey key) {
|
88 | 88 | 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); |
94 | 90 | if (overlay != null) {
|
95 | 91 | overlay.getMutation().applyToLocalView(document, null, Timestamp.now());
|
96 | 92 | }
|
97 |
| - |
98 | 93 | return document;
|
99 | 94 | }
|
100 | 95 |
|
@@ -279,16 +274,13 @@ LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset
|
279 | 274 | : Collections.emptyMap();
|
280 | 275 |
|
281 | 276 | 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()); |
286 | 283 | }
|
287 |
| - largestBatchId = Math.max(largestBatchId, overlay.getLargestBatchId()); |
288 |
| - } |
289 |
| - |
290 |
| - if (!mutatedKeys.isEmpty()) { |
291 |
| - docs.putAll(remoteDocumentCache.getAll(mutatedKeys)); |
292 | 284 | }
|
293 | 285 |
|
294 | 286 | ImmutableSortedMap<DocumentKey, Document> localDocs =
|
@@ -325,4 +317,11 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
|
325 | 317 | }
|
326 | 318 | return results;
|
327 | 319 | }
|
| 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 | + } |
328 | 327 | }
|
0 commit comments