diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 090eb4c591..6be12c6663 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -31,7 +31,6 @@ import org.apache.tinkerpop.gremlin.server.util.MetricManager; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Transaction; -import org.apache.tinkerpop.gremlin.structure.io.IoCore; import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; import org.slf4j.Logger; @@ -107,9 +106,7 @@ public HugeGraph graph(String name) { } public Serializer serializer(Graph g) { - // TODO: cache Serializer - return new JsonSerializer(g.io(IoCore.graphson()).writer() - .wrapAdjacencyList(true).create()); + return JsonSerializer.instance(); } public void rollbackAll() { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java index cc1f45571c..de235a0047 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/serializer/JsonSerializer.java @@ -29,7 +29,6 @@ import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter; import org.apache.tinkerpop.gremlin.structure.util.CloseableIterator; import com.baidu.hugegraph.HugeException; @@ -51,23 +50,15 @@ public class JsonSerializer implements Serializer { - private final GraphSONWriter writer; - - private static final int BUF_SIZE = 128; private static final int LBUF_SIZE = 1024; - public JsonSerializer(GraphSONWriter writer) { - this.writer = writer; + private static JsonSerializer INSTANCE = new JsonSerializer(); + + private JsonSerializer() { } - private String writeObject(Object object) { - try (ByteArrayOutputStream out = new ByteArrayOutputStream(BUF_SIZE)) { - this.writer.writeObject(out, object); - return out.toString(API.CHARSET); - } catch (Exception e) { - throw new HugeException("Failed to serialize %s", e, - object.getClass().getSimpleName()); - } + public static JsonSerializer instance() { + return INSTANCE; } private String writeList(String label, List list) { @@ -136,7 +127,7 @@ private String writeIterator(String label, Iterator itor, @Override public String writePropertyKey(PropertyKey propertyKey) { - return writeObject(propertyKey); + return JsonUtil.toJson(propertyKey); } @Override @@ -146,7 +137,7 @@ public String writePropertyKeys(List propertyKeys) { @Override public String writeVertexLabel(VertexLabel vertexLabel) { - return writeObject(vertexLabel); + return JsonUtil.toJson(vertexLabel); } @Override @@ -156,7 +147,7 @@ public String writeVertexLabels(List vertexLabels) { @Override public String writeEdgeLabel(EdgeLabel edgeLabel) { - return writeObject(edgeLabel); + return JsonUtil.toJson(edgeLabel); } @Override @@ -166,7 +157,7 @@ public String writeEdgeLabels(List edgeLabels) { @Override public String writeIndexlabel(IndexLabel indexLabel) { - return writeObject(indexLabel); + return JsonUtil.toJson(indexLabel); } @Override @@ -188,7 +179,7 @@ public String writeCreatedIndexLabel(IndexLabel.CreatedIndexLabel cil) { @Override public String writeVertex(Vertex vertex) { - return writeObject(vertex); + return JsonUtil.toJson(vertex); } @Override @@ -198,7 +189,7 @@ public String writeVertices(Iterator vertices, boolean paging) { @Override public String writeEdge(Edge edge) { - return writeObject(edge); + return JsonUtil.toJson(edge); } @Override @@ -209,9 +200,9 @@ public String writeEdges(Iterator edges, boolean paging) { @Override public String writeIds(String name, Collection ids) { if (ids instanceof List) { - return writeList(name, (List) ids); + return this.writeList(name, (List) ids); } else { - return writeList(name, new ArrayList<>(ids)); + return this.writeList(name, new ArrayList<>(ids)); } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java index 5e34cbade8..a456f8147a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java @@ -89,9 +89,9 @@ public class HugeGraphSONModule extends TinkerPopJacksonModule { TYPE_DEFINITIONS.put(EdgeLabel.class, "EdgeLabel"); TYPE_DEFINITIONS.put(IndexLabel.class, "IndexLabel"); - // HugeGraph vertex/edge serializer + // HugeGraph vertex serializer TYPE_DEFINITIONS.put(HugeVertex.class, "HugeVertex"); - TYPE_DEFINITIONS.put(HugeEdge.class, "HugeEdge"); + // TYPE_DEFINITIONS.put(HugeEdge.class, "HugeEdge"); // HugeGraph shard serializer TYPE_DEFINITIONS.put(Shard.class, "Shard"); @@ -128,7 +128,13 @@ private HugeGraphSONModule() { addSerializer(IndexLabel.class, new IndexLabelSerializer()); addSerializer(HugeVertex.class, new HugeVertexSerializer()); - addSerializer(HugeEdge.class, new HugeEdgeSerializer()); + /* + * Use customized edge serializer need to be compatible with V1 and V2 + * Graphson, and seems need to implement edge deserializer,it is + * a little complicated. + * Honestly, I don't know why there is no problem with vertex serializer + */ + // addSerializer(HugeEdge.class, new HugeEdgeSerializer()); addSerializer(Shard.class, new ShardSerializer()); } diff --git a/hugegraph-test/src/main/resources/fast-methods.filter b/hugegraph-test/src/main/resources/fast-methods.filter index 74ab5ed249..5053265b83 100644 --- a/hugegraph-test/src/main/resources/fast-methods.filter +++ b/hugegraph-test/src/main/resources/fast-methods.filter @@ -61,6 +61,11 @@ org.apache.tinkerpop.gremlin.structure.GraphTest.shouldRemoveVertices: Random UU org.apache.tinkerpop.gremlin.structure.GraphTest.shouldRemoveEdges: Random UUID as edge label org.apache.tinkerpop.gremlin.structure.TransactionTest.shouldExecuteWithCompetingThreads: Hang the tests because property 'test' have both String and long value +## vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializeVertex: Vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializePath: Vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializeTree: Vertex properties doesn't have nested structures with HugeVertexSerializer + #################### process suite #################### # unsupported automatic edge id, therefore number of edge is wrong diff --git a/hugegraph-test/src/main/resources/methods.filter b/hugegraph-test/src/main/resources/methods.filter index efbc460ddb..b9597433a3 100644 --- a/hugegraph-test/src/main/resources/methods.filter +++ b/hugegraph-test/src/main/resources/methods.filter @@ -61,6 +61,11 @@ org.apache.tinkerpop.gremlin.structure.GraphTest.shouldRemoveVertices: Random UU org.apache.tinkerpop.gremlin.structure.GraphTest.shouldRemoveEdges: Random UUID as edge label org.apache.tinkerpop.gremlin.structure.TransactionTest.shouldExecuteWithCompetingThreads: Hang the tests because property 'test' have both String and long value +## vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializeVertex: Vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializePath: Vertex properties doesn't have nested structures with HugeVertexSerializer +org.apache.tinkerpop.gremlin.structure.SerializationTest.GraphSONTest.shouldSerializeTree: Vertex properties doesn't have nested structures with HugeVertexSerializer + #################### process suite #################### # unsupported automatic edge id, therefore number of edge is wrong