You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/08/13 22:03:20 UTC
[03/22] incubator-tinkerpop git commit: Merge branch 'master' of
https://github.com/apache/incubator-tinkerpop
Merge branch 'master' of https://github.com/apache/incubator-tinkerpop
Conflicts:
gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoTest.java
Continued fleshing out GraphSONLegacyReader for use with hadoop-gremlin
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/536946e3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/536946e3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/536946e3
Branch: refs/heads/tp30-legson
Commit: 536946e36afd84dbc5c921bb74ef8010c5822778
Parents: 0fa53ea
Author: ebice <eb...@behaviormatrix.com>
Authored: Fri Jun 19 16:35:38 2015 -0400
Committer: ebice <eb...@behaviormatrix.com>
Committed: Fri Jun 19 16:35:38 2015 -0400
----------------------------------------------------------------------
.../io/graphson/GraphSONLegacyReader.java | 70 ++++++--
.../io/graphson/GraphSONTokensTP2.java | 33 +++-
.../io/graphson/LegacyGraphSONReader.java | 172 -------------------
.../io/graphson/LegacyGraphSONUtility.java | 160 ++++++++++++++++-
.../gremlin/structure/util/star/StarGraph.java | 4 +-
.../io/graphson/GraphSONLegacyInputFormat.java | 2 +-
6 files changed, 244 insertions(+), 197 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONLegacyReader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONLegacyReader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONLegacyReader.java
index 0c53ad1..bdf3e43 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONLegacyReader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONLegacyReader.java
@@ -82,10 +82,12 @@ public final class GraphSONLegacyReader implements GraphReader {
// dual pass - create all vertices and store to cache the ids. then create edges. as long as we don't
// have vertex labels in the output we can't do this single pass
final Map<StarGraph.StarVertex,Vertex> cache = new HashMap<>();
+ //final Map<Object,Vertex> cache = new HashMap<>();
final AtomicLong counter = new AtomicLong(0);
final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();
final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();
+ final Graph.Features.VertexFeatures vertexFeatures = graphToWriteTo.features().vertex();
final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
br.lines().<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), null, null, Direction.OUT))).forEach(vertex -> {
@@ -157,19 +159,57 @@ public final class GraphSONLegacyReader implements GraphReader {
final Function<Attachable<Vertex>, Vertex> vertexAttachMethod,
final Function<Attachable<Edge>, Edge> edgeAttachMethod,
final Direction attachEdgesOfThisDirection) throws IOException {
- final Map<String, Object> vertexData = mapper.readValue(inputStream, mapTypeReference);
- final StarGraph starGraph = StarGraphGraphSONSerializer.readStarGraphVertex(vertexData);
- if (vertexAttachMethod != null) vertexAttachMethod.apply(starGraph.getStarVertex());
- if (vertexData.containsKey(GraphSONTokens.OUT_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.OUT))
- StarGraphGraphSONSerializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.OUT_E);
+ final StarGraph starGraph = StarGraph.open();
- if (vertexData.containsKey(GraphSONTokens.IN_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.IN))
- StarGraphGraphSONSerializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.IN_E);
+ final JsonFactory factory = mapper.getFactory();
+ try (JsonParser parser = factory.createParser(inputStream)) {
+
+ final JsonNode node = parser.readValueAsTree();
+ final Map<String, Object> vertexData = LegacyGraphSONUtility.readProperties(node);
+ starGraph.addVertex(T.id, vertexData.get(GraphSONTokensTP2._ID), T.label, vertexData.get(GraphSONTokensTP2._LABEL));
+ for (Map.Entry<String, Object> p : vertexData.entrySet()) {
+ if (LegacyGraphSONUtility.isReservedKey(p.getKey()))
+ continue;
+ starGraph.getStarVertex().property(p.getKey(), p.getValue()); //cardinality? .getCardinality(p.getKey())
+ }
+ if (vertexAttachMethod != null) vertexAttachMethod.apply(starGraph.getStarVertex());
+
+ if (vertexData.containsKey(GraphSONTokensTP2._OUT_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.OUT))
+ readEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.OUT_E);
+
+ if (vertexData.containsKey(GraphSONTokensTP2._IN_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.IN))
+ readEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.IN_E);
+
+ } catch (Exception ex) {
+ throw new IOException(ex);
+ }
return starGraph.getStarVertex();
}
+ public static void readEdges(final Function<Attachable<Edge>, Edge> edgeMaker,
+ final StarGraph starGraph,
+ final Map<String, Object> vertexData,
+ final String direction) throws IOException {
+ final Map<String, List<Map<String,Object>>> edgeDatas = (Map<String, List<Map<String,Object>>>) vertexData.get(direction);
+ for (Map.Entry<String, List<Map<String,Object>>> edgeData : edgeDatas.entrySet()) {
+ for (Map<String,Object> inner : edgeData.getValue()) {
+ final StarGraph.StarEdge starEdge;
+ String edgeLabel = inner.get(GraphSONTokensTP2._LABEL).toString();
+ if (direction.equals(GraphSONTokens.OUT_E))
+ starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addOutEdge(edgeLabel, starGraph.addVertex(T.id, inner.get(GraphSONTokensTP2._IN_V)), T.id, inner.get(GraphSONTokensTP2._ID));
+ else
+ starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addInEdge(edgeLabel, starGraph.addVertex(T.id, inner.get(GraphSONTokensTP2._OUT_V)), T.id, inner.get(GraphSONTokensTP2._ID));
+ for (Map.Entry<String, Object> epd : inner.entrySet()) {
+ if (!LegacyGraphSONUtility.isReservedKey(epd.getKey()))
+ starEdge.property(epd.getKey(), epd.getValue());
+ }
+ if (edgeMaker != null) edgeMaker.apply(starEdge);
+ }
+ }
+ }
+
/**
* Read an {@link Edge} from output generated by {@link GraphSONWriter#writeEdge(OutputStream, Edge)} or via
* an {@link Edge} passed to {@link GraphSONWriter#writeObject(OutputStream, Object)}.
@@ -184,11 +224,11 @@ public final class GraphSONLegacyReader implements GraphReader {
final Map<String,Object> edgeProperties = edgeData.containsKey(GraphSONTokens.PROPERTIES) ?
(Map<String, Object>) edgeData.get(GraphSONTokens.PROPERTIES) : Collections.EMPTY_MAP;
- final DetachedEdge edge = new DetachedEdge(edgeData.get(GraphSONTokens.ID),
- edgeData.get(GraphSONTokens.LABEL).toString(),
+ final DetachedEdge edge = new DetachedEdge(edgeData.get(GraphSONTokensTP2._ID),
+ edgeData.get(GraphSONTokensTP2._LABEL).toString(),
edgeProperties,
- Pair.with(edgeData.get(GraphSONTokens.OUT), edgeData.get(GraphSONTokens.OUT_LABEL).toString()),
- Pair.with(edgeData.get(GraphSONTokens.IN), edgeData.get(GraphSONTokens.IN_LABEL).toString()));
+ Pair.with(edgeData.get(GraphSONTokensTP2._OUT_V), null),
+ Pair.with(edgeData.get(GraphSONTokensTP2._IN_V), null));
return edgeAttachMethod.apply(edge);
}
@@ -206,12 +246,14 @@ public final class GraphSONLegacyReader implements GraphReader {
@Override
public VertexProperty readVertexProperty(final InputStream inputStream,
final Function<Attachable<VertexProperty>, VertexProperty> vertexPropertyAttachMethod) throws IOException {
- final Map<String, Object> vpData = mapper.readValue(inputStream, mapTypeReference);
+ /*final Map<String, Object> vpData = mapper.readValue(inputStream, mapTypeReference);
final Map<String, Object> metaProperties = (Map<String, Object>) vpData.get(GraphSONTokens.PROPERTIES);
final DetachedVertexProperty vp = new DetachedVertexProperty(vpData.get(GraphSONTokens.ID),
vpData.get(GraphSONTokens.LABEL).toString(),
vpData.get(GraphSONTokens.VALUE), metaProperties);
return vertexPropertyAttachMethod.apply(vp);
+ */
+ return null;
}
/**
@@ -225,9 +267,11 @@ public final class GraphSONLegacyReader implements GraphReader {
@Override
public Property readProperty(final InputStream inputStream,
final Function<Attachable<Property>, Property> propertyAttachMethod) throws IOException {
- final Map<String, Object> propertyData = mapper.readValue(inputStream, mapTypeReference);
+ /*final Map<String, Object> propertyData = mapper.readValue(inputStream, mapTypeReference);
final DetachedProperty p = new DetachedProperty(propertyData.get(GraphSONTokens.KEY).toString(), propertyData.get(GraphSONTokens.VALUE));
return propertyAttachMethod.apply(p);
+ */
+ return null;
}
/**
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokensTP2.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokensTP2.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokensTP2.java
index f7f1814..c7575a1 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokensTP2.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokensTP2.java
@@ -1,7 +1,32 @@
package org.apache.tinkerpop.gremlin.structure.io.graphson;
-/**
- * Created by ebice on 6/19/2015.
- */
-public class GraphSONTokensTP2 {
+public final class GraphSONTokensTP2 {
+
+ private GraphSONTokensTP2() {}
+
+ public static final String _ID = "_id";
+ public static final String _LABEL = "_label";
+ public static final String _TYPE = "_type";
+ public static final String _OUT_V = "_outV";
+ public static final String _IN_V = "_inV";
+ public static final String _OUT_E = "_outE";
+ public static final String _IN_E = "_inE";
+ public static final String VALUE = "value";
+ public static final String TYPE = "type";
+ public static final String TYPE_LIST = "list";
+ public static final String TYPE_STRING = "string";
+ public static final String TYPE_DOUBLE = "double";
+ public static final String TYPE_INTEGER = "integer";
+ public static final String TYPE_FLOAT = "float";
+ public static final String TYPE_MAP = "map";
+ public static final String TYPE_BOOLEAN = "boolean";
+ public static final String TYPE_LONG = "long";
+ public static final String TYPE_SHORT = "short";
+ public static final String TYPE_BYTE = "byte";
+ public static final String TYPE_UNKNOWN = "unknown";
+
+ public static final String VERTICES = "vertices";
+ public static final String EDGES = "edges";
+ public static final String MODE = "mode";
+
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONReader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONReader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONReader.java
index ba8255a..d727068 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONReader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONReader.java
@@ -244,176 +244,4 @@ public final class LegacyGraphSONReader implements GraphReader {
}
}
- static class LegacyGraphSONUtility {
- private static final String EMPTY_STRING = "";
- private final Graph g;
- private final Graph.Features.VertexFeatures vertexFeatures;
- private final Graph.Features.EdgeFeatures edgeFeatures;
- private final Map<Object,Vertex> cache;
-
- public LegacyGraphSONUtility(final Graph g, final Graph.Features.VertexFeatures vertexFeatures,
- final Graph.Features.EdgeFeatures edgeFeatures,
- final Map<Object, Vertex> cache) {
- this.g = g;
- this.vertexFeatures = vertexFeatures;
- this.edgeFeatures = edgeFeatures;
- this.cache = cache;
- }
-
- public Vertex vertexFromJson(final JsonNode json) throws IOException {
- final Map<String, Object> props = readProperties(json);
-
- final Object vertexId = getTypedValueFromJsonNode(json.get(GraphSONTokensTP2._ID));
- final Vertex v = vertexFeatures.willAllowId(vertexId) ? g.addVertex(T.id, vertexId) : g.addVertex();
- cache.put(vertexId, v);
-
- for (Map.Entry<String, Object> entry : props.entrySet()) {
- v.property(g.features().vertex().getCardinality(entry.getKey()), entry.getKey(), entry.getValue());
- }
-
- return v;
- }
-
- public Edge edgeFromJson(final JsonNode json, final Vertex out, final Vertex in) throws IOException {
- final Map<String, Object> props = LegacyGraphSONUtility.readProperties(json);
-
- final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokensTP2._ID));
- final JsonNode nodeLabel = json.get(GraphSONTokensTP2._LABEL);
- final String label = nodeLabel == null ? EMPTY_STRING : nodeLabel.textValue();
-
- final Edge e = edgeFeatures.willAllowId(edgeId) ? out.addEdge(label, in, T.id, edgeId) : out.addEdge(label, in) ;
- for (Map.Entry<String, Object> entry : props.entrySet()) {
- e.property(entry.getKey(), entry.getValue());
- }
-
- return e;
- }
-
- static Map<String, Object> readProperties(final JsonNode node) {
- final Map<String, Object> map = new HashMap<>();
-
- final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
- while (iterator.hasNext()) {
- final Map.Entry<String, JsonNode> entry = iterator.next();
-
- if (!isReservedKey(entry.getKey())) {
- // it generally shouldn't be as such but graphson containing null values can't be shoved into
- // element property keys or it will result in error
- final Object o = readProperty(entry.getValue());
- if (o != null) {
- map.put(entry.getKey(), o);
- }
- }
- }
-
- return map;
- }
-
- private static boolean isReservedKey(final String key) {
- return key.equals(GraphSONTokensTP2._ID) || key.equals(GraphSONTokensTP2._TYPE) || key.equals(GraphSONTokensTP2._LABEL)
- || key.equals(GraphSONTokensTP2._OUT_V) || key.equals(GraphSONTokensTP2._IN_V);
- }
-
- private static Object readProperty(final JsonNode node) {
- final Object propertyValue;
-
- if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_UNKNOWN)) {
- propertyValue = null;
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_BOOLEAN)) {
- propertyValue = node.get(GraphSONTokensTP2.VALUE).booleanValue();
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_FLOAT)) {
- propertyValue = Float.parseFloat(node.get(GraphSONTokensTP2.VALUE).asText());
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_BYTE)) {
- propertyValue = Byte.parseByte(node.get(GraphSONTokensTP2.VALUE).asText());
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_SHORT)) {
- propertyValue = Short.parseShort(node.get(GraphSONTokensTP2.VALUE).asText());
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_DOUBLE)) {
- propertyValue = node.get(GraphSONTokensTP2.VALUE).doubleValue();
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_INTEGER)) {
- propertyValue = node.get(GraphSONTokensTP2.VALUE).intValue();
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_LONG)) {
- propertyValue = node.get(GraphSONTokensTP2.VALUE).longValue();
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_STRING)) {
- propertyValue = node.get(GraphSONTokensTP2.VALUE).textValue();
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_LIST)) {
- propertyValue = readProperties(node.get(GraphSONTokensTP2.VALUE).elements());
- } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_MAP)) {
- propertyValue = readProperties(node.get(GraphSONTokensTP2.VALUE));
- } else {
- propertyValue = node.textValue();
- }
-
- return propertyValue;
- }
-
- private static List readProperties(final Iterator<JsonNode> listOfNodes) {
- final List<Object> array = new ArrayList<>();
-
- while (listOfNodes.hasNext()) {
- array.add(readProperty(listOfNodes.next()));
- }
-
- return array;
- }
-
- static Object getTypedValueFromJsonNode(final JsonNode node) {
- Object theValue = null;
-
- if (node != null && !node.isNull()) {
- if (node.isBoolean()) {
- theValue = node.booleanValue();
- } else if (node.isDouble()) {
- theValue = node.doubleValue();
- } else if (node.isFloatingPointNumber()) {
- theValue = node.floatValue();
- } else if (node.isInt()) {
- theValue = node.intValue();
- } else if (node.isLong()) {
- theValue = node.longValue();
- } else if (node.isTextual()) {
- theValue = node.textValue();
- } else if (node.isArray()) {
- // this is an array so just send it back so that it can be
- // reprocessed to its primitive components
- theValue = node;
- } else if (node.isObject()) {
- // this is an object so just send it back so that it can be
- // reprocessed to its primitive components
- theValue = node;
- } else {
- theValue = node.textValue();
- }
- }
-
- return theValue;
- }
- }
-
- public final static class GraphSONTokensTP2 {
-
- private GraphSONTokensTP2() {}
-
- public static final String _ID = "_id";
- public static final String _LABEL = "_label";
- public static final String _TYPE = "_type";
- public static final String _OUT_V = "_outV";
- public static final String _IN_V = "_inV";
- public static final String VALUE = "value";
- public static final String TYPE = "type";
- public static final String TYPE_LIST = "list";
- public static final String TYPE_STRING = "string";
- public static final String TYPE_DOUBLE = "double";
- public static final String TYPE_INTEGER = "integer";
- public static final String TYPE_FLOAT = "float";
- public static final String TYPE_MAP = "map";
- public static final String TYPE_BOOLEAN = "boolean";
- public static final String TYPE_LONG = "long";
- public static final String TYPE_SHORT = "short";
- public static final String TYPE_BYTE = "byte";
- public static final String TYPE_UNKNOWN = "unknown";
-
- public static final String VERTICES = "vertices";
- public static final String EDGES = "edges";
- public static final String MODE = "mode";
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONUtility.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONUtility.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONUtility.java
index 932212b..5d306b2 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONUtility.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/LegacyGraphSONUtility.java
@@ -1,7 +1,157 @@
package org.apache.tinkerpop.gremlin.structure.io.graphson;
-/**
- * Created by ebice on 6/19/2015.
- */
-public class LegacyGraphSONUtility {
-}
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.T;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+
+import java.io.IOException;
+import java.util.*;
+
+public final class LegacyGraphSONUtility {
+
+ private static final String EMPTY_STRING = "";
+ private final Graph g;
+ private final Graph.Features.VertexFeatures vertexFeatures;
+ private final Graph.Features.EdgeFeatures edgeFeatures;
+ private final Map<Object,Vertex> cache;
+
+ public LegacyGraphSONUtility(final Graph g, final Graph.Features.VertexFeatures vertexFeatures,
+ final Graph.Features.EdgeFeatures edgeFeatures,
+ final Map<Object, Vertex> cache) {
+ this.g = g;
+ this.vertexFeatures = vertexFeatures;
+ this.edgeFeatures = edgeFeatures;
+ this.cache = cache;
+ }
+
+ public Vertex vertexFromJson(final JsonNode json) throws IOException {
+ final Map<String, Object> props = readProperties(json);
+
+ final Object vertexId = getTypedValueFromJsonNode(json.get(GraphSONTokensTP2._ID));
+ final Vertex v = vertexFeatures.willAllowId(vertexId) ? g.addVertex(T.id, vertexId) : g.addVertex();
+ cache.put(vertexId, v);
+
+ for (Map.Entry<String, Object> entry : props.entrySet()) {
+ v.property(g.features().vertex().getCardinality(entry.getKey()), entry.getKey(), entry.getValue());
+ }
+
+ return v;
+ }
+
+ public Edge edgeFromJson(final JsonNode json, final Vertex out, final Vertex in) throws IOException {
+ final Map<String, Object> props = LegacyGraphSONUtility.readProperties(json);
+
+ final Object edgeId = getTypedValueFromJsonNode(json.get(GraphSONTokensTP2._ID));
+ final JsonNode nodeLabel = json.get(GraphSONTokensTP2._LABEL);
+ final String label = nodeLabel == null ? EMPTY_STRING : nodeLabel.textValue();
+
+ final Edge e = edgeFeatures.willAllowId(edgeId) ? out.addEdge(label, in, T.id, edgeId) : out.addEdge(label, in) ;
+ for (Map.Entry<String, Object> entry : props.entrySet()) {
+ e.property(entry.getKey(), entry.getValue());
+ }
+
+ return e;
+ }
+
+ public static Map<String, Object> readProperties(final JsonNode node) {
+ final Map<String, Object> map = new HashMap<>();
+
+ final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
+ while (iterator.hasNext()) {
+ final Map.Entry<String, JsonNode> entry = iterator.next();
+
+ if (!isReservedKey(entry.getKey())) {
+ // it generally shouldn't be as such but graphson containing null values can't be shoved into
+ // element property keys or it will result in error
+ final Object o = readProperty(entry.getValue());
+ if (o != null) {
+ map.put(entry.getKey(), o);
+ }
+ }
+ }
+
+ return map;
+ }
+
+ public static boolean isReservedKey(final String key) {
+ return key.equals(GraphSONTokensTP2._ID) || key.equals(GraphSONTokensTP2._TYPE) || key.equals(GraphSONTokensTP2._LABEL)
+ || key.equals(GraphSONTokensTP2._OUT_V) || key.equals(GraphSONTokensTP2._IN_V)
+ || key.equals(GraphSONTokensTP2._IN_E) || key.equals(GraphSONTokensTP2._OUT_E);
+ }
+
+ private static Object readProperty(final JsonNode node) {
+ final Object propertyValue;
+
+ if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_UNKNOWN)) {
+ propertyValue = null;
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_BOOLEAN)) {
+ propertyValue = node.get(GraphSONTokensTP2.VALUE).booleanValue();
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_FLOAT)) {
+ propertyValue = Float.parseFloat(node.get(GraphSONTokensTP2.VALUE).asText());
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_BYTE)) {
+ propertyValue = Byte.parseByte(node.get(GraphSONTokensTP2.VALUE).asText());
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_SHORT)) {
+ propertyValue = Short.parseShort(node.get(GraphSONTokensTP2.VALUE).asText());
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_DOUBLE)) {
+ propertyValue = node.get(GraphSONTokensTP2.VALUE).doubleValue();
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_INTEGER)) {
+ propertyValue = node.get(GraphSONTokensTP2.VALUE).intValue();
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_LONG)) {
+ propertyValue = node.get(GraphSONTokensTP2.VALUE).longValue();
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_STRING)) {
+ propertyValue = node.get(GraphSONTokensTP2.VALUE).textValue();
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_LIST)) {
+ propertyValue = readProperties(node.get(GraphSONTokensTP2.VALUE).elements());
+ } else if (node.get(GraphSONTokensTP2.TYPE).textValue().equals(GraphSONTokensTP2.TYPE_MAP)) {
+ propertyValue = readProperties(node.get(GraphSONTokensTP2.VALUE));
+ } else {
+ propertyValue = node.textValue();
+ }
+
+ return propertyValue;
+ }
+
+ private static List readProperties(final Iterator<JsonNode> listOfNodes) {
+ final List<Object> array = new ArrayList<>();
+
+ while (listOfNodes.hasNext()) {
+ array.add(readProperty(listOfNodes.next()));
+ }
+
+ return array;
+ }
+
+ static Object getTypedValueFromJsonNode(final JsonNode node) {
+ Object theValue = null;
+
+ if (node != null && !node.isNull()) {
+ if (node.isBoolean()) {
+ theValue = node.booleanValue();
+ } else if (node.isDouble()) {
+ theValue = node.doubleValue();
+ } else if (node.isFloatingPointNumber()) {
+ theValue = node.floatValue();
+ } else if (node.isInt()) {
+ theValue = node.intValue();
+ } else if (node.isLong()) {
+ theValue = node.longValue();
+ } else if (node.isTextual()) {
+ theValue = node.textValue();
+ } else if (node.isArray()) {
+ // this is an array so just send it back so that it can be
+ // reprocessed to its primitive components
+ theValue = node;
+ } else if (node.isObject()) {
+ // this is an object so just send it back so that it can be
+ // reprocessed to its primitive components
+ theValue = node;
+ } else {
+ theValue = node.textValue();
+ }
+ }
+
+ return theValue;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
index 9541cdf..bd5953f 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
@@ -286,7 +286,7 @@ public final class StarGraph implements Graph, Serializable {
return this.property(VertexProperty.Cardinality.single, key, value, keyValues);
}
- Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
+ public Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
ElementHelper.validateLabel(label);
ElementHelper.legalPropertyKeyValueArray(keyValues);
if (null == this.outEdges)
@@ -302,7 +302,7 @@ public final class StarGraph implements Graph, Serializable {
return outEdge;
}
- Edge addInEdge(final String label, final Vertex outVertex, final Object... keyValues) {
+ public Edge addInEdge(final String label, final Vertex outVertex, final Object... keyValues) {
ElementHelper.validateLabel(label);
ElementHelper.legalPropertyKeyValueArray(keyValues);
if (null == this.inEdges)
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/536946e3/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/graphson/GraphSONLegacyInputFormat.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/graphson/GraphSONLegacyInputFormat.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/graphson/GraphSONLegacyInputFormat.java
index ed38f46..af66d11 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/graphson/GraphSONLegacyInputFormat.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/graphson/GraphSONLegacyInputFormat.java
@@ -20,7 +20,7 @@ public final class GraphSONLegacyInputFormat extends FileInputFormat<NullWritabl
@Override
public RecordReader<NullWritable, VertexWritable> createRecordReader(final InputSplit split, final TaskAttemptContext context) throws IOException, InterruptedException {
- RecordReader<NullWritable, VertexWritable> reader = new GraphSONRecordReader();
+ RecordReader<NullWritable, VertexWritable> reader = new GraphSONLegacyRecordReader();
reader.initialize(split, context);
return reader;
}