Skip to content

Commit b77d78c

Browse files
authored
Merge 74ba500 into 2c8ab01
2 parents 2c8ab01 + 74ba500 commit b77d78c

File tree

9 files changed

+111
-0
lines changed

9 files changed

+111
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public void testAutoIndexCreationSetSuccessfully() {
136136

137137
results = waitFor(collection.whereEqualTo("match", true).get());
138138
assertEquals(1, results.size());
139+
140+
assertDoesNotThrow(() -> db.getPersistentCacheIndexManager().deleteAllIndexes());
141+
assertEquals(1, results.size());
139142
}
140143

141144
@Test
@@ -161,6 +164,9 @@ public void testAutoIndexCreationSetSuccessfullyUsingDefault() {
161164

162165
results = waitFor(collection.whereEqualTo("match", true).get());
163166
assertEquals(1, results.size());
167+
168+
assertDoesNotThrow(() -> db.getPersistentCacheIndexManager().deleteAllIndexes());
169+
assertEquals(1, results.size());
164170
}
165171

166172
@Test
@@ -175,5 +181,9 @@ public void testAutoIndexCreationAfterFailsTermination() {
175181
expectError(
176182
() -> db.getPersistentCacheIndexManager().disableIndexAutoCreation(),
177183
"The client has already been terminated");
184+
185+
expectError(
186+
() -> db.getPersistentCacheIndexManager().deleteAllIndexes(),
187+
"The client has already been terminated");
178188
}
179189
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public void enableIndexAutoCreation() {
5151
public void disableIndexAutoCreation() {
5252
client.setIndexAutoCreationEnabled(false);
5353
}
54+
55+
/**
56+
* Removes all persistent cache indexes. Please note this function will also deletes indexes
57+
* generated by {@link FirebaseFirestore#setIndexConfiguration(String)}, which is deprecated.
58+
*/
59+
public void deleteAllIndexes() {
60+
client.deleteAllFieldIndexes();
61+
}
5462
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ public void setIndexAutoCreationEnabled(boolean enabled) {
358358
asyncQueue.enqueueAndForget(() -> localStore.setIndexAutoCreationEnabled(enabled));
359359
}
360360

361+
public void deleteAllFieldIndexes() {
362+
verifyNotTerminated();
363+
asyncQueue.enqueueAndForget(() -> localStore.deleteAllFieldIndexes());
364+
}
365+
361366
public void removeSnapshotsInSyncListener(EventListener<Void> listener) {
362367
// Checks for shutdown but does not raise error, allowing remove after shutdown to be a no-op.
363368
if (isTerminated()) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ enum IndexType {
7878
/** Removes the given field index and deletes all index values. */
7979
void deleteFieldIndex(FieldIndex index);
8080

81+
/** Removes all field indexes and deletes all index values. */
82+
void deleteAllFieldIndexes();
83+
8184
/** Creates a full matched field index which serves the given target. */
8285
void createTargetIndexes(Target target);
8386

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,10 @@ public void configureFieldIndexes(List<FieldIndex> newFieldIndexes) {
802802
});
803803
}
804804

805+
public void deleteAllFieldIndexes() {
806+
persistence.runTransaction("Delete All Indexes", () -> indexManager.deleteAllFieldIndexes());
807+
}
808+
805809
public void setIndexAutoCreationEnabled(boolean enabled) {
806810
queryEngine.setIndexAutoCreationEnabled(enabled);
807811
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public void deleteFieldIndex(FieldIndex index) {
6060
// Field indices are not supported with memory persistence.
6161
}
6262

63+
@Override
64+
public void deleteAllFieldIndexes() {
65+
// Field indices are not supported with memory persistence.
66+
}
67+
6368
@Override
6469
public void createTargetIndexes(Target target) {}
6570

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,16 @@ public void deleteFieldIndex(FieldIndex index) {
233233
}
234234
}
235235

236+
@Override
237+
public void deleteAllFieldIndexes() {
238+
db.execute("DELETE FROM index_configuration");
239+
db.execute("DELETE FROM index_entries");
240+
db.execute("DELETE FROM index_state");
241+
242+
nextIndexToUpdate.clear();
243+
memoizedIndexes.clear();
244+
}
245+
236246
@Override
237247
public void createTargetIndexes(Target target) {
238248
hardAssert(started, "IndexManager not started");

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ protected void setRelativeIndexReadCostPerDocument(double newCost) {
234234
queryEngine.setRelativeIndexReadCostPerDocument(newCost);
235235
}
236236

237+
protected void deleteAllIndexes() {
238+
localStore.deleteAllFieldIndexes();
239+
}
240+
237241
private void releaseTarget(int targetId) {
238242
localStore.releaseTarget(targetId);
239243
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,68 @@ public void testDisableIndexAutoCreationWorks() {
575575
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
576576
}
577577

578+
@Test
579+
public void testDeleteAllIndexesWorksWithIndexAutoCreation() {
580+
Query query = query("coll").filter(filter("value", "==", "match"));
581+
int targetId = allocateQuery(query);
582+
583+
setIndexAutoCreationEnabled(true);
584+
setMinCollectionSizeToAutoCreateIndex(0);
585+
setRelativeIndexReadCostPerDocument(2);
586+
587+
applyRemoteEvent(addedRemoteEvent(doc("coll/a", 10, map("value", "match")), targetId));
588+
applyRemoteEvent(addedRemoteEvent(doc("coll/b", 10, map("value", Double.NaN)), targetId));
589+
applyRemoteEvent(addedRemoteEvent(doc("coll/c", 10, map("value", null)), targetId));
590+
applyRemoteEvent(addedRemoteEvent(doc("coll/d", 10, map("value", "mismatch")), targetId));
591+
applyRemoteEvent(addedRemoteEvent(doc("coll/e", 10, map("value", "match")), targetId));
592+
593+
// First time query is running without indexes.
594+
// Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
595+
// Full matched index should be created.
596+
executeQuery(query);
597+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
598+
assertQueryReturned("coll/a", "coll/e");
599+
600+
setIndexAutoCreationEnabled(false);
601+
602+
backfillIndexes();
603+
604+
executeQuery(query);
605+
assertRemoteDocumentsRead(/* byKey= */ 2, /* byCollection= */ 0);
606+
assertQueryReturned("coll/a", "coll/e");
607+
608+
deleteAllIndexes();
609+
610+
executeQuery(query);
611+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
612+
assertQueryReturned("coll/a", "coll/e");
613+
}
614+
615+
@Test
616+
public void testDeleteAllIndexesWorksWithManualAddedIndexes() {
617+
FieldIndex index =
618+
fieldIndex(
619+
"coll", 0, FieldIndex.INITIAL_STATE, "matches", FieldIndex.Segment.Kind.ASCENDING);
620+
configureFieldIndexes(singletonList(index));
621+
622+
Query query = query("coll").filter(filter("matches", "==", true));
623+
int targetId = allocateQuery(query);
624+
625+
applyRemoteEvent(addedRemoteEvent(doc("coll/a", 10, map("matches", true)), targetId));
626+
627+
backfillIndexes();
628+
629+
executeQuery(query);
630+
assertRemoteDocumentsRead(/* byKey= */ 1, /* byCollection= */ 0);
631+
assertQueryReturned("coll/a");
632+
633+
deleteAllIndexes();
634+
635+
executeQuery(query);
636+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 1);
637+
assertQueryReturned("coll/a");
638+
}
639+
578640
@Test
579641
public void testIndexAutoCreationWorksWithMutation() {
580642
Query query =

0 commit comments

Comments
 (0)