Skip to content

Commit bdeff87

Browse files
Simpliufy
1 parent cd83e9e commit bdeff87

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.firebase.firestore.model.mutation.MutationBatch;
3232
import com.google.firebase.firestore.model.mutation.Overlay;
3333
import com.google.firebase.firestore.model.mutation.PatchMutation;
34+
import com.google.firebase.firestore.util.Function;
3435
import java.util.ArrayList;
3536
import java.util.Collections;
3637
import java.util.HashMap;
@@ -111,7 +112,7 @@ Document getDocument(DocumentKey key) {
111112
*/
112113
ImmutableSortedMap<DocumentKey, Document> getDocuments(Iterable<DocumentKey> keys) {
113114
Map<DocumentKey, MutableDocument> docs = remoteDocumentCache.getAll(keys);
114-
return getLocalViewOfDocuments(docs, new HashSet<>());
115+
return getLocalViewOfDocuments(docs, documentOverlayCache::getOverlay, new HashSet<>());
115116
}
116117

117118
/**
@@ -123,11 +124,13 @@ ImmutableSortedMap<DocumentKey, Document> getDocuments(Iterable<DocumentKey> key
123124
* useful to determine if some documents overlay needs to be recalculated.
124125
*/
125126
ImmutableSortedMap<DocumentKey, Document> getLocalViewOfDocuments(
126-
Map<DocumentKey, MutableDocument> docs, Set<DocumentKey> existenceStateChanged) {
127+
Map<DocumentKey, MutableDocument> docs,
128+
Function<DocumentKey, Overlay> overlays,
129+
Set<DocumentKey> existenceStateChanged) {
127130
ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
128131
Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
129132
for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
130-
Overlay overlay = documentOverlayCache.getOverlay(entry.getKey());
133+
Overlay overlay = overlays.apply(entry.getKey());
131134
// Recalculate an overlay if the document's existence state is changed due to a remote
132135
// event *and* the overlay is a PatchMutation. This is because document existence state
133136
// can change if some patch mutation's preconditions are met.
@@ -262,18 +265,18 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
262265
* @param offset The offset to index into.
263266
* @param count The number of documents to return
264267
* @return A LocalDocumentsRResult with the documents that follow the provided offset and the last
265-
* processed batch id.
268+
* processed batch id.
266269
*/
267270
LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset, int count) {
268271
Map<DocumentKey, MutableDocument> docs =
269272
remoteDocumentCache.getAll(collectionGroup, offset, count);
270-
Map<DocumentKey, Overlay> overlays = Collections.emptyMap();
273+
final Map<DocumentKey, Overlay> overlays = new HashMap<>();
271274

272-
int largestBatchId = 0;
275+
int largestBatchId = -1;
273276
if (count - docs.size() > 0) {
274-
overlays =
277+
overlays.putAll(
275278
documentOverlayCache.getOverlays(
276-
collectionGroup, offset.getLargestBatchId(), count - docs.size());
279+
collectionGroup, offset.getLargestBatchId(), count - docs.size()));
277280
List<DocumentKey> mutatedKeys = new ArrayList<>();
278281
for (Map.Entry<DocumentKey, Overlay> entry : overlays.entrySet()) {
279282
if (!docs.containsKey(entry.getKey())) {
@@ -284,17 +287,14 @@ LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset
284287
docs.putAll(remoteDocumentCache.getAll(mutatedKeys));
285288
}
286289

287-
ImmutableSortedMap<DocumentKey, Document> localDocs = emptyDocumentMap();
288-
for (MutableDocument doc : docs.values()) {
289-
Overlay overlay =
290-
overlays.containsKey(doc.getKey())
291-
? overlays.get(doc.getKey())
292-
: documentOverlayCache.getOverlay(doc.getKey());
293-
if (overlay != null) {
294-
overlay.getMutation().applyToLocalView(doc, null, Timestamp.now());
295-
}
296-
localDocs = localDocs.insert(doc.getKey(), doc);
297-
}
290+
ImmutableSortedMap<DocumentKey, Document> localDocs =
291+
getLocalViewOfDocuments(
292+
docs,
293+
key ->
294+
overlays.containsKey(key)
295+
? overlays.get(key)
296+
: documentOverlayCache.getOverlay(key),
297+
Collections.emptySet());
298298
return new LocalDocumentsResult(largestBatchId, localDocs);
299299
}
300300

0 commit comments

Comments
 (0)