Skip to content

Commit a97f6ff

Browse files
ctruedenclaude
andcommitted
Use cached JAR metadata to avoid reanalyzing them
The intent of adding this metadata was always to use it to avoid analyzing a JAR more than once over jgo's installation lifetime. However, only cache *writing* was implemented -- cache *reading* was not being done. 🤦 This commit fixes that. Co-authored-by: Claude Sonnet 4.5 <[email protected]>
1 parent 961cc6b commit a97f6ff

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

src/jgo/env/builder.py

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from ..constants import DEFAULT_JGO_CACHE
1515
from ..parse.coordinate import Coordinate
1616
from .bytecode import detect_jar_java_version
17-
from .cache import write_metadata_cache
17+
from .cache import is_cache_valid, read_metadata_cache, write_metadata_cache
1818
from .environment import Environment
1919
from .jar import (
20+
ModuleInfo,
2021
autocomplete_main_class,
2122
classify_jar,
2223
detect_main_class_from_jar,
@@ -875,45 +876,68 @@ def process_artifact(artifact, source_path):
875876
# Compute SHA256 first (needed for cache and lockfile)
876877
sha256 = compute_sha256(source_path) if source_path.exists() else None
877878

878-
# Detect minimum Java version from bytecode
879-
min_java_ver = detect_jar_java_version(source_path)
880-
881-
# Use fast module detection (no subprocess)
882-
module_info = detect_module_info(source_path)
883-
884-
# Get jar tool lazily only when we need it
885-
jar_tool = get_jar_tool_lazy()
886-
887-
if jar_tool:
888-
# Baseline jar tool available - use precise classification
889-
if module_info.is_modular:
890-
# Fast path: JAR has module-info.class or Automatic-Module-Name
891-
# Type 1: Has module-info.class (not automatic)
892-
# Type 2: Has Automatic-Module-Name (is automatic)
893-
jar_type = 2 if module_info.is_automatic else 1
894-
else:
895-
# Slow path: Need subprocess to distinguish type 3 vs 4
896-
_log.debug(
897-
f"Analyzing JAR for modularizability: {artifact.filename}"
898-
)
899-
jar_type = classify_jar(source_path, jar_tool)
900-
else:
901-
# No jar tool available - use simple module detection
902-
jar_type = None # Not classified
903-
904-
# Write to cache for next time
879+
# Try to read from metadata cache first
880+
cached_metadata = None
905881
if sha256:
906-
write_metadata_cache(
882+
cached_metadata = read_metadata_cache(
907883
artifact.groupId,
908884
artifact.artifactId,
909885
artifact.version,
910886
artifact.filename,
911887
self.cache_dir,
912-
sha256,
913-
jar_type,
914-
min_java_ver,
915-
module_info,
916888
)
889+
# Validate cache by SHA256
890+
if cached_metadata and is_cache_valid(cached_metadata, sha256):
891+
# Cache hit! Use cached values
892+
min_java_ver = cached_metadata.min_java_version
893+
jar_type = cached_metadata.jar_type
894+
module_info = ModuleInfo(**cached_metadata.module_info)
895+
_log.debug(f"Using cached metadata for {artifact.filename}")
896+
else:
897+
# Cache miss or invalid - will compute below
898+
cached_metadata = None
899+
900+
# If we didn't get valid cached data, compute it now
901+
if cached_metadata is None:
902+
# Detect minimum Java version from bytecode
903+
min_java_ver = detect_jar_java_version(source_path)
904+
905+
# Use fast module detection (no subprocess)
906+
module_info = detect_module_info(source_path)
907+
908+
# Get jar tool lazily only when we need it
909+
jar_tool = get_jar_tool_lazy()
910+
911+
if jar_tool:
912+
# Baseline jar tool available - use precise classification
913+
if module_info.is_modular:
914+
# Fast path: JAR has module-info.class or Automatic-Module-Name
915+
# Type 1: Has module-info.class (not automatic)
916+
# Type 2: Has Automatic-Module-Name (is automatic)
917+
jar_type = 2 if module_info.is_automatic else 1
918+
else:
919+
# Slow path: Need subprocess to distinguish type 3 vs 4
920+
_log.debug(
921+
f"Analyzing JAR for modularizability: {artifact.filename}"
922+
)
923+
jar_type = classify_jar(source_path, jar_tool)
924+
else:
925+
# No jar tool available - use simple module detection
926+
jar_type = None # Not classified
927+
928+
# Write to cache for next time
929+
if sha256:
930+
write_metadata_cache(
931+
artifact.groupId,
932+
artifact.artifactId,
933+
artifact.version,
934+
artifact.filename,
935+
self.cache_dir,
936+
sha256,
937+
jar_type,
938+
min_java_ver,
939+
module_info,
940+
)
917941

918942
# Determine target directory based on jar_type
919943
if jar_type is not None:

0 commit comments

Comments
 (0)