* 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)
| 5023 | * that just appeared resolves at once. The ceiling only bounds the work. |
| 5024 | */ |
| 5025 | getRetryableFailedReferences(names: string[], perNameCeiling: number = 500, perModuleCeiling: number = 5000): UnresolvedReference[] { |
| 5026 | if (names.length === 0) return []; |
| 5027 | |
| 5028 | const lookups = [ |
| 5029 | { column: 'name_tail', where: "status = 'failed'" }, |
| 5030 | // idx_unresolved_failed_module_name; its leading `status` is the |
| 5031 | // equality term that keeps the planner off idx_unresolved_status and |
| 5032 | // idx_unresolved_name, which read hundreds of thousands of rows here. |
| 5033 | { column: 'reference_name', where: MODULE_PARKED_SQL }, |
| 5034 | // A member reference's member: idx_unresolved_failed_module_member, for |
| 5035 | // the same reason. A whole name is the lookup above's. |
| 5036 | { column: MEMBER_TAIL_SQL, where: `${MODULE_PARKED_SQL} AND instr(reference_name, '.') > 0` }, |
| 5037 | ]; |
| 5038 | // Pass 1: per-key counts, chunked under the SQLite parameter limit. |
| 5039 | const counts = lookups.map(({ column, where }) => { |
| 5040 | const byKey = new Map<string, number>(); |
| 5041 | for (let i = 0; i < names.length; i += SQLITE_PARAM_CHUNK_SIZE) { |
| 5042 | const chunk = names.slice(i, i + SQLITE_PARAM_CHUNK_SIZE); |
| 5043 | const placeholders = chunk.map(() => '?').join(','); |
| 5044 | const found = this.db |
| 5045 | .prepare(`SELECT ${column} AS key, COUNT(*) AS count FROM unresolved_refs WHERE ${where} AND ${column} IN (${placeholders}) GROUP BY ${column}`) |
| 5046 | .all(...chunk) as Array<{ key: string; count: number }>; |
| 5047 | for (const row of found) byKey.set(row.key, row.count); |
| 5048 | } |
| 5049 | return byKey; |
| 5050 | }); |
| 5051 | // A member reference parked under its module had its member's name as its |
| 5052 | // tail before, so the two lookups by that name share one ceiling: a name |
| 5053 | // too common to look up stays so wherever its references are parked, and |
| 5054 | // the references still under it are not retried more for it. |
| 5055 | const [byTail, , byMember] = counts; |
| 5056 | const population = (lookup: number, key: string, count: number): number => |
| 5057 | lookup === 1 ? count : (byTail!.get(key) ?? 0) + (byMember!.get(key) ?? 0); |
| 5058 | |
| 5059 | const rows = new Map<number, UnresolvedRefRow>(); |
| 5060 | for (const [lookup, { column, where }] of lookups.entries()) { |
| 5061 | const retryKeys = [...counts[lookup]!] |
| 5062 | .filter(([key, count]) => (lookup === 0 && isModuleKey(key) ? count <= perModuleCeiling : population(lookup, key, count) <= perNameCeiling)) |
| 5063 | .map(([key]) => key); |
| 5064 | |
| 5065 | // Pass 2: load the surviving rows; a row two lookups find is kept once. |
| 5066 | for (let i = 0; i < retryKeys.length; i += SQLITE_PARAM_CHUNK_SIZE) { |
| 5067 | const chunk = retryKeys.slice(i, i + SQLITE_PARAM_CHUNK_SIZE); |
| 5068 | const placeholders = chunk.map(() => '?').join(','); |
| 5069 | const chunkRows = this.db |
| 5070 | .prepare(`SELECT * FROM unresolved_refs WHERE ${where} AND ${column} IN (${placeholders})`) |
| 5071 | .all(...chunk) as UnresolvedRefRow[]; |
| 5072 | // Loop, not spread — same V8 argument-limit hazard as |
| 5073 | // getUnresolvedReferencesByFiles (#1558): a large definition delta can |
| 5074 | // select an unbounded number of failed rows per chunk. |
| 5075 | for (const row of chunkRows) rows.set(row.id, row); |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | // By receiver: one range of idx_unresolved_failed_module_name per name, |
| 5080 | // `store.` up to `store/` ('/' follows '.'), read one past the ceiling. |
| 5081 | const byReceiver = this.db.prepare( |
| 5082 | `SELECT * FROM unresolved_refs WHERE ${MODULE_PARKED_SQL} AND reference_name >= ? AND reference_name < ? LIMIT ?` |