Skip to content

Commit

Permalink
Minor refactoring of PersistentCache and JBuildGrabber.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Apr 24, 2024
1 parent d458877 commit 044fd4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
25 changes: 23 additions & 2 deletions jgrab-runner/src/main/java/com/athaydes/jgrab/Dependency.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.athaydes.jgrab;

import java.util.Comparator;
import java.util.Set;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* A single dependency on an external module.
*/
Expand Down Expand Up @@ -39,6 +42,24 @@ public static Dependency of( String declaration ) {
return new Dependency( parts[ 0 ], parts[ 1 ], parts.length == 2 ? "latest" : parts[ 2 ] );
}

public static String hashOf( Collection<Dependency> dependencies ) {
var list = new ArrayList<>( dependencies );
list.sort( COMPARATOR );

MessageDigest digest;
try {
digest = MessageDigest.getInstance( "SHA-256" );
} catch ( NoSuchAlgorithmException e ) {
throw new RuntimeException( e );
}
for ( var dependency : list ) {
digest.update( dependency.group.getBytes( UTF_8 ) );
digest.update( dependency.module.getBytes( UTF_8 ) );
digest.update( dependency.version.getBytes( UTF_8 ) );
}
return Base64.getUrlEncoder().encodeToString( digest.digest() );
}

public static Set<Dependency> parseDependencies( Stream<String> codeLines ) {
return codeLines.flatMap( line -> {
Matcher matcher = JGRAB_PATTERN.matcher( line );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.athaydes.jgrab.daemon;

import com.athaydes.jgrab.Dependency;
import com.athaydes.jgrab.JGrabHome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -10,11 +11,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -27,23 +24,12 @@ class PersistentCache {

private static final Logger logger = LoggerFactory.getLogger( PersistentCache.class );

private static final File JGRAB_HOME;

static {
String userHome = System.getProperty( "user.home" );
if ( userHome == null ) {
userHome = ".";
}

JGRAB_HOME = new File( userHome, ".jgrab" );
}

private final File cacheFile;
private final AtomicBoolean isCacheLoaded = new AtomicBoolean( false );
private final Map<Set<Dependency>, List<File>> cache = new HashMap<>();

PersistentCache() {
this( new File( JGRAB_HOME, "deps-cache" ) );
this( new File( JGrabHome.getDir(), "deps-cache" ) );
}

PersistentCache( File cacheFile ) {
Expand Down Expand Up @@ -77,7 +63,9 @@ void save() throws IOException {

try ( BufferedWriter writer = Files.newBufferedWriter( tempCacheFile.toPath(), StandardOpenOption.WRITE ) ) {
for (Map.Entry<Set<Dependency>, List<File>> entry : cache.entrySet()) {
String deps = entry.getKey().stream()
var dependencies = new ArrayList<>( entry.getKey() );
dependencies.sort( Dependency.COMPARATOR );
String deps = dependencies.stream()
.map( Dependency::canonicalNotation )
.collect( Collectors.joining( "," ) );
String libs = entry.getValue().stream()
Expand All @@ -97,7 +85,7 @@ void save() throws IOException {
Files.move( tempCacheFile.toPath(), cacheFile.toPath() );
logger.info( "Dependencies cache saved at {}", cacheFile );
} else {
logger.warn( "Unable to save cache to {}. The file could not be re-created. Cache was save at {}",
logger.warn( "Unable to save cache to {}. The file could not be re-created. Cache was saved at {}",
cacheFile, tempCacheFile );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

import java.io.File;
import java.io.FilenameFilter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
import static jbuild.artifact.file.ArtifactFileWriter.WriteMode.FLAT_DIR;

Expand Down Expand Up @@ -85,21 +82,7 @@ private File outputDirHashOf( Collection<Dependency> toGrab ) {
//noinspection ResultOfMethodCallIgnored
cacheDir.mkdirs();

var list = new ArrayList<>( toGrab );
list.sort( Dependency.COMPARATOR );

MessageDigest digest;
try {
digest = MessageDigest.getInstance( "SHA-256" );
} catch ( NoSuchAlgorithmException e ) {
throw new RuntimeException( e );
}
for ( var dependency : list ) {
digest.update( dependency.group.getBytes( UTF_8 ) );
digest.update( dependency.module.getBytes( UTF_8 ) );
digest.update( dependency.version.getBytes( UTF_8 ) );
}
return new File( cacheDir, Base64.getUrlEncoder().encodeToString( digest.digest() ) );
return new File( cacheDir, Dependency.hashOf( toGrab ) );
}

private static Set<? extends Artifact> artifacts( Collection<Dependency> toGrab ) {
Expand Down

0 comments on commit 044fd4a

Please sign in to comment.