Skip to content

Commit 9a36a37

Browse files
authored
Implement reset methods for XXHash checksum algorithms (#6762)
1 parent 0650415 commit 9a36a37

File tree

6 files changed

+159
-108
lines changed

6 files changed

+159
-108
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Implement reset() for XxHashChecksum to allow checksum reuse."
6+
}

core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/XxHashChecksum.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@
2525
@SdkInternalApi
2626
public final class XxHashChecksum implements SdkChecksum {
2727

28-
private final XXHash xxHash;
28+
private XXHash xxHash;
29+
private final ChecksumAlgorithm algorithm;
2930

3031
public XxHashChecksum(ChecksumAlgorithm algorithm) {
32+
this.algorithm = algorithm;
33+
xxHash = initializeXxHash(algorithm);
34+
}
35+
36+
private XXHash initializeXxHash(ChecksumAlgorithm algorithm) {
37+
XXHash xxHash;
3138
if (algorithm == DefaultChecksumAlgorithm.XXHASH64) {
3239
xxHash = XXHash.newXXHash64();
3340
} else if (algorithm == DefaultChecksumAlgorithm.XXHASH3) {
@@ -37,6 +44,7 @@ public XxHashChecksum(ChecksumAlgorithm algorithm) {
3744
} else {
3845
throw new UnsupportedOperationException("Unsupported algorithm: " + algorithm.algorithmId());
3946
}
47+
return xxHash;
4048
}
4149

4250
@Override
@@ -65,7 +73,8 @@ public void update(byte[] b, int off, int len) {
6573

6674
@Override
6775
public void reset() {
68-
throw new UnsupportedOperationException("mark and reset is not supported");
76+
IoUtils.closeQuietlyV2(xxHash, null);
77+
xxHash = initializeXxHash(algorithm);
6978
}
7079

7180
@Override

core/checksums/src/test/java/software/amazon/awssdk/checksums/internal/XxHashChecksumTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ void mark_throwsUnsupportedOperationException(ChecksumAlgorithm algorithm, Strin
5757

5858
@ParameterizedTest
5959
@MethodSource("xxHashAlgorithms")
60-
void reset_throwsUnsupportedOperationException(ChecksumAlgorithm algorithm, String expectedBase64) {
60+
void reset_allowsReuseOfChecksum(ChecksumAlgorithm algorithm, String expectedBase64) {
6161
SdkChecksum checksum = ChecksumProvider.crtXxHash(algorithm);
62-
assertThrows(UnsupportedOperationException.class, () -> checksum.reset());
62+
checksum.update("some other data".getBytes(StandardCharsets.UTF_8));
63+
checksum.reset();
64+
checksum.update("Hello world".getBytes(StandardCharsets.UTF_8));
65+
assertEquals(expectedBase64, BinaryUtils.toBase64(checksum.getChecksumBytes()));
6366
}
6467

6568
@ParameterizedTest

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/AsyncRequestCompressionTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public void asyncNonStreamingOperation_payloadSizeLessThanCompressionThreshold_d
120120
public void asyncStreamingOperation_compressionEnabled_compressesCorrectly() {
121121
mockAsyncHttpClient.stubNextResponse(mockResponse(), Duration.ofMillis(500));
122122

123-
mockAsyncHttpClient.setAsyncRequestBodyLength(compressedBody.length());
124123
PutOperationWithStreamingRequestCompressionRequest request =
125124
PutOperationWithStreamingRequestCompressionRequest.builder().build();
126125
asyncClient.putOperationWithStreamingRequestCompression(request, customAsyncRequestBodyWithoutContentLength(),
@@ -166,7 +165,6 @@ public void asyncStreamingOperation_compressionEnabledWithRetry_compressesCorrec
166165
mockAsyncHttpClient.stubNextResponse(mockResponse(), Duration.ofMillis(500));
167166
mockAsyncHttpClient.stubNextResponse(mockResponse(), Duration.ofMillis(500));
168167

169-
mockAsyncHttpClient.setAsyncRequestBodyLength(compressedBody.length());
170168
PutOperationWithStreamingRequestCompressionRequest request =
171169
PutOperationWithStreamingRequestCompressionRequest.builder().build();
172170
asyncClient.putOperationWithStreamingRequestCompression(request, customAsyncRequestBodyWithoutContentLength(),

0 commit comments

Comments
 (0)