Skip to content

Commit

Permalink
refactor: replace individual variables with ConnectionProperty
Browse files Browse the repository at this point in the history
Replace individual variables in ConnectionOptions and ConnectionImpl with
references to ConnectionProperties. This reduces the amount of code bloat,
especially in ConnectionOptions, as all connection property parsing is
now handled by the ConnectionState class in a generic way.

This setup also reduces the amount of code that is needed to add a new
connection property, as there is only one source of truth: the list of
properties in the ConnectionProperties class.

Following steps that will reduce the amount of code bloat further are:
1. Replace all the regular expressions for SET and SHOW statements with
   a simple token-based parser.
2. Cleaning up ConnectionOptions further by removing the duplicate list
   of ConnectionProperties there. These can be removed once the JDBC
   driver has been updated to use the new list of properties.
  • Loading branch information
olavloite committed Sep 7, 2024
1 parent 6fe3c6d commit 94c2d17
Show file tree
Hide file tree
Showing 15 changed files with 999 additions and 916 deletions.
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

0 comments on commit 94c2d17

Please sign in to comment.