Skip to content

Commit d739c66

Browse files
committed
move auto index flag to runtime
1 parent a89c0aa commit d739c66

File tree

10 files changed

+98
-47
lines changed

10 files changed

+98
-47
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package com.google.firebase.firestore;
1616

1717
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertTrue;
1820

1921
import androidx.test.ext.junit.runners.AndroidJUnit4;
2022
import com.google.android.gms.tasks.Task;
@@ -101,4 +103,21 @@ public void testBadIndexDoesNotCrashClient() {
101103
+ " \"fieldOverrides\": []\n"
102104
+ "}"));
103105
}
106+
107+
@Test
108+
public void testAutomaticIndexingSetSuccessfully()
109+
throws ExecutionException, InterruptedException {
110+
// Use persistent disk cache (default)
111+
FirebaseFirestore db = testFirestore();
112+
FirebaseFirestoreSettings settings =
113+
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
114+
// Use persistent disk cache (default)
115+
.setLocalCacheSettings(PersistentCacheSettings.newBuilder().build())
116+
.build();
117+
db.setFirestoreSettings(settings);
118+
Tasks.await(db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(true));
119+
assertTrue(db.getClient().getLocalStore().getQueryEngine().getAutomaticIndexingEnabled());
120+
Tasks.await(db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(false));
121+
assertFalse(db.getClient().getLocalStore().getQueryEngine().getAutomaticIndexingEnabled());
122+
}
104123
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import androidx.annotation.Keep;
2424
import androidx.annotation.NonNull;
2525
import androidx.annotation.Nullable;
26+
import androidx.annotation.RestrictTo;
2627
import androidx.annotation.VisibleForTesting;
2728
import com.google.android.gms.tasks.Task;
2829
import com.google.android.gms.tasks.TaskCompletionSource;
@@ -104,6 +105,8 @@ public interface InstanceRegistry {
104105
private volatile FirestoreClient client;
105106
private final GrpcMetadataProvider metadataProvider;
106107

108+
@Nullable private PersistentCacheManager persistentCacheManager;
109+
107110
@NonNull
108111
public static FirebaseFirestore getInstance() {
109112
FirebaseApp app = FirebaseApp.getInstance();
@@ -355,6 +358,19 @@ public Task<Void> setIndexConfiguration(@NonNull String json) {
355358
return client.configureFieldIndexes(parsedIndexes);
356359
}
357360

361+
@RestrictTo(RestrictTo.Scope.LIBRARY)
362+
@Nullable
363+
public PersistentCacheManager getPersistentCacheIndexManager() {
364+
ensureClientConfigured();
365+
if (persistentCacheManager != null) {
366+
return persistentCacheManager;
367+
}
368+
if (settings.getCacheSettings() instanceof PersistentCacheSettings) {
369+
persistentCacheManager = new PersistentCacheManager(client);
370+
}
371+
return persistentCacheManager;
372+
}
373+
358374
/**
359375
* Gets a {@code CollectionReference} instance that refers to the collection at the specified path
360376
* within the database.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.google.firebase.firestore;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.RestrictTo;
5+
import com.google.android.gms.tasks.Task;
6+
import com.google.firebase.firestore.core.FirestoreClient;
7+
8+
// TODO(csi): Remove the `hide` and scope annotations.
9+
/** @hide */
10+
@RestrictTo(RestrictTo.Scope.LIBRARY)
11+
public class PersistentCacheManager {
12+
@NonNull private FirestoreClient client;
13+
14+
@RestrictTo(RestrictTo.Scope.LIBRARY)
15+
public PersistentCacheManager(FirestoreClient client) {
16+
this.client = client;
17+
}
18+
19+
public Task<Void> setAutomaticIndexingEnabled(boolean isEnabled) {
20+
return client.setAutomaticIndexingEnabled(isEnabled);
21+
}
22+
}

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package com.google.firebase.firestore;
1515

1616
import androidx.annotation.NonNull;
17-
import androidx.annotation.RestrictTo;
1817

1918
/**
2019
* Configures the SDK to use a persistent cache. Firestore documents and mutations are persisted
@@ -38,11 +37,8 @@ public static PersistentCacheSettings.Builder newBuilder() {
3837

3938
private final long sizeBytes;
4039

41-
private final boolean autoClientIndexingEnabled;
42-
43-
private PersistentCacheSettings(long sizeBytes, boolean autoClientIndexingEnabled) {
40+
private PersistentCacheSettings(long sizeBytes) {
4441
this.sizeBytes = sizeBytes;
45-
this.autoClientIndexingEnabled = autoClientIndexingEnabled;
4642
}
4743

4844
@Override
@@ -78,18 +74,11 @@ public long getSizeBytes() {
7874
return sizeBytes;
7975
}
8076

81-
@RestrictTo(RestrictTo.Scope.LIBRARY)
82-
public boolean autoClientIndexingEnabled() {
83-
return autoClientIndexingEnabled;
84-
}
85-
8677
/** A Builder for creating {@code PersistentCacheSettings} instance. */
8778
public static class Builder {
8879

8980
private long sizeBytes = FirebaseFirestoreSettings.DEFAULT_CACHE_SIZE_BYTES;
9081

91-
private boolean autoClientIndexingEnabled = false;
92-
9382
private Builder() {}
9483

9584
/**
@@ -109,16 +98,10 @@ public Builder setSizeBytes(long sizeBytes) {
10998
return this;
11099
}
111100

112-
@NonNull
113-
public Builder enableAutoClientIndexing(boolean value) {
114-
this.autoClientIndexingEnabled = value;
115-
return this;
116-
}
117-
118101
/** Creates a {@code PersistentCacheSettings} instance from this builder instance. */
119102
@NonNull
120103
public PersistentCacheSettings build() {
121-
return new PersistentCacheSettings(sizeBytes, autoClientIndexingEnabled);
104+
return new PersistentCacheSettings(sizeBytes);
122105
}
123106
}
124107
}

firebase-firestore/src/main/java/com/google/firebase/firestore/core/FirestoreClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.annotation.SuppressLint;
2020
import android.content.Context;
2121
import androidx.annotation.Nullable;
22+
import androidx.annotation.RestrictTo;
2223
import com.google.android.gms.tasks.Task;
2324
import com.google.android.gms.tasks.TaskCompletionSource;
2425
import com.google.android.gms.tasks.Tasks;
@@ -139,6 +140,11 @@ public FirestoreClient(
139140
});
140141
}
141142

143+
@RestrictTo(RestrictTo.Scope.LIBRARY)
144+
public LocalStore getLocalStore() {
145+
return localStore;
146+
}
147+
142148
public Task<Void> disableNetwork() {
143149
this.verifyNotTerminated();
144150
return asyncQueue.enqueue(() -> remoteStore.disableNetwork());
@@ -353,6 +359,11 @@ public Task<Void> configureFieldIndexes(List<FieldIndex> fieldIndices) {
353359
return asyncQueue.enqueue(() -> localStore.configureFieldIndexes(fieldIndices));
354360
}
355361

362+
public Task<Void> setAutomaticIndexingEnabled(boolean isEnabled) {
363+
verifyNotTerminated();
364+
return asyncQueue.enqueue(() -> localStore.setAutomaticIndexingEnabled(isEnabled));
365+
}
366+
356367
public void removeSnapshotsInSyncListener(EventListener<Void> listener) {
357368
// Checks for shutdown but does not raise error, allowing remove after shutdown to be a no-op.
358369
if (isTerminated()) {

firebase-firestore/src/main/java/com/google/firebase/firestore/core/MemoryComponentProvider.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.firebase.firestore.FirebaseFirestoreSettings;
2020
import com.google.firebase.firestore.MemoryCacheSettings;
2121
import com.google.firebase.firestore.MemoryLruGcSettings;
22-
import com.google.firebase.firestore.PersistentCacheSettings;
2322
import com.google.firebase.firestore.local.IndexBackfiller;
2423
import com.google.firebase.firestore.local.LocalSerializer;
2524
import com.google.firebase.firestore.local.LocalStore;
@@ -61,12 +60,7 @@ protected EventManager createEventManager(Configuration configuration) {
6160

6261
@Override
6362
protected LocalStore createLocalStore(Configuration configuration) {
64-
boolean autoIndexEnabled =
65-
configuration.getSettings().getCacheSettings() != null
66-
&& ((PersistentCacheSettings) configuration.getSettings().getCacheSettings())
67-
.autoClientIndexingEnabled();
68-
return new LocalStore(
69-
getPersistence(), new QueryEngine(), configuration.getInitialUser(), autoIndexEnabled);
63+
return new LocalStore(getPersistence(), new QueryEngine(), configuration.getInitialUser());
7064
}
7165

7266
@Override

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.util.SparseArray;
2222
import androidx.annotation.NonNull;
2323
import androidx.annotation.Nullable;
24+
import androidx.annotation.RestrictTo;
2425
import androidx.annotation.VisibleForTesting;
2526
import com.google.firebase.Timestamp;
2627
import com.google.firebase.database.collection.ImmutableSortedMap;
@@ -147,22 +148,11 @@ public final class LocalStore implements BundleCallback {
147148
/** Used to generate targetIds for queries tracked locally. */
148149
private final TargetIdGenerator targetIdGenerator;
149150

150-
private boolean autoIndexEnabled;
151-
152151
public LocalStore(Persistence persistence, QueryEngine queryEngine, User initialUser) {
153-
this(persistence, queryEngine, initialUser, false);
154-
}
155-
156-
public LocalStore(
157-
Persistence persistence,
158-
QueryEngine queryEngine,
159-
User initialUser,
160-
boolean autoIndexEnabled) {
161152
hardAssert(
162153
persistence.isStarted(), "LocalStore was passed an unstarted persistence implementation");
163154
this.persistence = persistence;
164155
this.queryEngine = queryEngine;
165-
this.autoIndexEnabled = autoIndexEnabled;
166156

167157
targetCache = persistence.getTargetCache();
168158
bundleCache = persistence.getBundleCache();
@@ -186,7 +176,7 @@ private void initializeUserComponents(User user) {
186176
new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
187177

188178
remoteDocuments.setIndexManager(indexManager);
189-
queryEngine.initialize(localDocuments, indexManager, autoIndexEnabled);
179+
queryEngine.initialize(localDocuments, indexManager);
190180
}
191181

192182
public void start() {
@@ -211,6 +201,11 @@ public LocalDocumentsView getLocalDocumentsForCurrentUser() {
211201
return localDocuments;
212202
}
213203

204+
@RestrictTo(RestrictTo.Scope.LIBRARY)
205+
public QueryEngine getQueryEngine() {
206+
return queryEngine;
207+
}
208+
214209
// PORTING NOTE: no shutdown for LocalStore or persistence components on Android.
215210

216211
public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {
@@ -813,6 +808,10 @@ public void configureFieldIndexes(List<FieldIndex> newFieldIndexes) {
813808
});
814809
}
815810

811+
public void setAutomaticIndexingEnabled(boolean isEnabled) {
812+
queryEngine.setAutomaticIndexingEnabled(isEnabled);
813+
}
814+
816815
/** Mutable state for the transaction in allocateQuery. */
817816
private static class AllocateQueryHolder {
818817
TargetData cached;

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static com.google.firebase.firestore.util.Assert.hardAssert;
1818

19+
import androidx.annotation.RestrictTo;
1920
import com.google.firebase.database.collection.ImmutableSortedMap;
2021
import com.google.firebase.database.collection.ImmutableSortedSet;
2122
import com.google.firebase.firestore.core.Query;
@@ -65,16 +66,23 @@ public class QueryEngine {
6566
private IndexManager indexManager;
6667
private boolean initialized;
6768

68-
private boolean autoIndexEnabled;
69+
private boolean automaticIndexingEnabled = false;
6970

70-
public void initialize(
71-
LocalDocumentsView localDocumentsView, IndexManager indexManager, boolean autoIndexEnabled) {
71+
public void initialize(LocalDocumentsView localDocumentsView, IndexManager indexManager) {
7272
this.localDocumentsView = localDocumentsView;
7373
this.indexManager = indexManager;
74-
this.autoIndexEnabled = autoIndexEnabled;
7574
this.initialized = true;
7675
}
7776

77+
@RestrictTo(RestrictTo.Scope.LIBRARY)
78+
public boolean getAutomaticIndexingEnabled() {
79+
return this.automaticIndexingEnabled;
80+
}
81+
82+
public void setAutomaticIndexingEnabled(boolean isEnabled) {
83+
this.automaticIndexingEnabled = isEnabled;
84+
}
85+
7886
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
7987
Query query,
8088
SnapshotVersion lastLimboFreeSnapshotVersion,
@@ -93,7 +101,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
93101

94102
QueryContext counter = new QueryContext();
95103
result = executeFullCollectionScan(query, counter);
96-
if (result != null && autoIndexEnabled) {
104+
if (result != null && automaticIndexingEnabled) {
97105
CreateCacheIndices(query, counter, result.size());
98106
}
99107
return result;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@ void resetCounts() {
6969
}
7070

7171
@Override
72-
public void initialize(
73-
LocalDocumentsView localDocuments, IndexManager indexManager, boolean autoIndexEnabled) {
72+
public void initialize(LocalDocumentsView localDocuments, IndexManager indexManager) {
7473
LocalDocumentsView wrappedView =
7574
new LocalDocumentsView(
7675
wrapRemoteDocumentCache(localDocuments.getRemoteDocumentCache()),
7776
localDocuments.getMutationQueue(),
7877
wrapOverlayCache(localDocuments.getDocumentOverlayCache()),
7978
indexManager);
80-
queryEngine.initialize(wrappedView, indexManager, autoIndexEnabled);
79+
queryEngine.initialize(wrappedView, indexManager);
8180
}
8281

8382
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
125125
return super.getDocumentsMatchingQuery(query, offset);
126126
}
127127
};
128-
queryEngine.initialize(localDocuments, indexManager, false);
128+
queryEngine.initialize(localDocuments, indexManager);
129129
}
130130

131131
abstract Persistence getPersistence();

0 commit comments

Comments
 (0)