|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + firestoreClientDeleteAllFieldIndexes, |
| 20 | + firestoreClientSetPersistentCacheIndexAutoCreationEnabled, |
| 21 | + FirestoreClient |
| 22 | +} from '../core/firestore_client'; |
| 23 | +import { cast } from '../util/input_validation'; |
| 24 | +import { logDebug, logWarn } from '../util/log'; |
| 25 | + |
| 26 | +import { ensureFirestoreConfigured, Firestore } from './database'; |
| 27 | + |
| 28 | +/** |
| 29 | + * A `PersistentCacheIndexManager` which you can config persistent cache indexes |
| 30 | + * used for local query execution. |
| 31 | + * |
| 32 | + * To use, call `getPersistentCacheIndexManager()` to get an instance. |
| 33 | + * |
| 34 | + * TODO(CSI) Remove @internal to make the API publicly available. |
| 35 | + * @internal |
| 36 | + */ |
| 37 | +export class PersistentCacheIndexManager { |
| 38 | + readonly type: 'PersistentCacheIndexManager' = 'PersistentCacheIndexManager'; |
| 39 | + |
| 40 | + /** @hideconstructor */ |
| 41 | + constructor(readonly _client: FirestoreClient) {} |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Returns the PersistentCache Index Manager used by the given `Firestore` |
| 46 | + * object. |
| 47 | + * |
| 48 | + * @return The `PersistentCacheIndexManager` instance, or `null` if local |
| 49 | + * persistent storage is not in use. |
| 50 | + * |
| 51 | + * TODO(CSI) Remove @internal to make the API publicly available. |
| 52 | + * @internal |
| 53 | + */ |
| 54 | +export function getPersistentCacheIndexManager( |
| 55 | + firestore: Firestore |
| 56 | +): PersistentCacheIndexManager | null { |
| 57 | + firestore = cast(firestore, Firestore); |
| 58 | + |
| 59 | + const cachedInstance = persistentCacheIndexManagerByFirestore.get(firestore); |
| 60 | + if (cachedInstance) { |
| 61 | + return cachedInstance; |
| 62 | + } |
| 63 | + |
| 64 | + const client = ensureFirestoreConfigured(firestore); |
| 65 | + if (client._uninitializedComponentsProvider?._offlineKind !== 'persistent') { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + const instance = new PersistentCacheIndexManager(client); |
| 70 | + persistentCacheIndexManagerByFirestore.set(firestore, instance); |
| 71 | + return instance; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Enables SDK to create persistent cache indexes automatically for local query |
| 76 | + * execution when SDK believes cache indexes can help improves performance. |
| 77 | + * |
| 78 | + * This feature is disabled by default. |
| 79 | + * |
| 80 | + * TODO(CSI) Remove @internal to make the API publicly available. |
| 81 | + * @internal |
| 82 | + */ |
| 83 | +export function enablePersistentCacheIndexAutoCreation( |
| 84 | + indexManager: PersistentCacheIndexManager |
| 85 | +): void { |
| 86 | + setPersistentCacheIndexAutoCreationEnabled(indexManager, true); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Stops creating persistent cache indexes automatically for local query |
| 91 | + * execution. The indexes which have been created by calling |
| 92 | + * `enablePersistentCacheIndexAutoCreation()` still take effect. |
| 93 | + * |
| 94 | + * TODO(CSI) Remove @internal to make the API publicly available. |
| 95 | + * @internal |
| 96 | + */ |
| 97 | +export function disablePersistentCacheIndexAutoCreation( |
| 98 | + indexManager: PersistentCacheIndexManager |
| 99 | +): void { |
| 100 | + setPersistentCacheIndexAutoCreationEnabled(indexManager, false); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Removes all persistent cache indexes. |
| 105 | + * |
| 106 | + * Please note this function will also deletes indexes generated by |
| 107 | + * `setIndexConfiguration()`, which is deprecated. |
| 108 | + * |
| 109 | + * TODO(CSI) Remove @internal to make the API publicly available. |
| 110 | + * @internal |
| 111 | + */ |
| 112 | +export function deleteAllPersistentCacheIndexes( |
| 113 | + indexManager: PersistentCacheIndexManager |
| 114 | +): void { |
| 115 | + indexManager._client.verifyNotTerminated(); |
| 116 | + |
| 117 | + const promise = firestoreClientDeleteAllFieldIndexes(indexManager._client); |
| 118 | + |
| 119 | + promise |
| 120 | + .then(_ => logDebug('deleting all persistent cache indexes succeeded')) |
| 121 | + .catch(error => |
| 122 | + logWarn('deleting all persistent cache indexes failed', error) |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +function setPersistentCacheIndexAutoCreationEnabled( |
| 127 | + indexManager: PersistentCacheIndexManager, |
| 128 | + isEnabled: boolean |
| 129 | +): void { |
| 130 | + indexManager._client.verifyNotTerminated(); |
| 131 | + |
| 132 | + const promise = firestoreClientSetPersistentCacheIndexAutoCreationEnabled( |
| 133 | + indexManager._client, |
| 134 | + isEnabled |
| 135 | + ); |
| 136 | + |
| 137 | + promise |
| 138 | + .then(_ => |
| 139 | + logDebug( |
| 140 | + `setting persistent cache index auto creation ` + |
| 141 | + `isEnabled=${isEnabled} succeeded` |
| 142 | + ) |
| 143 | + ) |
| 144 | + .catch(error => |
| 145 | + logWarn( |
| 146 | + `setting persistent cache index auto creation ` + |
| 147 | + `isEnabled=${isEnabled} failed`, |
| 148 | + error |
| 149 | + ) |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Maps `Firestore` instances to their corresponding |
| 155 | + * `PersistentCacheIndexManager` instances. |
| 156 | + * |
| 157 | + * Use a `WeakMap` so that the mapping will be automatically dropped when the |
| 158 | + * `Firestore` instance is garbage collected. This emulates a private member |
| 159 | + * as described in https://goo.gle/454yvug. |
| 160 | + */ |
| 161 | +const persistentCacheIndexManagerByFirestore = new WeakMap< |
| 162 | + Firestore, |
| 163 | + PersistentCacheIndexManager |
| 164 | +>(); |
0 commit comments