Skip to content

Commit 82e8d95

Browse files
aksOpsclaude
andcommitted
fix: address code review findings from refactoring
- Remove dead parse() override in ExpressRouteDetector (never called since detect() delegates to detectWithRegex via AbstractTypeScriptDetector) - Make extractClassName() static in AbstractJavaMessagingDetector (uses no instance state) - Add cache versioning to AnalysisCache: cache_meta table tracks schema version, auto-clears stale caches on upgrade (e.g. MD5→SHA-256 hash change). Only clears and logs when upgrading from a known older version, not for fresh databases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4ab7b83 commit 82e8d95

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/main/java/io/github/randomcodespace/iq/cache/AnalysisCache.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public class AnalysisCache implements Closeable {
3838

3939
private static final Logger log = LoggerFactory.getLogger(AnalysisCache.class);
4040

41+
/** Bump when hash algorithm or schema changes to force cache invalidation. */
42+
private static final int CACHE_VERSION = 2;
43+
4144
private static final String SCHEMA_SQL = """
45+
CREATE TABLE IF NOT EXISTS cache_meta (
46+
meta_key VARCHAR PRIMARY KEY,
47+
meta_value VARCHAR NOT NULL
48+
);
49+
4250
CREATE TABLE IF NOT EXISTS files (
4351
content_hash VARCHAR PRIMARY KEY,
4452
path VARCHAR NOT NULL,
@@ -114,6 +122,35 @@ private void initDb() throws SQLException {
114122
}
115123
}
116124
}
125+
checkCacheVersion();
126+
}
127+
128+
/**
129+
* If the cache was created with a different version (e.g. MD5 vs SHA-256 hashes),
130+
* clear all cached data so files are re-analyzed with the current algorithm.
131+
*/
132+
private void checkCacheVersion() throws SQLException {
133+
String storedVersion = null;
134+
try (var ps = conn.prepareStatement("SELECT meta_value FROM cache_meta WHERE meta_key = 'version'");
135+
var rs = ps.executeQuery()) {
136+
if (rs.next()) {
137+
storedVersion = rs.getString(1);
138+
}
139+
}
140+
if (!String.valueOf(CACHE_VERSION).equals(storedVersion)) {
141+
if (storedVersion != null) {
142+
log.info("Cache version mismatch (stored={}, current={}) — clearing stale cache",
143+
storedVersion, CACHE_VERSION);
144+
try (var stmt = conn.createStatement()) {
145+
stmt.execute("DELETE FROM edges");
146+
stmt.execute("DELETE FROM nodes");
147+
stmt.execute("DELETE FROM files");
148+
}
149+
}
150+
try (var stmt = conn.createStatement()) {
151+
stmt.execute("MERGE INTO cache_meta (meta_key, meta_value) VALUES ('version', '" + CACHE_VERSION + "')");
152+
}
153+
}
117154
}
118155

119156
// --- Commit tracking ---

src/main/java/io/github/randomcodespace/iq/detector/java/AbstractJavaMessagingDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class AbstractJavaMessagingDetector extends AbstractRegexDetecto
2222
* Extract the first class name from the source text.
2323
* Returns null if no class is found.
2424
*/
25-
protected String extractClassName(String text) {
25+
protected static String extractClassName(String text) {
2626
String[] lines = text.split("\n", -1);
2727
for (String line : lines) {
2828
Matcher cm = CLASS_RE.matcher(line);

src/main/java/io/github/randomcodespace/iq/detector/typescript/ExpressRouteDetector.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public String getName() {
4444
return "typescript.express_routes";
4545
}
4646

47-
@Override
48-
protected ParseTree parse(DetectorContext ctx) {
49-
// Not called when detect() is overridden, kept for potential future use
50-
return null;
51-
}
5247

5348
@Override
5449
protected DetectorResult detectWithAst(ParseTree tree, DetectorContext ctx) {

0 commit comments

Comments
 (0)