44import static com .google .firebase .firestore .testutil .TestUtil .docMap ;
55import static com .google .firebase .firestore .testutil .TestUtil .filter ;
66import static com .google .firebase .firestore .testutil .TestUtil .map ;
7+ import static com .google .firebase .firestore .testutil .TestUtil .patchMutation ;
78import static com .google .firebase .firestore .testutil .TestUtil .query ;
89import static org .junit .Assert .assertEquals ;
910
1011import com .google .android .gms .common .internal .Preconditions ;
12+ import com .google .firebase .Timestamp ;
1113import com .google .firebase .database .collection .ImmutableSortedMap ;
1214import com .google .firebase .database .collection .ImmutableSortedSet ;
1315import com .google .firebase .firestore .auth .User ;
1820import com .google .firebase .firestore .model .DocumentSet ;
1921import com .google .firebase .firestore .model .FieldIndex ;
2022import com .google .firebase .firestore .model .MutableDocument ;
23+ import com .google .firebase .firestore .model .mutation .Mutation ;
24+ import com .google .firebase .firestore .model .mutation .MutationBatch ;
2125import java .util .ArrayList ;
2226import java .util .Arrays ;
2327import java .util .Collections ;
@@ -99,6 +103,19 @@ protected void addDocument(MutableDocument... docs) {
99103 });
100104 }
101105
106+ protected void addMutation (Mutation mutation ) {
107+ persistence .runTransaction (
108+ "addMutation" ,
109+ () -> {
110+ MutationBatch batch =
111+ mutationQueue .addMutationBatch (
112+ Timestamp .now (), Collections .emptyList (), Collections .singletonList (mutation ));
113+ Map <DocumentKey , Mutation > overlayMap =
114+ Collections .singletonMap (mutation .getKey (), mutation );
115+ documentOverlayCache .saveOverlays (batch .getBatchId (), overlayMap );
116+ });
117+ }
118+
102119 protected <T > T expectOptimizedCollectionScan (Callable <T > c ) throws Exception {
103120 try {
104121 expectFullCollectionScan = false ;
@@ -169,6 +186,18 @@ private void createTestingCollection(
169186 }
170187 }
171188
189+ private void createMutationForCollection (String basePath , int totalSetCount ) {
190+ ArrayList <Integer > indexes = new ArrayList <>();
191+ for (int index = 0 ; index < totalSetCount * 10 ; index ++) {
192+ indexes .add (index );
193+ }
194+ Collections .shuffle (indexes );
195+
196+ for (int i = 0 ; i < totalSetCount ; i ++) {
197+ addMutation (patchMutation (basePath + "/" + indexes .get (i ), map ("a" , 5 )));
198+ }
199+ }
200+
172201 @ Test
173202 public void testCombinesIndexedWithNonIndexedResults () throws Exception {
174203 // Every set contains 10 documents
@@ -192,6 +221,7 @@ public void testCombinesIndexedWithNonIndexedResults() throws Exception {
192221 Query query = query (basePath ).filter (filter ("match" , "==" , true ));
193222 indexManager .createTargetIndices (query .toTarget ());
194223 createTestingCollection (basePath , totalSetCount , portion , numOfFields );
224+ createMutationForCollection (basePath , totalSetCount );
195225
196226 QueryContext counterWithoutIndex = new QueryContext ();
197227 long beforeAutoStart = System .nanoTime ();
0 commit comments