@@ -88,54 +88,13 @@ IndexManager getIndexManager() {
88
88
* for it.
89
89
*/
90
90
Document getDocument (DocumentKey key ) {
91
- if (Persistence .OVERLAY_SUPPORT_ENABLED ) {
92
- Mutation overlay = documentOverlayCache .getOverlay (key );
93
- MutableDocument fromOverlay = remoteDocumentCache .get (key );
94
- if (overlay != null ) {
95
- overlay .applyToLocalView (fromOverlay , null , Timestamp .now ());
96
- }
97
-
98
- // TODO(Overlay): Remove below and just return `fromOverlay`.
99
- List <MutationBatch > batches = mutationQueue .getAllMutationBatchesAffectingDocumentKey (key );
100
- Document fromMutationQueue = getDocument (key , batches );
101
- hardAssert (
102
- fromOverlay .equals (fromMutationQueue ),
103
- "Document from overlay does not match mutation queue" );
104
-
105
- return fromOverlay ;
106
- }
107
- List <MutationBatch > batches = mutationQueue .getAllMutationBatchesAffectingDocumentKey (key );
108
- return getDocument (key , batches );
109
- }
110
-
111
- // Internal version of {@code getDocument} that allows reusing batches.
112
- private Document getDocument (DocumentKey key , List <MutationBatch > inBatches ) {
113
- MutableDocument document = remoteDocumentCache .get (key );
114
- for (MutationBatch batch : inBatches ) {
115
- batch .applyToLocalView (document );
116
- }
117
- return document ;
118
- }
119
-
120
- /**
121
- * Applies the given {@code batches} to the given {@code docs}. The docs are updated to reflect
122
- * the contents of the mutations.
123
- *
124
- * <p>Returns a {@link DocumentKey} to {@link FieldMask} map, representing the fields mutated for
125
- * each document. This is useful to build overlays.
126
- */
127
- private Map <DocumentKey , FieldMask > applyLocalMutationsToDocuments (
128
- Map <DocumentKey , MutableDocument > docs , List <MutationBatch > batches ) {
129
- Map <DocumentKey , FieldMask > changedMasks = new HashMap <>();
130
- for (Map .Entry <DocumentKey , MutableDocument > base : docs .entrySet ()) {
131
- FieldMask mask = null ;
132
- for (MutationBatch batch : batches ) {
133
- mask = batch .applyToLocalView (base .getValue (), mask );
134
- }
135
- changedMasks .put (base .getKey (), mask );
91
+ Mutation overlay = documentOverlayCache .getOverlay (key );
92
+ MutableDocument fromOverlay = remoteDocumentCache .get (key );
93
+ if (overlay != null ) {
94
+ overlay .applyToLocalView (fromOverlay , null , Timestamp .now ());
136
95
}
137
96
138
- return changedMasks ;
97
+ return fromOverlay ;
139
98
}
140
99
141
100
/**
@@ -160,31 +119,25 @@ ImmutableSortedMap<DocumentKey, Document> getDocuments(Iterable<DocumentKey> key
160
119
ImmutableSortedMap <DocumentKey , Document > getLocalViewOfDocuments (
161
120
Map <DocumentKey , MutableDocument > docs , Set <DocumentKey > existenceStateChanged ) {
162
121
ImmutableSortedMap <DocumentKey , Document > results = emptyDocumentMap ();
163
- if (Persistence .OVERLAY_SUPPORT_ENABLED ) {
164
- Map <DocumentKey , MutableDocument > recalculateDocuments = new HashMap <>();
165
- for (Map .Entry <DocumentKey , MutableDocument > entry : docs .entrySet ()) {
166
- Mutation overlay = documentOverlayCache .getOverlay (entry .getKey ());
167
- // Recalculate an overlay if the document's existence state is changed due to a remote
168
- // event *and* the overlay is a PatchMutation. This is because document existence state
169
- // can change if some patch mutation's preconditions are met.
170
- // NOTE: we recalculate when `overlay` is null as well, because there might be a patch
171
- // mutation whose precondition does not match before the change (hence overlay==null),
172
- // but would now match.
173
- if (existenceStateChanged .contains (entry .getKey ())
174
- && (overlay == null || overlay instanceof PatchMutation )) {
175
- recalculateDocuments .put (entry .getKey (), docs .get (entry .getKey ()));
176
- } else if (overlay != null ) {
177
- overlay .applyToLocalView (entry .getValue (), null , Timestamp .now ());
178
- }
122
+ Map <DocumentKey , MutableDocument > recalculateDocuments = new HashMap <>();
123
+ for (Map .Entry <DocumentKey , MutableDocument > entry : docs .entrySet ()) {
124
+ Mutation overlay = documentOverlayCache .getOverlay (entry .getKey ());
125
+ // Recalculate an overlay if the document's existence state is changed due to a remote
126
+ // event *and* the overlay is a PatchMutation. This is because document existence state
127
+ // can change if some patch mutation's preconditions are met.
128
+ // NOTE: we recalculate when `overlay` is null as well, because there might be a patch
129
+ // mutation whose precondition does not match before the change (hence overlay==null),
130
+ // but would now match.
131
+ if (existenceStateChanged .contains (entry .getKey ())
132
+ && (overlay == null || overlay instanceof PatchMutation )) {
133
+ recalculateDocuments .put (entry .getKey (), docs .get (entry .getKey ()));
134
+ } else if (overlay != null ) {
135
+ overlay .applyToLocalView (entry .getValue (), null , Timestamp .now ());
179
136
}
180
-
181
- recalculateAndSaveOverlays (recalculateDocuments );
182
- } else {
183
- List <MutationBatch > batches =
184
- mutationQueue .getAllMutationBatchesAffectingDocumentKeys (docs .keySet ());
185
- applyLocalMutationsToDocuments (docs , batches );
186
137
}
187
138
139
+ recalculateAndSaveOverlays (recalculateDocuments );
140
+
188
141
for (Map .Entry <DocumentKey , MutableDocument > entry : docs .entrySet ()) {
189
142
results = results .insert (entry .getKey (), entry .getValue ());
190
143
}
@@ -298,31 +251,9 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
298
251
return results ;
299
252
}
300
253
254
+ /** Queries the remote documents and overlays by doing a full collection scan. */
301
255
private ImmutableSortedMap <DocumentKey , Document > getDocumentsMatchingCollectionQuery (
302
256
Query query , IndexOffset offset ) {
303
- if (Persistence .OVERLAY_SUPPORT_ENABLED ) {
304
- // TODO(Overlay): Remove the assert and just return `fromOverlay`.
305
- ImmutableSortedMap <DocumentKey , Document > fromOverlay =
306
- getDocumentsMatchingCollectionQueryFromOverlayCache (query , offset );
307
- // TODO(Overlay): Delete below before merging. The code passes, but there are tests
308
- // looking at how many documents read from remote document, and this would double
309
- // the count.
310
- /*
311
- ImmutableSortedMap<DocumentKey, Document> fromMutationQueue =
312
- getDocumentsMatchingCollectionQueryFromMutationQueue(query, sinceReadTime);
313
- hardAssert(
314
- fromOverlay.equals(fromMutationQueue),
315
- "Documents from overlay do not match mutation queue version.");
316
- */
317
- return fromOverlay ;
318
- } else {
319
- return getDocumentsMatchingCollectionQueryFromMutationQueue (query , offset );
320
- }
321
- }
322
-
323
- /** Queries the remote documents and overlays by doing a full collection scan. */
324
- private ImmutableSortedMap <DocumentKey , Document >
325
- getDocumentsMatchingCollectionQueryFromOverlayCache (Query query , IndexOffset offset ) {
326
257
Map <DocumentKey , MutableDocument > remoteDocuments =
327
258
remoteDocumentCache .getAll (query .getPath (), offset );
328
259
Map <DocumentKey , Mutation > overlays = documentOverlayCache .getOverlays (query .getPath (), -1 );
0 commit comments