Skip to content
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

Split ConfigOption read into two steps: parse() and convert() #40

Merged
merged 5 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions src/main/java/com/baidu/hugegraph/config/HugeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public HugeConfig(Configuration config) {
Iterator<String> keys = config.getKeys();
while (keys.hasNext()) {
String key = keys.next();
if (key.contains("..")) {
key = key.replace("..", ".");
}
this.addProperty(key, config.getProperty(key));
}
this.checkRequiredOptions();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/baidu/hugegraph/config/OptionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public OptionHolder() {

protected void registerOptions() {
for (Field field : this.getClass().getFields()) {
if (!TypedOption.class.isAssignableFrom(field.getType())) {
// Skip if not option
continue;
}
try {
TypedOption<?, ?> option = (TypedOption<?, ?>) field.get(this);
// Fields of subclass first, don't overwrite by superclass
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/baidu/hugegraph/config/OptionSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;

import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;

public final class OptionSpace {
Expand Down Expand Up @@ -63,6 +65,11 @@ public static void register(String module, String holder) {
try {
Method method = clazz.getMethod(INSTANCE_METHOD);
instance = (OptionHolder) method.invoke(null);
if (instance == null) {
exception = new ConfigException(
"Returned null from %s() method",
INSTANCE_METHOD);
}
} catch (NoSuchMethodException e) {
LOG.warn("Class {} does not has static method {}.",
holder, INSTANCE_METHOD);
Expand Down Expand Up @@ -91,17 +98,18 @@ public static void register(String module, OptionHolder holder) {
LOG.warn("Already registered option holder: {} ({})",
module, holders.get(module));
}
E.checkArgumentNotNull(holder, "OptionHolder can't be null");
holders.put(module, holder.getClass());
options.putAll(holder.options());
LOG.debug("Registered options for OptionHolder: {}",
holder.getClass().getSimpleName());
}

public static Set<String> keys() {
return options.keySet();
return Collections.unmodifiableSet(options.keySet());
}

public static Boolean containKey(String key) {
public static boolean containKey(String key) {
return options.containsKey(key);
}

Expand Down
20 changes: 12 additions & 8 deletions src/main/java/com/baidu/hugegraph/config/TypedOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,21 @@ protected Object parse(Object value, Class<?> dataType) {

protected void check(Object value) {
E.checkNotNull(value, "value", this.name);
E.checkArgument(this.dataType.isInstance(value),
"Invalid type of value '%s' for option '%s', " +
"expect type %s but got %s", value, this.name,
this.dataType.getSimpleName(),
value.getClass().getSimpleName());
if (!this.dataType.isInstance(value)) {
throw new ConfigException(
"Invalid type of value '%s' for option '%s', " +
"expect type %s but got %s", value, this.name,
this.dataType.getSimpleName(),
value.getClass().getSimpleName());
}

if (this.checkFunc != null) {
@SuppressWarnings("unchecked")
T result = (T) value;
E.checkArgument(this.checkFunc.apply(result),
"Invalid option value for '%s': %s",
this.name, value);
if (!this.checkFunc.apply(result)) {
throw new ConfigException("Invalid option value for '%s': %s",
this.name, value);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

import com.baidu.hugegraph.testutil.AssertTest;
import com.baidu.hugegraph.testutil.WhiteboxTest;
import com.baidu.hugegraph.unit.concurrent.RowLockTest;
import com.baidu.hugegraph.unit.concurrent.LockGroupTest;
import com.baidu.hugegraph.unit.concurrent.RowLockTest;
import com.baidu.hugegraph.unit.config.HugeConfigTest;
import com.baidu.hugegraph.unit.config.OptionSpaceTest;
import com.baidu.hugegraph.unit.date.SafeDateFormatTest;
import com.baidu.hugegraph.unit.event.EventHubTest;
import com.baidu.hugegraph.unit.iterator.ExtendableIteratorTest;
Expand Down Expand Up @@ -60,6 +61,7 @@
LockGroupTest.class,
RowLockTest.class,
HugeConfigTest.class,
OptionSpaceTest.class,
SafeDateFormatTest.class,
EventHubTest.class,
PerfUtilTest.class,
Expand Down
Loading