|
3 | 3 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
|
4 | 4 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.toDataMap;
|
5 | 5 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
|
| 6 | +import static com.google.firebase.firestore.testutil.TestUtil.doc; |
| 7 | +import static com.google.firebase.firestore.testutil.TestUtil.docMap; |
| 8 | +import static com.google.firebase.firestore.testutil.TestUtil.filter; |
6 | 9 | import static com.google.firebase.firestore.testutil.TestUtil.map;
|
| 10 | +import static com.google.firebase.firestore.testutil.TestUtil.query; |
7 | 11 | import static org.junit.Assert.assertEquals;
|
8 | 12 |
|
| 13 | +import android.content.Context; |
| 14 | +import androidx.test.core.app.ApplicationProvider; |
9 | 15 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
| 16 | +import com.google.android.gms.common.internal.Preconditions; |
10 | 17 | import com.google.android.gms.tasks.Task;
|
| 18 | +import com.google.firebase.database.collection.ImmutableSortedMap; |
| 19 | +import com.google.firebase.database.collection.ImmutableSortedSet; |
| 20 | +import com.google.firebase.firestore.auth.User; |
| 21 | +import com.google.firebase.firestore.core.Query; |
| 22 | +import com.google.firebase.firestore.core.View; |
| 23 | +import com.google.firebase.firestore.local.DocumentOverlayCache; |
| 24 | +import com.google.firebase.firestore.local.IndexManager; |
| 25 | +import com.google.firebase.firestore.local.LocalDocumentsView; |
| 26 | +import com.google.firebase.firestore.local.LocalSerializer; |
| 27 | +import com.google.firebase.firestore.local.LruGarbageCollector; |
| 28 | +import com.google.firebase.firestore.local.MutationQueue; |
| 29 | +import com.google.firebase.firestore.local.Persistence; |
| 30 | +import com.google.firebase.firestore.local.QueryContext; |
| 31 | +import com.google.firebase.firestore.local.QueryEngine; |
| 32 | +import com.google.firebase.firestore.local.RemoteDocumentCache; |
| 33 | +import com.google.firebase.firestore.local.SQLitePersistence; |
| 34 | +import com.google.firebase.firestore.model.DatabaseId; |
| 35 | +import com.google.firebase.firestore.model.Document; |
| 36 | +import com.google.firebase.firestore.model.DocumentKey; |
| 37 | +import com.google.firebase.firestore.model.DocumentSet; |
| 38 | +import com.google.firebase.firestore.model.FieldIndex; |
| 39 | +import com.google.firebase.firestore.model.MutableDocument; |
| 40 | +import com.google.firebase.firestore.remote.RemoteSerializer; |
11 | 41 | import java.util.ArrayList;
|
12 | 42 | import java.util.Arrays;
|
13 | 43 | import java.util.Collections;
|
14 | 44 | import java.util.List;
|
15 | 45 | import java.util.Map;
|
| 46 | +import java.util.concurrent.Callable; |
| 47 | +import java.util.concurrent.TimeUnit; |
| 48 | +import javax.annotation.Nullable; |
| 49 | +import org.junit.Before; |
16 | 50 | import org.junit.Test;
|
17 | 51 | import org.junit.runner.RunWith;
|
18 | 52 |
|
@@ -127,4 +161,211 @@ public void testIndexedWithNonIndexedTime() {
|
127 | 161 | }
|
128 | 162 | }
|
129 | 163 | }
|
| 164 | + |
| 165 | + private Persistence persistence; |
| 166 | + private RemoteDocumentCache remoteDocumentCache; |
| 167 | + private MutationQueue mutationQueue; |
| 168 | + private DocumentOverlayCache documentOverlayCache; |
| 169 | + |
| 170 | + protected IndexManager indexManager; |
| 171 | + protected QueryEngine queryEngine; |
| 172 | + |
| 173 | + private @Nullable Boolean expectFullCollectionScan; |
| 174 | + |
| 175 | + public static SQLitePersistence createSQLitePersistence() { |
| 176 | + DatabaseId databaseId = DatabaseId.forProject("projectId"); |
| 177 | + LocalSerializer serializer = new LocalSerializer(new RemoteSerializer(databaseId)); |
| 178 | + Context context = ApplicationProvider.getApplicationContext(); |
| 179 | + SQLitePersistence persistence = |
| 180 | + new SQLitePersistence( |
| 181 | + context, "test", databaseId, serializer, LruGarbageCollector.Params.Default()); |
| 182 | + persistence.start(); |
| 183 | + return persistence; |
| 184 | + } |
| 185 | + |
| 186 | + @Before |
| 187 | + public void setUp() { |
| 188 | + expectFullCollectionScan = null; |
| 189 | + |
| 190 | + persistence = createSQLitePersistence(); |
| 191 | + |
| 192 | + indexManager = persistence.getIndexManager(User.UNAUTHENTICATED); |
| 193 | + mutationQueue = persistence.getMutationQueue(User.UNAUTHENTICATED, indexManager); |
| 194 | + documentOverlayCache = persistence.getDocumentOverlayCache(User.UNAUTHENTICATED); |
| 195 | + remoteDocumentCache = persistence.getRemoteDocumentCache(); |
| 196 | + queryEngine = new QueryEngine(); |
| 197 | + |
| 198 | + indexManager.start(); |
| 199 | + mutationQueue.start(); |
| 200 | + |
| 201 | + remoteDocumentCache.setIndexManager(indexManager); |
| 202 | + |
| 203 | + LocalDocumentsView localDocuments = |
| 204 | + new LocalDocumentsView( |
| 205 | + remoteDocumentCache, mutationQueue, documentOverlayCache, indexManager) { |
| 206 | + @Override |
| 207 | + public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery( |
| 208 | + com.google.firebase.firestore.core.Query query, FieldIndex.IndexOffset offset) { |
| 209 | + assertEquals( |
| 210 | + "Observed query execution mode did not match expectation", |
| 211 | + expectFullCollectionScan, |
| 212 | + FieldIndex.IndexOffset.NONE.equals(offset)); |
| 213 | + return super.getDocumentsMatchingQuery(query, offset); |
| 214 | + } |
| 215 | + }; |
| 216 | + queryEngine.initialize(localDocuments, indexManager, false); |
| 217 | + } |
| 218 | + |
| 219 | + /** Adds the provided documents to the remote document cache. */ |
| 220 | + protected void addDocument(MutableDocument... docs) { |
| 221 | + persistence.runTransaction( |
| 222 | + "addDocument", |
| 223 | + () -> { |
| 224 | + for (MutableDocument doc : docs) { |
| 225 | + remoteDocumentCache.add(doc, doc.getVersion()); |
| 226 | + } |
| 227 | + }); |
| 228 | + } |
| 229 | + |
| 230 | + protected <T> T expectOptimizedCollectionScan(Callable<T> c) throws Exception { |
| 231 | + try { |
| 232 | + expectFullCollectionScan = false; |
| 233 | + return c.call(); |
| 234 | + } finally { |
| 235 | + expectFullCollectionScan = null; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private <T> T expectFullCollectionScan(Callable<T> c) throws Exception { |
| 240 | + try { |
| 241 | + expectFullCollectionScan = true; |
| 242 | + return c.call(); |
| 243 | + } finally { |
| 244 | + expectFullCollectionScan = null; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + protected DocumentSet runQuery( |
| 249 | + com.google.firebase.firestore.core.Query query, boolean autoIndexing, QueryContext counter) { |
| 250 | + Preconditions.checkNotNull( |
| 251 | + expectFullCollectionScan, |
| 252 | + "Encountered runQuery() call not wrapped in expectOptimizedCollectionQuery()/expectFullCollectionQuery()"); |
| 253 | + ImmutableSortedMap<DocumentKey, Document> docs = |
| 254 | + queryEngine.getDocumentsMatchingQueryTest(query, autoIndexing, counter); |
| 255 | + View view = |
| 256 | + new View(query, new ImmutableSortedSet<>(Collections.emptyList(), DocumentKey::compareTo)); |
| 257 | + View.DocumentChanges viewDocChanges = view.computeDocChanges(docs); |
| 258 | + return view.applyChanges(viewDocChanges).getSnapshot().getDocuments(); |
| 259 | + } |
| 260 | + |
| 261 | + private void createTestingDocument( |
| 262 | + String basePath, int documentID, boolean isMatched, int numOfFields) { |
| 263 | + Map<String, Object> fields = map("match", isMatched); |
| 264 | + |
| 265 | + // Randomly generate the rest of fields |
| 266 | + for (int i = 2; i <= numOfFields; i++) { |
| 267 | + int valueIndex = (int) (Math.random() * values.size()) % values.size(); |
| 268 | + fields.put("field" + i, values.get(valueIndex)); |
| 269 | + } |
| 270 | + |
| 271 | + MutableDocument doc = doc(basePath + "/" + documentID, 1, fields); |
| 272 | + addDocument(doc); |
| 273 | + |
| 274 | + indexManager.updateIndexEntries(docMap(doc)); |
| 275 | + indexManager.updateCollectionGroup(basePath, FieldIndex.IndexOffset.fromDocument(doc)); |
| 276 | + } |
| 277 | + |
| 278 | + private void createTestingCollection( |
| 279 | + String basePath, int totalSetCount, int portion /*0 - 10*/, int numOfFields /* 1 - 30*/) { |
| 280 | + int documentCounter = 0; |
| 281 | + for (int i = 1; i <= totalSetCount; i++) { |
| 282 | + // Generate a random order list of 0 ... 9 |
| 283 | + ArrayList<Integer> indexes = new ArrayList<>(); |
| 284 | + for (int index = 0; index < 10; index++) { |
| 285 | + indexes.add(index); |
| 286 | + } |
| 287 | + Collections.shuffle(indexes); |
| 288 | + |
| 289 | + for (int match = 0; match < portion; match++) { |
| 290 | + int currentID = documentCounter + indexes.get(match); |
| 291 | + createTestingDocument(basePath, currentID, true, numOfFields); |
| 292 | + } |
| 293 | + for (int unmatch = portion; unmatch < 10; unmatch++) { |
| 294 | + int currentID = documentCounter + indexes.get(unmatch); |
| 295 | + createTestingDocument(basePath, currentID, false, numOfFields); |
| 296 | + } |
| 297 | + documentCounter += 10; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void testCombinesIndexedWithNonIndexedResults() throws Exception { |
| 303 | + // Every set contains 10 documents |
| 304 | + final int numOfSet = 100; |
| 305 | + // could overflow. Currently it is safe when numOfSet set to 1000 and running on macbook M1 |
| 306 | + long totalBeforeIndex = 0; |
| 307 | + long totalAfterIndex = 0; |
| 308 | + long totalDocumentCount = 0; |
| 309 | + long totalResultCount = 0; |
| 310 | + |
| 311 | + // Temperate heuristic |
| 312 | + double without = 3.7; |
| 313 | + double with = 4.9; |
| 314 | + |
| 315 | + for (int totalSetCount = 1; totalSetCount <= numOfSet; totalSetCount *= 10) { |
| 316 | + // portion stands for the percentage of documents matching query |
| 317 | + for (int portion = 0; portion <= 10; portion++) { |
| 318 | + for (int numOfFields = 1; numOfFields <= 31; numOfFields += 10) { |
| 319 | + String basePath = "documentCount" + totalSetCount; |
| 320 | + // Auto indexing |
| 321 | + Query query = query(basePath).filter(filter("match", "==", true)); |
| 322 | + indexManager.createTargetIndices(query.toTarget()); |
| 323 | + createTestingCollection(basePath, totalSetCount, portion, numOfFields); |
| 324 | + |
| 325 | + QueryContext counterWithoutIndex = new QueryContext(); |
| 326 | + long beforeAutoStart = System.nanoTime(); |
| 327 | + DocumentSet results = |
| 328 | + expectFullCollectionScan(() -> runQuery(query, false, counterWithoutIndex)); |
| 329 | + long beforeAutoEnd = System.nanoTime(); |
| 330 | + long millisecondsBeforeAuto = |
| 331 | + TimeUnit.MILLISECONDS.convert( |
| 332 | + (beforeAutoEnd - beforeAutoStart), TimeUnit.NANOSECONDS); |
| 333 | + totalBeforeIndex += (beforeAutoEnd - beforeAutoStart); |
| 334 | + totalDocumentCount += counterWithoutIndex.fullScanCount; |
| 335 | + assertEquals(portion * totalSetCount, results.size()); |
| 336 | + |
| 337 | + QueryContext counterWithIndex = new QueryContext(); |
| 338 | + long autoStart = System.nanoTime(); |
| 339 | + results = expectOptimizedCollectionScan(() -> runQuery(query, true, counterWithIndex)); |
| 340 | + long autoEnd = System.nanoTime(); |
| 341 | + long millisecondsAfterAuto = |
| 342 | + TimeUnit.MILLISECONDS.convert((autoEnd - autoStart), TimeUnit.NANOSECONDS); |
| 343 | + totalAfterIndex += (autoEnd - autoStart); |
| 344 | + assertEquals(portion * totalSetCount, results.size()); |
| 345 | + totalResultCount += results.size(); |
| 346 | + if (millisecondsBeforeAuto > millisecondsAfterAuto) { |
| 347 | + System.out.println( |
| 348 | + "Auto Indexing saves time when total of documents inside collection is " |
| 349 | + + totalSetCount * 10 |
| 350 | + + ". The matching percentage is " |
| 351 | + + portion |
| 352 | + + "0%. And each document contains " |
| 353 | + + numOfFields |
| 354 | + + " fields.\n" |
| 355 | + + "Weight result for without auto indexing is " |
| 356 | + + without * counterWithoutIndex.fullScanCount |
| 357 | + + ". And weight result for auto indexing is " |
| 358 | + + with * results.size()); |
| 359 | + } |
| 360 | + } |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + System.out.println( |
| 365 | + "The time heuristic is " |
| 366 | + + (totalBeforeIndex / totalDocumentCount) |
| 367 | + + " before auto indexing"); |
| 368 | + System.out.println( |
| 369 | + "The time heuristic is " + (totalAfterIndex / totalResultCount) + " after auto indexing"); |
| 370 | + } |
130 | 371 | }
|
0 commit comments