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 ))).orderBy (orderBy ("time" , "desc" ));
405
+ int targetId = allocateQuery (query );
406
+
407
+ setIndexAutoCreationEnabled (true );
408
+ setMinCollectionSizeToAutoCreateIndex (0 );
409
+ setRelativeIndexReadCostPerDocument (2 );
410
+
411
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("time" , 0 , "b" , true )), targetId ));
412
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("time" , 2 , "b" , false )), targetId ));
413
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("time" , 7 , "a" , 5 , "b" , false )), targetId ));
414
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("time" , 3 , "a" , true )), targetId ));
415
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("time" , 1 , "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 ("time" , 7 , "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,18 @@ 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 = query ("coll" ).filter (filter ("matches" , "==" , "foo" )). filter ( filter ( "count" , ">" , 10 )) ;
464
496
int targetId = allocateQuery (query );
465
497
466
498
setIndexAutoCreationEnabled (true );
467
499
setMinCollectionSizeToAutoCreateIndex (0 );
468
500
setRelativeIndexReadCostPerDocument (2 );
469
501
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 ));
502
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("matches" , "foo" , "count" , 11 )), targetId ));
503
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("matches" , "foo" , "count" , 9 )), targetId ));
504
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("matches" , "foo " )), targetId ));
505
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("matches" , 7 , "count" , 11 )), targetId ));
506
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("matches" , "foo" , "count" , 21 )), targetId ));
475
507
476
508
// First time query is running without indexes.
477
509
// Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
@@ -483,7 +515,7 @@ public void testIndexAutoCreationWorksWhenBackfillerRunsHalfway() {
483
515
setBackfillerMaxDocumentsToProcess (2 );
484
516
backfillIndexes ();
485
517
486
- applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("matches" , "foo" )), targetId ));
518
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/f" , 20 , map ("matches" , "foo" , "count" , 15 )), targetId ));
487
519
488
520
executeQuery (query );
489
521
assertRemoteDocumentsRead (/* byKey= */ 1 , /* byCollection= */ 2 );
@@ -492,7 +524,7 @@ public void testIndexAutoCreationWorksWhenBackfillerRunsHalfway() {
492
524
493
525
@ Test
494
526
public void testIndexCreatedByIndexAutoCreationExistsAfterTurnOffAutoCreation () {
495
- Query query = query ("coll" ).filter (filter ("value" , "not-in" , Collections .singletonList (3 )));
527
+ Query query = query ("coll" ).filter (filter ("value" , "not-in" , Collections .singletonList (3 ))). orderBy ( orderBy ( "value" , "asc" )) ;
496
528
int targetId = allocateQuery (query );
497
529
498
530
setIndexAutoCreationEnabled (true );
@@ -564,49 +596,51 @@ public void testDisableIndexAutoCreationWorks() {
564
596
565
597
executeQuery (query2 );
566
598
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
599
+ assertQueryReturned ("foo/a" , "foo/e" );
567
600
568
601
backfillIndexes ();
569
602
570
603
// Run the query in second time, test index won't be created
571
604
executeQuery (query2 );
572
605
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
606
+ assertQueryReturned ("foo/a" , "foo/e" );
573
607
}
574
608
575
609
@ Test
576
610
public void testDeleteAllIndexesWorksWithIndexAutoCreation () {
577
- Query query = query ("coll" ).filter (filter ("value" , "==" , "match" ));
611
+ Query query = query ("coll" ).filter (filter ("value" , "==" , "match" )). orderBy ( orderBy ( "time" , "asc" )) ;
578
612
int targetId = allocateQuery (query );
579
613
580
614
setIndexAutoCreationEnabled (true );
581
615
setMinCollectionSizeToAutoCreateIndex (0 );
582
616
setRelativeIndexReadCostPerDocument (2 );
583
617
584
- applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("value" , "match" )), targetId ));
585
- applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("value" , Double .NaN )), targetId ));
586
- applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("value" , null )), targetId ));
587
- applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("value" , "mismatch" )), targetId ));
588
- applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("value" , "match" )), targetId ));
618
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/a" , 10 , map ("time" , 1 , " value" , "match" )), targetId ));
619
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/b" , 10 , map ("time" , 2 , " value" , Double .NaN )), targetId ));
620
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/c" , 10 , map ("time" , 7 , " value" , null )), targetId ));
621
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/d" , 10 , map ("time" , 3 , " value" , "mismatch" )), targetId ));
622
+ applyRemoteEvent (addedRemoteEvent (doc ("coll/e" , 10 , map ("time" , 0 , " value" , "match" )), targetId ));
589
623
590
624
// First time query is running without indexes.
591
625
// Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
592
626
// Full matched index should be created.
593
627
executeQuery (query );
594
628
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
595
- assertQueryReturned ("coll/a " , "coll/e " );
629
+ assertQueryReturned ("coll/e " , "coll/a " );
596
630
597
631
setIndexAutoCreationEnabled (false );
598
632
599
633
backfillIndexes ();
600
634
601
635
executeQuery (query );
602
636
assertRemoteDocumentsRead (/* byKey= */ 2 , /* byCollection= */ 0 );
603
- assertQueryReturned ("coll/a " , "coll/e " );
637
+ assertQueryReturned ("coll/e " , "coll/a " );
604
638
605
639
deleteAllIndexes ();
606
640
607
641
executeQuery (query );
608
642
assertRemoteDocumentsRead (/* byKey= */ 0 , /* byCollection= */ 2 );
609
- assertQueryReturned ("coll/a " , "coll/e " );
643
+ assertQueryReturned ("coll/e " , "coll/a " );
610
644
}
611
645
612
646
@ Test
0 commit comments