Skip to content

Commit

Permalink
chore: reformat (databendlabs#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiHanZ authored and cdmikechen committed Oct 29, 2024
1 parent 8bd9a6a commit 19d92ee
Show file tree
Hide file tree
Showing 55 changed files with 486 additions and 539 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.databend.jdbc;

import com.databend.client.DatabendClient;
import com.databend.client.QueryResults;
import com.databend.client.QueryRowField;
import com.databend.client.data.ColumnTypeHandler;
Expand Down Expand Up @@ -365,7 +364,7 @@ private Object column(int index)
if (value == null || value.toString().equals("NULL")) {
wasNull.set(true);
return null;
}else {
} else {
wasNull.set(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.databend.client.PaginationOptions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.checkerframework.checker.units.qual.C;

import java.sql.Connection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -158,7 +156,7 @@ public LoadBalancingPolicy() {
}
}

private static class AutoDiscovery extends AbstractConnectionProperty<Boolean> {
private static class AutoDiscovery extends AbstractConnectionProperty<Boolean> {
public AutoDiscovery() {
super("auto_discovery", Optional.of("false"), NOT_REQUIRED, ALLOWED, BOOLEAN_CONVERTER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

interface ConnectionProperty<T>
{
interface ConnectionProperty<T> {
String getKey();

Optional<String> getDefault();
Expand All @@ -31,8 +30,7 @@ Optional<T> getValue(Properties properties)
throws SQLException;

default T getRequiredValue(Properties properties)
throws SQLException
{
throws SQLException {
return getValue(properties).orElseThrow(() ->
new SQLException(format("Connection property '%s' is required", getKey())));
}
Expand All @@ -42,8 +40,7 @@ void validate(Properties properties)
}

abstract class AbstractConnectionProperty<T>
implements ConnectionProperty<T>
{
implements ConnectionProperty<T> {
protected static final Predicate<Properties> NOT_REQUIRED = properties -> false;
protected static final Predicate<Properties> ALLOWED = properties -> true;
protected static final Converter<String> STRING_CONVERTER = value -> value;
Expand All @@ -70,17 +67,16 @@ abstract class AbstractConnectionProperty<T>
private final String[] choices;

protected AbstractConnectionProperty(String key, Optional<String> defaultValue,
Predicate<Properties> isRequired, Predicate<Properties> isAllowed, Converter<T> converter, String[] choices, String[] aliases)
{
Predicate<Properties> isRequired, Predicate<Properties> isAllowed, Converter<T> converter, String[] choices, String[] aliases) {
this.key = requireNonNull(key, "key is null");
this.defaultValue = requireNonNull(defaultValue, "defaultValue is null");
this.isRequired = requireNonNull(isRequired, "isRequired is null");
this.isAllowed = requireNonNull(isAllowed, "isAllowed is null");
this.converter = requireNonNull(converter, "converter is null");
if (choices == null || choices.length == 0) {
this.choices = inferChoices(converter);;
}
else {
this.choices = inferChoices(converter);
;
} else {
this.choices = choices;
}
}
Expand All @@ -90,8 +86,7 @@ protected AbstractConnectionProperty(
Optional<String> defaultValue,
Predicate<Properties> isRequired,
Predicate<Properties> isAllowed,
Converter<T> converter)
{
Converter<T> converter) {
this.key = requireNonNull(key, "key is null");
this.defaultValue = requireNonNull(defaultValue, "defaultValue is null");
this.isRequired = requireNonNull(isRequired, "isRequired is null");
Expand All @@ -105,8 +100,7 @@ protected AbstractConnectionProperty(
String key,
Predicate<Properties> required,
Predicate<Properties> allowed,
Converter<T> converter)
{
Converter<T> converter) {
this(key, Optional.empty(), required, allowed, converter);
}

Expand All @@ -115,68 +109,59 @@ protected AbstractConnectionProperty(
Predicate<Properties> required,
Predicate<Properties> allowed,
Converter<T> converter,
String[] aliases)
{
String[] aliases) {
this(key, Optional.empty(), required, allowed, converter, null, aliases);
}

protected static <T> Predicate<T> checkedPredicate(CheckedPredicate<T> predicate)
{
protected static <T> Predicate<T> checkedPredicate(CheckedPredicate<T> predicate) {
return t -> {
try {
return predicate.test(t);
}
catch (SQLException e) {
} catch (SQLException e) {
return false;
}
};
}

private String[] inferChoices(Converter<T> converter) {
String[] choices = null;
Class<? super T> type = new TypeToken<T>(getClass()) {}.getRawType();
Class<? super T> type = new TypeToken<T>(getClass()) {
}.getRawType();
if (type == Boolean.class) {
choices = new String[] {"true", "false"};
}
else if (Enum.class.isAssignableFrom(type)) {
choices = new String[]{"true", "false"};
} else if (Enum.class.isAssignableFrom(type)) {
choices = Stream.of(type.getEnumConstants())
.map(Object::toString)
.toArray(String[]::new);
}
else {
} else {
choices = null;
}
return choices;
return choices;
}

@Override
public String getKey()
{
public String getKey() {
return key;
}

@Override
public Optional<String> getDefault()
{
public Optional<String> getDefault() {
return defaultValue;
}

@Override
public boolean isRequired(Properties properties)
{
public boolean isRequired(Properties properties) {
return isRequired.test(properties);
}

@Override
public boolean isAllowed(Properties properties)
{
public boolean isAllowed(Properties properties) {
return isAllowed.test(properties);
}

@Override
public Optional<T> getValue(Properties properties)
throws SQLException
{
throws SQLException {
String value = properties.getProperty(key);
if (value == null) {
if (isRequired(properties)) {
Expand All @@ -187,8 +172,7 @@ public Optional<T> getValue(Properties properties)

try {
return Optional.of(converter.convert(value));
}
catch (RuntimeException e) {
} catch (RuntimeException e) {
if (value.isEmpty()) {
throw new SQLException(format("Connection property '%s' value is empty", key), e);
}
Expand All @@ -197,8 +181,7 @@ public Optional<T> getValue(Properties properties)
}

@Override
public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties)
{
public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties) {
String currentValue = mergedProperties.getProperty(key);
DriverPropertyInfo result = new DriverPropertyInfo(key, currentValue);
result.required = isRequired.test(mergedProperties);
Expand All @@ -208,22 +191,19 @@ public DriverPropertyInfo getDriverPropertyInfo(Properties mergedProperties)

@Override
public void validate(Properties properties)
throws SQLException
{
throws SQLException {
if (properties.containsKey(key) && !isAllowed(properties)) {
throw new SQLException(format("Connection property '%s' is not allowed", key));
}

getValue(properties);
}

interface Converter<T>
{
interface Converter<T> {
T convert(String value);
}

protected interface CheckedPredicate<T>
{
protected interface CheckedPredicate<T> {
boolean test(T t)
throws SQLException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.net.URI;
import java.util.List;
import java.util.Random;

public class DatabendClientLoadBalancingPolicy {
static class DisabledPolicy extends DatabendClientLoadBalancingPolicy {
Expand All @@ -12,6 +11,7 @@ public String toString() {
}
// do nothing
}

static class RandomPolicy extends DatabendClientLoadBalancingPolicy {
@Override
protected URI pickUri(String query_id, DatabendNodes nodes) {
Expand Down Expand Up @@ -56,6 +56,7 @@ public String toString() {
return "RoundRobin";
}
}

/**
* Policy that disable load balance and always use the first node.
*/
Expand Down Expand Up @@ -88,6 +89,7 @@ static DatabendClientLoadBalancingPolicy create(String name) {

/**
* Policy to pick a node based on the least loaded algorithm.
*
* @param nodes the list of URIs to choose from
* @return the URI to use
*/
Expand All @@ -101,6 +103,7 @@ protected URI pickUri(String query_id, DatabendNodes nodes) {

/**
* Get int hash value of given query id
*
* @param query_id the query id used for choosing load balancing node
* @return hash value of the query id
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.databend.jdbc;

import com.databend.client.*;
import com.databend.client.ClientSettings;
import com.databend.client.DatabendClient;
import com.databend.client.DatabendClientV1;
import com.databend.client.DatabendSession;
import com.databend.client.PaginationOptions;
import com.databend.client.QueryRequest;
import com.databend.client.StageAttachment;
import com.databend.jdbc.annotation.NotImplemented;
import com.databend.jdbc.cloud.DatabendCopyParams;
import com.databend.jdbc.cloud.DatabendPresignClient;
Expand All @@ -9,10 +15,13 @@
import okhttp3.Headers;
import okhttp3.OkHttpClient;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
Expand All @@ -30,15 +39,22 @@
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.*;
import java.util.function.Consumer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.zip.GZIPOutputStream;

import static com.databend.client.ClientSettings.*;
Expand Down Expand Up @@ -145,7 +161,6 @@ public static URI parseRouteHint(String routeHint) {
}



private static void checkResultSet(int resultSetType, int resultSetConcurrency)
throws SQLFeatureNotSupportedException {
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) {
Expand Down Expand Up @@ -660,6 +675,7 @@ public void accept(DatabendSession session) {
/**
* Retry executing a query in case of connection errors. fail over mechanism is used to retry the query when connect error occur
* It will find next target host based on configured Load balancing Policy.
*
* @param sql The SQL statement to execute.
* @param attach The stage attachment to use for the query.
* @return A DatabendClient instance representing the successful query execution.
Expand All @@ -670,7 +686,7 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws
Exception e = null;
int times = getMaxFailoverRetries() + 1;

for( int i = 1; i <= times; i++) {
for (int i = 1; i <= times; i++) {
if (e != null && !(e.getCause() instanceof ConnectException)) {
throw new SQLException("Error start query: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e);
}
Expand Down Expand Up @@ -712,7 +728,7 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws
throw new SQLException("Error executing query: " + "SQL: " + sql + " " + e1.getMessage() + " cause: " + e1.getCause(), e1);
}
}
throw new SQLException("Failover Retry Error executing query after" + getMaxFailoverRetries() + "failover retry: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e);
throw new SQLException("Failover Retry Error executing query after" + getMaxFailoverRetries() + "failover retry: " + "SQL: " + sql + " " + e.getMessage() + " cause: " + e.getCause(), e);
}

DatabendClient startQuery(String sql) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.databend.client.QueryRowField;
import com.databend.client.data.DatabendDataType;
import com.databend.client.data.DatabendRawType;
import com.databend.client.data.DatabendTypes;
import com.google.common.base.Joiner;

import java.sql.Connection;
Expand All @@ -17,10 +16,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.databend.jdbc.DriverInfo.DRIVER_NAME;
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION;
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MAJOR;
import static com.databend.jdbc.DriverInfo.DRIVER_VERSION_MINOR;
import static com.databend.jdbc.DriverInfo.*;
import static java.util.Objects.requireNonNull;

public class DatabendDatabaseMetaData implements DatabaseMetaData {
Expand Down Expand Up @@ -1051,7 +1047,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
emptyStringLikeFilter(filters, "table_schema", schemaPattern);
if (tableNamePattern != null) {
optionalStringLikeFilter(filters, "table_name", tableNamePattern.replace("\\", ""));
}else {
} else {
optionalStringLikeFilter(filters, "table_name", null);
}
optionalStringLikeFilter(filters, "column_name", columnNamePattern);
Expand Down
Loading

0 comments on commit 19d92ee

Please sign in to comment.