diff --git a/src/main/java/com/bazel_diff/BazelClient.java b/src/main/java/com/bazel_diff/BazelClient.java index 2ae2824..2b91882 100644 --- a/src/main/java/com/bazel_diff/BazelClient.java +++ b/src/main/java/com/bazel_diff/BazelClient.java @@ -12,15 +12,15 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; -import java.util.HashSet; +import java.util.HashMap; import java.util.List; -import java.util.Set; +import java.util.Map; import java.util.stream.Collectors; import java.util.Arrays; interface BazelClient { List queryAllTargets() throws IOException; - Set queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException; + Map queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException; } class BazelClientImpl implements BazelClient { @@ -54,12 +54,12 @@ public List queryAllTargets() throws IOException { } @Override - public Set queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException { + public Map queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException { return processBazelSourcefileTargets(performBazelQuery("kind('source file', deps(//...))"), true); } - private Set processBazelSourcefileTargets(List targets, Boolean readSourcefileTargets) throws IOException, NoSuchAlgorithmException { - Set sourceTargets = new HashSet<>(); + private Map processBazelSourcefileTargets(List targets, Boolean readSourcefileTargets) throws IOException, NoSuchAlgorithmException { + Map sourceTargets = new HashMap<>(); for (Build.Target target : targets) { Build.SourceFile sourceFile = target.getSourceFile(); if (sourceFile != null) { @@ -73,7 +73,7 @@ private Set processBazelSourcefileTargets(List hashAllBazelTargetsAndSourcefiles(Set seedFilepaths) throws IOException, NoSuchAlgorithmException { - Set bazelSourcefileTargets = bazelClient.queryAllSourcefileTargets(); + Map bazelSourcefileTargets = bazelClient.queryAllSourcefileTargets(); return hashAllTargets(createSeedForFilepaths(seedFilepaths), bazelSourcefileTargets); } @@ -47,7 +47,7 @@ public Set getImpactedTargets( private byte[] createDigestForTarget( BazelTarget target, Map allRulesMap, - Set bazelSourcefileTargets, + Map bazelSourcefileTargets, Map ruleHashes, byte[] seedHash ) throws NoSuchAlgorithmException { @@ -73,7 +73,7 @@ private byte[] createDigestForRule( BazelRule rule, Map allRulesMap, Map ruleHashes, - Set bazelSourcefileTargets, + Map bazelSourcefileTargets, byte[] seedHash ) throws NoSuchAlgorithmException { byte[] existingByteArray = ruleHashes.get(rule.getName()); @@ -122,14 +122,10 @@ private byte[] createSeedForFilepaths(Set seedFilepaths) throws IOExceptio private byte[] getDigestForSourceTargetName( String sourceTargetName, - Set bazelSourcefileTargets + Map bazelSourcefileTargets ) throws NoSuchAlgorithmException { - for (BazelSourceFileTarget sourceFileTarget : bazelSourcefileTargets) { - if (sourceFileTarget.getName().equals(sourceTargetName)) { - return sourceFileTarget.getDigest(); - } - } - return null; + BazelSourceFileTarget target = bazelSourcefileTargets.get(sourceTargetName); + return target != null ? target.getDigest() : null; } private String convertByteArrayToString(byte[] bytes) { @@ -150,7 +146,7 @@ private String getNameForTarget(BazelTarget target) { return null; } - private Map hashAllTargets(byte[] seedHash, Set bazelSourcefileTargets) throws IOException, NoSuchAlgorithmException { + private Map hashAllTargets(byte[] seedHash, Map bazelSourcefileTargets) throws IOException, NoSuchAlgorithmException { List allTargets = bazelClient.queryAllTargets(); Map targetHashes = new HashMap<>(); Map ruleHashes = new HashMap<>();