MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / getNodesByIds

Method getNodesByIds

src/db/queries.ts:1212–1247  ·  view source on GitHub ↗

* 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[])

Source from the content-addressed store, hash-verified

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>();

Callers 15

rnCrossPlatformEdgesFunction · 0.45
findCopybookEntriesMethod · 0.45
renderUnlinkedFunction · 0.45
buildFlowFunction · 0.45
routeRootsFunction · 0.45
buildNodeFunction · 0.45
buildMembersFunction · 0.45
buildFileFunction · 0.45
buildNodeRefsFunction · 0.45
buildScreensFunction · 0.45
attributeFunction · 0.45
mentionsOfFunction · 0.45

Calls 10

cacheNodeMethod · 0.95
rowToNodeFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
getMethod · 0.65
deleteMethod · 0.65
allMethod · 0.65
prepareMethod · 0.65
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected