Skip to content

Commit d2d50c7

Browse files
committed
Add deleteAllIndexes
1 parent fb12477 commit d2d50c7

File tree

8 files changed

+95
-0
lines changed

8 files changed

+95
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ public void enableIndexAutoCreation() {
5151
public void disableIndexAutoCreation() {
5252
client.setIndexAutoCreationEnabled(false);
5353
}
54+
55+
public void deleteAllIndexes() {
56+
client.deleteAllFieldIndexes();
57+
}
5458
}

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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,66 @@ public void testDisableIndexAutoCreationWorks() {
575575
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
576576
}
577577

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

0 commit comments

Comments
 (0)