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/04/28 17:29:43 UTC

incubator-tinkerpop git commit: Moved "reader" helper methods to StarGraphGraphSONSerializer.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master be0653e04 -> 8a46154ee


Moved "reader" helper methods to StarGraphGraphSONSerializer.

Not really "deserializers" but provides a standard way to read the Map generated by the real serializer.  This allows the addOut/InEdge to go back to being protected in scope.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/8a46154e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/8a46154e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/8a46154e

Branch: refs/heads/master
Commit: 8a46154ee1c72ff7d319695173f02d92e963753c
Parents: be0653e
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Apr 28 11:28:40 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Apr 28 11:28:40 2015 -0400

----------------------------------------------------------------------
 .../structure/io/graphson/GraphSONReader.java   | 57 ++-----------------
 .../gremlin/structure/util/star/StarGraph.java  |  4 +-
 .../util/star/StarGraphGraphSONSerializer.java  | 58 ++++++++++++++++++++
 3 files changed, 64 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8a46154e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONReader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONReader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONReader.java
index a91cc24..b25c32b 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONReader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONReader.java
@@ -21,16 +21,15 @@ package org.apache.tinkerpop.gremlin.structure.io.graphson;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.tinkerpop.gremlin.structure.Direction;
-import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
 import org.apache.tinkerpop.gremlin.structure.util.Attachable;
 import org.apache.tinkerpop.gremlin.structure.util.batch.BatchGraph;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
 import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
+import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer;
 import org.apache.tinkerpop.gremlin.util.function.FunctionUtils;
 import org.javatuples.Pair;
 
@@ -42,7 +41,6 @@ import java.io.InputStreamReader;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Function;
@@ -112,14 +110,14 @@ public class GraphSONReader implements GraphReader {
                              final Function<Attachable<Edge>, Edge> edgeAttachMethod,
                              final Direction attachEdgesOfThisDirection) throws IOException {
         final Map<String, Object> vertexData = mapper.readValue(inputStream, mapTypeReference);
-        final StarGraph starGraph = readStarGraphData(vertexData);
+        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))
-            readAdjacentVertexEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.OUT_E);
+            StarGraphGraphSONSerializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.OUT_E);
 
         if (vertexData.containsKey(GraphSONTokens.IN_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.IN))
-            readAdjacentVertexEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.IN_E);
+            StarGraphGraphSONSerializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.IN_E);
 
         return starGraph.getStarVertex();
     }
@@ -144,53 +142,6 @@ public class GraphSONReader implements GraphReader {
         return mapper.readValue(inputStream, clazz);
     }
 
-    private static void readAdjacentVertexEdges(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;
-                if (direction.equals(GraphSONTokens.OUT_E))
-                    starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addOutEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.IN)), T.id, inner.get(GraphSONTokens.ID));
-                else
-                    starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addInEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.OUT)), T.id, inner.get(GraphSONTokens.ID));
-
-                if (inner.containsKey(GraphSONTokens.PROPERTIES)) {
-                    final Map<String, Object> edgePropertyData = (Map<String, Object>) inner.get(GraphSONTokens.PROPERTIES);
-                    for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
-                        starEdge.property(epd.getKey(), epd.getValue());
-                    }
-                }
-
-                if (edgeMaker != null) edgeMaker.apply(starEdge);
-            }
-        }
-    }
-
-    private static StarGraph readStarGraphData(final Map<String, Object> vertexData) throws IOException {
-        final StarGraph starGraph = StarGraph.open();
-        starGraph.addVertex(T.id, vertexData.get(GraphSONTokens.ID), T.label, vertexData.get(GraphSONTokens.LABEL));
-        if (vertexData.containsKey(GraphSONTokens.PROPERTIES)) {
-            final Map<String, List<Map<String, Object>>> properties = (Map<String, List<Map<String, Object>>>) vertexData.get(GraphSONTokens.PROPERTIES);
-            for (Map.Entry<String, List<Map<String, Object>>> property : properties.entrySet()) {
-                for (Map<String, Object> p : property.getValue()) {
-                    // todo: cardinality - same as gryo right now???
-                    final StarGraph.StarVertexProperty vp = (StarGraph.StarVertexProperty) starGraph.getStarVertex().property(VertexProperty.Cardinality.list, property.getKey(), p.get(GraphSONTokens.VALUE), T.id, p.get(GraphSONTokens.ID));
-                    if (p.containsKey(GraphSONTokens.PROPERTIES)) {
-                        final Map<String, Object> edgePropertyData = (Map<String, Object>) p.get(GraphSONTokens.PROPERTIES);
-                        for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
-                            vp.property(epd.getKey(), epd.getValue());
-                        }
-                    }
-                }
-            }
-        }
-
-        return starGraph;
-    }
-
     public static Builder build() {
         return new Builder();
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8a46154e/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 9ca7d0d..04e432d 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
@@ -276,7 +276,7 @@ public final class StarGraph implements Graph, Serializable {
             return this.property(VertexProperty.Cardinality.single, key, value, keyValues);
         }
 
-        public Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
+        Edge addOutEdge(final String label, final Vertex inVertex, final Object... keyValues) {
             ElementHelper.validateLabel(label);
             ElementHelper.legalPropertyKeyValueArray(keyValues);
             if (null == this.outEdges)
@@ -292,7 +292,7 @@ public final class StarGraph implements Graph, Serializable {
             return outEdge;
         }
 
-        public Edge addInEdge(final String label, final Vertex outVertex, final Object... keyValues) {
+        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/8a46154e/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializer.java
index eea84af..42aa73b 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGraphSONSerializer.java
@@ -28,8 +28,10 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.T;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;
+import org.apache.tinkerpop.gremlin.structure.util.Attachable;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -37,6 +39,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Function;
 
 /**
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -160,6 +163,61 @@ public class StarGraphGraphSONSerializer extends StdSerializer<StarGraphGraphSON
         }
     }
 
+    /**
+     * A helper function for reading vertex edges from a serialized {@link StarGraph} (i.e. a {@link Map}) generated by
+     * {@link StarGraphGraphSONSerializer}.
+     */
+    public static void readStarGraphEdges(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;
+                if (direction.equals(GraphSONTokens.OUT_E))
+                    starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addOutEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.IN)), T.id, inner.get(GraphSONTokens.ID));
+                else
+                    starEdge = (StarGraph.StarEdge) starGraph.getStarVertex().addInEdge(edgeData.getKey(), starGraph.addVertex(T.id, inner.get(GraphSONTokens.OUT)), T.id, inner.get(GraphSONTokens.ID));
+
+                if (inner.containsKey(GraphSONTokens.PROPERTIES)) {
+                    final Map<String, Object> edgePropertyData = (Map<String, Object>) inner.get(GraphSONTokens.PROPERTIES);
+                    for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
+                        starEdge.property(epd.getKey(), epd.getValue());
+                    }
+                }
+
+                if (edgeMaker != null) edgeMaker.apply(starEdge);
+            }
+        }
+    }
+
+    /**
+     * A helper function for reading a serialized {@link StarGraph} from a {@link Map} generated by
+     * {@link StarGraphGraphSONSerializer}.
+     */
+    public static StarGraph readStarGraphVertex(final Map<String, Object> vertexData) throws IOException {
+        final StarGraph starGraph = StarGraph.open();
+        starGraph.addVertex(T.id, vertexData.get(GraphSONTokens.ID), T.label, vertexData.get(GraphSONTokens.LABEL));
+        if (vertexData.containsKey(GraphSONTokens.PROPERTIES)) {
+            final Map<String, List<Map<String, Object>>> properties = (Map<String, List<Map<String, Object>>>) vertexData.get(GraphSONTokens.PROPERTIES);
+            for (Map.Entry<String, List<Map<String, Object>>> property : properties.entrySet()) {
+                for (Map<String, Object> p : property.getValue()) {
+                    // todo: cardinality - same as gryo right now???
+                    final StarGraph.StarVertexProperty vp = (StarGraph.StarVertexProperty) starGraph.getStarVertex().property(VertexProperty.Cardinality.list, property.getKey(), p.get(GraphSONTokens.VALUE), T.id, p.get(GraphSONTokens.ID));
+                    if (p.containsKey(GraphSONTokens.PROPERTIES)) {
+                        final Map<String, Object> edgePropertyData = (Map<String, Object>) p.get(GraphSONTokens.PROPERTIES);
+                        for (Map.Entry<String, Object> epd : edgePropertyData.entrySet()) {
+                            vp.property(epd.getKey(), epd.getValue());
+                        }
+                    }
+                }
+            }
+        }
+
+        return starGraph;
+    }
+
     public static class DirectionalStarGraph {
         private final Direction direction;
         private final StarGraph starGraphToSerialize;