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

Fix bug in List<Date> property #534

Merged
merged 2 commits into from
May 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void parseProperty(Id pkeyId, byte[] val, HugeElement owner) {
PropertyKey pkey = owner.graph().propertyKey(pkeyId);

// Parse value
Object value = KryoUtil.fromKryo(val, pkey.clazz());
Object value = KryoUtil.fromKryo(val, pkey.implementClazz());

// Set properties of vertex/edge
if (pkey.cardinality() == Cardinality.SINGLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void parseProperty(Id key, String colValue, HugeElement owner) {
PropertyKey pkey = owner.graph().propertyKey(key);

// Parse value
Object value = JsonUtil.fromJson(colValue, pkey.clazz());
Object value = JsonUtil.fromJson(colValue, pkey.implementClazz());

// Set properties of vertex/edge
if (pkey.cardinality() == Cardinality.SINGLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void parseProperty(String colName, String colValue,
PropertyKey pkey = owner.graph().propertyKey(readId(colParts[1]));

// Parse value
Object value = JsonUtil.fromJson(colValue, pkey.clazz());
Object value = JsonUtil.fromJson(colValue, pkey.implementClazz());

// Set properties of vertex/edge
if (pkey.cardinality() == Cardinality.SINGLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -84,7 +83,24 @@ public PropertyKey properties(Id... properties) {
return this;
}

public Class<?> clazz() {
public String clazz() {
String dataType = this.dataType().clazz().getSimpleName();
switch (this.cardinality) {
case SINGLE:
return dataType;
// A set of values: Set<DataType>
case SET:
return String.format("Set<%s>", dataType);
// A list of values: List<DataType>
case LIST:
return String.format("List<%s>", dataType);
default:
throw new AssertionError(String.format(
"Unsupported cardinality: '%s'", this.cardinality));
}
}

public Class<?> implementClazz() {
Class<?> cls;
switch (this.cardinality) {
case SINGLE:
Expand All @@ -96,7 +112,7 @@ public Class<?> clazz() {
break;
// A list of values: List<DataType>
case LIST:
cls = LinkedList.class;
cls = ArrayList.class;
break;
default:
throw new AssertionError(String.format(
Expand Down Expand Up @@ -177,6 +193,16 @@ public <V> Object serialValue(V value) {
return validValue;
}

public <V> V validValueOrThrow(V value) {
V validValue = this.validValue(value);
E.checkArgument(validValue != null,
"Invalid property value '%s' for key '%s', " +
"expect a value of type %s, actual type %s",
value, this.name(), this.clazz(),
value.getClass().getSimpleName());
return validValue;
}

public <V> V validValue(V value) {
return this.convValue(value, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.CollectionUtil;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

public abstract class HugeElement implements Element, GraphType {

Expand Down Expand Up @@ -213,24 +215,20 @@ private <V> HugeProperty<V> addProperty(PropertyKey pkey, V value,
this.setProperty(property);
}

Collection<V> values = null;
Collection<V> values;
if (value instanceof Collection) {
values = (Collection<V>) value;
} else if (value.getClass().isArray()) {
values = CollectionUtil.toList(value);
}

if (values != null) {
E.checkArgument(pkey.checkDataType(values),
"Invalid type of property values %s for key '%s'",
value, pkey.name());
property.value().addAll(values);
} else {
E.checkArgument(pkey.checkDataType(value),
"Invalid type of property value '%s' for key '%s'",
value, pkey.name());
property.value().add(value);
if (pkey.cardinality() == Cardinality.SET) {
values = ImmutableSet.of(value);
} else {
assert pkey.cardinality() == Cardinality.LIST;
values = ImmutableList.of(value);
}
}
property.value().addAll(pkey.validValueOrThrow(values));

// Any better ways?
return (HugeProperty) property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,7 @@ public HugeProperty(HugeElement owner, PropertyKey pkey, V value) {

this.owner = owner;
this.pkey = pkey;
this.value = pkey.validValue(value);

if (this.value == null) {
E.checkArgument(false,
"Invalid property value '%s' for key '%s', " +
"expect a value of type %s, actual type %s",
value, pkey.name(),
pkey.clazz().getSimpleName(),
value.getClass().getSimpleName());
}
this.value = pkey.validValueOrThrow(value);
}

public PropertyKey propertyKey() {
Expand Down