Skip to content

Commit d25f862

Browse files
Cleanup
1 parent 29f4b5d commit d25f862

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

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

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
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;
3534
import java.util.ArrayList;
3635
import java.util.Collections;
3736
import java.util.HashMap;
@@ -112,36 +111,31 @@ Document getDocument(DocumentKey key) {
112111
*/
113112
ImmutableSortedMap<DocumentKey, Document> getDocuments(Iterable<DocumentKey> keys) {
114113
Map<DocumentKey, MutableDocument> docs = remoteDocumentCache.getAll(keys);
115-
return getLocalViewOfDocuments(docs, documentOverlayCache::getOverlay, new HashSet<>());
114+
return getLocalViewOfDocuments(docs, new HashSet<>());
116115
}
117116

118-
/**
119-
* Similar to {@link #getDocuments}, but creates the local view from the given {@code baseDocs}
120-
* without retrieving documents from the local store.
121-
*
122-
* @param docs The documents to apply local mutations to get the local views.
123-
* @param existenceStateChanged The set of document keys whose existence state is changed. This is
124-
* useful to determine if some documents overlay needs to be recalculated.
125-
*/
126117
private ImmutableSortedMap<DocumentKey, Document> getLocalViewOfDocuments(
127118
Map<DocumentKey, MutableDocument> docs,
128-
Function<DocumentKey, Overlay> overlays,
119+
Map<DocumentKey, Overlay> cachedOverlays,
129120
Set<DocumentKey> existenceStateChanged) {
130121
ImmutableSortedMap<DocumentKey, Document> results = emptyDocumentMap();
131122
Map<DocumentKey, MutableDocument> recalculateDocuments = new HashMap<>();
132-
for (Map.Entry<DocumentKey, MutableDocument> entry : docs.entrySet()) {
133-
Overlay overlay = overlays.apply(entry.getKey());
123+
for (MutableDocument doc : docs.values()) {
124+
Overlay overlay =
125+
cachedOverlays.containsKey(doc.getKey())
126+
? cachedOverlays.get(doc.getKey())
127+
: documentOverlayCache.getOverlay(entry.getKey());
134128
// Recalculate an overlay if the document's existence state is changed due to a remote
135129
// event *and* the overlay is a PatchMutation. This is because document existence state
136130
// can change if some patch mutation's preconditions are met.
137131
// NOTE: we recalculate when `overlay` is null as well, because there might be a patch
138132
// mutation whose precondition does not match before the change (hence overlay==null),
139133
// but would now match.
140-
if (existenceStateChanged.contains(entry.getKey())
134+
if (existenceStateChanged.contains(doc.getKey())
141135
&& (overlay == null || overlay.getMutation() instanceof PatchMutation)) {
142-
recalculateDocuments.put(entry.getKey(), docs.get(entry.getKey()));
136+
recalculateDocuments.put(doc.getKey(), doc);
143137
} else if (overlay != null) {
144-
overlay.getMutation().applyToLocalView(entry.getValue(), null, Timestamp.now());
138+
overlay.getMutation().applyToLocalView(doc, null, Timestamp.now());
145139
}
146140
}
147141

@@ -162,9 +156,8 @@ private ImmutableSortedMap<DocumentKey, Document> getLocalViewOfDocuments(
162156
* useful to determine if some documents overlay needs to be recalculated.
163157
*/
164158
ImmutableSortedMap<DocumentKey, Document> getLocalViewOfDocuments(
165-
Map<DocumentKey, MutableDocument> docs,
166-
Set<DocumentKey> existenceStateChanged) {
167-
return getLocalViewOfDocuments(docs, documentOverlayCache::getOverlay, existenceStateChanged);
159+
Map<DocumentKey, MutableDocument> docs, Set<DocumentKey> existenceStateChanged) {
160+
return getLocalViewOfDocuments(docs, Collections.emptyMap(), existenceStateChanged);
168161
}
169162

170163
private void recalculateAndSaveOverlays(Map<DocumentKey, MutableDocument> docs) {
@@ -282,33 +275,28 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
282275
* processed batch id.
283276
*/
284277
LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset, int count) {
278+
int largestBatchId = -1;
285279
Map<DocumentKey, MutableDocument> docs =
286280
remoteDocumentCache.getAll(collectionGroup, offset, count);
287-
final Map<DocumentKey, Overlay> overlays = new HashMap<>();
281+
Map<DocumentKey, Overlay> overlays =
282+
count - docs.size() > 0
283+
? documentOverlayCache.getOverlays(
284+
collectionGroup, offset.getLargestBatchId(), count - docs.size())
285+
: Collections.emptyMap();
288286

289-
int largestBatchId = -1;
290-
if (count - docs.size() > 0) {
291-
overlays.putAll(
292-
documentOverlayCache.getOverlays(
293-
collectionGroup, offset.getLargestBatchId(), count - docs.size()));
287+
if (!overlays.isEmpty()) {
294288
List<DocumentKey> mutatedKeys = new ArrayList<>();
295-
for (Map.Entry<DocumentKey, Overlay> entry : overlays.entrySet()) {
296-
if (!docs.containsKey(entry.getKey())) {
297-
mutatedKeys.add(entry.getKey());
289+
for (Overlay overlay : overlays.values()) {
290+
if (!docs.containsKey(overlay.getKey())) {
291+
mutatedKeys.add(overlay.getKey());
298292
}
299-
largestBatchId = Math.max(largestBatchId, entry.getValue().getLargestBatchId());
293+
largestBatchId = Math.max(largestBatchId, overlay.getLargestBatchId());
300294
}
301295
docs.putAll(remoteDocumentCache.getAll(mutatedKeys));
302296
}
303297

304298
ImmutableSortedMap<DocumentKey, Document> localDocs =
305-
getLocalViewOfDocuments(
306-
docs,
307-
key ->
308-
overlays.containsKey(key)
309-
? overlays.get(key)
310-
: documentOverlayCache.getOverlay(key),
311-
Collections.emptySet());
299+
getLocalViewOfDocuments(docs, overlays, Collections.emptySet());
312300
return new LocalDocumentsResult(largestBatchId, localDocs);
313301
}
314302

0 commit comments

Comments
 (0)