Skip to content

Commit

Permalink
Fix can't insert data with List<Date> property (#534)
Browse files Browse the repository at this point in the history
Change-Id: I232ab865e5599b6994955f671607696dd24a1d8c
  • Loading branch information
Linary authored and javeme committed May 30, 2019
1 parent 0061d4f commit 0b5a702
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 33 deletions.
2 changes: 1 addition & 1 deletion hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.6.2</version>
<version>1.6.3</version>
</dependency>

<!-- tinkerpop -->
Expand Down
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 @@ -213,24 +213,14 @@ private <V> HugeProperty<V> addProperty(PropertyKey pkey, V value,
this.setProperty(property);
}

Collection<V> values = null;
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);
Collection<V> values;
if (pkey.cardinality() == Cardinality.SET) {
values = CollectionUtil.toSet(value);
} else {
E.checkArgument(pkey.checkDataType(value),
"Invalid type of property value '%s' for key '%s'",
value, pkey.name());
property.value().add(value);
assert pkey.cardinality() == Cardinality.LIST;
values = CollectionUtil.toList(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

0 comments on commit 0b5a702

Please sign in to comment.