Skip to content

refactor: replace individual variables with ConnectionProperty #3322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.parseTimeUnit;
import static com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.toChronoUnit;

import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Options.RpcPriority;
import com.google.cloud.spanner.SpannerException;
Expand All @@ -27,16 +32,17 @@
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.protobuf.Duration;
import com.google.protobuf.util.Durations;
import com.google.spanner.v1.DirectedReadOptions;
import com.google.spanner.v1.RequestOptions.Priority;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -172,8 +178,11 @@ public Integer convert(String value) {
}
}

/** Converter from string to {@link Duration}. */
/** Converter from string to protobuf {@link Duration}. */
static class DurationConverter implements ClientSideStatementValueConverter<Duration> {
static final DurationConverter INSTANCE =
new DurationConverter("('(\\d{1,19})(s|ms|us|ns)'|(^\\d{1,19})|NULL)");

private final Pattern allowedValues;

public DurationConverter(String allowedValues) {
Expand All @@ -193,13 +202,16 @@ public Duration convert(String value) {
Matcher matcher = allowedValues.matcher(value);
if (matcher.find()) {
if (matcher.group(0).equalsIgnoreCase("null")) {
return Durations.fromNanos(0L);
return Duration.ofMillis(0L);
} else {
Duration duration =
ReadOnlyStalenessUtil.createDuration(
Long.parseLong(matcher.group(1)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(2)));
if (duration.getSeconds() == 0L && duration.getNanos() == 0) {
Duration duration;
if (matcher.group(3) != null) {
duration = Duration.ofMillis(Long.parseLong(matcher.group(3)));
} else {
ChronoUnit unit = toChronoUnit(parseTimeUnit(matcher.group(2)));
duration = Duration.of(Long.parseLong(matcher.group(1)), unit);
}
if (duration.isZero()) {
return null;
}
return duration;
Expand Down Expand Up @@ -231,18 +243,14 @@ public Duration convert(String value) {
if (matcher.find()) {
Duration duration;
if (matcher.group(0).equalsIgnoreCase("default")) {
return Durations.fromNanos(0L);
return Duration.ofMillis(0L);
} else if (matcher.group(2) == null) {
duration =
ReadOnlyStalenessUtil.createDuration(
Long.parseLong(matcher.group(0)), TimeUnit.MILLISECONDS);
duration = Duration.ofMillis(Long.parseLong(matcher.group(0)));
} else {
duration =
ReadOnlyStalenessUtil.createDuration(
Long.parseLong(matcher.group(1)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(2)));
ChronoUnit unit = toChronoUnit(parseTimeUnit(matcher.group(2)));
duration = Duration.of(Long.parseLong(matcher.group(1)), unit);
}
if (duration.getSeconds() == 0L && duration.getNanos() == 0) {
if (duration.isZero()) {
return null;
}
return duration;
Expand All @@ -254,6 +262,10 @@ public Duration convert(String value) {
/** Converter from string to possible values for read only staleness ({@link TimestampBound}). */
static class ReadOnlyStalenessConverter
implements ClientSideStatementValueConverter<TimestampBound> {
static final ReadOnlyStalenessConverter INSTANCE =
new ReadOnlyStalenessConverter(
"'((STRONG)|(MIN_READ_TIMESTAMP)[\\t ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(READ_TIMESTAMP)[\\t ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(MAX_STALENESS)[\\t ]+((\\d{1,19})(s|ms|us|ns))|(EXACT_STALENESS)[\\t ]+((\\d{1,19})(s|ms|us|ns)))'");

private final Pattern allowedValues;
private final CaseInsensitiveEnumMap<Mode> values = new CaseInsensitiveEnumMap<>(Mode.class);

Expand Down Expand Up @@ -297,7 +309,7 @@ public TimestampBound convert(String value) {
try {
return TimestampBound.ofExactStaleness(
Long.parseLong(matcher.group(groupIndex + 2)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(groupIndex + 3)));
parseTimeUnit(matcher.group(groupIndex + 3)));
} catch (IllegalArgumentException e) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT, e.getMessage());
Expand All @@ -306,7 +318,7 @@ public TimestampBound convert(String value) {
try {
return TimestampBound.ofMaxStaleness(
Long.parseLong(matcher.group(groupIndex + 2)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(groupIndex + 3)));
parseTimeUnit(matcher.group(groupIndex + 3)));
} catch (IllegalArgumentException e) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT, e.getMessage());
Expand Down Expand Up @@ -539,7 +551,12 @@ public PgTransactionMode convert(String value) {
}
}

/** Converter for converting strings to {@link RpcPriority} values. */
/**
* Converter for converting strings to {@link Priority} values.
*
* @deprecated Use {@link RpcPriorityEnumConverter} instead.
*/
@Deprecated
static class RpcPriorityConverter implements ClientSideStatementValueConverter<Priority> {
private final CaseInsensitiveEnumMap<Priority> values =
new CaseInsensitiveEnumMap<>(Priority.class);
Expand Down Expand Up @@ -569,12 +586,43 @@ public Priority convert(String value) {
}
}

/** Converter for converting strings to {@link RpcPriority} values. */
static class RpcPriorityEnumConverter implements ClientSideStatementValueConverter<RpcPriority> {
static final RpcPriorityEnumConverter INSTANCE = new RpcPriorityEnumConverter();

private final CaseInsensitiveEnumMap<RpcPriority> values =
new CaseInsensitiveEnumMap<>(RpcPriority.class);

private RpcPriorityEnumConverter() {}

/** Constructor needed for reflection. */
public RpcPriorityEnumConverter(String allowedValues) {}

@Override
public Class<RpcPriority> getParameterClass() {
return RpcPriority.class;
}

@Override
public RpcPriority convert(String value) {
if ("null".equalsIgnoreCase(value)) {
return RpcPriority.UNSPECIFIED;
}
return values.get(value);
}
}

/** Converter for converting strings to {@link SavepointSupport} values. */
static class SavepointSupportConverter
implements ClientSideStatementValueConverter<SavepointSupport> {
static final SavepointSupportConverter INSTANCE = new SavepointSupportConverter();

private final CaseInsensitiveEnumMap<SavepointSupport> values =
new CaseInsensitiveEnumMap<>(SavepointSupport.class);

private SavepointSupportConverter() {}

/** Constructor needed for reflection. */
public SavepointSupportConverter(String allowedValues) {}

@Override
Expand All @@ -588,6 +636,30 @@ public SavepointSupport convert(String value) {
}
}

/** Converter for converting strings to {@link DdlInTransactionMode} values. */
static class DdlInTransactionModeConverter
implements ClientSideStatementValueConverter<DdlInTransactionMode> {
static final DdlInTransactionModeConverter INSTANCE = new DdlInTransactionModeConverter();

private final CaseInsensitiveEnumMap<DdlInTransactionMode> values =
new CaseInsensitiveEnumMap<>(DdlInTransactionMode.class);

private DdlInTransactionModeConverter() {}

/** Constructor needed for reflection. */
public DdlInTransactionModeConverter(String allowedValues) {}

@Override
public Class<DdlInTransactionMode> getParameterClass() {
return DdlInTransactionMode.class;
}

@Override
public DdlInTransactionMode convert(String value) {
return values.get(value);
}
}

static class ExplainCommandConverter implements ClientSideStatementValueConverter<String> {
@Override
public Class<String> getParameterClass() {
Expand Down Expand Up @@ -648,4 +720,71 @@ public String convert(String filePath) {
return filePath;
}
}

static class CredentialsProviderConverter
implements ClientSideStatementValueConverter<CredentialsProvider> {
static final CredentialsProviderConverter INSTANCE = new CredentialsProviderConverter();

private CredentialsProviderConverter() {}

@Override
public Class<CredentialsProvider> getParameterClass() {
return CredentialsProvider.class;
}

@Override
public CredentialsProvider convert(String credentialsProviderName) {
if (!Strings.isNullOrEmpty(credentialsProviderName)) {
try {
Class<? extends CredentialsProvider> clazz =
(Class<? extends CredentialsProvider>) Class.forName(credentialsProviderName);
Constructor<? extends CredentialsProvider> constructor = clazz.getDeclaredConstructor();
return constructor.newInstance();
} catch (ClassNotFoundException classNotFoundException) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Unknown or invalid CredentialsProvider class name: " + credentialsProviderName,
classNotFoundException);
} catch (NoSuchMethodException noSuchMethodException) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Credentials provider "
+ credentialsProviderName
+ " does not have a public no-arg constructor.",
noSuchMethodException);
} catch (InvocationTargetException
| InstantiationException
| IllegalAccessException exception) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Failed to create an instance of "
+ credentialsProviderName
+ ": "
+ exception.getMessage(),
exception);
}
}
return null;
}
}

/** Converter for converting strings to {@link Dialect} values. */
static class DialectConverter implements ClientSideStatementValueConverter<Dialect> {
static final DialectConverter INSTANCE = new DialectConverter();

private final CaseInsensitiveEnumMap<Dialect> values =
new CaseInsensitiveEnumMap<>(Dialect.class);

private DialectConverter() {}

@Override
public Class<Dialect> getParameterClass() {
return Dialect.class;
}

@Override
public Dialect convert(String value) {
return values.get(value);
}
}
}
Loading
Loading