Skip to content

Commit

Permalink
Support client-specific property
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Nov 30, 2021
1 parent 1b0714a commit e0a3a65
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;

import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseOption;
import com.clickhouse.client.config.ClickHouseDefaults;

Expand Down Expand Up @@ -186,33 +183,6 @@ public ClickHouseClientBuilder options(Map<ClickHouseOption, Serializable> optio
return this;
}

/**
* Sets options.
*
* @param options options
* @return this builder
*/
public ClickHouseClientBuilder options(Properties options) {
if (options != null && !options.isEmpty()) {
for (Entry<Object, Object> e : options.entrySet()) {
Object key = e.getKey();
Object value = e.getValue();
if (key == null || value == null) {
continue;
}

ClickHouseClientOption o = ClickHouseClientOption.fromKey(key.toString());
if (o != null) {
this.options.put(o, ClickHouseOption.fromString(value.toString(), o.getValueType()));
}
}

resetConfig();
}

return this;
}

/*
* public ClickHouseClientBuilder addUserType(Object... userTypeMappers) {
* resetConfig(); return this; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.Serializable;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseCompression;
import com.clickhouse.client.ClickHouseFormat;
import com.clickhouse.client.ClickHouseProtocol;

Expand Down Expand Up @@ -87,7 +86,7 @@ public enum ClickHouseDefaults implements ClickHouseOption {

private final String key;
private final Serializable defaultValue;
private final Class<?> clazz;
private final Class<? extends Serializable> clazz;
private final String description;

<T extends Serializable> ClickHouseDefaults(String key, T defaultValue, String description) {
Expand Down Expand Up @@ -118,7 +117,7 @@ public String getPrefix() {
}

@Override
public Class<?> getValueType() {
public Class<? extends Serializable> getValueType() {
return clazz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ default String getPrefix() {
*
* @return value type of the option, defaults to String
*/
Class<?> getValueType();
Class<? extends Serializable> getValueType();

/**
* Gets name of the option.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,106 +8,110 @@
import com.clickhouse.client.ClickHouseFormat;

public class ClickHouseConfigOptionTest {
static enum ClickHouseTestOption implements ClickHouseOption {
STR("string_option", "string", "string option"),
STR0("string_option0", "string0", "string option without environment variable support"),
STR1("string_option0", "string1", "string option without environment variable and system property support"),
INT("integer_option", 2333, "integer option"),
INT0("integer_option0", 23330, "integer option without environment variable support"),
INT1("integer_option1", 23331, "integer option without environment variable and system property support"),
BOOL("boolean_option", false, "boolean option"),
BOOL0("boolean_option0", true, "boolean option without environment variable support"),
BOOL1("boolean_option1", false, "boolean option without environment variable and system property support");

private final String key;
private final Serializable defaultValue;
private final Class<? extends Serializable> clazz;
private final String description;

<T extends Serializable> ClickHouseTestOption(String key, T defaultValue, String description) {
this.key = ClickHouseChecker.nonNull(key, "key");
this.defaultValue = defaultValue;
this.clazz = defaultValue.getClass();
this.description = ClickHouseChecker.nonNull(description, "description");
static enum ClickHouseTestOption implements ClickHouseOption {
STR("string_option", "string", "string option"),
STR0("string_option0", "string0", "string option without environment variable support"),
STR1("string_option0", "string1",
"string option without environment variable and system property support"),
INT("integer_option", 2333, "integer option"),
INT0("integer_option0", 23330, "integer option without environment variable support"),
INT1("integer_option1", 23331,
"integer option without environment variable and system property support"),
BOOL("boolean_option", false, "boolean option"),
BOOL0("boolean_option0", true, "boolean option without environment variable support"),
BOOL1("boolean_option1", false,
"boolean option without environment variable and system property support");

private final String key;
private final Serializable defaultValue;
private final Class<? extends Serializable> clazz;
private final String description;

<T extends Serializable> ClickHouseTestOption(String key, T defaultValue, String description) {
this.key = ClickHouseChecker.nonNull(key, "key");
this.defaultValue = defaultValue;
this.clazz = defaultValue.getClass();
this.description = ClickHouseChecker.nonNull(description, "description");
}

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

@Override
public Serializable getDefaultValue() {
return defaultValue;
}

@Override
public Class<? extends Serializable> getValueType() {
return clazz;
}

@Override
public String getDescription() {
return description;
}
}

@Override
public String getKey() {
return key;
@Test(groups = { "unit" })
public void testFromString() {
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString(null, String.class));
Assert.assertEquals(ClickHouseOption.fromString("", String.class), "");

Assert.assertEquals(ClickHouseOption.fromString("", Boolean.class), Boolean.FALSE);
Assert.assertEquals(ClickHouseOption.fromString("Yes", Boolean.class), Boolean.FALSE);
Assert.assertEquals(ClickHouseOption.fromString("1", Boolean.class), Boolean.TRUE);
Assert.assertEquals(ClickHouseOption.fromString("true", Boolean.class), Boolean.TRUE);
Assert.assertEquals(ClickHouseOption.fromString("True", Boolean.class), Boolean.TRUE);

Assert.assertEquals(ClickHouseOption.fromString("", Integer.class), Integer.valueOf(0));
Assert.assertEquals(ClickHouseOption.fromString("0", Integer.class), Integer.valueOf(0));
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString(null, Integer.class));

Assert.assertEquals(ClickHouseOption.fromString("0.1", Float.class), Float.valueOf(0.1F));
Assert.assertEquals(ClickHouseOption.fromString("NaN", Float.class), Float.valueOf(Float.NaN));

Assert.assertEquals(ClickHouseOption.fromString("Map", ClickHouseDataType.class),
ClickHouseDataType.Map);
Assert.assertEquals(ClickHouseOption.fromString("RowBinary", ClickHouseFormat.class),
ClickHouseFormat.RowBinary);
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString("NonExistFormat", ClickHouseFormat.class));
}

@Override
public Serializable getDefaultValue() {
return defaultValue;
@Test(groups = { "unit" })
public void testGetEffectiveDefaultValue() {
// environment variables are set in pom.xml
Assert.assertEquals(ClickHouseTestOption.STR.getEffectiveDefaultValue(),
ClickHouseTestOption.STR.getDefaultValueFromEnvVar().get());
Assert.assertEquals(ClickHouseTestOption.INT.getEffectiveDefaultValue(),
Integer.parseInt(ClickHouseTestOption.INT.getDefaultValueFromEnvVar().get()));
Assert.assertEquals(ClickHouseTestOption.BOOL.getEffectiveDefaultValue(),
Boolean.valueOf(ClickHouseTestOption.BOOL.getDefaultValueFromEnvVar().get()));

String sv = "system.property";
int iv = 12345;
boolean bv = true;
System.setProperty(ClickHouseTestOption.STR0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.STR0.name().toLowerCase(), sv);
System.setProperty(ClickHouseTestOption.INT0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.INT0.name().toLowerCase(), String.valueOf(iv));
System.setProperty(ClickHouseTestOption.BOOL0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.BOOL0.name().toLowerCase(), String.valueOf(bv));

Assert.assertEquals(ClickHouseTestOption.STR0.getEffectiveDefaultValue(), sv);
Assert.assertEquals(ClickHouseTestOption.INT0.getEffectiveDefaultValue(), iv);
Assert.assertEquals(ClickHouseTestOption.BOOL0.getEffectiveDefaultValue(), bv);

Assert.assertEquals(ClickHouseTestOption.STR1.getEffectiveDefaultValue(),
ClickHouseTestOption.STR1.getDefaultValue());
Assert.assertEquals(ClickHouseTestOption.INT1.getEffectiveDefaultValue(),
ClickHouseTestOption.INT1.getDefaultValue());
Assert.assertEquals(ClickHouseTestOption.BOOL1.getEffectiveDefaultValue(),
ClickHouseTestOption.BOOL1.getDefaultValue());
}

@Override
public Class<?> getValueType() {
return clazz;
}

@Override
public String getDescription() {
return description;
}
}

@Test(groups = { "unit" })
public void testFromString() {
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString(null, String.class));
Assert.assertEquals(ClickHouseOption.fromString("", String.class), "");

Assert.assertEquals(ClickHouseOption.fromString("", Boolean.class), Boolean.FALSE);
Assert.assertEquals(ClickHouseOption.fromString("Yes", Boolean.class), Boolean.FALSE);
Assert.assertEquals(ClickHouseOption.fromString("1", Boolean.class), Boolean.TRUE);
Assert.assertEquals(ClickHouseOption.fromString("true", Boolean.class), Boolean.TRUE);
Assert.assertEquals(ClickHouseOption.fromString("True", Boolean.class), Boolean.TRUE);

Assert.assertEquals(ClickHouseOption.fromString("", Integer.class), Integer.valueOf(0));
Assert.assertEquals(ClickHouseOption.fromString("0", Integer.class), Integer.valueOf(0));
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString(null, Integer.class));

Assert.assertEquals(ClickHouseOption.fromString("0.1", Float.class), Float.valueOf(0.1F));
Assert.assertEquals(ClickHouseOption.fromString("NaN", Float.class), Float.valueOf(Float.NaN));

Assert.assertEquals(ClickHouseOption.fromString("Map", ClickHouseDataType.class), ClickHouseDataType.Map);
Assert.assertEquals(ClickHouseOption.fromString("RowBinary", ClickHouseFormat.class),
ClickHouseFormat.RowBinary);
Assert.assertThrows(IllegalArgumentException.class,
() -> ClickHouseOption.fromString("NonExistFormat", ClickHouseFormat.class));
}

@Test(groups = { "unit" })
public void testGetEffectiveDefaultValue() {
// environment variables are set in pom.xml
Assert.assertEquals(ClickHouseTestOption.STR.getEffectiveDefaultValue(),
ClickHouseTestOption.STR.getDefaultValueFromEnvVar().get());
Assert.assertEquals(ClickHouseTestOption.INT.getEffectiveDefaultValue(),
Integer.parseInt(ClickHouseTestOption.INT.getDefaultValueFromEnvVar().get()));
Assert.assertEquals(ClickHouseTestOption.BOOL.getEffectiveDefaultValue(),
Boolean.valueOf(ClickHouseTestOption.BOOL.getDefaultValueFromEnvVar().get()));

String sv = "system.property";
int iv = 12345;
boolean bv = true;
System.setProperty(ClickHouseTestOption.STR0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.STR0.name().toLowerCase(), sv);
System.setProperty(ClickHouseTestOption.INT0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.INT0.name().toLowerCase(), String.valueOf(iv));
System.setProperty(ClickHouseTestOption.BOOL0.getPrefix().toLowerCase() + "_"
+ ClickHouseTestOption.BOOL0.name().toLowerCase(), String.valueOf(bv));

Assert.assertEquals(ClickHouseTestOption.STR0.getEffectiveDefaultValue(), sv);
Assert.assertEquals(ClickHouseTestOption.INT0.getEffectiveDefaultValue(), iv);
Assert.assertEquals(ClickHouseTestOption.BOOL0.getEffectiveDefaultValue(), bv);

Assert.assertEquals(ClickHouseTestOption.STR1.getEffectiveDefaultValue(),
ClickHouseTestOption.STR1.getDefaultValue());
Assert.assertEquals(ClickHouseTestOption.INT1.getEffectiveDefaultValue(),
ClickHouseTestOption.INT1.getDefaultValue());
Assert.assertEquals(ClickHouseTestOption.BOOL1.getEffectiveDefaultValue(),
ClickHouseTestOption.BOOL1.getDefaultValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public enum ClickHouseGrpcOption implements ClickHouseOption {

private final String key;
private final Serializable defaultValue;
private final Class<?> clazz;
private final Class<? extends Serializable> clazz;
private final String description;

<T extends Serializable> ClickHouseGrpcOption(String key, T defaultValue, String description) {
Expand All @@ -63,7 +63,7 @@ public String getKey() {
}

@Override
public Class<?> getValueType() {
public Class<? extends Serializable> getValueType() {
return clazz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum ClickHouseHttpOption implements ClickHouseOption {

private final String key;
private final Serializable defaultValue;
private final Class<?> clazz;
private final Class<? extends Serializable> clazz;
private final String description;

<T extends Serializable> ClickHouseHttpOption(String key, T defaultValue, String description) {
Expand All @@ -70,7 +70,7 @@ public String getKey() {
}

@Override
public Class<?> getValueType() {
public Class<? extends Serializable> getValueType() {
return clazz;
}
}
Loading

0 comments on commit e0a3a65

Please sign in to comment.