24
24
import static com .google .firebase .firestore .testutil .TestUtil .key ;
25
25
import static com .google .firebase .firestore .testutil .TestUtil .keyMap ;
26
26
import static com .google .firebase .firestore .testutil .TestUtil .map ;
27
+ import static com .google .firebase .firestore .testutil .TestUtil .orFilters ;
27
28
import static com .google .firebase .firestore .testutil .TestUtil .orderBy ;
28
29
import static com .google .firebase .firestore .testutil .TestUtil .query ;
29
30
import static com .google .firebase .firestore .testutil .TestUtil .setMutation ;
@@ -398,19 +399,50 @@ public void testCanAutoCreateIndexes() {
398
399
assertQueryReturned ("coll/a" , "coll/e" , "coll/f" );
399
400
}
400
401
402
+ @ Test
403
+ public void testCanAutoCreateIndexesWorksWithOrQuery () {
404
+ Query query = query ("coll" ).filter (orFilters (filter ("a" , "==" , 3 ), filter ("b" , "==" , true )));
405
+ int targetId = allocateQuery (query );
406
+
407
+ setIndexAutoCreationEnabled (true );
408
+ setMinCollectionSizeToAutoCreateIndex (0 );
409
+ setRelativeIndexReadCostPerDocument (2 );
410
+
411
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("b" , true )), targetId ));
412
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("b" , false )), targetId ));
413
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("a" , 5 , "b" , false )), targetId ));
414
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("a" , true )), targetId ));
415
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("a" , 3 , "b" , true )), targetId ));
416
+
417
+ // First time query runs without indexes.
418
+ // Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
419
+ // Full matched index should be created.
420
+ executeQuery (query );
421
+ assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
422
+ assertQueryReturned ("coll/e" , "coll/a" );
423
+
424
+ backfillIndexes ();
425
+
426
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("a" , 3 , "b" , false )), targetId ));
427
+
428
+ executeQuery (query );
429
+ assertRemoteDocumentsRead (/* byKey= */ 2 , /* byCollection= */ 1 );
430
+ assertQueryReturned ("coll/f" , "coll/e" , "coll/a" );
431
+ }
432
+
401
433
@ Test
402
434
public void testDoesNotAutoCreateIndexesForSmallCollections () {
403
- Query query = query ("coll" ).filter (filter ("count" , ">=" , 3 ));
435
+ Query query = query ("coll" ).filter (filter ("foo" , "==" , 9 )). filter ( filter ( " count" , ">=" , 3 ));
404
436
int targetId = allocateQuery (query );
405
437
406
438
setIndexAutoCreationEnabled (true );
407
439
setRelativeIndexReadCostPerDocument (2 );
408
440
409
- applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("count" , 5 )), targetId ));
410
- applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("count" , 1 )), targetId ));
411
- applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("count" , 0 )), targetId ));
412
- applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("count" , 1 )), targetId ));
413
- applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("count" , 3 )), targetId ));
441
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("foo" , 9 , " count" , 5 )), targetId ));
442
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("foo" , 8 , " count" , 6 )), targetId ));
443
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("foo" , 9 , " count" , 0 )), targetId ));
444
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("count" , 4 )), targetId ));
445
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("foo" , 9 , " count" , 3 )), targetId ));
414
446
415
447
// SDK will not create indexes since collection size is too small.
416
448
executeQuery (query );
@@ -419,7 +451,7 @@ public void testDoesNotAutoCreateIndexesForSmallCollections() {
419
451
420
452
backfillIndexes ();
421
453
422
- applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("count" , 4 )), targetId ));
454
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("foo" , 9 , " count" , 4 )), targetId ));
423
455
424
456
executeQuery (query );
425
457
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 3 );
@@ -460,18 +492,22 @@ public void testDoesNotAutoCreateIndexesWhenIndexLookUpIsExpensive() {
460
492
461
493
@ Test
462
494
public void testIndexAutoCreationWorksWhenBackfillerRunsHalfway () {
463
- Query query = query ("coll" ).filter (filter ("matches" , "==" , "foo" ));
495
+ Query query =
496
+ query ("coll" ).filter (filter ("matches" , "==" , "foo" )).filter (filter ("count" , ">" , 10 ));
464
497
int targetId = allocateQuery (query );
465
498
466
499
setIndexAutoCreationEnabled (true );
467
500
setMinCollectionSizeToAutoCreateIndex (0 );
468
501
setRelativeIndexReadCostPerDocument (2 );
469
502
470
- applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("matches" , "foo" )), targetId ));
471
- applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("matches" , "" )), targetId ));
472
- applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("matches" , "bar" )), targetId ));
473
- applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("matches" , 7 )), targetId ));
474
- applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("matches" , "foo" )), targetId ));
503
+ applyRemoteEvent (
504
+ addedRemoteEvent (doc ("coll/a" , 10 , map ("matches" , "foo" , "count" , 11 )), targetId ));
505
+ applyRemoteEvent (
506
+ addedRemoteEvent (doc ("coll/b" , 10 , map ("matches" , "foo" , "count" , 9 )), targetId ));
507
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("matches" , "foo" )), targetId ));
508
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("matches" , 7 , "count" , 11 )), targetId ));
509
+ applyRemoteEvent (
510
+ addedRemoteEvent (doc ("coll/e" , 10 , map ("matches" , "foo" , "count" , 21 )), targetId ));
475
511
476
512
// First time query is running without indexes.
477
513
// Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
@@ -483,7 +519,8 @@ public void testIndexAutoCreationWorksWhenBackfillerRunsHalfway() {
483
519
setBackfillerMaxDocumentsToProcess (2 );
484
520
backfillIndexes ();
485
521
486
- applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("matches" , "foo" )), targetId ));
522
+ applyRemoteEvent (
523
+ addedRemoteEvent (doc ("coll/f" , 20 , map ("matches" , "foo" , "count" , 15 )), targetId ));
487
524
488
525
executeQuery (query );
489
526
assertRemoteDocumentsRead (/* byKey= */ 1 , /* byCollection= */ 2 );
@@ -564,12 +601,14 @@ public void testDisableIndexAutoCreationWorks() {
564
601
565
602
executeQuery (query2 );
566
603
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
604
+ assertQueryReturned ("foo/a" , "foo/e" );
567
605
568
606
backfillIndexes ();
569
607
570
608
// Run the query in second time, test index won't be created
571
609
executeQuery (query2 );
572
610
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
611
+ assertQueryReturned ("foo/a" , "foo/e" );
573
612
}
574
613
575
614
@ Test
@@ -594,8 +633,6 @@ public void testDeleteAllIndexesWorksWithIndexAutoCreation() {
594
633
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
595
634
assertQueryReturned ("coll/a" , "coll/e" );
596
635
597
- setIndexAutoCreationEnabled (false );
598
-
599
636
backfillIndexes ();
600
637
601
638
executeQuery (query );
@@ -607,6 +644,13 @@ public void testDeleteAllIndexesWorksWithIndexAutoCreation() {
607
644
executeQuery (query );
608
645
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
609
646
assertQueryReturned ("coll/a" , "coll/e" );
647
+
648
+ // Field index is created again.
649
+ backfillIndexes ();
650
+
651
+ executeQuery (query );
652
+ assertRemoteDocumentsRead (/* byKey= */ 2 , /* byCollection= */ 0 );
653
+ assertQueryReturned ("coll/a" , "coll/e" );
610
654
}
611
655
612
656
@ Test
0 commit comments