@@ -63,6 +63,7 @@ import {
6363 filter ,
6464 key ,
6565 orderBy ,
66+ orFilter ,
6667 query ,
6768 setMutation ,
6869 version
@@ -534,20 +535,63 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
534535 test . assertQueryReturned ( 'coll/a' , 'coll/e' , 'coll/f' ) ;
535536 } ) ;
536537
538+ it ( 'can auto-create indexes works with or query' , async ( ) => {
539+ const query_ = query (
540+ 'coll' ,
541+ orFilter ( filter ( 'a' , '==' , 3 ) , filter ( 'b' , '==' , true ) )
542+ ) ;
543+ const targetId = await test . allocateQuery ( query_ ) ;
544+ test . configureIndexAutoCreation ( {
545+ isEnabled : true ,
546+ indexAutoCreationMinCollectionSize : 0 ,
547+ relativeIndexReadCostPerDocument : 2
548+ } ) ;
549+
550+ await test . applyRemoteEvents (
551+ docAddedRemoteEvent ( doc ( 'coll/a' , 10 , { b : true } ) , [ targetId ] ) ,
552+ docAddedRemoteEvent ( doc ( 'coll/b' , 10 , { b : false } ) , [ targetId ] ) ,
553+ docAddedRemoteEvent ( doc ( 'coll/c' , 10 , { a : 5 , b : false } ) , [ targetId ] ) ,
554+ docAddedRemoteEvent ( doc ( 'coll/d' , 10 , { a : true } ) , [ targetId ] ) ,
555+ docAddedRemoteEvent ( doc ( 'coll/e' , 10 , { a : 3 , b : true } ) , [ targetId ] )
556+ ) ;
557+
558+ // First time query runs without indexes.
559+ // Based on current heuristic, collection document counts (5) >
560+ // 2 * resultSize (2).
561+ // Full matched index should be created.
562+ await test . executeQuery ( query_ ) ;
563+ test . assertRemoteDocumentsRead ( 0 , 2 ) ;
564+ test . assertQueryReturned ( 'coll/a' , 'coll/e' ) ;
565+
566+ await test . backfillIndexes ( ) ;
567+
568+ await test . applyRemoteEvent (
569+ docAddedRemoteEvent ( doc ( 'coll/f' , 20 , { a : 3 , b : false } ) , [ targetId ] )
570+ ) ;
571+
572+ await test . executeQuery ( query_ ) ;
573+ test . assertRemoteDocumentsRead ( 2 , 1 ) ;
574+ test . assertQueryReturned ( 'coll/a' , 'coll/e' , 'coll/f' ) ;
575+ } ) ;
576+
537577 it ( 'does not auto-create indexes for small collections' , async ( ) => {
538- const query_ = query ( 'coll' , filter ( 'count' , '>=' , 3 ) ) ;
578+ const query_ = query (
579+ 'coll' ,
580+ filter ( 'foo' , '==' , 9 ) ,
581+ filter ( 'count' , '>=' , 3 )
582+ ) ;
539583 const targetId = await test . allocateQuery ( query_ ) ;
540584 test . configureIndexAutoCreation ( {
541585 isEnabled : true ,
542586 relativeIndexReadCostPerDocument : 2
543587 } ) ;
544588
545589 await test . applyRemoteEvents (
546- docAddedRemoteEvent ( doc ( 'coll/a' , 10 , { count : 5 } ) , [ targetId ] ) ,
547- docAddedRemoteEvent ( doc ( 'coll/b' , 10 , { count : 1 } ) , [ targetId ] ) ,
548- docAddedRemoteEvent ( doc ( 'coll/c' , 10 , { count : 0 } ) , [ targetId ] ) ,
549- docAddedRemoteEvent ( doc ( 'coll/d' , 10 , { count : 1 } ) , [ targetId ] ) ,
550- docAddedRemoteEvent ( doc ( 'coll/e' , 10 , { count : 3 } ) , [ targetId ] )
590+ docAddedRemoteEvent ( doc ( 'coll/a' , 10 , { foo : 9 , count : 5 } ) , [ targetId ] ) ,
591+ docAddedRemoteEvent ( doc ( 'coll/b' , 10 , { foo : 8 , count : 6 } ) , [ targetId ] ) ,
592+ docAddedRemoteEvent ( doc ( 'coll/c' , 10 , { foo : 9 , count : 0 } ) , [ targetId ] ) ,
593+ docAddedRemoteEvent ( doc ( 'coll/d' , 10 , { count : 4 } ) , [ targetId ] ) ,
594+ docAddedRemoteEvent ( doc ( 'coll/e' , 10 , { foo : 9 , count : 3 } ) , [ targetId ] )
551595 ) ;
552596
553597 // SDK will not create indexes since collection size is too small.
@@ -558,7 +602,7 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
558602 await test . backfillIndexes ( ) ;
559603
560604 await test . applyRemoteEvent (
561- docAddedRemoteEvent ( doc ( 'coll/f' , 20 , { count : 4 } ) , [ targetId ] )
605+ docAddedRemoteEvent ( doc ( 'coll/f' , 20 , { foo : 9 , count : 4 } ) , [ targetId ] )
562606 ) ;
563607
564608 await test . executeQuery ( query_ ) ;
@@ -602,7 +646,11 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
602646 } ) ;
603647
604648 it ( 'index auto creation works when backfiller runs halfway' , async ( ) => {
605- const query_ = query ( 'coll' , filter ( 'matches' , '==' , 'foo' ) ) ;
649+ const query_ = query (
650+ 'coll' ,
651+ filter ( 'matches' , '==' , 'foo' ) ,
652+ filter ( 'count' , '>' , 10 )
653+ ) ;
606654 const targetId = await test . allocateQuery ( query_ ) ;
607655 test . configureIndexAutoCreation ( {
608656 isEnabled : true ,
@@ -611,11 +659,19 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
611659 } ) ;
612660
613661 await test . applyRemoteEvents (
614- docAddedRemoteEvent ( doc ( 'coll/a' , 10 , { matches : 'foo' } ) , [ targetId ] ) ,
615- docAddedRemoteEvent ( doc ( 'coll/b' , 10 , { matches : '' } ) , [ targetId ] ) ,
616- docAddedRemoteEvent ( doc ( 'coll/c' , 10 , { matches : 'bar' } ) , [ targetId ] ) ,
617- docAddedRemoteEvent ( doc ( 'coll/d' , 10 , { matches : 7 } ) , [ targetId ] ) ,
618- docAddedRemoteEvent ( doc ( 'coll/e' , 10 , { matches : 'foo' } ) , [ targetId ] )
662+ docAddedRemoteEvent ( doc ( 'coll/a' , 10 , { matches : 'foo' , count : 11 } ) , [
663+ targetId
664+ ] ) ,
665+ docAddedRemoteEvent ( doc ( 'coll/b' , 10 , { matches : 'foo' , count : 9 } ) , [
666+ targetId
667+ ] ) ,
668+ docAddedRemoteEvent ( doc ( 'coll/c' , 10 , { matches : 'foo' } ) , [ targetId ] ) ,
669+ docAddedRemoteEvent ( doc ( 'coll/d' , 10 , { matches : 7 , count : 11 } ) , [
670+ targetId
671+ ] ) ,
672+ docAddedRemoteEvent ( doc ( 'coll/e' , 10 , { matches : 'foo' , count : 21 } ) , [
673+ targetId
674+ ] )
619675 ) ;
620676
621677 // First time query runs without indexes.
@@ -629,7 +685,9 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
629685 await test . backfillIndexes ( { maxDocumentsToProcess : 2 } ) ;
630686
631687 await test . applyRemoteEvent (
632- docAddedRemoteEvent ( doc ( 'coll/f' , 20 , { matches : 'foo' } ) , [ targetId ] )
688+ docAddedRemoteEvent ( doc ( 'coll/f' , 20 , { matches : 'foo' , count : 15 } ) , [
689+ targetId
690+ ] )
633691 ) ;
634692
635693 await test . executeQuery ( query_ ) ;
@@ -718,9 +776,14 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
718776
719777 await test . executeQuery ( query2 ) ;
720778 test . assertRemoteDocumentsRead ( 0 , 2 ) ;
779+ test . assertQueryReturned ( 'foo/a' , 'foo/e' ) ;
780+
721781 await test . backfillIndexes ( ) ;
782+
783+ // Run the query in second time, test index won't be created
722784 await test . executeQuery ( query2 ) ;
723785 test . assertRemoteDocumentsRead ( 0 , 2 ) ;
786+ test . assertQueryReturned ( 'foo/a' , 'foo/e' ) ;
724787 } ) ;
725788
726789 it ( 'index auto creation works with mutation' , async ( ) => {
@@ -786,7 +849,6 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
786849 test . assertRemoteDocumentsRead ( 0 , 2 ) ;
787850 test . assertQueryReturned ( 'coll/a' , 'coll/e' ) ;
788851
789- test . configureIndexAutoCreation ( { isEnabled : false } ) ;
790852 await test . backfillIndexes ( ) ;
791853 await test . executeQuery ( query_ ) ;
792854 test . assertRemoteDocumentsRead ( 2 , 0 ) ;
@@ -796,6 +858,13 @@ describe('LocalStore w/ IndexedDB Persistence (Non generic)', () => {
796858 await test . executeQuery ( query_ ) ;
797859 test . assertRemoteDocumentsRead ( 0 , 2 ) ;
798860 test . assertQueryReturned ( 'coll/a' , 'coll/e' ) ;
861+
862+ // Field index is created again.
863+ await test . backfillIndexes ( ) ;
864+
865+ await test . executeQuery ( query_ ) ;
866+ test . assertRemoteDocumentsRead ( 2 , 0 ) ;
867+ test . assertQueryReturned ( 'coll/a' , 'coll/e' ) ;
799868 } ) ;
800869
801870 it ( 'delete all indexes works with manual added indexes' , async ( ) => {
0 commit comments