Class AiResultCache

java.lang.Object
org.egothor.methodatlas.AiResultCache

public final class AiResultCache extends Object
In-memory cache of AI classification results loaded from a previous MethodAtlas scan output.

Cache entries are keyed by content_hash — the per-class SHA-256 fingerprint produced by the -content-hash flag. A cache hit means the class source is identical to the previous run; the stored AI classification is reused without an API call.

When the source CSV was produced without -content-hash, the content_hash column is absent and no entries are loaded. All lookups return empty and the AI is called normally for every class.

Instances are obtained via load(Path) or the no-op empty() singleton.

See Also:
  • Method Details

    • empty

      public static AiResultCache empty()
      Returns an empty cache that always produces misses.
    • load

      public static AiResultCache load(Path csvPath) throws IOException
      Loads a cache from a MethodAtlas CSV output file.

      Only rows with a non-empty content_hash value and a non-null ai_security_relevant column (AI was enabled for that scan) are included. Rows missing either field are silently skipped.

      Parameters:
      csvPath - path to a MethodAtlas CSV produced with -content-hash -ai
      Returns:
      loaded cache; never null
      Throws:
      IOException - if the file cannot be read
    • lookup

      public Optional<AiClassSuggestion> lookup(String contentHash)
      Returns the cached classification for the class with the given content hash, or empty when the hash is absent from the cache.
      Parameters:
      contentHash - SHA-256 fingerprint of the class source, or null
      Returns:
      cached suggestion, or empty on a miss or null hash
    • isActive

      public boolean isActive()
      Returns true when this cache contains at least one entry.

      When false, content hashes do not need to be computed for lookups because all results would be misses regardless.

    • hits

      public int hits()
      Returns the number of successful cache lookups so far.
    • misses

      public int misses()
      Returns the number of unsuccessful cache lookups so far.