Skip to content

refactor: share chunked statement-cache primitive between builder and structure#1890

Open
carlos-alm wants to merge 1 commit into
fix/issue-1766-defineproperty-kind-filter-parityfrom
fix/issue-1767-structure-chunked-stmt-cache
Open

refactor: share chunked statement-cache primitive between builder and structure#1890
carlos-alm wants to merge 1 commit into
fix/issue-1766-defineproperty-kind-filter-parityfrom
fix/issue-1767-structure-chunked-stmt-cache

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • batchUpdateRoles (features/structure.ts) duplicated the same chunk-size-keyed prepared-statement cache shape already deduped in builder/helpers.ts's getOrCreateBatchStmt (used by getNodeStmt/getEdgeStmt/getExportStmt).
  • Checked import-direction precedent before deciding where the shared primitive belongs: zero files outside domain/graph/builder/ import directly from files inside it (everything goes through the domain/graph/builder.ts barrel, which only re-exports the builder's public API) — a chunk-keyed statement cache has nothing to do with "graph building" as a domain concern, so extending that barrel would be a boundary exception for a domain-agnostic utility. shared/ already hosts the single-statement analog (db/repository/cached-stmt.ts) and other cross-cutting DB/type utilities.
  • Extracted src/shared/chunked-stmt-cache.ts (getOrCreateChunkStmt + the per-db WeakMap wrapper getOrCreatePerDbChunkStmt), refactored builder/helpers.ts's existing usage to go through it too, and adopted it in batchUpdateRoles — one real implementation now, not two similar ones.

Closes #1767

Test plan

  • npm test — full suite green (3542 passed, 0 failed)
  • npm run lint / npm run typecheck — clean
  • Same SQL, same chunk sizes, same caching scope (builder's WeakMaps stay persisted across calls; structure's cache stays a fresh per-call Map) — pure refactor, no behavior change
  • codegraph diff-impact --staged -T — 6 functions touched, 26 transitive callers, consistent with an internal-only refactor with no public signature changes

Stacked on #1889 (base branch fix/issue-1766-defineproperty-kind-filter-parity) — only the shared/builder/structure diff is this issue's change.

… structure

batchUpdateRoles in features/structure.ts re-implemented the same
chunk-size-keyed prepared-statement cache already used by
getNodeStmt/getEdgeStmt/getExportStmt in
domain/graph/builder/helpers.ts (check cache by chunk size, prepare +
cache on miss) — the SQL and cache scope differed, but the caching
mechanics were identical.

Extracted the shared mechanics into src/shared/chunked-stmt-cache.ts:
getOrCreateChunkStmt (flat Map<chunkSize, stmt>) and its per-database
wrapper getOrCreatePerDbChunkStmt (WeakMap<db, Map<chunkSize, stmt>>).
builder/helpers.ts's three statement getters now delegate to
getOrCreatePerDbChunkStmt against their existing WeakMap caches (same
persisted-across-calls semantics as before); batchUpdateRoles now
calls getOrCreateChunkStmt against a Map created fresh per call (same
per-call-only lifetime as before). No SQL, chunking, or caching-scope
changes on either side — one implementation instead of two copies of
the same shape.

Placed the primitive in shared/ rather than exporting it from
builder/helpers.ts: nothing outside domain/graph/builder/ currently
imports from that directory directly — index.ts, cli/commands/build.ts,
and features/branch-compare.ts all go through the
domain/graph/builder.ts barrel, which only re-exports the builder's
public API (buildGraph, collectFiles, etc). Adding a generic,
domain-agnostic statement-cache helper to that barrel's surface for a
features/ consumer would be the first such exception; shared/ already
hosts this class of cross-cutting, domain-free utility (paginate.ts,
globs.ts, sleep.ts).

docs check acknowledged: pure internal dedup refactor, no new
feature, language, command, or architecture surface to document.

Fixes #1767

Impact: 6 functions changed, 26 affected
@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extracts a duplicate chunk-size-keyed prepared-statement cache pattern into src/shared/chunked-stmt-cache.ts, then wires both builder/helpers.ts and features/structure.ts through it. It is a pure internal refactor — no SQL, chunk sizes, or external contracts change.

  • New shared primitive: getOrCreateChunkStmt (simple Map variant) and getOrCreatePerDbChunkStmt (per-database WeakMap wrapper) replace identical inline cache-check/prepare/store logic that existed independently in both files.
  • helpers.ts: Drops the now-redundant local getOrCreateBatchStmt; three callers (getNodeStmt, getEdgeStmt, getExportStmt) delegate to the shared getOrCreatePerDbChunkStmt with the same persistent WeakMap semantics.
  • structure.ts: batchUpdateRoles now delegates to getOrCreateChunkStmt, preserving the original per-call fresh Map scope; imports SqliteStatement as DbSqliteStatement to avoid collision with the file-local minimal interface SqliteStatement.

Confidence Score: 5/5

Safe to merge — no SQL, chunk sizes, or public API signatures change; behavior is identical in both call sites.

All three changed files are consistent with each other. The two exported functions compose cleanly, types from types.ts propagate through without gaps, and the distinction between the persistent per-db WeakMap scope (builder) and the fresh per-call Map scope (structure) is correctly preserved. No logic changes exist that could introduce regressions.

No files require special attention.

Important Files Changed

Filename Overview
src/shared/chunked-stmt-cache.ts New shared module exporting two well-documented cache helpers; types align with types.ts throughout and delegation from getOrCreatePerDbChunkStmt to getOrCreateChunkStmt is correct.
src/domain/graph/builder/helpers.ts Drops local getOrCreateBatchStmt; three call sites updated to getOrCreatePerDbChunkStmt with identical semantics. Module-level WeakMap caches and their types are unchanged.
src/features/structure.ts batchUpdateRoles now uses getOrCreateChunkStmt; per-call Map scope is preserved. DbSqliteStatement alias correctly resolves the name collision with the file-local interface SqliteStatement.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[builder/helpers.ts\ngetNodeStmt / getEdgeStmt / getExportStmt] -->|calls| B[getOrCreatePerDbChunkStmt]
    C[features/structure.ts\nbatchUpdateRoles] -->|calls| D[getOrCreateChunkStmt]
    B -->|delegates to| D
    D -->|cache miss: db.prepare| E[(SQLite DB)]
    B -->|cache miss: creates per-db Map| F[(WeakMap keyed by db)]
    F --> D
    subgraph shared/chunked-stmt-cache.ts
        B
        D
    end
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[builder/helpers.ts\ngetNodeStmt / getEdgeStmt / getExportStmt] -->|calls| B[getOrCreatePerDbChunkStmt]
    C[features/structure.ts\nbatchUpdateRoles] -->|calls| D[getOrCreateChunkStmt]
    B -->|delegates to| D
    D -->|cache miss: db.prepare| E[(SQLite DB)]
    B -->|cache miss: creates per-db Map| F[(WeakMap keyed by db)]
    F --> D
    subgraph shared/chunked-stmt-cache.ts
        B
        D
    end
Loading

Reviews (1): Last reviewed commit: "refactor: share chunked statement-cache ..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

6 functions changed26 callers affected across 6 files

  • getNodeStmt in src/domain/graph/builder/helpers.ts:361 (15 transitive callers)
  • getEdgeStmt in src/domain/graph/builder/helpers.ts:371 (17 transitive callers)
  • getExportStmt in src/domain/graph/builder/helpers.ts:429 (15 transitive callers)
  • batchUpdateRoles in src/features/structure.ts:536 (3 transitive callers)
  • getOrCreateChunkStmt in src/shared/chunked-stmt-cache.ts:21 (12 transitive callers)
  • getOrCreatePerDbChunkStmt in src/shared/chunked-stmt-cache.ts:46 (16 transitive callers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant