Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteAllPersistentCacheIndexes() implemented (but hidden for now) #7586

6 changes: 6 additions & 0 deletions .changeset/healthy-peas-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/firestore': patch
'firebase': patch
---

Implemented internal logic to delete all client-side indexes
1 change: 1 addition & 0 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"packages/util/dist/src/environment.d.ts",
"packages/util/dist/src/compat.d.ts",
"packages/util/dist/src/obj.d.ts",
"packages/firestore/src/api/persistent_cache_index_manager.ts",
"packages/firestore/src/protos/firestore_bundle_proto.ts",
"packages/firestore/src/protos/firestore_proto_api.ts",
"packages/firestore/src/util/error.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export {
export {
PersistentCacheIndexManager,
getPersistentCacheIndexManager,
deleteAllPersistentCacheIndexes,
enablePersistentCacheIndexAutoCreation,
disablePersistentCacheIndexAutoCreation
} from './api/persistent_cache_index_manager';
Expand Down
54 changes: 53 additions & 1 deletion packages/firestore/src/api/persistent_cache_index_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/

import {
firestoreClientDeleteAllFieldIndexes,
firestoreClientSetPersistentCacheIndexAutoCreationEnabled,
FirestoreClient
FirestoreClient,
TestingHooks as FirestoreClientTestingHooks
} from '../core/firestore_client';
import { cast } from '../util/input_validation';
import { logDebug, logWarn } from '../util/log';
import { testingHooksSpi } from '../util/testing_hooks_spi';

import { ensureFirestoreConfigured, Firestore } from './database';

Expand Down Expand Up @@ -99,6 +102,31 @@ export function disablePersistentCacheIndexAutoCreation(
setPersistentCacheIndexAutoCreationEnabled(indexManager, false);
}

/**
* Removes all persistent cache indexes.
*
* Please note this function will also deletes indexes generated by
* `setIndexConfiguration()`, which is deprecated.
*
* TODO(CSI) Remove @internal to make the API publicly available.
* @internal
*/
export function deleteAllPersistentCacheIndexes(
indexManager: PersistentCacheIndexManager
): void {
indexManager._client.verifyNotTerminated();

const promise = firestoreClientDeleteAllFieldIndexes(indexManager._client);

promise
.then(_ => logDebug('deleting all persistent cache indexes succeeded'))
.catch(error =>
logWarn('deleting all persistent cache indexes failed', error)
);

testingHooksSpi?.notifyPersistentCacheDeleteAllIndexes(promise);
}

function setPersistentCacheIndexAutoCreationEnabled(
indexManager: PersistentCacheIndexManager,
isEnabled: boolean
Expand All @@ -124,6 +152,8 @@ function setPersistentCacheIndexAutoCreationEnabled(
error
)
);

testingHooksSpi?.notifyPersistentCacheIndexAutoCreationToggle(promise);
}

/**
Expand All @@ -138,3 +168,25 @@ const persistentCacheIndexManagerByFirestore = new WeakMap<
Firestore,
PersistentCacheIndexManager
>();

/**
* Test-only hooks into the SDK for use exclusively by tests.
*/
export class TestingHooks {
private constructor() {
throw new Error('creating instances is not supported');
}

static setIndexAutoCreationSettings(
indexManager: PersistentCacheIndexManager,
settings: {
indexAutoCreationMinCollectionSize?: number;
relativeIndexReadCostPerDocument?: number;
}
): Promise<void> {
return FirestoreClientTestingHooks.setPersistentCacheIndexAutoCreationSettings(
indexManager._client,
settings
);
}
}
49 changes: 48 additions & 1 deletion packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import {
CredentialsProvider
} from '../api/credentials';
import { User } from '../auth/user';
import { IndexType } from '../local/index_manager';
import { LocalStore } from '../local/local_store';
import {
localStoreConfigureFieldIndexes,
localStoreDeleteAllFieldIndexes,
localStoreExecuteQuery,
localStoreGetNamedQuery,
localStoreHandleUserChange,
localStoreReadDocument,
localStoreSetIndexAutoCreationEnabled
localStoreSetIndexAutoCreationEnabled,
TestingHooks as LocalStoreTestingHooks
} from '../local/local_store_impl';
import { Persistence } from '../local/persistence';
import { Document } from '../model/document';
Expand Down Expand Up @@ -841,3 +844,47 @@ export function firestoreClientSetPersistentCacheIndexAutoCreationEnabled(
);
});
}

export function firestoreClientDeleteAllFieldIndexes(
client: FirestoreClient
): Promise<void> {
return client.asyncQueue.enqueue(async () => {
return localStoreDeleteAllFieldIndexes(await getLocalStore(client));
});
}

/**
* Test-only hooks into the SDK for use exclusively by tests.
*/
export class TestingHooks {
private constructor() {
throw new Error('creating instances is not supported');
}

static getQueryIndexType(
client: FirestoreClient,
query: Query
): Promise<IndexType> {
return client.asyncQueue.enqueue(async () => {
const localStore = await getLocalStore(client);
return LocalStoreTestingHooks.getQueryIndexType(localStore, query);
});
}

static setPersistentCacheIndexAutoCreationSettings(
client: FirestoreClient,
settings: {
indexAutoCreationMinCollectionSize?: number;
relativeIndexReadCostPerDocument?: number;
}
): Promise<void> {
const settingsCopy = { ...settings };
return client.asyncQueue.enqueue(async () => {
const localStore = await getLocalStore(client);
LocalStoreTestingHooks.setIndexAutoCreationSettings(
localStore,
settingsCopy
);
});
}
}
5 changes: 5 additions & 0 deletions packages/firestore/src/local/index_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export interface IndexManager {
index: FieldIndex
): PersistencePromise<void>;

/** Removes all field indexes and deletes all index values. */
deleteAllFieldIndexes(
transaction: PersistenceTransaction
): PersistencePromise<void>;

/** Creates a full matched field index which serves the given target. */
createTargetIndexes(
transaction: PersistenceTransaction,
Expand Down
13 changes: 13 additions & 0 deletions packages/firestore/src/local/indexeddb_index_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ export class IndexedDbIndexManager implements IndexManager {
);
}

deleteAllFieldIndexes(
transaction: PersistenceTransaction
): PersistencePromise<void> {
const indexes = indexConfigurationStore(transaction);
const entries = indexEntriesStore(transaction);
const states = indexStateStore(transaction);

return indexes
.deleteAll()
.next(() => entries.deleteAll())
.next(() => states.deleteAll());
}

createTargetIndexes(
transaction: PersistenceTransaction,
target: Target
Expand Down
27 changes: 26 additions & 1 deletion packages/firestore/src/local/local_store_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import { BATCHID_UNKNOWN } from '../util/types';

import { BundleCache } from './bundle_cache';
import { DocumentOverlayCache } from './document_overlay_cache';
import { IndexManager } from './index_manager';
import { IndexManager, IndexType } from './index_manager';
import { IndexedDbMutationQueue } from './indexeddb_mutation_queue';
import { IndexedDbPersistence } from './indexeddb_persistence';
import { IndexedDbTargetCache } from './indexeddb_target_cache';
Expand Down Expand Up @@ -1535,6 +1535,18 @@ export function localStoreSetIndexAutoCreationEnabled(
localStoreImpl.queryEngine.indexAutoCreationEnabled = isEnabled;
}

export function localStoreDeleteAllFieldIndexes(
localStore: LocalStore
): Promise<void> {
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
const indexManager = localStoreImpl.indexManager;
return localStoreImpl.persistence.runTransaction(
'Delete All Indexes',
'readwrite',
transaction => indexManager.deleteAllFieldIndexes(transaction)
);
}

/**
* Test-only hooks into the SDK for use exclusively by tests.
*/
Expand All @@ -1560,4 +1572,17 @@ export class TestingHooks {
settings.relativeIndexReadCostPerDocument;
}
}

static getQueryIndexType(
localStore: LocalStore,
query: Query
): Promise<IndexType> {
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
const target = queryToTarget(query);
return localStoreImpl.persistence.runTransaction(
'local_store_impl TestingHooks getQueryIndexType',
'readonly',
txn => localStoreImpl.indexManager.getIndexType(txn, target)
);
}
}
7 changes: 7 additions & 0 deletions packages/firestore/src/local/memory_index_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export class MemoryIndexManager implements IndexManager {
return PersistencePromise.resolve();
}

deleteAllFieldIndexes(
transaction: PersistenceTransaction
): PersistencePromise<void> {
// Field indices are not supported with memory persistence.
return PersistencePromise.resolve();
}

createTargetIndexes(
transaction: PersistenceTransaction,
target: Target
Expand Down
Loading
Loading