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