diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/CommitResponse.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/CommitResponse.java index 43179972b9b..85975e10f6c 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/CommitResponse.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/CommitResponse.java @@ -19,6 +19,7 @@ import com.google.cloud.Timestamp; import com.google.common.base.Preconditions; import java.util.Objects; +import javax.annotation.Nullable; /** Represents a response from a commit operation. */ public class CommitResponse { @@ -41,6 +42,18 @@ public Timestamp getCommitTimestamp() { return Timestamp.fromProto(proto.getCommitTimestamp()); } + /** + * Returns a {@link Timestamp} representing the timestamp at which all reads in the transaction + * ran at, if the transaction ran at repeatable read isolation in internal test environments, and + * otherwise returns null. + */ + public @Nullable Timestamp getSnapshotTimestamp() { + if (proto.getSnapshotTimestamp() == com.google.protobuf.Timestamp.getDefaultInstance()) { + return null; + } + return Timestamp.fromProto(proto.getSnapshotTimestamp()); + } + /** * @return true if the {@link CommitResponse} includes {@link CommitStats} */ diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/CommitResponseTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/CommitResponseTest.java index 26905c749d0..6ac22a28937 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/CommitResponseTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/CommitResponseTest.java @@ -101,4 +101,22 @@ public void testHasCommitStats() { CommitResponse responseWithCommitStats = new CommitResponse(protoWithCommitStats); assertTrue(responseWithCommitStats.hasCommitStats()); } + + @Test + public void testGetSnapshotTimestamp() { + com.google.spanner.v1.CommitResponse protoWithoutSnapshotTimestamp = + com.google.spanner.v1.CommitResponse.getDefaultInstance(); + CommitResponse responseWithoutSnapshotTimestamp = + new CommitResponse(protoWithoutSnapshotTimestamp); + assertEquals(null, responseWithoutSnapshotTimestamp.getSnapshotTimestamp()); + + com.google.protobuf.Timestamp timestamp = + com.google.protobuf.Timestamp.newBuilder().setSeconds(123L).setNanos(456).build(); + com.google.spanner.v1.CommitResponse protoWithSnapshotTimestamp = + com.google.spanner.v1.CommitResponse.newBuilder().setSnapshotTimestamp(timestamp).build(); + CommitResponse responseWithSnapshotTimestamp = new CommitResponse(protoWithSnapshotTimestamp); + assertEquals( + Timestamp.ofTimeSecondsAndNanos(123L, 456), + responseWithSnapshotTimestamp.getSnapshotTimestamp()); + } }