@@ -248,14 +248,9 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
248
248
return results ;
249
249
}
250
250
251
+ /** Queries the remote documents and overlays by doing a full collection scan. */
251
252
private ImmutableSortedMap <DocumentKey , Document > getDocumentsMatchingCollectionQuery (
252
253
Query query , IndexOffset offset ) {
253
- return getDocumentsMatchingCollectionQueryFromOverlayCache (query , offset );
254
- }
255
-
256
- /** Queries the remote documents and overlays by doing a full collection scan. */
257
- private ImmutableSortedMap <DocumentKey , Document >
258
- getDocumentsMatchingCollectionQueryFromOverlayCache (Query query , IndexOffset offset ) {
259
254
ImmutableSortedMap <DocumentKey , MutableDocument > remoteDocuments =
260
255
remoteDocumentCache .getAllDocumentsMatchingQuery (query , offset );
261
256
Map <DocumentKey , Mutation > overlays = documentOverlayCache .getOverlays (query .getPath (), -1 );
@@ -288,79 +283,4 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
288
283
289
284
return results ;
290
285
}
291
-
292
- /** Queries the remote documents and mutation queue, by doing a full collection scan. */
293
- private ImmutableSortedMap <DocumentKey , Document >
294
- getDocumentsMatchingCollectionQueryFromMutationQueue (Query query , IndexOffset offset ) {
295
- ImmutableSortedMap <DocumentKey , MutableDocument > remoteDocuments =
296
- remoteDocumentCache .getAllDocumentsMatchingQuery (query , offset );
297
-
298
- // TODO(indexing): We should plumb sinceReadTime through to the mutation queue
299
- List <MutationBatch > matchingBatches = mutationQueue .getAllMutationBatchesAffectingQuery (query );
300
-
301
- remoteDocuments = addMissingBaseDocuments (matchingBatches , remoteDocuments );
302
-
303
- for (MutationBatch batch : matchingBatches ) {
304
- for (Mutation mutation : batch .getMutations ()) {
305
- // Only process documents belonging to the collection.
306
- if (!query .getPath ().isImmediateParentOf (mutation .getKey ().getPath ())) {
307
- continue ;
308
- }
309
-
310
- DocumentKey key = mutation .getKey ();
311
- MutableDocument document = remoteDocuments .get (key );
312
- if (document == null ) {
313
- // Create invalid document to apply mutations on top of
314
- document = MutableDocument .newInvalidDocument (key );
315
- remoteDocuments = remoteDocuments .insert (key , document );
316
- }
317
- mutation .applyToLocalView (
318
- document , FieldMask .fromSet (new HashSet <>()), batch .getLocalWriteTime ());
319
- if (!document .isFoundDocument ()) {
320
- remoteDocuments = remoteDocuments .remove (key );
321
- }
322
- }
323
- }
324
-
325
- ImmutableSortedMap <DocumentKey , Document > results = emptyDocumentMap ();
326
- for (Map .Entry <DocumentKey , MutableDocument > docEntry : remoteDocuments ) {
327
- // Finally, insert the documents that still match the query
328
- if (query .matches (docEntry .getValue ())) {
329
- results = results .insert (docEntry .getKey (), docEntry .getValue ());
330
- }
331
- }
332
-
333
- return results ;
334
- }
335
-
336
- /**
337
- * It is possible that a {@code PatchMutation} can make a document match a query, even if the
338
- * version in the {@code RemoteDocumentCache} is not a match yet (waiting for server to ack). To
339
- * handle this, we find all document keys affected by the {@code PatchMutation}s that are not in
340
- * {@code existingDocs} yet, and back fill them via {@code remoteDocumentCache.getAll}, otherwise
341
- * those {@code PatchMutation}s will be ignored because no base document can be found, and lead to
342
- * missing results for the query.
343
- */
344
- private ImmutableSortedMap <DocumentKey , MutableDocument > addMissingBaseDocuments (
345
- List <MutationBatch > matchingBatches ,
346
- ImmutableSortedMap <DocumentKey , MutableDocument > existingDocs ) {
347
- HashSet <DocumentKey > missingDocKeys = new HashSet <>();
348
- for (MutationBatch batch : matchingBatches ) {
349
- for (Mutation mutation : batch .getMutations ()) {
350
- if (mutation instanceof PatchMutation && !existingDocs .containsKey (mutation .getKey ())) {
351
- missingDocKeys .add (mutation .getKey ());
352
- }
353
- }
354
- }
355
-
356
- ImmutableSortedMap <DocumentKey , MutableDocument > mergedDocs = existingDocs ;
357
- Map <DocumentKey , MutableDocument > missingDocs = remoteDocumentCache .getAll (missingDocKeys );
358
- for (Map .Entry <DocumentKey , MutableDocument > entry : missingDocs .entrySet ()) {
359
- if (entry .getValue ().isFoundDocument ()) {
360
- mergedDocs = mergedDocs .insert (entry .getKey (), entry .getValue ());
361
- }
362
- }
363
-
364
- return mergedDocs ;
365
- }
366
286
}
0 commit comments