Skip to content

Commit 87e3ac1

Browse files
committed
Address feedbacks other than adding tests and comments
1 parent 5b56b0b commit 87e3ac1

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/IndexingTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.firebase.firestore;
1616

1717
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
18-
import static org.junit.Assert.fail;
18+
import static com.google.firebase.firestore.testutil.TestUtil.assertDoesNotThrow;
1919

2020
import androidx.test.ext.junit.runners.AndroidJUnit4;
2121
import com.google.android.gms.tasks.Task;
@@ -125,12 +125,4 @@ public void testAutomaticIndexingSetSuccessfullyUseDefault() {
125125
assertDoesNotThrow(
126126
() -> db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(false));
127127
}
128-
129-
public void assertDoesNotThrow(Runnable block) {
130-
try {
131-
block.run();
132-
} catch (Exception e) {
133-
fail("Should not have thrown " + e);
134-
}
135-
}
136128
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalDocumentsView.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ void recalculateAndSaveOverlays(Set<DocumentKey> documentKeys) {
260260
* @param offset Read time and key to start scanning by (exclusive).
261261
*/
262262
ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
263-
Query query, IndexOffset offset, @Nullable QueryContext counter) {
263+
Query query, IndexOffset offset, @Nullable QueryContext context) {
264264
ResourcePath path = query.getPath();
265265
if (query.isDocumentQuery()) {
266266
return getDocumentsMatchingDocumentQuery(path);
267267
} else if (query.isCollectionGroupQuery()) {
268-
return getDocumentsMatchingCollectionGroupQuery(query, offset, counter);
268+
return getDocumentsMatchingCollectionGroupQuery(query, offset, context);
269269
} else {
270-
return getDocumentsMatchingCollectionQuery(query, offset, counter);
270+
return getDocumentsMatchingCollectionQuery(query, offset, context);
271271
}
272272
}
273273

@@ -289,7 +289,7 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingDocumentQu
289289
}
290290

291291
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionGroupQuery(
292-
Query query, IndexOffset offset, @Nullable QueryContext counter) {
292+
Query query, IndexOffset offset, @Nullable QueryContext context) {
293293
hardAssert(
294294
query.getPath().isEmpty(),
295295
"Currently we only support collection group queries at the root.");
@@ -302,7 +302,7 @@ private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollection
302302
for (ResourcePath parent : parents) {
303303
Query collectionQuery = query.asCollectionQueryAtPath(parent.append(collectionId));
304304
ImmutableSortedMap<DocumentKey, Document> collectionResults =
305-
getDocumentsMatchingCollectionQuery(collectionQuery, offset, counter);
305+
getDocumentsMatchingCollectionQuery(collectionQuery, offset, context);
306306
for (Map.Entry<DocumentKey, Document> docEntry : collectionResults) {
307307
results = results.insert(docEntry.getKey(), docEntry.getValue());
308308
}
@@ -367,11 +367,11 @@ private void populateOverlays(Map<DocumentKey, Overlay> overlays, Set<DocumentKe
367367
}
368368

369369
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingCollectionQuery(
370-
Query query, IndexOffset offset, @Nullable QueryContext counter) {
370+
Query query, IndexOffset offset, @Nullable QueryContext context) {
371371
Map<DocumentKey, Overlay> overlays =
372372
documentOverlayCache.getOverlays(query.getPath(), offset.getLargestBatchId());
373373
Map<DocumentKey, MutableDocument> remoteDocuments =
374-
remoteDocumentCache.getDocumentsMatchingQuery(query, offset, overlays.keySet(), counter);
374+
remoteDocumentCache.getDocumentsMatchingQuery(query, offset, overlays.keySet(), context);
375375

376376
// As documents might match the query because of their overlay we need to include documents
377377
// for all overlays in the initial document set.

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryRemoteDocumentCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
101101
Query query,
102102
IndexOffset offset,
103103
@Nonnull Set<DocumentKey> mutatedKeys,
104-
@Nullable QueryContext counter) {
104+
@Nullable QueryContext context) {
105105
Map<DocumentKey, MutableDocument> result = new HashMap<>();
106106

107107
// Documents are ordered by key, so we can use a prefix scan to narrow down the documents

firebase-firestore/src/main/java/com/google/firebase/firestore/local/QueryContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
public class QueryContext {
1818
public QueryContext() {}
1919

20-
private int documentCount = 0;
20+
private int documentReadCount = 0;
2121

22-
public int getDocumentCount() {
23-
return documentCount;
22+
public int getDocumentReadCount() {
23+
return documentReadCount;
2424
}
2525

2626
public void increaseDocumentCount() {
27-
documentCount++;
27+
documentReadCount++;
2828
}
2929
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/QueryEngine.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,33 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
9393
return result;
9494
}
9595

96-
QueryContext counter = new QueryContext();
97-
result = executeFullCollectionScan(query, counter);
96+
QueryContext context = new QueryContext();
97+
result = executeFullCollectionScan(query, context);
9898
if (result != null && automaticIndexingEnabled) {
99-
CreateCacheIndices(query, counter, result.size());
99+
CreateCacheIndexes(query, context, result.size());
100100
}
101101
return result;
102102
}
103103

104104
// TODO(csi): Auto experiment data.
105-
private void CreateCacheIndices(Query query, QueryContext counter, int resultSize) {
106-
if (counter.getDocumentCount() > 2 * resultSize) {
105+
private void CreateCacheIndexes(Query query, QueryContext context, int resultSize) {
106+
String decisionStr = "";
107+
if (context.getDocumentReadCount() > 2 * resultSize) {
107108
indexManager.createTargetIndices(query.toTarget());
109+
} else {
110+
decisionStr = " not";
111+
}
112+
113+
if (Logger.isDebugEnabled()) {
114+
Logger.debug(
115+
LOG_TAG,
116+
"Query ran locally using a full collection scan, walking through %s documents in total "
117+
+ "and returning %s documents. The SDK has decided%s to create cache indexes "
118+
+ "for this query, as using cache indexes may%s help improve performance.",
119+
context.getDocumentReadCount(),
120+
resultSize,
121+
decisionStr,
122+
decisionStr);
108123
}
109124
}
110125

@@ -260,11 +275,11 @@ private boolean needsRefill(
260275
}
261276

262277
private ImmutableSortedMap<DocumentKey, Document> executeFullCollectionScan(
263-
Query query, QueryContext counter) {
278+
Query query, QueryContext context) {
264279
if (Logger.isDebugEnabled()) {
265280
Logger.debug(LOG_TAG, "Using full collection scan to execute query: %s", query.toString());
266281
}
267-
return localDocumentsView.getDocumentsMatchingQuery(query, IndexOffset.NONE, counter);
282+
return localDocumentsView.getDocumentsMatchingQuery(query, IndexOffset.NONE, context);
268283
}
269284

270285
/**

firebase-firestore/src/main/java/com/google/firebase/firestore/local/RemoteDocumentCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
9595
Query query,
9696
IndexOffset offset,
9797
@Nonnull Set<DocumentKey> mutatedKeys,
98-
@Nullable QueryContext counter);
98+
@Nullable QueryContext context);
9999
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private Map<DocumentKey, MutableDocument> getAll(
186186
IndexOffset offset,
187187
int count,
188188
@Nullable Function<MutableDocument, Boolean> filter,
189-
@Nullable QueryContext counter) {
189+
@Nullable QueryContext context) {
190190
Timestamp readTime = offset.getReadTime().getTimestamp();
191191
DocumentKey documentKey = offset.getDocumentKey();
192192

@@ -225,8 +225,8 @@ private Map<DocumentKey, MutableDocument> getAll(
225225
.forEach(
226226
row -> {
227227
processRowInBackground(backgroundQueue, results, row, filter);
228-
if (counter != null) {
229-
counter.increaseDocumentCount();
228+
if (context != null) {
229+
context.increaseDocumentCount();
230230
}
231231
});
232232
backgroundQueue.drain();
@@ -276,13 +276,13 @@ public Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
276276
Query query,
277277
IndexOffset offset,
278278
@Nonnull Set<DocumentKey> mutatedKeys,
279-
@Nullable QueryContext counter) {
279+
@Nullable QueryContext context) {
280280
return getAll(
281281
Collections.singletonList(query.getPath()),
282282
offset,
283283
Integer.MAX_VALUE,
284284
(MutableDocument doc) -> query.matches(doc) || mutatedKeys.contains(doc.getKey()),
285-
counter);
285+
context);
286286
}
287287

288288
private MutableDocument decodeMaybeDocument(

firebase-firestore/src/test/java/com/google/firebase/firestore/local/CountingQueryEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
179179
Query query,
180180
IndexOffset offset,
181181
@NonNull Set<DocumentKey> mutatedKeys,
182-
@Nullable QueryContext counter) {
182+
@Nullable QueryContext context) {
183183
Map<DocumentKey, MutableDocument> result =
184184
subject.getDocumentsMatchingQuery(query, offset, mutatedKeys);
185185
documentsReadByCollection[0] += result.size();

0 commit comments

Comments
 (0)