* Batch lookup: fetch many nodes by ID in a single SQL round-trip. * * Replaces the N+1 pattern in graph traversal where every edge would * trigger its own `getNodeById` call. For a function with 50 callers * this collapses 50 point reads into one IN-list query (~10-50x * faster end-t
(ids: readonly string[])
| 1210 | * the SQL query only touches the misses. |
| 1211 | */ |
| 1212 | getNodesByIds(ids: readonly string[]): Map<string, Node> { |
| 1213 | const out = new Map<string, Node>(); |
| 1214 | if (ids.length === 0) return out; |
| 1215 | |
| 1216 | // Serve cache hits first; build the miss list for SQL. |
| 1217 | const misses: string[] = []; |
| 1218 | for (const id of ids) { |
| 1219 | const cached = this.nodeCache.get(id); |
| 1220 | if (cached !== undefined) { |
| 1221 | // LRU touch |
| 1222 | this.nodeCache.delete(id); |
| 1223 | this.nodeCache.set(id, cached); |
| 1224 | out.set(id, cached); |
| 1225 | } else { |
| 1226 | misses.push(id); |
| 1227 | } |
| 1228 | } |
| 1229 | if (misses.length === 0) return out; |
| 1230 | |
| 1231 | // Chunk under SQLite's parameter limit (default 999, raised to 32766 |
| 1232 | // in better-sqlite3 builds — chunk at 500 for safety across both |
| 1233 | // backends and to keep the query plan simple). |
| 1234 | for (let i = 0; i < misses.length; i += SQLITE_PARAM_CHUNK_SIZE) { |
| 1235 | const chunk = misses.slice(i, i + SQLITE_PARAM_CHUNK_SIZE); |
| 1236 | const placeholders = chunk.map(() => '?').join(','); |
| 1237 | const rows = this.db |
| 1238 | .prepare(`SELECT * FROM nodes WHERE id IN (${placeholders})`) |
| 1239 | .all(...chunk) as NodeRow[]; |
| 1240 | for (const row of rows) { |
| 1241 | const node = rowToNode(row); |
| 1242 | out.set(node.id, node); |
| 1243 | this.cacheNode(node); |
| 1244 | } |
| 1245 | } |
| 1246 | return out; |
| 1247 | } |
| 1248 | |
| 1249 | private getExistingNodeIds(ids: readonly string[]): Set<string> { |
| 1250 | const out = new Set<string>(); |
no test coverage detected