30
30
import static junit .framework .TestCase .assertTrue ;
31
31
32
32
import com .google .firebase .firestore .auth .User ;
33
+ import com .google .firebase .firestore .core .Query ;
33
34
import com .google .firebase .firestore .core .Target ;
34
35
import com .google .firebase .firestore .model .DocumentKey ;
35
36
import com .google .firebase .firestore .model .FieldIndex ;
@@ -365,58 +366,38 @@ public void testBackfillMutationsFromHighWaterMark() {
365
366
366
367
@ Test
367
368
public void testBackfillUpdatesExistingDocToNewValue () {
368
- backfiller .setMaxDocumentsToProcess (1 );
369
- addFieldIndex ("coll1" , "foo" );
370
- FieldIndex fieldIndex = indexManager .getFieldIndexes ("coll1" ).iterator ().next ();
369
+ Query queryA = query ("coll" ).filter (filter ("foo" , "==" , 2 ));
370
+ addFieldIndex ("coll" , "foo" );
371
+
372
+ addDoc ("coll/doc" , version (10 ), "foo" , 1 );
371
373
372
- addDoc ("coll1/docA" , version (10 ), "foo" , 1 );
373
374
int documentsProcessed = backfiller .backfill ();
374
375
assertEquals (1 , documentsProcessed );
375
- Target target = query ("coll1" ).filter (filter ("foo" , "==" , 2 )).toTarget ();
376
- Set <DocumentKey > matching = indexManager .getDocumentsMatchingTarget (fieldIndex , target );
377
- assertEquals (0 , matching .size ());
376
+ verifyQueryResults (queryA );
378
377
379
378
// Update doc to new remote version with new value.
380
- addDoc ("coll1/docA " , version (40 ), "foo" , 2 );
379
+ addDoc ("coll/doc " , version (40 ), "foo" , 2 );
381
380
documentsProcessed = backfiller .backfill ();
382
- assertEquals (1 , documentsProcessed );
383
- matching = indexManager .getDocumentsMatchingTarget (fieldIndex , target );
384
- assertEquals (1 , matching .size ());
381
+
382
+ verifyQueryResults (queryA , "coll/doc" );
385
383
}
386
384
387
385
@ Test
388
386
public void testBackfillUpdatesDocsThatNoLongerMatch () {
389
- backfiller . setMaxDocumentsToProcess ( 1 );
390
- addFieldIndex ("coll1 " , "foo" );
391
- FieldIndex fieldIndex = indexManager . getFieldIndexes ( "coll1" ). iterator (). next ( );
387
+ Query queryA = query ( "coll" ). filter ( filter ( "foo" , ">" , 0 ) );
388
+ addFieldIndex ("coll " , "foo" );
389
+ addDoc ( "coll/doc" , version ( 10 ), "foo" , 1 );
392
390
393
- addDoc ("coll1/docA" , version (10 ), "foo" , 1 );
394
391
int documentsProcessed = backfiller .backfill ();
395
392
assertEquals (1 , documentsProcessed );
396
- Target target = query ("coll1" ).filter (filter ("foo" , ">" , 0 )).toTarget ();
397
- Set <DocumentKey > matching = indexManager .getDocumentsMatchingTarget (fieldIndex , target );
398
- assertEquals (1 , matching .size ());
393
+ verifyQueryResults (queryA , "coll/doc" );
399
394
400
395
// Update doc to new remote version with new value that doesn't match field index.
401
- addDoc ("coll1/docA" , version (40 ), "foo" , -1 );
402
- documentsProcessed = backfiller .backfill ();
403
- assertEquals (1 , documentsProcessed );
404
- matching = indexManager .getDocumentsMatchingTarget (fieldIndex , target );
405
- assertTrue (matching .isEmpty ());
406
- }
407
-
408
- @ Test
409
- public void testBackfillUpdatesToLatestReadTimeInCollection () {
410
- // check that offset is set to rdc latest read time if no docs match current coll
411
- addFieldIndex ("coll1" , "foo" );
412
- addDoc ("coll1/doc" , version (5 ), "foo" , 1 );
396
+ addDoc ("coll/doc" , version (40 ), "foo" , -1 );
413
397
414
- int documentsProcessed = backfiller .backfill ();
398
+ documentsProcessed = backfiller .backfill ();
415
399
assertEquals (1 , documentsProcessed );
416
-
417
- // Offset should be set to latest read time in the cache if no documents were indexed.
418
- Iterator <FieldIndex > it = indexManager .getFieldIndexes ("coll1" ).iterator ();
419
- assertEquals (version (5 ), it .next ().getIndexState ().getOffset ().getReadTime ());
400
+ verifyQueryResults (queryA );
420
401
}
421
402
422
403
@ Test
@@ -434,7 +415,7 @@ public void testBackfillDoesNotProcessSameDocumentTwice() {
434
415
}
435
416
436
417
@ Test
437
- public void testBackfillAppliesOverlayToRemoteDoc () {
418
+ public void testBackfillAppliesSetToRemoteDoc () {
438
419
addFieldIndex ("coll" , "foo" );
439
420
addDoc ("coll/doc" , version (5 ), "boo" , 1 );
440
421
@@ -446,14 +427,35 @@ public void testBackfillAppliesOverlayToRemoteDoc() {
446
427
documentsProcessed = backfiller .backfill ();
447
428
assertEquals (1 , documentsProcessed );
448
429
449
- FieldIndex fieldIndex = indexManager .getFieldIndexes ("coll" ).iterator ().next ();
450
- Target target = query ("coll" ).filter (filter ("foo" , "==" , 1 )).toTarget ();
451
- Set <DocumentKey > matching = indexManager .getDocumentsMatchingTarget (fieldIndex , target );
452
- assertEquals (1 , matching .size ());
430
+ verifyQueryResults ("coll" , "coll/doc" );
453
431
}
454
432
455
433
@ Test
456
- public void testBackfillAppliesDeleteMutationOnRemoteDoc () {
434
+ public void testBackfillAppliesPatchToRemoteDoc () {
435
+ Query queryA = query ("coll" ).orderBy (orderBy ("a" ));
436
+ Query queryB = query ("coll" ).orderBy (orderBy ("b" ));
437
+
438
+ addFieldIndex ("coll" , "a" );
439
+ addFieldIndex ("coll" , "b" );
440
+ addDoc ("coll/doc" , version (5 ), "a" , 1 );
441
+
442
+ int documentsProcessed = backfiller .backfill ();
443
+ assertEquals (1 , documentsProcessed );
444
+
445
+ verifyQueryResults (queryA , "coll/doc" );
446
+ verifyQueryResults (queryB );
447
+
448
+ Mutation patch = patchMutation ("coll/doc" , map ("b" , 1 ));
449
+ addMutationToOverlay ("coll/doc" , patch );
450
+ documentsProcessed = backfiller .backfill ();
451
+ assertEquals (1 , documentsProcessed );
452
+
453
+ verifyQueryResults (queryA , "coll/doc" );
454
+ verifyQueryResults (queryB , "coll/doc" );
455
+ }
456
+
457
+ @ Test
458
+ public void testBackfillAppliesDeleteToRemoteDoc () {
457
459
addFieldIndex ("coll" , "foo" );
458
460
addDoc ("coll/doc" , version (5 ), "foo" , 1 );
459
461
@@ -471,6 +473,28 @@ public void testBackfillAppliesDeleteMutationOnRemoteDoc() {
471
473
assertTrue (matching .isEmpty ());
472
474
}
473
475
476
+ @ Test
477
+ public void testReindexesDocumentsWhenNewIndexIsAdded () {
478
+ Query queryA = query ("coll" ).orderBy (orderBy ("a" ));
479
+ Query queryB = query ("coll" ).orderBy (orderBy ("b" ));
480
+
481
+ addFieldIndex ("coll" , "a" );
482
+ addDoc ("coll/doc1" , version (1 ), "a" , 1 );
483
+ addDoc ("coll/doc2" , version (1 ), "b" , 1 );
484
+
485
+ int documentsProcessed = backfiller .backfill ();
486
+ assertEquals (2 , documentsProcessed );
487
+ verifyQueryResults (queryA , "coll/doc1" );
488
+ verifyQueryResults (queryB );
489
+
490
+ addFieldIndex ("coll" , "b" );
491
+ documentsProcessed = backfiller .backfill ();
492
+ assertEquals (2 , documentsProcessed );
493
+
494
+ verifyQueryResults (queryA , "coll/doc1" );
495
+ verifyQueryResults (queryB , "coll/doc2" );
496
+ }
497
+
474
498
private void addFieldIndex (String collectionGroup , String fieldName ) {
475
499
FieldIndex fieldIndex =
476
500
fieldIndex (collectionGroup , fieldName , FieldIndex .Segment .Kind .ASCENDING );
@@ -499,12 +523,20 @@ private void addFieldIndex(String collectionGroup, String fieldName, long sequen
499
523
indexManager .addFieldIndex (fieldIndex );
500
524
}
501
525
502
- private void verifyQueryResults (String collectionGroup , String ... expectedKeys ) {
503
- Target target = query ( collectionGroup ). orderBy ( orderBy ( "foo" )) .toTarget ();
526
+ private void verifyQueryResults (Query query , String ... expectedKeys ) {
527
+ Target target = query .toTarget ();
504
528
FieldIndex persistedIndex = indexManager .getFieldIndex (target );
505
- Set <DocumentKey > actualKeys = indexManager .getDocumentsMatchingTarget (persistedIndex , target );
506
- assertThat (actualKeys )
507
- .containsExactlyElementsIn (Arrays .stream (expectedKeys ).map (TestUtil ::key ).toArray ());
529
+ if (persistedIndex != null ) {
530
+ Set <DocumentKey > actualKeys = indexManager .getDocumentsMatchingTarget (persistedIndex , target );
531
+ assertThat (actualKeys )
532
+ .containsExactlyElementsIn (Arrays .stream (expectedKeys ).map (TestUtil ::key ).toArray ());
533
+ } else {
534
+ assertEquals (0 , expectedKeys .length );
535
+ }
536
+ }
537
+
538
+ private void verifyQueryResults (String collectionGroup , String ... expectedKeys ) {
539
+ verifyQueryResults (query (collectionGroup ).orderBy (orderBy ("foo" )), expectedKeys );
508
540
}
509
541
510
542
/** Creates a document and adds it to the RemoteDocumentCache. */
0 commit comments