Skip to content

Commit 695c966

Browse files
authored
Merge 0608fe2 into 75df024
2 parents 75df024 + 0608fe2 commit 695c966

15 files changed

+483
-0
lines changed

.changeset/healthy-peas-heal.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': patch
3+
'firebase': patch
4+
---
5+
6+
Implemented internal logic to delete all client-side indexes

packages/firestore/src/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export {
205205
export {
206206
PersistentCacheIndexManager,
207207
getPersistentCacheIndexManager,
208+
deleteAllPersistentCacheIndexes,
208209
enablePersistentCacheIndexAutoCreation,
209210
disablePersistentCacheIndexAutoCreation
210211
} from './api/persistent_cache_index_manager';

packages/firestore/src/api/persistent_cache_index_manager.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import {
19+
firestoreClientDeleteAllFieldIndexes,
1920
firestoreClientSetPersistentCacheIndexAutoCreationEnabled,
2021
FirestoreClient,
2122
TestingHooks as FirestoreClientTestingHooks
@@ -101,6 +102,31 @@ export function disablePersistentCacheIndexAutoCreation(
101102
setPersistentCacheIndexAutoCreationEnabled(indexManager, false);
102103
}
103104

105+
/**
106+
* Removes all persistent cache indexes.
107+
*
108+
* Please note this function will also deletes indexes generated by
109+
* `setIndexConfiguration()`, which is deprecated.
110+
*
111+
* TODO(CSI) Remove @internal to make the API publicly available.
112+
* @internal
113+
*/
114+
export function deleteAllPersistentCacheIndexes(
115+
indexManager: PersistentCacheIndexManager
116+
): void {
117+
indexManager._client.verifyNotTerminated();
118+
119+
const promise = firestoreClientDeleteAllFieldIndexes(indexManager._client);
120+
121+
promise
122+
.then(_ => logDebug('deleting all persistent cache indexes succeeded'))
123+
.catch(error =>
124+
logWarn('deleting all persistent cache indexes failed', error)
125+
);
126+
127+
testingHooksSpi?.notifyPersistentCacheDeleteAllIndexes(promise);
128+
}
129+
104130
function setPersistentCacheIndexAutoCreationEnabled(
105131
indexManager: PersistentCacheIndexManager,
106132
isEnabled: boolean

packages/firestore/src/core/firestore_client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { IndexType } from '../local/index_manager';
2727
import { LocalStore } from '../local/local_store';
2828
import {
2929
localStoreConfigureFieldIndexes,
30+
localStoreDeleteAllFieldIndexes,
3031
localStoreExecuteQuery,
3132
localStoreGetNamedQuery,
3233
localStoreHandleUserChange,
@@ -844,6 +845,14 @@ export function firestoreClientSetPersistentCacheIndexAutoCreationEnabled(
844845
});
845846
}
846847

848+
export function firestoreClientDeleteAllFieldIndexes(
849+
client: FirestoreClient
850+
): Promise<void> {
851+
return client.asyncQueue.enqueue(async () => {
852+
return localStoreDeleteAllFieldIndexes(await getLocalStore(client));
853+
});
854+
}
855+
847856
/**
848857
* Test-only hooks into the SDK for use exclusively by tests.
849858
*/

packages/firestore/src/local/index_manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export interface IndexManager {
105105
index: FieldIndex
106106
): PersistencePromise<void>;
107107

108+
/** Removes all field indexes and deletes all index values. */
109+
deleteAllFieldIndexes(
110+
transaction: PersistenceTransaction
111+
): PersistencePromise<void>;
112+
108113
/** Creates a full matched field index which serves the given target. */
109114
createTargetIndexes(
110115
transaction: PersistenceTransaction,

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ export class IndexedDbIndexManager implements IndexManager {
252252
);
253253
}
254254

255+
deleteAllFieldIndexes(
256+
transaction: PersistenceTransaction
257+
): PersistencePromise<void> {
258+
const indexes = indexConfigurationStore(transaction);
259+
const entries = indexEntriesStore(transaction);
260+
const states = indexStateStore(transaction);
261+
262+
return indexes
263+
.deleteAll()
264+
.next(() => entries.deleteAll())
265+
.next(() => states.deleteAll());
266+
}
267+
255268
createTargetIndexes(
256269
transaction: PersistenceTransaction,
257270
target: Target

packages/firestore/src/local/local_store_impl.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,18 @@ export function localStoreSetIndexAutoCreationEnabled(
15351535
localStoreImpl.queryEngine.indexAutoCreationEnabled = isEnabled;
15361536
}
15371537

1538+
export function localStoreDeleteAllFieldIndexes(
1539+
localStore: LocalStore
1540+
): Promise<void> {
1541+
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
1542+
const indexManager = localStoreImpl.indexManager;
1543+
return localStoreImpl.persistence.runTransaction(
1544+
'Delete All Indexes',
1545+
'readwrite',
1546+
transaction => indexManager.deleteAllFieldIndexes(transaction)
1547+
);
1548+
}
1549+
15381550
/**
15391551
* Test-only hooks into the SDK for use exclusively by tests.
15401552
*/

packages/firestore/src/local/memory_index_manager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export class MemoryIndexManager implements IndexManager {
6666
return PersistencePromise.resolve();
6767
}
6868

69+
deleteAllFieldIndexes(
70+
transaction: PersistenceTransaction
71+
): PersistencePromise<void> {
72+
// Field indices are not supported with memory persistence.
73+
return PersistencePromise.resolve();
74+
}
75+
6976
createTargetIndexes(
7077
transaction: PersistenceTransaction,
7178
target: Target

packages/firestore/src/util/testing_hooks.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,29 @@ export class TestingHooks {
8989
);
9090
}
9191

92+
/**
93+
* Registers a callback to be notified when
94+
* `deleteAllPersistentCacheIndexes()` is invoked.
95+
*
96+
* The relative order in which callbacks are notified is unspecified; do not
97+
* rely on any particular ordering. If a given callback is registered multiple
98+
* times then it will be notified multiple times, once per registration.
99+
*
100+
* @param callback the callback to invoke when
101+
* `deleteAllPersistentCacheIndexes()` is invoked.
102+
*
103+
* @return a function that, when called, unregisters the given callback; only
104+
* the first invocation of the returned function does anything; all subsequent
105+
* invocations do nothing.
106+
*/
107+
static onPersistentCacheDeleteAllIndexes(
108+
callback: PersistentCacheDeleteAllIndexesCallback
109+
): Unsubscribe {
110+
return TestingHooksSpiImpl.instance.onPersistentCacheDeleteAllIndexes(
111+
callback
112+
);
113+
}
114+
92115
/**
93116
* Determines the type of client-side index that will be used when executing the
94117
* given query against the local cache.
@@ -171,6 +194,22 @@ export type PersistentCacheIndexAutoCreationToggleCallback = (
171194
promise: Promise<void>
172195
) => unknown;
173196

197+
/**
198+
* The signature of callbacks registered with
199+
* `TestingHooks.onPersistentCacheDeleteAllIndexes()`.
200+
*
201+
* The `promise` argument will be fulfilled when the asynchronous work started
202+
* by the call to `deleteAllPersistentCacheIndexes()` completes successfully, or
203+
* will be rejected if it fails.
204+
*
205+
* The return value of the callback, if any, is ignored.
206+
*
207+
* @internal
208+
*/
209+
export type PersistentCacheDeleteAllIndexesCallback = (
210+
promise: Promise<void>
211+
) => unknown;
212+
174213
/**
175214
* The implementation of `TestingHooksSpi`.
176215
*/
@@ -183,6 +222,11 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
183222
private readonly persistentCacheIndexAutoCreationToggleCallbacksById =
184223
new Map<Symbol, PersistentCacheIndexAutoCreationToggleCallback>();
185224

225+
private readonly persistentCacheDeleteAllIndexesCallbacksById = new Map<
226+
Symbol,
227+
PersistentCacheDeleteAllIndexesCallback
228+
>();
229+
186230
private constructor() {}
187231

188232
static get instance(): TestingHooksSpiImpl {
@@ -222,6 +266,21 @@ class TestingHooksSpiImpl implements TestingHooksSpi {
222266
this.persistentCacheIndexAutoCreationToggleCallbacksById
223267
);
224268
}
269+
270+
notifyPersistentCacheDeleteAllIndexes(promise: Promise<void>): void {
271+
this.persistentCacheDeleteAllIndexesCallbacksById.forEach(callback =>
272+
callback(promise)
273+
);
274+
}
275+
276+
onPersistentCacheDeleteAllIndexes(
277+
callback: PersistentCacheDeleteAllIndexesCallback
278+
): Unsubscribe {
279+
return registerCallback(
280+
callback,
281+
this.persistentCacheDeleteAllIndexesCallbacksById
282+
);
283+
}
225284
}
226285

227286
function registerCallback<T>(

packages/firestore/src/util/testing_hooks_spi.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export interface TestingHooksSpi {
5757
* promise.
5858
*/
5959
notifyPersistentCacheIndexAutoCreationToggle(promise: Promise<void>): void;
60+
61+
/**
62+
* Invokes all callbacks registered with
63+
* `TestingHooks.onPersistentCacheDeleteAllIndexes()` with the given
64+
* promise.
65+
*/
66+
notifyPersistentCacheDeleteAllIndexes(promise: Promise<void>): void;
6067
}
6168

6269
/**

0 commit comments

Comments
 (0)