Skip to content

Commit 3f7a970

Browse files
committed
Address Denver's feedback
1 parent 39756f4 commit 3f7a970

File tree

8 files changed

+19
-16
lines changed

8 files changed

+19
-16
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestore.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.google.firebase.firestore.model.ResourcePath;
5050
import com.google.firebase.firestore.remote.FirestoreChannel;
5151
import com.google.firebase.firestore.remote.GrpcMetadataProvider;
52+
import com.google.firebase.firestore.util.Assert;
5253
import com.google.firebase.firestore.util.AsyncQueue;
5354
import com.google.firebase.firestore.util.ByteBufferInputStream;
5455
import com.google.firebase.firestore.util.Executors;
@@ -63,6 +64,7 @@
6364
import java.util.ArrayList;
6465
import java.util.List;
6566
import java.util.concurrent.Executor;
67+
import java.util.concurrent.atomic.AtomicReference;
6668
import org.json.JSONArray;
6769
import org.json.JSONException;
6870
import org.json.JSONObject;
@@ -105,7 +107,9 @@ public interface InstanceRegistry {
105107
private volatile FirestoreClient client;
106108
private final GrpcMetadataProvider metadataProvider;
107109

108-
@Nullable private PersistentCacheIndexManager persistentCacheIndexManager;
110+
@Nullable
111+
private AtomicReference<PersistentCacheIndexManager> persistentCacheIndexManager =
112+
new AtomicReference<>();
109113

110114
@NonNull
111115
private static FirebaseApp getDefaultFirebaseApp() {
@@ -418,14 +422,15 @@ public Task<Void> setIndexConfiguration(@NonNull String json) {
418422
@Nullable
419423
public PersistentCacheIndexManager getPersistentCacheIndexManager() {
420424
ensureClientConfigured();
421-
if (persistentCacheIndexManager != null) {
422-
return persistentCacheIndexManager;
423-
}
425+
Assert.hardAssertNonNull(
426+
persistentCacheIndexManager, "persistentCacheIndexManager has not been initialized.");
427+
424428
if (settings.isPersistenceEnabled()
425429
|| settings.getCacheSettings() instanceof PersistentCacheSettings) {
426-
persistentCacheIndexManager = new PersistentCacheIndexManager(client);
430+
persistentCacheIndexManager.compareAndSet(null, new PersistentCacheIndexManager(client));
427431
}
428-
return persistentCacheIndexManager;
432+
433+
return persistentCacheIndexManager.get();
429434
}
430435

431436
/**

firebase-firestore/src/main/java/com/google/firebase/firestore/PersistentCacheIndexManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// TODO(csi): Remove the `hide` and scope annotations.
2828
/** @hide */
2929
@RestrictTo(RestrictTo.Scope.LIBRARY)
30-
public class PersistentCacheIndexManager {
30+
public final class PersistentCacheIndexManager {
3131
@NonNull private FirestoreClient client;
3232

3333
@RestrictTo(RestrictTo.Scope.LIBRARY)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616

1717
/** A tracker to keep a record of important details during database local query execution. */
1818
public class QueryContext {
19-
public QueryContext() {}
20-
2119
/** Counts the number of documents passed through during local query execution. */
2220
private int documentReadCount = 0;
2321

2422
public int getDocumentReadCount() {
2523
return documentReadCount;
2624
}
2725

28-
public void increaseDocumentCount() {
26+
public void incrementDocumentReadCount() {
2927
documentReadCount++;
3028
}
3129
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
105105
QueryContext context = new QueryContext();
106106
result = executeFullCollectionScan(query, context);
107107
if (result != null && automaticIndexingEnabled) {
108-
CreateCacheIndexes(query, context, result.size());
108+
createCacheIndexes(query, context, result.size());
109109
}
110110
return result;
111111
}
@@ -115,7 +115,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
115115
* context and query result size.
116116
*/
117117
// TODO(csi): Auto experiment data.
118-
private void CreateCacheIndexes(Query query, QueryContext context, int resultSize) {
118+
private void createCacheIndexes(Query query, QueryContext context, int resultSize) {
119119
if (context.getDocumentReadCount() < minCollectionSizeToAutoCreateIndex) {
120120
Logger.debug(
121121
LOG_TAG,

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
@@ -241,7 +241,7 @@ public void createTargetIndices(Target target) {
241241
IndexType type = getIndexType(subTarget);
242242
if (type == IndexType.NONE || type == IndexType.PARTIAL) {
243243
TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(subTarget);
244-
addFieldIndex(targetIndexMatcher.BuildTargetIndex());
244+
addFieldIndex(targetIndexMatcher.buildTargetIndex());
245245
}
246246
}
247247
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private Map<DocumentKey, MutableDocument> getAll(
224224
processRowInBackground(backgroundQueue, results, row, filter);
225225
if (context != null) {
226226
// Increases the counter by 1 for every document processed.
227-
context.increaseDocumentCount();
227+
context.incrementDocumentReadCount();
228228
}
229229
});
230230
backgroundQueue.drain();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public boolean servedByIndex(FieldIndex index) {
193193
}
194194

195195
/** Returns a full matched field index for this target. */
196-
public FieldIndex BuildTargetIndex() {
196+
public FieldIndex buildTargetIndex() {
197197
// We want to make sure only one segment created for one field. For example, in case like
198198
// a == 3 and a > 2, index, a ASCENDING, will only be created once.
199199
Set<FieldPath> uniqueFields = new HashSet<>();

firebase-firestore/src/test/java/com/google/firebase/firestore/model/TargetIndexMatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ public void testBuildTargetIndexWithInAndOrderBySameField() {
815815
private void validateBuildTargetIndexCreateFullMatchIndex(Query query) {
816816
Target target = query.toTarget();
817817
TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(target);
818-
FieldIndex expectedIndex = targetIndexMatcher.BuildTargetIndex();
818+
FieldIndex expectedIndex = targetIndexMatcher.buildTargetIndex();
819819
assertTrue(targetIndexMatcher.servedByIndex(expectedIndex));
820820
// Check the index created is a FULL MATCH index
821821
assertTrue(expectedIndex.getSegments().size() >= target.getSegmentCount());

0 commit comments

Comments
 (0)