* Failed refs whose name tail matches one of the given symbol names — the * candidates a sync should retry after files carrying those names changed * (#1240). Names matching more than `perNameCeiling` failed refs are * skipped entirely: at that population a name is external/builtin noise
(names: string[], perNameCeiling: number = 500, perModuleCeiling: number = 5000)
| 4912 | * that just appeared resolves at once. The ceiling only bounds the work. |
| 4913 | */ |
| 4914 | getRetryableFailedReferences(names: string[], perNameCeiling: number = 500, perModuleCeiling: number = 5000): UnresolvedReference[] { |
| 4915 | if (names.length === 0) return []; |
| 4916 | |
| 4917 | const lookups = [ |
| 4918 | { column: 'name_tail', where: "status = 'failed'" }, |
| 4919 | // idx_unresolved_failed_module_name; its leading `status` is the |
| 4920 | // equality term that keeps the planner off idx_unresolved_status and |
| 4921 | // idx_unresolved_name, which read hundreds of thousands of rows here. |
| 4922 | { column: 'reference_name', where: MODULE_PARKED_SQL }, |
| 4923 | // A member reference's member: idx_unresolved_failed_module_member, for |
| 4924 | // the same reason. A whole name is the lookup above's. |
| 4925 | { column: MEMBER_TAIL_SQL, where: `${MODULE_PARKED_SQL} AND instr(reference_name, '.') > 0` }, |
| 4926 | ]; |
| 4927 | // Pass 1: per-key counts, chunked under the SQLite parameter limit. |
| 4928 | const counts = lookups.map(({ column, where }) => { |
| 4929 | const byKey = new Map<string, number>(); |
| 4930 | for (let i = 0; i < names.length; i += SQLITE_PARAM_CHUNK_SIZE) { |
| 4931 | const chunk = names.slice(i, i + SQLITE_PARAM_CHUNK_SIZE); |
| 4932 | const placeholders = chunk.map(() => '?').join(','); |
| 4933 | const found = this.db |
| 4934 | .prepare(`SELECT ${column} AS key, COUNT(*) AS count FROM unresolved_refs WHERE ${where} AND ${column} IN (${placeholders}) GROUP BY ${column}`) |
| 4935 | .all(...chunk) as Array<{ key: string; count: number }>; |
| 4936 | for (const row of found) byKey.set(row.key, row.count); |
| 4937 | } |
| 4938 | return byKey; |
| 4939 | }); |
| 4940 | // A member reference parked under its module had its member's name as its |
| 4941 | // tail before, so the two lookups by that name share one ceiling: a name |
| 4942 | // too common to look up stays so wherever its references are parked, and |
| 4943 | // the references still under it are not retried more for it. |
| 4944 | const [byTail, , byMember] = counts; |
| 4945 | const population = (lookup: number, key: string, count: number): number => |
| 4946 | lookup === 1 ? count : (byTail!.get(key) ?? 0) + (byMember!.get(key) ?? 0); |
| 4947 | |
| 4948 | const rows = new Map<number, UnresolvedRefRow>(); |
| 4949 | for (const [lookup, { column, where }] of lookups.entries()) { |
| 4950 | const retryKeys = [...counts[lookup]!] |
| 4951 | .filter(([key, count]) => (lookup === 0 && isModuleKey(key) ? count <= perModuleCeiling : population(lookup, key, count) <= perNameCeiling)) |
| 4952 | .map(([key]) => key); |
| 4953 | |
| 4954 | // Pass 2: load the surviving rows; a row two lookups find is kept once. |
| 4955 | for (let i = 0; i < retryKeys.length; i += SQLITE_PARAM_CHUNK_SIZE) { |
| 4956 | const chunk = retryKeys.slice(i, i + SQLITE_PARAM_CHUNK_SIZE); |
| 4957 | const placeholders = chunk.map(() => '?').join(','); |
| 4958 | const chunkRows = this.db |
| 4959 | .prepare(`SELECT * FROM unresolved_refs WHERE ${where} AND ${column} IN (${placeholders})`) |
| 4960 | .all(...chunk) as UnresolvedRefRow[]; |
| 4961 | // Loop, not spread — same V8 argument-limit hazard as |
| 4962 | // getUnresolvedReferencesByFiles (#1558): a large definition delta can |
| 4963 | // select an unbounded number of failed rows per chunk. |
| 4964 | for (const row of chunkRows) rows.set(row.id, row); |
| 4965 | } |
| 4966 | } |
| 4967 | |
| 4968 | // By receiver: one range of idx_unresolved_failed_module_name per name, |
| 4969 | // `store.` up to `store/` ('/' follows '.'), read one past the ceiling. |
| 4970 | const byReceiver = this.db.prepare( |
| 4971 | `SELECT * FROM unresolved_refs WHERE ${MODULE_PARKED_SQL} AND reference_name >= ? AND reference_name < ? LIMIT ?` |