Skip to content

Commit

Permalink
Adding ability for the cache for dependencies to re-gen
Browse files Browse the repository at this point in the history
* When the pom file has changed (by comparing a hash of the file) we
  will regen the cache.
* this should work for all the cases that have a pom
* there is a TODO to add this behavior for gradle, but that didn't ever
  seem to be cached.

Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley committed Feb 18, 2025
1 parent 175fd3f commit 606d2ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -93,6 +94,8 @@ func (p *javaServiceClient) findGradleBuild() string {
func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][]*provider.Dep, error) {
p.log.V(4).Info("running dependency analysis")

// TODO: shawn-hurley does not appear that this is returning early if there is a cache.
// We should add these to a cache with the hash of the gradle build file.
if p.GetBuildTool() == gradle {
p.log.V(2).Info("gradle found - retrieving dependencies")
m := map[uri.URI][]*provider.Dep{}
Expand All @@ -109,11 +112,29 @@ func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][]
return m, err
}

p.depsMutex.RLock()
val := p.depsCache
p.depsMutex.RUnlock()
if val != nil {
return val, nil
if pomFile := p.findPom(); pomFile != "" {
// Read pom and create a hash.
// if pom hash and depCache return cache
file, err := os.Open(pomFile)
if err != nil {
p.log.Error(err, "unable to open the pom file", "pom path", pomFile)
return nil, err
}
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
p.log.Error(err, "unable to copy file to hash", "pom path", pomFile)
return nil, err
}
hashString := string(hash.Sum(nil))
if p.depsFileHash != nil && *p.depsFileHash == hashString && p.depsCache != nil {
p.depsMutex.RLock()
val := p.depsCache
p.depsMutex.RUnlock()
if val != nil {
return val, nil
}
}
p.depsFileHash = &hashString
}

var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type javaServiceClient struct {
mvnSettingsFile string
globalSettings string
depsMutex sync.RWMutex
depsFileHash *string
depsCache map[uri.URI][]*provider.Dep
depsLocationCache map[string]int
includedPaths []string
Expand Down

0 comments on commit 606d2ea

Please sign in to comment.