Skip to content

Commit

Permalink
chore: replace individual variables with ConnectionProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Sep 7, 2024
1 parent 6fe3c6d commit 1b54384
Show file tree
Hide file tree
Showing 8 changed files with 928 additions and 831 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.spanner.connection;

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 @@ -31,6 +33,8 @@
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.util.Base64;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -172,8 +176,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 @@ -195,10 +202,15 @@ public Duration convert(String value) {
if (matcher.group(0).equalsIgnoreCase("null")) {
return Durations.fromNanos(0L);
} else {
Duration duration =
ReadOnlyStalenessUtil.createDuration(
Long.parseLong(matcher.group(1)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(2)));
Duration duration;
if (matcher.group(3) != null) {
duration = Durations.fromMillis(Long.parseLong(matcher.group(3)));
} else {
duration =
ReadOnlyStalenessUtil.createDuration(
Long.parseLong(matcher.group(1)),
ReadOnlyStalenessUtil.parseTimeUnit(matcher.group(2)));
}
if (duration.getSeconds() == 0L && duration.getNanos() == 0) {
return null;
}
Expand Down Expand Up @@ -254,6 +266,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 @@ -539,7 +555,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 +590,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 +640,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 +724,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

0 comments on commit 1b54384

Please sign in to comment.