Skip to content

Commit 165396e

Browse files
committed
Support old way to enable persistent for PersistentCacheManager
1 parent 14e1cce commit 165396e

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +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.assertFalse;
19-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.Assert.fail;
2019

2120
import androidx.test.ext.junit.runners.AndroidJUnit4;
2221
import com.google.android.gms.tasks.Task;
@@ -105,19 +104,33 @@ public void testBadIndexDoesNotCrashClient() {
105104
}
106105

107106
@Test
108-
public void testAutomaticIndexingSetSuccessfully()
109-
throws ExecutionException, InterruptedException {
107+
public void testAutomaticIndexingSetSuccessfully() {
110108
// Use persistent disk cache (default)
111109
FirebaseFirestore db = testFirestore();
112110
FirebaseFirestoreSettings settings =
113111
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
114-
// Use persistent disk cache (default)
115112
.setLocalCacheSettings(PersistentCacheSettings.newBuilder().build())
116113
.build();
117114
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());
115+
assertDoesNotThrow(() -> db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(true));
116+
assertDoesNotThrow(
117+
() -> db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(false));
118+
}
119+
120+
@Test
121+
public void testAutomaticIndexingSetSuccessfullyUseDefault() {
122+
// Use persistent disk cache (default)
123+
FirebaseFirestore db = testFirestore();
124+
assertDoesNotThrow(() -> db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(true));
125+
assertDoesNotThrow(
126+
() -> db.getPersistentCacheIndexManager().setAutomaticIndexingEnabled(false));
127+
}
128+
129+
public void assertDoesNotThrow(Runnable block) {
130+
try {
131+
block.run();
132+
} catch (Exception e) {
133+
fail("Should not have thrown " + e);
134+
}
122135
}
123136
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ public PersistentCacheManager getPersistentCacheIndexManager() {
410410
if (persistentCacheManager != null) {
411411
return persistentCacheManager;
412412
}
413-
if (settings.getCacheSettings() instanceof PersistentCacheSettings) {
413+
if (settings.isPersistenceEnabled()
414+
|| settings.getCacheSettings() instanceof PersistentCacheSettings) {
414415
persistentCacheManager = new PersistentCacheManager(client);
415416
}
416417
return persistentCacheManager;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import androidx.annotation.NonNull;
44
import androidx.annotation.RestrictTo;
5-
import com.google.android.gms.tasks.Task;
65
import com.google.firebase.firestore.core.FirestoreClient;
76

87
// TODO(csi): Remove the `hide` and scope annotations.
@@ -16,7 +15,7 @@ public PersistentCacheManager(FirestoreClient client) {
1615
this.client = client;
1716
}
1817

19-
public Task<Void> setAutomaticIndexingEnabled(boolean isEnabled) {
20-
return client.setAutomaticIndexingEnabled(isEnabled);
18+
public void setAutomaticIndexingEnabled(boolean isEnabled) {
19+
client.setAutomaticIndexingEnabled(isEnabled);
2120
}
2221
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.annotation.SuppressLint;
2020
import android.content.Context;
2121
import androidx.annotation.Nullable;
22-
import androidx.annotation.RestrictTo;
2322
import com.google.android.gms.tasks.Task;
2423
import com.google.android.gms.tasks.TaskCompletionSource;
2524
import com.google.android.gms.tasks.Tasks;
@@ -140,11 +139,6 @@ public FirestoreClient(
140139
});
141140
}
142141

143-
@RestrictTo(RestrictTo.Scope.LIBRARY)
144-
public LocalStore getLocalStore() {
145-
return localStore;
146-
}
147-
148142
public Task<Void> disableNetwork() {
149143
this.verifyNotTerminated();
150144
return asyncQueue.enqueue(() -> remoteStore.disableNetwork());
@@ -359,9 +353,9 @@ public Task<Void> configureFieldIndexes(List<FieldIndex> fieldIndices) {
359353
return asyncQueue.enqueue(() -> localStore.configureFieldIndexes(fieldIndices));
360354
}
361355

362-
public Task<Void> setAutomaticIndexingEnabled(boolean isEnabled) {
356+
public void setAutomaticIndexingEnabled(boolean isEnabled) {
363357
verifyNotTerminated();
364-
return asyncQueue.enqueue(() -> localStore.setAutomaticIndexingEnabled(isEnabled));
358+
asyncQueue.enqueue(() -> localStore.setAutomaticIndexingEnabled(isEnabled));
365359
}
366360

367361
public void removeSnapshotsInSyncListener(EventListener<Void> listener) {

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.util.SparseArray;
2222
import androidx.annotation.NonNull;
2323
import androidx.annotation.Nullable;
24-
import androidx.annotation.RestrictTo;
2524
import androidx.annotation.VisibleForTesting;
2625
import com.google.firebase.Timestamp;
2726
import com.google.firebase.database.collection.ImmutableSortedMap;
@@ -201,11 +200,6 @@ public LocalDocumentsView getLocalDocumentsForCurrentUser() {
201200
return localDocuments;
202201
}
203202

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

211205
public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {

0 commit comments

Comments
 (0)