From 0c4c950d503e3561daae73e861eeaf058319394d Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 09:27:17 -0400 Subject: [PATCH 01/10] [JanusGraph] Fixed - should contain id property --- .../spring/data/gremlin/common/Constants.java | 1 + .../result/GremlinResultVertexReader.java | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java b/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java index 268d70d2..62463518 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java @@ -15,6 +15,7 @@ public class Constants { public static final String PROPERTY_LABEL = "label"; public static final String PROPERTY_TYPE = "type"; public static final String PROPERTY_VALUE = "value"; + public static final String PROPERTY_VALUE_WITH_AT = "@value"; public static final String PROPERTY_PROPERTIES = "properties"; public static final String PROPERTY_INV = "inV"; public static final String PROPERTY_OUTV = "outV"; diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java index 10e8795e..5e90c356 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java @@ -14,6 +14,8 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -31,14 +33,22 @@ private void validate(List results, GremlinSource source) { Assert.isInstanceOf(Map.class, result.getObject(), "should be one instance of Map"); - @SuppressWarnings("unchecked") final Map map = (Map) result.getObject(); + Map map = (Map) result.getObject(); + + while (map.containsKey(PROPERTY_VALUE_WITH_AT) && !(map instanceof LinkedHashMap)) { + final Object value = map.get(PROPERTY_VALUE_WITH_AT); + if (value instanceof ArrayList && ((ArrayList) value).size() > 0) { + map = (Map) ((ArrayList) value).get(0); + } else { + map = (Map) value; + } + } Assert.isTrue(map.containsKey(PROPERTY_ID), "should contain id property"); Assert.isTrue(map.containsKey(PROPERTY_LABEL), "should contain label property"); - Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain type property"); +// Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain type property"); Assert.isTrue(map.containsKey(PROPERTY_PROPERTIES), "should contain properties property"); - Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_VERTEX), "must be vertex type"); - +// Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_VERTEX), "must be vertex type"); Assert.isInstanceOf(Map.class, map.get(PROPERTY_PROPERTIES), "should be one instance of Map"); } From 2f143bb019af8cce6c8d4e55f73aa9db212c65cb Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 12:01:29 -0400 Subject: [PATCH 02/10] Fixed code logic issue --- .../conversion/result/GremlinResultVertexReader.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java index 5e90c356..4bd4954d 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java @@ -18,6 +18,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static com.microsoft.spring.data.gremlin.common.Constants.*; @@ -28,14 +29,14 @@ private void validate(List results, GremlinSource source) { Assert.notNull(results, "Results should not be null."); Assert.notNull(source, "GremlinSource should not be null."); Assert.isTrue(results.size() == 1, "Vertex should contain only one result."); + } - final Result result = results.get(0); - + private Map getVertexProperties (@NonNull Result result) { Assert.isInstanceOf(Map.class, result.getObject(), "should be one instance of Map"); Map map = (Map) result.getObject(); - while (map.containsKey(PROPERTY_VALUE_WITH_AT) && !(map instanceof LinkedHashMap)) { + while ( (map instanceof LinkedHashMap) && map.containsKey(PROPERTY_VALUE_WITH_AT) ) { final Object value = map.get(PROPERTY_VALUE_WITH_AT); if (value instanceof ArrayList && ((ArrayList) value).size() > 0) { map = (Map) ((ArrayList) value).get(0); @@ -50,6 +51,8 @@ private void validate(List results, GremlinSource source) { Assert.isTrue(map.containsKey(PROPERTY_PROPERTIES), "should contain properties property"); // Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_VERTEX), "must be vertex type"); Assert.isInstanceOf(Map.class, map.get(PROPERTY_PROPERTIES), "should be one instance of Map"); + + return map; } @Override @@ -61,7 +64,7 @@ public void read(@NonNull List results, @NonNull GremlinSource source) { validate(results, source); - final Map map = (Map) results.get(0).getObject(); + final Map map = getVertexProperties(results.get(0)); final Map properties = (Map) map.get(PROPERTY_PROPERTIES); super.readResultProperties(properties, source); From e2d26a8392a3aa83fca3bb09aba5df9dd8f77f17 Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 13:15:39 -0400 Subject: [PATCH 03/10] Fixed issue --- .../result/AbstractGremlinResultReader.java | 39 +++++++++++++++++-- .../result/GremlinResultVertexReader.java | 25 ++++++------ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index 1b7881b1..e503c49e 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -15,10 +15,31 @@ import java.util.LinkedHashMap; import java.util.Map; +import static com.microsoft.spring.data.gremlin.common.Constants.*; +import static com.microsoft.spring.data.gremlin.common.Constants.PROPERTY_VALUE_WITH_AT; + @NoArgsConstructor // TODO: seems only for Vertex. public abstract class AbstractGremlinResultReader { + /** + * Vertex's properties returned from gremlin-driver has a complicated data structure + * This function helps to renovate it to a simple Map + * @return Map of list properties + */ + protected Map getProperties (@NonNull Map map) { + while ((map instanceof LinkedHashMap) && map.containsKey(PROPERTY_VALUE_WITH_AT)) { + final Object value = map.get(PROPERTY_VALUE_WITH_AT); + if (value instanceof ArrayList && ((ArrayList) value).size() > 0) { + map = (Map) ((ArrayList) value).get(0); + } else { + map = (Map) value; + } + } + + return map; + } + /** * properties's organization is a little complicated. *

@@ -30,16 +51,28 @@ public abstract class AbstractGremlinResultReader { private Object readProperty(@NonNull Object value) { Assert.isInstanceOf(ArrayList.class, value, "should be instance of ArrayList"); - @SuppressWarnings("unchecked") final ArrayList> mapList - = (ArrayList>) value; + @SuppressWarnings("unchecked") final ArrayList> mapList + = (ArrayList>) value; Assert.isTrue(mapList.size() == 1, "should be only 1 element in ArrayList"); - return mapList.get(0).get(Constants.PROPERTY_VALUE); + final Map renovatedMap = getProperties(mapList.get(0)); + + return renovatedMap.get(Constants.PROPERTY_VALUE); } protected void readResultProperties(@NonNull Map properties, @NonNull GremlinSource source) { source.getProperties().clear(); properties.forEach((key, value) -> source.setProperty(key, this.readProperty(value))); } + + protected void readIdentifier(@NonNull Map dataMap, @NonNull GremlinSource source) { + Assert.isTrue( + source.getLabel().equals(dataMap.get(PROPERTY_LABEL)), + "mapping an invalid object data: " + dataMap.get(PROPERTY_LABEL)); + + if (source.getId() == null && dataMap.containsKey(PROPERTY_ID)) { + source.setId(dataMap.get(PROPERTY_ID)); + } + } } diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java index 4bd4954d..ae219f8f 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java @@ -14,11 +14,9 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static com.microsoft.spring.data.gremlin.common.Constants.*; @@ -36,14 +34,7 @@ private Map getVertexProperties (@NonNull Result result) { Map map = (Map) result.getObject(); - while ( (map instanceof LinkedHashMap) && map.containsKey(PROPERTY_VALUE_WITH_AT) ) { - final Object value = map.get(PROPERTY_VALUE_WITH_AT); - if (value instanceof ArrayList && ((ArrayList) value).size() > 0) { - map = (Map) ((ArrayList) value).get(0); - } else { - map = (Map) value; - } - } + map = getProperties(map); Assert.isTrue(map.containsKey(PROPERTY_ID), "should contain id property"); Assert.isTrue(map.containsKey(PROPERTY_LABEL), "should contain label property"); @@ -55,6 +46,16 @@ private Map getVertexProperties (@NonNull Result result) { return map; } + private Object getVertexProperty (@NonNull Map map, @NonNull String propertyKey) { + Object value = map.get(propertyKey); + + while ((value instanceof LinkedHashMap) && ((LinkedHashMap) value).containsKey(PROPERTY_VALUE_WITH_AT)) { + value = ((LinkedHashMap) value).get(PROPERTY_VALUE_WITH_AT); + } + + return value; + } + @Override @SuppressWarnings("unchecked") public void read(@NonNull List results, @NonNull GremlinSource source) { @@ -72,7 +73,7 @@ public void read(@NonNull List results, @NonNull GremlinSource source) { final String className = source.getProperties().get(GREMLIN_PROPERTY_CLASSNAME).toString(); source.setIdField(GremlinUtils.getIdField(GremlinUtils.toEntityClass(className))); - source.setId(map.get(PROPERTY_ID)); - source.setLabel(map.get(PROPERTY_LABEL).toString()); + source.setId(getVertexProperty(map, PROPERTY_ID)); + source.setLabel(getVertexProperty(map, PROPERTY_LABEL).toString()); } } From 7b2121418a3bd2d5c2cd94581923cd683bc2468e Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 14:35:56 -0400 Subject: [PATCH 04/10] Fixed EdgeReader issue --- .../result/AbstractGremlinResultReader.java | 10 ++++++++ .../result/GremlinResultEdgeReader.java | 24 +++++++++++-------- .../result/GremlinResultVertexReader.java | 15 ++---------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index e503c49e..1adae31b 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -40,6 +40,16 @@ protected Map getProperties (@NonNull Map map) { return map; } + protected Object getPropertyValue (@NonNull Map map, @NonNull String propertyKey) { + Object value = map.get(propertyKey); + + while ((value instanceof LinkedHashMap) && ((LinkedHashMap) value).containsKey(PROPERTY_VALUE_WITH_AT)) { + value = ((LinkedHashMap) value).get(PROPERTY_VALUE_WITH_AT); + } + + return value; + } + /** * properties's organization is a little complicated. *

diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java index a0d3341d..9ee49a42 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java @@ -35,19 +35,23 @@ private void validate(List results, GremlinSource source) { Assert.notNull(results, "Results should not be null."); Assert.notNull(source, "GremlinSource should not be null."); Assert.isTrue(results.size() == 1, "Edge should contain only one result."); + } - final Result result = results.get(0); - + private Map getEdgeProperties (@org.springframework.lang.NonNull Result result) { Assert.isInstanceOf(Map.class, result.getObject(), "should be one instance of Map"); - @SuppressWarnings("unchecked") final Map map = (Map) result.getObject(); + Map map = (Map) result.getObject(); + + map = getProperties(map); Assert.isTrue(map.containsKey(PROPERTY_ID), "should contain id property"); Assert.isTrue(map.containsKey(PROPERTY_LABEL), "should contain label property"); - Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain type property"); +// Assert.isTrue(map.containsKey(PROPERTY_TYPE), "should contain type property"); Assert.isTrue(map.containsKey(PROPERTY_INV), "should contain inV property"); Assert.isTrue(map.containsKey(PROPERTY_OUTV), "should contain outV property"); - Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_EDGE), "must be vertex type"); +// Assert.isTrue(map.get(PROPERTY_TYPE).equals(RESULT_TYPE_EDGE), "must be vertex type"); + + return map; } @Override @@ -60,16 +64,16 @@ public void read(@NonNull List results, @NonNull GremlinSource source) { validate(results, source); final GremlinSourceEdge sourceEdge = (GremlinSourceEdge) source; - final Map map = (Map) results.get(0).getObject(); + final Map map = getEdgeProperties(results.get(0)); this.readProperties(source, (Map) map.get(PROPERTY_PROPERTIES)); final String className = source.getProperties().get(GREMLIN_PROPERTY_CLASSNAME).toString(); sourceEdge.setIdField(GremlinUtils.getIdField(GremlinUtils.toEntityClass(className))); - sourceEdge.setId(map.get(PROPERTY_ID)); - sourceEdge.setLabel(map.get(PROPERTY_LABEL).toString()); - sourceEdge.setVertexIdFrom(map.get(PROPERTY_OUTV)); - sourceEdge.setVertexIdTo(map.get(PROPERTY_INV)); + sourceEdge.setId(getPropertyValue(map, PROPERTY_ID)); + sourceEdge.setLabel(getPropertyValue(map, PROPERTY_LABEL).toString()); + sourceEdge.setVertexIdFrom(getPropertyValue(map, PROPERTY_OUTV)); + sourceEdge.setVertexIdTo(getPropertyValue(map, PROPERTY_INV)); } } diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java index ae219f8f..ae642877 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultVertexReader.java @@ -14,7 +14,6 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -46,16 +45,6 @@ private Map getVertexProperties (@NonNull Result result) { return map; } - private Object getVertexProperty (@NonNull Map map, @NonNull String propertyKey) { - Object value = map.get(propertyKey); - - while ((value instanceof LinkedHashMap) && ((LinkedHashMap) value).containsKey(PROPERTY_VALUE_WITH_AT)) { - value = ((LinkedHashMap) value).get(PROPERTY_VALUE_WITH_AT); - } - - return value; - } - @Override @SuppressWarnings("unchecked") public void read(@NonNull List results, @NonNull GremlinSource source) { @@ -73,7 +62,7 @@ public void read(@NonNull List results, @NonNull GremlinSource source) { final String className = source.getProperties().get(GREMLIN_PROPERTY_CLASSNAME).toString(); source.setIdField(GremlinUtils.getIdField(GremlinUtils.toEntityClass(className))); - source.setId(getVertexProperty(map, PROPERTY_ID)); - source.setLabel(getVertexProperty(map, PROPERTY_LABEL).toString()); + source.setId(getPropertyValue(map, PROPERTY_ID)); + source.setLabel(getPropertyValue(map, PROPERTY_LABEL).toString()); } } From 5b159bb153af0087b10b13c4c11094fccf9191b1 Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 18:15:18 -0400 Subject: [PATCH 05/10] Removed unused function --- .../result/AbstractGremlinResultReader.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index 1adae31b..f56f8363 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -19,7 +19,6 @@ import static com.microsoft.spring.data.gremlin.common.Constants.PROPERTY_VALUE_WITH_AT; @NoArgsConstructor -// TODO: seems only for Vertex. public abstract class AbstractGremlinResultReader { /** @@ -75,14 +74,4 @@ protected void readResultProperties(@NonNull Map properties, @No source.getProperties().clear(); properties.forEach((key, value) -> source.setProperty(key, this.readProperty(value))); } - - protected void readIdentifier(@NonNull Map dataMap, @NonNull GremlinSource source) { - Assert.isTrue( - source.getLabel().equals(dataMap.get(PROPERTY_LABEL)), - "mapping an invalid object data: " + dataMap.get(PROPERTY_LABEL)); - - if (source.getId() == null && dataMap.containsKey(PROPERTY_ID)) { - source.setId(dataMap.get(PROPERTY_ID)); - } - } } From 56f3fff7b6072a42b5f653b2529ff494b9a0cc70 Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sat, 28 Sep 2019 18:16:13 -0400 Subject: [PATCH 06/10] Removed unused imports --- .../gremlin/conversion/result/AbstractGremlinResultReader.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index f56f8363..a0e966af 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -15,7 +15,6 @@ import java.util.LinkedHashMap; import java.util.Map; -import static com.microsoft.spring.data.gremlin.common.Constants.*; import static com.microsoft.spring.data.gremlin.common.Constants.PROPERTY_VALUE_WITH_AT; @NoArgsConstructor From c37de684ce9c6ad7d00fcedffc612b4ad9de01de Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sun, 29 Sep 2019 07:54:29 -0400 Subject: [PATCH 07/10] Fixed Edge data structure issue --- .../spring/data/gremlin/common/Constants.java | 1 + .../result/AbstractGremlinResultReader.java | 13 ++++++---- .../result/GremlinResultEdgeReader.java | 25 +++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java b/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java index 62463518..cdaf4971 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/common/Constants.java @@ -19,6 +19,7 @@ public class Constants { public static final String PROPERTY_PROPERTIES = "properties"; public static final String PROPERTY_INV = "inV"; public static final String PROPERTY_OUTV = "outV"; + public static final String PROPERTY_RELATION_ID = "relationId"; public static final String RESULT_TYPE_VERTEX = "vertex"; public static final String RESULT_TYPE_EDGE = "edge"; diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index a0e966af..ff9ceffa 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -11,6 +11,7 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; @@ -57,14 +58,16 @@ protected Object getPropertyValue (@NonNull Map map, @NonNull St * T is LinkedHashMap */ private Object readProperty(@NonNull Object value) { - Assert.isInstanceOf(ArrayList.class, value, "should be instance of ArrayList"); + if (value instanceof ArrayList) { + @SuppressWarnings("unchecked") final ArrayList> mapList + = (ArrayList>) value; - @SuppressWarnings("unchecked") final ArrayList> mapList - = (ArrayList>) value; + Assert.isTrue(mapList.size() == 1, "should be only 1 element in ArrayList"); - Assert.isTrue(mapList.size() == 1, "should be only 1 element in ArrayList"); + value = mapList.get(0); + } - final Map renovatedMap = getProperties(mapList.get(0)); + final Map renovatedMap = getProperties((Map) value); return renovatedMap.get(Constants.PROPERTY_VALUE); } diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java index 9ee49a42..69f198ea 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java @@ -10,11 +10,11 @@ import com.microsoft.spring.data.gremlin.conversion.source.GremlinSourceEdge; import com.microsoft.spring.data.gremlin.exception.GremlinUnexpectedSourceTypeException; import lombok.NoArgsConstructor; -import lombok.NonNull; import org.apache.tinkerpop.gremlin.driver.Result; -import org.springframework.lang.Nullable; +import org.springframework.lang.NonNull; import org.springframework.util.Assert; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -23,14 +23,6 @@ @NoArgsConstructor public class GremlinResultEdgeReader extends AbstractGremlinResultReader implements GremlinResultsReader { - private void readProperties(@NonNull GremlinSource source, @Nullable Map map) { - if (map != null) { - @SuppressWarnings("unchecked") final Map properties = (Map) map; - - properties.forEach(source::setProperty); - } - } - private void validate(List results, GremlinSource source) { Assert.notNull(results, "Results should not be null."); Assert.notNull(source, "GremlinSource should not be null."); @@ -54,6 +46,17 @@ private Map getEdgeProperties (@org.springframework.lang.NonNull return map; } + @Override + protected Object getPropertyValue (@NonNull Map map, @NonNull String propertyKey) { + Object value = super.getPropertyValue(map, propertyKey); + + if (value instanceof LinkedHashMap && ((LinkedHashMap) value).containsKey(PROPERTY_RELATION_ID)) { + value = ((LinkedHashMap) value).get(PROPERTY_RELATION_ID); + } + + return value; + } + @Override @SuppressWarnings("unchecked") public void read(@NonNull List results, @NonNull GremlinSource source) { @@ -66,7 +69,7 @@ public void read(@NonNull List results, @NonNull GremlinSource source) { final GremlinSourceEdge sourceEdge = (GremlinSourceEdge) source; final Map map = getEdgeProperties(results.get(0)); - this.readProperties(source, (Map) map.get(PROPERTY_PROPERTIES)); + super.readResultProperties((Map) map.get(PROPERTY_PROPERTIES), source); final String className = source.getProperties().get(GREMLIN_PROPERTY_CLASSNAME).toString(); From db65e28d6fc90e5796c80869b8dc5cdd3a040e22 Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Sun, 29 Sep 2019 08:44:52 -0400 Subject: [PATCH 08/10] Fixed code style issue --- .../result/AbstractGremlinResultReader.java | 12 ++++++------ .../conversion/result/GremlinResultEdgeReader.java | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java index ff9ceffa..d01ff8f8 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/AbstractGremlinResultReader.java @@ -11,7 +11,6 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; @@ -27,16 +26,17 @@ public abstract class AbstractGremlinResultReader { * @return Map of list properties */ protected Map getProperties (@NonNull Map map) { - while ((map instanceof LinkedHashMap) && map.containsKey(PROPERTY_VALUE_WITH_AT)) { - final Object value = map.get(PROPERTY_VALUE_WITH_AT); + Map propertyMap = map; + while ((propertyMap instanceof LinkedHashMap) && propertyMap.containsKey(PROPERTY_VALUE_WITH_AT)) { + final Object value = propertyMap.get(PROPERTY_VALUE_WITH_AT); if (value instanceof ArrayList && ((ArrayList) value).size() > 0) { - map = (Map) ((ArrayList) value).get(0); + propertyMap = (Map) ((ArrayList) value).get(0); } else { - map = (Map) value; + propertyMap = (Map) value; } } - return map; + return propertyMap; } protected Object getPropertyValue (@NonNull Map map, @NonNull String propertyKey) { diff --git a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java index 69f198ea..382ef50e 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/conversion/result/GremlinResultEdgeReader.java @@ -29,7 +29,7 @@ private void validate(List results, GremlinSource source) { Assert.isTrue(results.size() == 1, "Edge should contain only one result."); } - private Map getEdgeProperties (@org.springframework.lang.NonNull Result result) { + private Map getEdgeProperties (@NonNull Result result) { Assert.isInstanceOf(Map.class, result.getObject(), "should be one instance of Map"); Map map = (Map) result.getObject(); From aa8197a9f2f250b45c9971d3e226dec58bcf9d26 Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Mon, 30 Sep 2019 19:08:27 -0400 Subject: [PATCH 09/10] Fixed findAll() function --- .../data/gremlin/query/GremlinTemplate.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java index f84a554b..8c532315 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java @@ -8,6 +8,7 @@ import com.microsoft.spring.data.gremlin.annotation.EdgeFrom; import com.microsoft.spring.data.gremlin.annotation.EdgeTo; import com.microsoft.spring.data.gremlin.annotation.GeneratedValue; +import com.microsoft.spring.data.gremlin.common.Constants; import com.microsoft.spring.data.gremlin.common.GremlinEntityType; import com.microsoft.spring.data.gremlin.common.GremlinFactory; import com.microsoft.spring.data.gremlin.common.GremlinUtils; @@ -41,10 +42,9 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; @@ -94,13 +94,31 @@ private List executeQuery(@NonNull List queries) { private List executeQueryParallel(@NonNull List queries) { return queries.parallelStream() .map(q -> getGremlinClient().submit(q).all()) - .collect(toList()).parallelStream().flatMap(f -> { + .collect(toList()) + .parallelStream() + .flatMap(f -> { try { return f.get().stream(); } catch (InterruptedException | ExecutionException e) { throw new GremlinQueryException("unable to complete query from gremlin", e); } }) + .flatMap(r -> { + final List results = new ArrayList<>(); + Object object = r.getObject(); + while (object instanceof LinkedHashMap + && ((LinkedHashMap) object).containsKey(Constants.PROPERTY_VALUE_WITH_AT)) { + object = ((LinkedHashMap) object).get(Constants.PROPERTY_VALUE_WITH_AT); + } + + if (object instanceof ArrayList) { + ((ArrayList) object).forEach(o -> results.add(new Result(o))); + } else { + results.add(new Result(object)); + } + + return results.stream(); + }) .collect(toList()); } @@ -349,7 +367,10 @@ private T recoverDomain(@NonNull GremlinSource source, @NonNull List List recoverDomainList(@NonNull GremlinSource source, @NonNull List results) { - return results.stream().map(r -> recoverDomain(source, Collections.singletonList(r))).collect(toList()); + return results + .stream() + .map(r -> recoverDomain(source, Collections.singletonList(r))) + .collect(toList()); } private T recoverGraphDomain(@NonNull GremlinSourceGraph source, @NonNull List results) { From 1e043e5bcf3242a61f03136ac5a96e386d5381fd Mon Sep 17 00:00:00 2001 From: Tin Nguyen Date: Mon, 30 Sep 2019 22:18:31 -0400 Subject: [PATCH 10/10] Support query data by defined query with the function findAllByQuery --- .../spring/data/gremlin/query/GremlinOperations.java | 2 ++ .../spring/data/gremlin/query/GremlinTemplate.java | 12 +++++++++++- .../data/gremlin/repository/GremlinRepository.java | 3 +++ .../repository/support/SimpleGremlinRepository.java | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinOperations.java b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinOperations.java index d92e0c5e..f583dc87 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinOperations.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinOperations.java @@ -49,5 +49,7 @@ public interface GremlinOperations { List find(GremlinQuery query, GremlinSource source); + List findByQuery(List queries, GremlinSource source); + MappingGremlinConverter getMappingConverter(); } diff --git a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java index 8c532315..8bfe86b9 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/query/GremlinTemplate.java @@ -44,7 +44,6 @@ import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.ExecutionException; -import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; @@ -403,5 +402,16 @@ public List find(@NonNull GremlinQuery query, @NonNull GremlinSource s return this.recoverDomainList(source, results); } + + @Override + public List findByQuery(@NonNull List queryList, @NonNull GremlinSource source) { + final List results = this.executeQuery(queryList); + + if (results.isEmpty()) { + return Collections.emptyList(); + } + + return this.recoverDomainList(source, results); + } } diff --git a/src/main/java/com/microsoft/spring/data/gremlin/repository/GremlinRepository.java b/src/main/java/com/microsoft/spring/data/gremlin/repository/GremlinRepository.java index a47ed614..df46331c 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/repository/GremlinRepository.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/repository/GremlinRepository.java @@ -10,12 +10,15 @@ import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable; +import java.util.List; @NoRepositoryBean public interface GremlinRepository extends CrudRepository { Iterable findAll(Class domainClass); + Iterable findAllByQuery(List queryList); + void deleteAll(GremlinEntityType type); void deleteAll(Class domainClass); diff --git a/src/main/java/com/microsoft/spring/data/gremlin/repository/support/SimpleGremlinRepository.java b/src/main/java/com/microsoft/spring/data/gremlin/repository/support/SimpleGremlinRepository.java index d284cb3e..541d059f 100644 --- a/src/main/java/com/microsoft/spring/data/gremlin/repository/support/SimpleGremlinRepository.java +++ b/src/main/java/com/microsoft/spring/data/gremlin/repository/support/SimpleGremlinRepository.java @@ -133,5 +133,9 @@ public void deleteAll(@NonNull Class domainClass) { public boolean existsById(@NonNull ID id) { return this.operations.existsById(id, this.information.createGremlinSource()); } + + public Iterable findAllByQuery(@NonNull List queries) { + return this.operations.findByQuery(queries, this.information.createGremlinSource()); + } }