Skip to content

Commit 9d8ed0f

Browse files
committed
Add deleteAllIndexes
1 parent 14a6a71 commit 9d8ed0f

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
@@ -52,4 +52,8 @@ public void enableIndexAutoCreation() {
5252
public void disableIndexAutoCreation() {
5353
client.disableIndexAutoCreation();
5454
}
55+
56+
public void deleteAllIndexes() {
57+
client.deleteAllFieldIndexes();
58+
}
5559
}

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
@@ -363,6 +363,11 @@ public void disableIndexAutoCreation() {
363363
asyncQueue.enqueueAndForget(() -> localStore.disableIndexAutoCreation());
364364
}
365365

366+
public void deleteAllFieldIndexes() {
367+
verifyNotTerminated();
368+
asyncQueue.enqueueAndForget(() -> localStore.deleteAllFieldIndexes());
369+
}
370+
366371
public void removeSnapshotsInSyncListener(EventListener<Void> listener) {
367372
// Checks for shutdown but does not raise error, allowing remove after shutdown to be a no-op.
368373
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 createTargetIndices(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 enableIndexAutoCreation() {
806810
queryEngine.enableIndexAutoCreation();
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 createTargetIndices(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 createTargetIndices(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
@@ -232,6 +232,10 @@ protected void disableIndexAutoCreation() {
232232
queryEngine.disableIndexAutoCreation();
233233
}
234234

235+
protected void deleteAllIndexes() {
236+
localStore.deleteAllFieldIndexes();
237+
}
238+
235239
private void releaseTarget(int targetId) {
236240
localStore.releaseTarget(targetId);
237241
}

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
@@ -503,6 +503,66 @@ public void testDisableIndexAutoCreationWorks() {
503503
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
504504
}
505505

506+
@Test
507+
public void testDeleteAllIndexesWorksWithIndexAutoCreation() {
508+
Query query = query("coll").filter(filter("matches", "==", true));
509+
int targetId = allocateQuery(query);
510+
511+
enableIndexAutoCreation();
512+
513+
applyRemoteEvent(addedRemoteEvent(doc("coll/a", 10, map("matches", true)), targetId));
514+
applyRemoteEvent(addedRemoteEvent(doc("coll/b", 10, map("matches", false)), targetId));
515+
applyRemoteEvent(addedRemoteEvent(doc("coll/c", 10, map("matches", false)), targetId));
516+
applyRemoteEvent(addedRemoteEvent(doc("coll/d", 10, map("matches", false)), targetId));
517+
applyRemoteEvent(addedRemoteEvent(doc("coll/e", 10, map("matches", true)), targetId));
518+
519+
// First time query is running without indexes.
520+
// Based on current heuristic, collection document counts (5) > 2 * resultSize (2).
521+
// Full matched index should be created.
522+
executeQuery(query);
523+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
524+
assertQueryReturned("coll/a", "coll/e");
525+
526+
disableIndexAutoCreation();
527+
528+
backfillIndexes();
529+
530+
executeQuery(query);
531+
assertRemoteDocumentsRead(/* byKey= */ 2, /* byCollection= */ 0);
532+
assertQueryReturned("coll/a", "coll/e");
533+
534+
deleteAllIndexes();
535+
536+
executeQuery(query);
537+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 2);
538+
assertQueryReturned("coll/a", "coll/e");
539+
}
540+
541+
@Test
542+
public void testDeleteAllIndexesWorksWithManualAddedIndexes() {
543+
FieldIndex index =
544+
fieldIndex(
545+
"coll", 0, FieldIndex.INITIAL_STATE, "matches", FieldIndex.Segment.Kind.ASCENDING);
546+
configureFieldIndexes(singletonList(index));
547+
548+
Query query = query("coll").filter(filter("matches", "==", true));
549+
int targetId = allocateQuery(query);
550+
551+
applyRemoteEvent(addedRemoteEvent(doc("coll/a", 10, map("matches", true)), targetId));
552+
553+
backfillIndexes();
554+
555+
executeQuery(query);
556+
assertRemoteDocumentsRead(/* byKey= */ 1, /* byCollection= */ 0);
557+
assertQueryReturned("coll/a");
558+
559+
deleteAllIndexes();
560+
561+
executeQuery(query);
562+
assertRemoteDocumentsRead(/* byKey= */ 0, /* byCollection= */ 1);
563+
assertQueryReturned("coll/a");
564+
}
565+
506566
@Test
507567
public void testIndexAutoCreationWorksWithMutation() {
508568
Query query = query("coll").filter(filter("matches", "==", true));

0 commit comments

Comments
 (0)