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 ;
34
35
import java .util .ArrayList ;
35
36
import java .util .Collections ;
36
37
import java .util .HashMap ;
@@ -111,7 +112,7 @@ Document getDocument(DocumentKey key) {
111
112
*/
112
113
ImmutableSortedMap <DocumentKey , Document > getDocuments (Iterable <DocumentKey > keys ) {
113
114
Map <DocumentKey , MutableDocument > docs = remoteDocumentCache .getAll (keys );
114
- return getLocalViewOfDocuments (docs , new HashSet <>());
115
+ return getLocalViewOfDocuments (docs , documentOverlayCache :: getOverlay , new HashSet <>());
115
116
}
116
117
117
118
/**
@@ -123,11 +124,13 @@ ImmutableSortedMap<DocumentKey, Document> getDocuments(Iterable<DocumentKey> key
123
124
* useful to determine if some documents overlay needs to be recalculated.
124
125
*/
125
126
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 ) {
127
130
ImmutableSortedMap <DocumentKey , Document > results = emptyDocumentMap ();
128
131
Map <DocumentKey , MutableDocument > recalculateDocuments = new HashMap <>();
129
132
for (Map .Entry <DocumentKey , MutableDocument > entry : docs .entrySet ()) {
130
- Overlay overlay = documentOverlayCache . getOverlay (entry .getKey ());
133
+ Overlay overlay = overlays . apply (entry .getKey ());
131
134
// Recalculate an overlay if the document's existence state is changed due to a remote
132
135
// event *and* the overlay is a PatchMutation. This is because document existence state
133
136
// can change if some patch mutation's preconditions are met.
@@ -262,18 +265,18 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
262
265
* @param offset The offset to index into.
263
266
* @param count The number of documents to return
264
267
* @return A LocalDocumentsRResult with the documents that follow the provided offset and the last
265
- * processed batch id.
268
+ * processed batch id.
266
269
*/
267
270
LocalDocumentsResult getNextDocuments (String collectionGroup , IndexOffset offset , int count ) {
268
271
Map <DocumentKey , MutableDocument > docs =
269
272
remoteDocumentCache .getAll (collectionGroup , offset , count );
270
- Map <DocumentKey , Overlay > overlays = Collections . emptyMap ();
273
+ final Map <DocumentKey , Overlay > overlays = new HashMap <> ();
271
274
272
- int largestBatchId = 0 ;
275
+ int largestBatchId = - 1 ;
273
276
if (count - docs .size () > 0 ) {
274
- overlays =
277
+ overlays . putAll (
275
278
documentOverlayCache .getOverlays (
276
- collectionGroup , offset .getLargestBatchId (), count - docs .size ());
279
+ collectionGroup , offset .getLargestBatchId (), count - docs .size ())) ;
277
280
List <DocumentKey > mutatedKeys = new ArrayList <>();
278
281
for (Map .Entry <DocumentKey , Overlay > entry : overlays .entrySet ()) {
279
282
if (!docs .containsKey (entry .getKey ())) {
@@ -284,17 +287,14 @@ LocalDocumentsResult getNextDocuments(String collectionGroup, IndexOffset offset
284
287
docs .putAll (remoteDocumentCache .getAll (mutatedKeys ));
285
288
}
286
289
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 ());
298
298
return new LocalDocumentsResult (largestBatchId , localDocs );
299
299
}
300
300
0 commit comments