Skip to content

Commit

Permalink
Minor performance improvements (#4637)
Browse files Browse the repository at this point in the history
* Minor performance improvements

* Avoid lower case when looking up the algorithm
  • Loading branch information
sugmanue authored Oct 26, 2023
1 parent b716347 commit 538c477
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public enum Algorithm {
SHA1("sha1", 28),
;

private static final Map<String, Algorithm> VALUE_MAP = EnumUtils.uniqueIndex(Algorithm.class, Algorithm::toString);
private static final Map<String, Algorithm> VALUE_MAP = EnumUtils.uniqueIndex(Algorithm.class,
a -> StringUtils.upperCase(a.value));

private final String value;
private final int length;
Expand All @@ -46,12 +47,17 @@ public static Algorithm fromValue(String value) {
if (value == null) {
return null;
}
String normalizedValue = StringUtils.lowerCase(value);
Algorithm algorithm = VALUE_MAP.get(normalizedValue);
// The clients will send the algorithm name in all upper case
// try using that name directly and if not found then normalize
// it and try again.
Algorithm algorithm = VALUE_MAP.get(value);
if (algorithm == null) {
throw new IllegalArgumentException("The provided value is not a valid algorithm " + value);
String normalizedValue = StringUtils.upperCase(value);
algorithm = VALUE_MAP.get(normalizedValue);
if (algorithm == null) {
throw new IllegalArgumentException("The provided value is not a valid algorithm " + value);
}
}

return algorithm;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

package software.amazon.awssdk.core.internal.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.checksums.Algorithm;
import software.amazon.awssdk.core.checksums.ChecksumSpecs;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.interceptor.trait.HttpChecksum;
import software.amazon.awssdk.utils.StringUtils;

/**
* Class to resolve the different Checksums specs from ExecutionAttributes.
Expand All @@ -37,8 +35,11 @@ private HttpChecksumResolver() {
}

public static ChecksumSpecs getResolvedChecksumSpecs(ExecutionAttributes executionAttributes) {
return Optional.ofNullable(executionAttributes.getAttribute(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS))
.orElseGet(() -> resolveChecksumSpecs(executionAttributes));
ChecksumSpecs checksumSpecs = executionAttributes.getAttribute(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS);
if (checksumSpecs != null) {
return checksumSpecs;
}
return resolveChecksumSpecs(executionAttributes);
}

public static ChecksumSpecs resolveChecksumSpecs(ExecutionAttributes executionAttributes) {
Expand All @@ -47,9 +48,8 @@ public static ChecksumSpecs resolveChecksumSpecs(ExecutionAttributes executionAt
return null;
}
boolean hasRequestValidation = hasRequestValidationMode(httpChecksumTraitInOperation);
String checksumHeaderName = httpChecksumTraitInOperation.requestAlgorithm() != null ?
HttpChecksumUtils.httpChecksumHeader(httpChecksumTraitInOperation.requestAlgorithm())
: null;
String requestAlgorithm = httpChecksumTraitInOperation.requestAlgorithm();
String checksumHeaderName = requestAlgorithm != null ? HttpChecksumUtils.httpChecksumHeader(requestAlgorithm) : null;
List<Algorithm> responseValidationAlgorithms = getResponseValidationAlgorithms(httpChecksumTraitInOperation);

return ChecksumSpecs.builder()
Expand All @@ -67,16 +67,14 @@ private static boolean hasRequestValidationMode(HttpChecksum httpChecksum) {
}

private static List<Algorithm> getResponseValidationAlgorithms(HttpChecksum httpChecksumTraitInOperation) {
List<Algorithm> responseValidationAlgorithms = null;

if (httpChecksumTraitInOperation.responseAlgorithms() != null &&
!httpChecksumTraitInOperation.responseAlgorithms().isEmpty()) {
responseValidationAlgorithms =
httpChecksumTraitInOperation.responseAlgorithms().stream().filter(StringUtils::isNotBlank)
.map(StringUtils::trim)
.map(Algorithm::fromValue)
.collect(Collectors.toList());
List<String> responseAlgorithms = httpChecksumTraitInOperation.responseAlgorithms();
if (responseAlgorithms != null && !responseAlgorithms.isEmpty()) {
List<Algorithm> responseValidationAlgorithms = new ArrayList<>(responseAlgorithms.size());
for (String algorithmName : responseAlgorithms) {
responseValidationAlgorithms.add(Algorithm.fromValue(algorithmName));
}
return responseValidationAlgorithms;
}
return responseValidationAlgorithms;
return null;
}
}

0 comments on commit 538c477

Please sign in to comment.