| 17 | const MAX_LIST_NUM_SUB_PATH = 204800; |
| 18 | |
| 19 | export default class DatabaseRemove { |
| 20 | path: string; |
| 21 | remote: RemoveRemote; |
| 22 | listRemote: ListRemote; |
| 23 | private deleteJobStack: Stack<() => Promise<boolean>, boolean>; |
| 24 | private listStack: Stack<() => Promise<string[]>, string[]>; |
| 25 | |
| 26 | /** |
| 27 | * Construct a new RTDB delete operation. |
| 28 | * |
| 29 | * @param instance RTBD instance ID. |
| 30 | * @param path path to delete. |
| 31 | * @param host db host. |
| 32 | * @param disableTriggers if true, suppresses any Cloud functions that would be triggered by this operation. |
| 33 | */ |
| 34 | constructor(instance: string, path: string, host: string, disableTriggers: boolean) { |
| 35 | this.path = path; |
| 36 | this.remote = new RTDBRemoveRemote(instance, host, disableTriggers); |
| 37 | this.deleteJobStack = new Stack({ |
| 38 | name: "delete stack", |
| 39 | concurrency: 1, |
| 40 | retries: 3, |
| 41 | }); |
| 42 | this.listRemote = new RTDBListRemote(instance, host); |
| 43 | this.listStack = new Stack({ |
| 44 | name: "list stack", |
| 45 | concurrency: 1, |
| 46 | retries: 3, |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | async execute(): Promise<void> { |
| 51 | await this.deletePath(this.path); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * First, attempt to delete the path, if the path is big (i.e. exceeds writeSizeLimit of tiny), |
| 56 | * it will perform multi-path recursive chunked deletes in rounds. |
| 57 | * Each round, it fetches listNumSubPath subPaths and issue batches based on batchSize. |
| 58 | * At the end of each round, it adjustes the batchSize based on whether the majority of the batches are small. |
| 59 | * |
| 60 | * listNumSubPath starts with INITIAL_LIST_NUM_SUB_PATH and grow expontentially until MAX_LIST_NUM_SUB_PATH. |
| 61 | * |
| 62 | * @param path path to delete |
| 63 | * @return true if this path is small (Does not exceed writeSizeLimit of tiny) |
| 64 | */ |
| 65 | private async deletePath(path: string): Promise<boolean> { |
| 66 | if (await this.deleteJobStack.run(() => this.remote.deletePath(path))) { |
| 67 | return true; |
| 68 | } |
| 69 | let listNumSubPath = INITIAL_LIST_NUM_SUB_PATH; |
| 70 | // The range of batchSize to gradually narrow down. |
| 71 | let batchSizeLow = 1; |
| 72 | let batchSizeHigh = MAX_LIST_NUM_SUB_PATH + 1; |
| 73 | let batchSize = INITIAL_DELETE_BATCH_SIZE; |
| 74 | while (true) { |
| 75 | const subPathList = await this.listStack.run(() => |
| 76 | this.listRemote.listPath(path, listNumSubPath), |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…