Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Jan 15, 2024
1 parent 92ca975 commit 416c506
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.connection.ConnectionOptions.RETRY_ABORTS_INTERNALLY_PROPERTY_NAME;

import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.AbortedException;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerException;
Expand All @@ -40,9 +37,6 @@
import com.google.common.hash.Hashing;
import com.google.common.hash.PrimitiveSink;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -71,15 +65,12 @@
*/
@VisibleForTesting
class ChecksumResultSet extends ReplaceableForwardingResultSet implements RetriableStatement {
private static final boolean USE_JVM_HASH = true;

private final ReadWriteTransaction transaction;
private volatile long numberOfNextCalls;
private final ParsedStatement statement;
private final AnalyzeMode analyzeMode;
private final QueryOption[] options;
private final ChecksumResultSet.ChecksumCalculator checksumCalculator =
new ChecksumCalculator(USE_JVM_HASH);
private final ChecksumResultSet.ChecksumCalculator checksumCalculator = new ChecksumCalculator();

ChecksumResultSet(
ReadWriteTransaction transaction,
Expand Down Expand Up @@ -140,8 +131,7 @@ HashCode getChecksum() {
@Override
public void retry(AbortedException aborted) throws AbortedException {
// Execute the same query and consume the result set to the same point as the original.
ChecksumResultSet.ChecksumCalculator newChecksumCalculator =
new ChecksumCalculator(USE_JVM_HASH);
ChecksumResultSet.ChecksumCalculator newChecksumCalculator = new ChecksumCalculator();
ResultSet resultSet = null;
long counter = 0L;
try {
Expand Down Expand Up @@ -197,65 +187,19 @@ public void retry(AbortedException aborted) throws AbortedException {
private static final class ChecksumCalculator {
private static final HashFunction SHA256_FUNCTION = Hashing.sha256();

private boolean isFirst = true;

private HashCode currentChecksum;

private final MessageDigest digest;

private ChecksumCalculator(boolean useJvmHash) {
// if (useJvmHash) {
// try {
// this.digest = MessageDigest.getInstance("SHA-256");
// } catch (NoSuchAlgorithmException exception) {
// // Note: This should never happen, as SHA-256 is standard supported on all JVM versions 8
// // and higher.
// throw SpannerExceptionFactory.newSpannerException(
// ErrorCode.INTERNAL,
// "SHA-256 is not supported on this JVM. Please set "
// + RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
// + "=false in the connection string.");
// }
// } else {
digest = null;
// }
}

private void calculateNextChecksum(Struct row) {
// if (digest == null) {
// Hasher hasher = SHA256_FUNCTION.newHasher();
// if (currentChecksum != null) {
// hasher.putBytes(currentChecksum.asBytes());
// }
// hasher.putObject(row, StructFunnel.INSTANCE);
// currentChecksum = hasher.hash();
// } else {
// if (currentChecksum != null) {
// currentChecksum = null;
// }
// for (int col = 0; col < row.getColumnCount(); col++) {
// if (isFirst) {
// digest.update(row.getColumnType(col).toString().getBytes(StandardCharsets.UTF_8));
// }
// digest.update(row.getValue(col).getAsString().getBytes(StandardCharsets.UTF_8));
// }
// isFirst = false;
// }
Hasher hasher = SHA256_FUNCTION.newHasher();
if (currentChecksum != null) {
hasher.putBytes(currentChecksum.asBytes());
}
hasher.putObject(row, StructFunnel.INSTANCE);
currentChecksum = hasher.hash();
}

private HashCode getChecksum() {
return HashCode.fromInt(1);
// if (digest != null) {
// if (currentChecksum == null) {
// try {
// MessageDigest clone = (MessageDigest) digest.clone();
// currentChecksum = HashCode.fromBytes(clone.digest());
// } catch (CloneNotSupportedException cloneNotSupportedException) {
// throw SpannerExceptionFactory.asSpannerException(cloneNotSupportedException);
// }
// }
// }
// return currentChecksum;
return currentChecksum;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,19 +772,17 @@ public ApiFuture<Void> commitAsync(CallType callType) {
*/
<T> T runWithRetry(Callable<T> callable) throws SpannerException {
while (true) {
// synchronized (abortedLock) {
checkAborted();
try {
checkRolledBackToSavepoint();
return callable.call();
} catch (final AbortedException aborted) {
handleAborted(aborted);
} catch (SpannerException e) {
throw e;
} catch (Exception e) {
throw SpannerExceptionFactory.asSpannerException(e);
}
// }
checkAborted();
try {
checkRolledBackToSavepoint();
return callable.call();
} catch (final AbortedException aborted) {
handleAborted(aborted);
} catch (SpannerException e) {
throw e;
} catch (Exception e) {
throw SpannerExceptionFactory.asSpannerException(e);
}
}
}

Expand Down

0 comments on commit 416c506

Please sign in to comment.