31
31
import com .google .firebase .firestore .model .mutation .MutationBatch ;
32
32
import com .google .firebase .firestore .model .mutation .Overlay ;
33
33
import com .google .firebase .firestore .model .mutation .PatchMutation ;
34
- import com .google .firebase .firestore .util .Function ;
35
34
import java .util .ArrayList ;
36
35
import java .util .Collections ;
37
36
import java .util .HashMap ;
@@ -112,36 +111,31 @@ Document getDocument(DocumentKey key) {
112
111
*/
113
112
ImmutableSortedMap <DocumentKey , Document > getDocuments (Iterable <DocumentKey > keys ) {
114
113
Map <DocumentKey , MutableDocument > docs = remoteDocumentCache .getAll (keys );
115
- return getLocalViewOfDocuments (docs , documentOverlayCache :: getOverlay , new HashSet <>());
114
+ return getLocalViewOfDocuments (docs , new HashSet <>());
116
115
}
117
116
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
- */
126
117
private ImmutableSortedMap <DocumentKey , Document > getLocalViewOfDocuments (
127
118
Map <DocumentKey , MutableDocument > docs ,
128
- Function <DocumentKey , Overlay > overlays ,
119
+ Map <DocumentKey , Overlay > cachedOverlays ,
129
120
Set <DocumentKey > existenceStateChanged ) {
130
121
ImmutableSortedMap <DocumentKey , Document > results = emptyDocumentMap ();
131
122
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 ());
134
128
// Recalculate an overlay if the document's existence state is changed due to a remote
135
129
// event *and* the overlay is a PatchMutation. This is because document existence state
136
130
// can change if some patch mutation's preconditions are met.
137
131
// NOTE: we recalculate when `overlay` is null as well, because there might be a patch
138
132
// mutation whose precondition does not match before the change (hence overlay==null),
139
133
// but would now match.
140
- if (existenceStateChanged .contains (entry .getKey ())
134
+ if (existenceStateChanged .contains (doc .getKey ())
141
135
&& (overlay == null || overlay .getMutation () instanceof PatchMutation )) {
142
- recalculateDocuments .put (entry .getKey (), docs . get ( entry . getKey ()) );
136
+ recalculateDocuments .put (doc .getKey (), doc );
143
137
} else if (overlay != null ) {
144
- overlay .getMutation ().applyToLocalView (entry . getValue () , null , Timestamp .now ());
138
+ overlay .getMutation ().applyToLocalView (doc , null , Timestamp .now ());
145
139
}
146
140
}
147
141
@@ -162,9 +156,8 @@ private ImmutableSortedMap<DocumentKey, Document> getLocalViewOfDocuments(
162
156
* useful to determine if some documents overlay needs to be recalculated.
163
157
*/
164
158
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 );
168
161
}
169
162
170
163
private void recalculateAndSaveOverlays (Map <DocumentKey , MutableDocument > docs ) {
@@ -282,33 +275,28 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
282
275
* processed batch id.
283
276
*/
284
277
LocalDocumentsResult getNextDocuments (String collectionGroup , IndexOffset offset , int count ) {
278
+ int largestBatchId = -1 ;
285
279
Map <DocumentKey , MutableDocument > docs =
286
280
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 ();
288
286
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 ()) {
294
288
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 ());
298
292
}
299
- largestBatchId = Math .max (largestBatchId , entry . getValue () .getLargestBatchId ());
293
+ largestBatchId = Math .max (largestBatchId , overlay .getLargestBatchId ());
300
294
}
301
295
docs .putAll (remoteDocumentCache .getAll (mutatedKeys ));
302
296
}
303
297
304
298
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 ());
312
300
return new LocalDocumentsResult (largestBatchId , localDocs );
313
301
}
314
302
0 commit comments