From 0b5a70268567d7cf32258fc72a024a3fd6306cf6 Mon Sep 17 00:00:00 2001 From: Linary Date: Thu, 30 May 2019 15:06:03 +0800 Subject: [PATCH] Fix can't insert data with List property (#534) Change-Id: I232ab865e5599b6994955f671607696dd24a1d8c --- hugegraph-core/pom.xml | 2 +- .../backend/serializer/BinarySerializer.java | 2 +- .../backend/serializer/TableSerializer.java | 2 +- .../backend/serializer/TextSerializer.java | 2 +- .../baidu/hugegraph/schema/PropertyKey.java | 32 +++++++++++++++++-- .../hugegraph/structure/HugeElement.java | 22 ++++--------- .../hugegraph/structure/HugeProperty.java | 11 +------ 7 files changed, 40 insertions(+), 33 deletions(-) diff --git a/hugegraph-core/pom.xml b/hugegraph-core/pom.xml index c7a91e9c3..39aebc0c6 100644 --- a/hugegraph-core/pom.xml +++ b/hugegraph-core/pom.xml @@ -19,7 +19,7 @@ com.baidu.hugegraph hugegraph-common - 1.6.2 + 1.6.3 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java index baadee43f..37b6ac8fa 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java @@ -168,7 +168,7 @@ public class BinarySerializer extends AbstractSerializer { 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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java index 0115a9146..538de8279 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TableSerializer.java @@ -96,7 +96,7 @@ public abstract class TableSerializer extends AbstractSerializer { 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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java index 6ba192dcc..5fa34a8e1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/TextSerializer.java @@ -123,7 +123,7 @@ public class TextSerializer extends AbstractSerializer { 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) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/PropertyKey.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/PropertyKey.java index e0cbc41f6..47179aaec 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/PropertyKey.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/PropertyKey.java @@ -24,7 +24,6 @@ import java.util.Collection; 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; @@ -84,7 +83,24 @@ public class PropertyKey extends SchemaElement implements Propfiable { 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 + case SET: + return String.format("Set<%s>", dataType); + // A list of values: List + 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: @@ -96,7 +112,7 @@ public class PropertyKey extends SchemaElement implements Propfiable { break; // A list of values: List case LIST: - cls = LinkedList.class; + cls = ArrayList.class; break; default: throw new AssertionError(String.format( @@ -177,6 +193,16 @@ public class PropertyKey extends SchemaElement implements Propfiable { return validValue; } + public 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 validValue(V value) { return this.convValue(value, true); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java index 9992b7ee9..dec6ac1b4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java @@ -213,24 +213,14 @@ public abstract class HugeElement implements Element, GraphType { this.setProperty(property); } - Collection values = null; - if (value instanceof Collection) { - values = (Collection) value; - } else if (value.getClass().isArray()) { + Collection values; + if (pkey.cardinality() == Cardinality.SET) { + values = CollectionUtil.toSet(value); + } else { + assert pkey.cardinality() == Cardinality.LIST; 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); - } + property.value().addAll(pkey.validValueOrThrow(values)); // Any better ways? return (HugeProperty) property; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeProperty.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeProperty.java index cdaa484b9..7d6e2a427 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeProperty.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeProperty.java @@ -42,16 +42,7 @@ public abstract class HugeProperty implements Property, GraphType { 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() {