Skip to content

Commit b6a71b7

Browse files
authored
perf: get base64 value for BYTES columns directly from protobuf value (GoogleCloudPlatform#3112)
Get the base64 string for BYTES columns directly from the unerlying protobuf value when this is available.
1 parent d26091d commit b6a71b7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/main/java/com/google/cloud/spanner/pgadapter/parsers/BinaryParser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.api.core.InternalApi;
1818
import com.google.cloud.ByteArray;
19+
import com.google.cloud.spanner.ProtobufResultSet;
1920
import com.google.cloud.spanner.ResultSet;
2021
import com.google.cloud.spanner.SpannerExceptionFactory;
2122
import com.google.cloud.spanner.Statement;
@@ -108,7 +109,7 @@ public static byte[] convertToPG(
108109
DataFormat format) {
109110
int bufferSize = sessionState.getBinaryConversionBufferSize();
110111
try {
111-
String base64 = resultSet.getValue(position).getAsString();
112+
String base64 = getBase64(resultSet, position);
112113
switch (format) {
113114
case SPANNER:
114115
case POSTGRESQL_BINARY:
@@ -128,7 +129,7 @@ public static byte[] convertToPG(
128129
}
129130
return null;
130131
case POSTGRESQL_TEXT:
131-
return bytesToHex(resultSet.getBytes(position).toByteArray());
132+
return bytesToHex(Base64.getDecoder().decode(base64));
132133
default:
133134
throw new IllegalArgumentException("unknown data format: " + format);
134135
}
@@ -137,6 +138,14 @@ public static byte[] convertToPG(
137138
}
138139
}
139140

141+
static String getBase64(ResultSet resultSet, int column) {
142+
if (resultSet instanceof ProtobufResultSet
143+
&& ((ProtobufResultSet) resultSet).canGetProtobufValue(column)) {
144+
return ((ProtobufResultSet) resultSet).getProtobufValue(column).getStringValue();
145+
}
146+
return resultSet.getValue(column).getAsString();
147+
}
148+
140149
static int copy(int length, InputStream from, DataOutputStream to, int bufferSize)
141150
throws IOException {
142151
byte[] buf = new byte[Math.min(length, bufferSize)];

0 commit comments

Comments
 (0)