Skip to content

Commit a89c0aa

Browse files
committed
Address feedback
1 parent c9990de commit a89c0aa

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
284284
/** Performs a simple document lookup for the given path. */
285285
private ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingDocumentQuery(
286286
ResourcePath path, QueryContext counter) {
287-
counter.fullScanCount++;
287+
counter.increaseDocumentCount();
288288
ImmutableSortedMap<DocumentKey, Document> result = emptyDocumentMap();
289289
// Just do a simple document lookup.
290290
Document doc = getDocument(DocumentKey.fromPath(path));

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
package com.google.firebase.firestore.local;
1616

1717
public class QueryContext {
18-
QueryContext() {}
18+
public QueryContext() {}
1919

20-
int fullScanCount = 0;
20+
private int documentCount = 0;
21+
22+
public int getDocumentCount() {
23+
return documentCount;
24+
}
25+
26+
public void increaseDocumentCount() {
27+
documentCount++;
28+
}
2129
}

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,12 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
8686
return result;
8787
}
8888

89-
QueryContext counter = new QueryContext();
90-
result = performQueryUsingRemoteKeys(query, remoteKeys, lastLimboFreeSnapshotVersion, counter);
89+
result = performQueryUsingRemoteKeys(query, remoteKeys, lastLimboFreeSnapshotVersion);
9190
if (result != null) {
92-
if (autoIndexEnabled) {
93-
CreateCacheIndices(query, counter, result.size());
94-
}
9591
return result;
9692
}
9793

98-
counter = new QueryContext();
94+
QueryContext counter = new QueryContext();
9995
result = executeFullCollectionScan(query, counter);
10096
if (result != null && autoIndexEnabled) {
10197
CreateCacheIndices(query, counter, result.size());
@@ -104,7 +100,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
104100
}
105101

106102
private void CreateCacheIndices(Query query, QueryContext counter, int resultSize) {
107-
if (counter.fullScanCount > 2 * resultSize) {
103+
if (counter.getDocumentCount() > 2 * resultSize) {
108104
indexManager.createTargetIndices(query.toTarget());
109105
}
110106
}
@@ -163,8 +159,7 @@ private void CreateCacheIndices(Query query, QueryContext counter, int resultSiz
163159
private @Nullable ImmutableSortedMap<DocumentKey, Document> performQueryUsingRemoteKeys(
164160
Query query,
165161
ImmutableSortedSet<DocumentKey> remoteKeys,
166-
SnapshotVersion lastLimboFreeSnapshotVersion,
167-
QueryContext counter) {
162+
SnapshotVersion lastLimboFreeSnapshotVersion) {
168163
if (query.matchesAllDocuments()) {
169164
// Don't use indexes for queries that can be executed by scanning the collection.
170165
return null;
@@ -177,7 +172,7 @@ private void CreateCacheIndices(Query query, QueryContext counter, int resultSiz
177172
}
178173

179174
ImmutableSortedMap<DocumentKey, Document> documents =
180-
localDocumentsView.getDocuments(remoteKeys, counter);
175+
localDocumentsView.getDocuments(remoteKeys);
181176
ImmutableSortedSet<Document> previousResults = applyQuery(query, documents);
182177

183178
if (needsRefill(query, remoteKeys.size(), previousResults, lastLimboFreeSnapshotVersion)) {
@@ -196,8 +191,7 @@ private void CreateCacheIndices(Query query, QueryContext counter, int resultSiz
196191
previousResults,
197192
query,
198193
IndexOffset.createSuccessor(
199-
lastLimboFreeSnapshotVersion, FieldIndex.INITIAL_LARGEST_BATCH_ID),
200-
counter);
194+
lastLimboFreeSnapshotVersion, FieldIndex.INITIAL_LARGEST_BATCH_ID));
201195
}
202196

203197
/** Applies the query filter and sorting to the provided documents. */
@@ -278,7 +272,7 @@ private ImmutableSortedMap<DocumentKey, Document> appendRemainingResults(
278272
Iterable<Document> indexedResults, Query query, IndexOffset offset, QueryContext counter) {
279273
// Retrieve all results for documents that were updated since the offset.
280274
ImmutableSortedMap<DocumentKey, Document> remainingResults =
281-
localDocumentsView.getDocumentsMatchingQuery(query, offset, counter);
275+
localDocumentsView.getDocumentsMatchingQuery(query, offset);
282276
for (Document entry : indexedResults) {
283277
remainingResults = remainingResults.insert(entry.getKey(), entry);
284278
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void createTargetIndices(Target target) {
239239

240240
for (Target subTarget : getSubTargets(target)) {
241241
IndexType type = getIndexType(subTarget);
242-
if (type == IndexType.NONE) {
242+
if (type == IndexType.NONE || type == IndexType.PARTIAL) {
243243
TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(subTarget);
244244
addFieldIndex(targetIndexMatcher.BuildTargetIndex());
245245
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Map<DocumentKey, MutableDocument> getAll(
144144
.forEach(
145145
row -> {
146146
processRowInBackground(backgroundQueue, results, row, /*filter*/ null);
147-
counter.fullScanCount++;
147+
counter.increaseDocumentCount();
148148
});
149149
}
150150
backgroundQueue.drain();
@@ -233,7 +233,7 @@ private Map<DocumentKey, MutableDocument> getAll(
233233
row -> {
234234
QueryContext singleCounter = new QueryContext();
235235
processRowInBackground(backgroundQueue, results, row, filter);
236-
counter.fullScanCount++;
236+
counter.increaseDocumentCount();
237237
});
238238
backgroundQueue.drain();
239239
return results;

firebase-firestore/src/main/java/com/google/firebase/firestore/model/TargetIndexMatcher.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ public FieldIndex BuildTargetIndex() {
213213
}
214214

215215
for (OrderBy orderBy : orderBys) {
216+
// Stop adding more segments if we see a order-by on key. Typically this is the default
217+
// implicit order-by which is covered in the index_entry table as a separate column.
218+
// If it is not the default order-by, the generated index will be missing some segments
219+
// optimized for order-bys, which is probably fine.
220+
if (orderBy.getField().isKeyField()) {
221+
continue;
222+
}
223+
216224
if (uniqueFields.contains(orderBy.getField())) {
217225
continue;
218226
}

0 commit comments

Comments
 (0)