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/11/20 01:00:51 UTC
[1/2] incubator-tinkerpop git commit: Support a native TinkerGraph
GraphSON serializer.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/TINKERPOP3-978 [created] ea6871ffa
Support a native TinkerGraph GraphSON serializer.
This change allows an ObjectMapper to take a TinkerGraph as a value. This is in contrast to the approach taken for something like GraphSONWriter.writeGraph() where the pieces of the Graph are iterated and serialized independent of the Graph object itself.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/e90c49fc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/e90c49fc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/e90c49fc
Branch: refs/heads/TINKERPOP3-978
Commit: e90c49fc737ad24f8f5ec5a89cf98e314533c25a
Parents: 086a258
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Nov 19 14:06:38 2015 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Nov 19 14:06:38 2015 -0500
----------------------------------------------------------------------
.../structure/io/graphson/GraphSONReader.java | 4 +-
.../structure/io/graphson/GraphSONTokens.java | 2 +
.../structure/io/graphson/GraphSONUtil.java | 10 +-
.../structure/io/graphson/GraphSONWriter.java | 2 +-
.../server/GremlinResultSetIntegrateTest.java | 25 ++-
.../tinkergraph/structure/TinkerIoRegistry.java | 176 ++++++++++++++++++-
.../TinkerGraphGryoSerializerTest.java | 81 +++++++++
.../structure/TinkerGraphSerializerTest.java | 81 ---------
.../tinkergraph/structure/TinkerGraphTest.java | 35 ++++
9 files changed, 325 insertions(+), 91 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/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 d10a382..7b5ade7 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
@@ -252,8 +252,8 @@ public final class GraphSONReader implements GraphReader {
private Stream<String> readVertexStrings(final InputStream inputStream) throws IOException {
if (unwrapAdjacencyList) {
final JsonNode root = mapper.readTree(inputStream);
- final JsonNode vertices = root.get("vertices");
- if (!vertices.getNodeType().equals(JsonNodeType.ARRAY)) throw new IOException("The 'vertices' key must be an array");
+ final JsonNode vertices = root.get(GraphSONTokens.VERTICES);
+ if (!vertices.getNodeType().equals(JsonNodeType.ARRAY)) throw new IOException(String.format("The '%s' key must be an array", GraphSONTokens.VERTICES));
return IteratorUtils.stream(vertices.elements()).map(Object::toString);
} else {
final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java
index 3ed9f41..599b6fa 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONTokens.java
@@ -32,7 +32,9 @@ public final class GraphSONTokens {
public static final String PROPERTIES = "properties";
public static final String KEY = "key";
public static final String EDGE = "edge";
+ public static final String EDGES = "edges";
public static final String VERTEX = "vertex";
+ public static final String VERTICES = "vertices";
public static final String IN = "inV";
public static final String OUT = "outV";
public static final String IN_E = "inE";
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java
index 136d266..9ff427c 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONUtil.java
@@ -34,18 +34,24 @@ public final class GraphSONUtil {
private GraphSONUtil() {}
+ public static void writeWithType(final Object object, final JsonGenerator jsonGenerator,
+ final SerializerProvider serializerProvider,
+ final TypeSerializer typeSerializer) throws IOException {
+ writeWithType(null, object, jsonGenerator, serializerProvider, typeSerializer);
+ }
+
public static void writeWithType(final String key, final Object object, final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider,
final TypeSerializer typeSerializer) throws IOException {
final JsonSerializer<Object> serializer = serializerProvider.findValueSerializer(object.getClass(), null);
if (typeSerializer != null) {
// serialize with types embedded
- jsonGenerator.writeFieldName(key);
+ if (key != null && !key.isEmpty()) jsonGenerator.writeFieldName(key);
serializer.serializeWithType(object, jsonGenerator, serializerProvider, typeSerializer);
} else {
// types are not embedded, but use the serializer when possible or else custom serializers will get
// bypassed and you end up with the default jackson serializer when you don't want it.
- jsonGenerator.writeFieldName(key);
+ if (key != null && !key.isEmpty()) jsonGenerator.writeFieldName(key);
serializer.serialize(object, jsonGenerator, serializerProvider);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONWriter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONWriter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONWriter.java
index 04818d6..37ed9a0 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONWriter.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONWriter.java
@@ -100,7 +100,7 @@ public final class GraphSONWriter implements GraphWriter {
public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException {
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
- if (wrapAdjacencyList) writer.write("{\"vertices\":[");
+ if (wrapAdjacencyList) writer.write("{\"" + GraphSONTokens.VERTICES + "\":[");
while (vertexIterator.hasNext()) {
writeVertex(baos, vertexIterator.next(), direction);
writer.write(new String(baos.toByteArray()));
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinResultSetIntegrateTest.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinResultSetIntegrateTest.java b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinResultSetIntegrateTest.java
index 1128d5a..4a8eca9 100644
--- a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinResultSetIntegrateTest.java
+++ b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinResultSetIntegrateTest.java
@@ -20,18 +20,24 @@ package org.apache.tinkerpop.gremlin.server;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
+import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
+import org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.io.IoTest;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedPath;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -64,7 +70,13 @@ public class GremlinResultSetIntegrateTest extends AbstractGremlinServerIntegrat
@Before
public void beforeTest() {
- cluster = Cluster.open();
+ final MessageSerializer serializer = new GryoMessageSerializerV1d0();
+ final Map<String,Object> c = new HashMap<>();
+ c.put("ioRegistries", Arrays.asList(TinkerIoRegistry.class.getName()));
+ c.put("custom", Arrays.asList("groovy.json.JsonBuilder;org.apache.tinkerpop.gremlin.driver.ser.JsonBuilderGryoSerializer"));
+
+ serializer.configure(c, null);
+ cluster = Cluster.build().serializer(serializer).create();
client = cluster.connect();
}
@@ -127,6 +139,17 @@ public class GremlinResultSetIntegrateTest extends AbstractGremlinServerIntegrat
}
@Test
+ public void shouldHandleTinkerGraphResult() throws Exception {
+ final ResultSet results = client.submit("graph");
+ final Graph graph = results.all().get().get(0).get(TinkerGraph.class);
+
+ // test is "lossy for id" because TinkerGraph is configured by default to use the ANY id manager
+ // and doesn't coerce to specific types - which is how it is on the server as well so we can expect
+ // some id shiftiness
+ IoTest.assertModernGraph(graph, true, true);
+ }
+
+ @Test
public void shouldHandleMapIteratedResult() throws Exception {
final ResultSet results = client.submit("g.V().groupCount().by(bothE().count())");
final List<Result> resultList = results.all().get();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java
index 50690a3..60cebe6 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java
@@ -18,18 +18,44 @@
*/
package org.apache.tinkerpop.gremlin.tinkergraph.structure;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.AbstractIoRegistry;
+import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
+import org.apache.tinkerpop.gremlin.structure.io.GraphWriter;
import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONIo;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONTokens;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONUtil;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoIo;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoWriter;
+import org.apache.tinkerpop.gremlin.structure.util.Attachable;
+import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
+import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
+import org.apache.tinkerpop.shaded.jackson.core.JsonGenerator;
+import org.apache.tinkerpop.shaded.jackson.core.JsonParser;
+import org.apache.tinkerpop.shaded.jackson.core.JsonProcessingException;
+import org.apache.tinkerpop.shaded.jackson.databind.DeserializationContext;
+import org.apache.tinkerpop.shaded.jackson.databind.SerializerProvider;
+import org.apache.tinkerpop.shaded.jackson.databind.deser.std.StdDeserializer;
+import org.apache.tinkerpop.shaded.jackson.databind.jsontype.TypeSerializer;
+import org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule;
+import org.apache.tinkerpop.shaded.jackson.databind.ser.std.StdSerializer;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
import org.apache.tinkerpop.shaded.kryo.io.Input;
import org.apache.tinkerpop.shaded.kryo.io.Output;
+import org.javatuples.Pair;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
/**
* An implementation of the {@link IoRegistry} interface that provides serializers with custom configurations for
@@ -48,7 +74,8 @@ public final class TinkerIoRegistry extends AbstractIoRegistry {
private static final TinkerIoRegistry INSTANCE = new TinkerIoRegistry();
private TinkerIoRegistry() {
- register(GryoIo.class, TinkerGraph.class, new TinkerGraphSerializer());
+ register(GryoIo.class, TinkerGraph.class, new TinkerGraphGryoSerializer());
+ register(GraphSONIo.class, null, new TinkerModule());
}
public static TinkerIoRegistry getInstance() {
@@ -56,10 +83,10 @@ public final class TinkerIoRegistry extends AbstractIoRegistry {
}
/**
- * Provides a method to serialize an entire {@link TinkerGraph} into itself. This is useful when shipping small
- * graphs around through Gremlin Server. Reuses the existing Kryo instance for serialization.
+ * Provides a method to serialize an entire {@link TinkerGraph} into itself for Gryo. This is useful when
+ * shipping small graphs around through Gremlin Server. Reuses the existing Kryo instance for serialization.
*/
- final static class TinkerGraphSerializer extends Serializer<TinkerGraph> {
+ final static class TinkerGraphGryoSerializer extends Serializer<TinkerGraph> {
@Override
public void write(final Kryo kryo, final Output output, final TinkerGraph graph) {
try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
@@ -86,4 +113,145 @@ public final class TinkerIoRegistry extends AbstractIoRegistry {
return graph;
}
}
+
+ /**
+ * Provides a method to serialize an entire {@link TinkerGraph} into itself for GraphSON. This is useful when
+ * shipping small graphs around through Gremlin Server.
+ */
+ final static class TinkerModule extends SimpleModule {
+ public TinkerModule() {
+ super("tinkergraph-1.0");
+ addSerializer(TinkerGraph.class, new TinkerGraphJacksonSerializer());
+ addDeserializer(TinkerGraph.class, new TinkerGraphJacksonDeserializer());
+ }
+ }
+
+ /**
+ * Serializes the graph into an edge list format. Edge list is a better choices than adjacency list (which is
+ * typically standard from the {@link GraphReader} and {@link GraphWriter} perspective) in this case because
+ * the use case for this isn't around massive graphs. The use case is for "small" subgraphs that are being
+ * shipped over the wire from Gremlin Server. Edge list is format is a bit easier for non-JVM languages to work
+ * with as a format and doesn't require a cache for loading (as vertex labels are not serialized in adjacency
+ * list).
+ */
+ final static class TinkerGraphJacksonSerializer extends StdSerializer<TinkerGraph> {
+
+ public TinkerGraphJacksonSerializer() {
+ super(TinkerGraph.class);
+ }
+
+ @Override
+ public void serialize(final TinkerGraph graph, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
+ throws IOException {
+ jsonGenerator.writeStartObject();
+
+ jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
+ jsonGenerator.writeStartArray();
+
+ final Iterator<Vertex> vertices = graph.vertices();
+ while (vertices.hasNext()) {
+ serializerProvider.defaultSerializeValue(vertices.next(), jsonGenerator);
+ }
+
+ jsonGenerator.writeEndArray();
+
+ jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
+ jsonGenerator.writeStartArray();
+
+ final Iterator<Edge> edges = graph.edges();
+ while (edges.hasNext()) {
+ serializerProvider.defaultSerializeValue(edges.next(), jsonGenerator);
+ }
+
+ jsonGenerator.writeEndArray();
+
+ jsonGenerator.writeEndObject();
+ }
+
+ @Override
+ public void serializeWithType(final TinkerGraph graph, final JsonGenerator jsonGenerator,
+ final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField(GraphSONTokens.CLASS, TinkerGraph.class.getName());
+
+ jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
+ jsonGenerator.writeStartArray();
+ jsonGenerator.writeString(ArrayList.class.getName());
+ jsonGenerator.writeStartArray();
+
+ final Iterator<Vertex> vertices = graph.vertices();
+ while (vertices.hasNext()) {
+ GraphSONUtil.writeWithType(vertices.next(), jsonGenerator, serializerProvider, typeSerializer);
+ }
+
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndArray();
+
+ jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
+ jsonGenerator.writeStartArray();
+ jsonGenerator.writeString(ArrayList.class.getName());
+ jsonGenerator.writeStartArray();
+
+ final Iterator<Edge> edges = graph.edges();
+ while (edges.hasNext()) {
+ GraphSONUtil.writeWithType(edges.next(), jsonGenerator, serializerProvider, typeSerializer);
+ }
+
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndArray();
+
+ jsonGenerator.writeEndObject();
+ }
+ }
+
+ /**
+ * Deserializes the edge list format.
+ */
+ static class TinkerGraphJacksonDeserializer extends StdDeserializer<TinkerGraph> {
+ public TinkerGraphJacksonDeserializer() {
+ super(TinkerGraph.class);
+ }
+
+ @Override
+ public TinkerGraph deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
+ final TinkerGraph graph = TinkerGraph.open();
+
+ final List<Map<String, Object>> edges;
+ final List<Map<String, Object>> vertices;
+ if (!jsonParser.getCurrentToken().isStructStart()) {
+ if (!jsonParser.getCurrentName().equals(GraphSONTokens.VERTICES))
+ throw new IOException(String.format("Expected a '%s' key", GraphSONTokens.VERTICES));
+
+ jsonParser.nextToken();
+ vertices = (List<Map<String, Object>>) deserializationContext.readValue(jsonParser, ArrayList.class);
+ jsonParser.nextToken();
+
+ if (!jsonParser.getCurrentName().equals(GraphSONTokens.EDGES))
+ throw new IOException(String.format("Expected a '%s' key", GraphSONTokens.EDGES));
+
+ jsonParser.nextToken();
+ edges = (List<Map<String, Object>>) deserializationContext.readValue(jsonParser, ArrayList.class);
+ } else {
+ final Map<String, Object> graphData = deserializationContext.readValue(jsonParser, HashMap.class);
+ vertices = (List<Map<String,Object>>) graphData.get("vertices");
+ edges = (List<Map<String,Object>>) graphData.get("edges");
+ }
+
+ for (Map<String, Object> vertexData : vertices) {
+ final DetachedVertex detached = new DetachedVertex(vertexData.get(GraphSONTokens.ID),
+ vertexData.get(GraphSONTokens.LABEL).toString(), (Map<String,Object>) vertexData.get(GraphSONTokens.PROPERTIES));
+ detached.attach(Attachable.Method.getOrCreate(graph));
+ }
+
+ for (Map<String, Object> edgeData : edges) {
+ final DetachedEdge detached = new DetachedEdge(edgeData.get(GraphSONTokens.ID),
+ edgeData.get(GraphSONTokens.LABEL).toString(), (Map<String,Object>) edgeData.get(GraphSONTokens.PROPERTIES),
+ Pair.with(edgeData.get(GraphSONTokens.OUT), edgeData.get(GraphSONTokens.OUT_LABEL).toString()),
+ Pair.with(edgeData.get(GraphSONTokens.IN), edgeData.get(GraphSONTokens.IN_LABEL).toString()));
+ detached.attach(Attachable.Method.getOrCreate(graph));
+ }
+
+ return graph;
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGryoSerializerTest.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGryoSerializerTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGryoSerializerTest.java
new file mode 100644
index 0000000..3decd5f
--- /dev/null
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphGryoSerializerTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.tinkergraph.structure;
+
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
+import org.apache.tinkerpop.shaded.kryo.Kryo;
+import org.apache.tinkerpop.shaded.kryo.Registration;
+import org.apache.tinkerpop.shaded.kryo.io.Input;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.Arrays;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for {@link TinkerIoRegistry.TinkerGraphGryoSerializer}
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TinkerGraphGryoSerializerTest {
+
+ @Mock
+ private Kryo kryo;
+ @Mock
+ private Registration registration;
+ @Mock
+ private Output output;
+ @Mock
+ private Input input;
+
+ private TinkerGraph graph = TinkerGraph.open();
+ private TinkerIoRegistry.TinkerGraphGryoSerializer serializer = new TinkerIoRegistry.TinkerGraphGryoSerializer();
+
+ @Before
+ public void setUp() throws Exception {
+
+ when(kryo.getRegistration((Class) any())).thenReturn(registration);
+ when(input.readBytes(anyInt())).thenReturn(Arrays.copyOf(GryoMapper.HEADER, 100));
+ }
+
+ @Test
+ public void shouldVerifyKryoUsedForWrite() throws Exception {
+ serializer.write(kryo, output, graph);
+ verify(kryo, atLeastOnce()).getRegistration((Class) any());
+ }
+
+ @Test
+ public void shouldVerifyKryoUsedForRead() throws Exception {
+ // Not possible to mock an entire deserialization so just verify the same kryo instances are being used
+ try {
+ serializer.read(kryo, input, TinkerGraph.class);
+ } catch (RuntimeException ex) {
+ verify(kryo, atLeastOnce()).readObject(any(), any());
+ verify(kryo, atLeastOnce()).readClassAndObject(any());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphSerializerTest.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphSerializerTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphSerializerTest.java
deleted file mode 100644
index 1c494b5..0000000
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphSerializerTest.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tinkerpop.gremlin.tinkergraph.structure;
-
-import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
-import org.apache.tinkerpop.shaded.kryo.Kryo;
-import org.apache.tinkerpop.shaded.kryo.Registration;
-import org.apache.tinkerpop.shaded.kryo.io.Input;
-import org.apache.tinkerpop.shaded.kryo.io.Output;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-
-import java.util.Arrays;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-/**
- * Unit tests for {@link org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerIoRegistry.TinkerGraphSerializer}
- */
-@RunWith(MockitoJUnitRunner.class)
-public class TinkerGraphSerializerTest {
-
- @Mock
- private Kryo kryo;
- @Mock
- private Registration registration;
- @Mock
- private Output output;
- @Mock
- private Input input;
-
- private TinkerGraph graph = TinkerGraph.open();
- private TinkerIoRegistry.TinkerGraphSerializer serializer = new TinkerIoRegistry.TinkerGraphSerializer();
-
- @Before
- public void setUp() throws Exception {
-
- when(kryo.getRegistration((Class) any())).thenReturn(registration);
- when(input.readBytes(anyInt())).thenReturn(Arrays.copyOf(GryoMapper.HEADER, 100));
- }
-
- @Test
- public void shouldVerifyKryoUsedForWrite() throws Exception {
- serializer.write(kryo, output, graph);
- verify(kryo, atLeastOnce()).getRegistration((Class) any());
- }
-
- @Test
- public void shouldVerifyKryoUsedForRead() throws Exception {
- // Not possible to mock an entire deserialization so just verify the same kryo instances are being used
- try {
- serializer.read(kryo, input, TinkerGraph.class);
- } catch (RuntimeException ex) {
- verify(kryo, atLeastOnce()).readObject(any(), any());
- verify(kryo, atLeastOnce()).readClassAndObject(any());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e90c49fc/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
index b3dc64e..d1bf381 100644
--- a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
+++ b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerGraphTest.java
@@ -20,12 +20,19 @@ package org.apache.tinkerpop.gremlin.tinkergraph.structure;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
+import org.apache.commons.io.IOUtils;
import org.apache.tinkerpop.gremlin.TestHelper;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
+import org.apache.tinkerpop.gremlin.structure.io.GraphWriter;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.structure.io.IoTest;
+import org.apache.tinkerpop.gremlin.structure.io.Mapper;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
+import org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper;
import org.junit.Ignore;
import org.junit.Test;
@@ -282,6 +289,34 @@ public class TinkerGraphTest {
}
}
}
+
+ @Test
+ public void shouldSerializeTinkerGraphToGraphSON() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createModern();
+ try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+ graph.io(IoCore.graphson()).writer().create().writeObject(out, graph);
+ try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
+ final TinkerGraph target = graph.io(IoCore.graphson()).reader().create().readObject(inputStream, TinkerGraph.class);
+ IoTest.assertModernGraph(target, true, false);
+ }
+ }
+ }
+
+ @Test
+ public void shouldSerializeTinkerGraphToGraphSONWithTypes() throws Exception {
+ final TinkerGraph graph = TinkerFactory.createModern();
+ final Mapper<ObjectMapper> mapper = graph.io(IoCore.graphson()).mapper().embedTypes(true).create();
+ try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+ final GraphWriter writer = GraphSONWriter.build().mapper(mapper).create();
+ writer.writeObject(out, graph);
+ try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray())) {
+ final GraphReader reader = GraphSONReader.build().mapper(mapper).create();
+ final TinkerGraph target = reader.readObject(inputStream, TinkerGraph.class);
+ IoTest.assertModernGraph(target, true, false);
+ }
+ }
+ }
+
@Test(expected = IllegalStateException.class)
public void shouldRequireGraphLocationIfFormatIsSet() {
final Configuration conf = new BaseConfiguration();
[2/2] incubator-tinkerpop git commit: Add GraphSON related tests over
REST for serializing TinkerGraph.
Posted by sp...@apache.org.
Add GraphSON related tests over REST for serializing TinkerGraph.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ea6871ff
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ea6871ff
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ea6871ff
Branch: refs/heads/TINKERPOP3-978
Commit: ea6871ffa9b4054f628f3b5fcee02c25b14b365a
Parents: e90c49f
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Nov 19 19:00:05 2015 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Nov 19 19:00:05 2015 -0500
----------------------------------------------------------------------
.../server/GremlinServerHttpIntegrateTest.java | 24 ++++++++++++++++++++
.../server/gremlin-server-integration.yaml | 4 ++--
2 files changed, 26 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ea6871ff/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinServerHttpIntegrateTest.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinServerHttpIntegrateTest.java b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinServerHttpIntegrateTest.java
index f46f022..c0f37f2 100644
--- a/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinServerHttpIntegrateTest.java
+++ b/gremlin-server/src/test/java/org/apache/tinkerpop/gremlin/server/GremlinServerHttpIntegrateTest.java
@@ -37,6 +37,7 @@ import java.io.File;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@@ -423,6 +424,29 @@ public class GremlinServerHttpIntegrateTest extends AbstractGremlinServerIntegra
}
@Test
+ public void should200OnPOSTWithGremlinJsonEndcodedBodyWithTinkerGraphResult() throws Exception {
+ final CloseableHttpClient httpclient = HttpClients.createDefault();
+ final HttpPost httppost = new HttpPost("http://localhost:8182");
+ httppost.addHeader("Content-Type", "application/json");
+ httppost.setEntity(new StringEntity("{\"gremlin\":\"org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory.createModern()\"}", Consts.UTF_8));
+
+ try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
+ assertEquals(200, response.getStatusLine().getStatusCode());
+ assertEquals("application/json", response.getEntity().getContentType().getValue());
+ final String json = EntityUtils.toString(response.getEntity());
+ final JsonNode resultJson = mapper.readTree(json);
+ final JsonNode data = resultJson.get("result").get("data");
+ assertEquals(1, data.size());
+
+ final List<JsonNode> vertices = data.get(0).findValues(GraphSONTokens.VERTICES);
+ final List<JsonNode> edges = data.get(0).findValues(GraphSONTokens.EDGES);
+
+ assertEquals(6, vertices.get(0).size());
+ assertEquals(6, edges.get(0).size());
+ }
+ }
+
+ @Test
public void should200OnPOSTWithGremlinJsonEndcodedBodyWithIteratorResultAndAliases() throws Exception {
// we can remove this first test when rebindings are completely removed
final CloseableHttpClient httpclientLegacy = HttpClients.createDefault();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ea6871ff/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/gremlin-server-integration.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/gremlin-server-integration.yaml b/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/gremlin-server-integration.yaml
index 7b6e5c2..1a57a15 100644
--- a/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/gremlin-server-integration.yaml
+++ b/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/gremlin-server-integration.yaml
@@ -36,8 +36,8 @@ scriptEngines: {
serializers:
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { useMapperFromGraph: graph, custom: [groovy.json.JsonBuilder;org.apache.tinkerpop.gremlin.driver.ser.JsonBuilderGryoSerializer]}}
- { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true}}
- - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0 }
- - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0 }
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { useMapperFromGraph: graph }}
+ - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { useMapperFromGraph: graph }}
processors:
- { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}
metrics: {