You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2016/06/06 16:35:17 UTC
[1/8] incubator-tinkerpop git commit: Introduce Kryo shim to support
serializer reuse
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/TINKERPOP-1321 [created] 4a185a376
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
new file mode 100644
index 0000000..d5ba90d
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
@@ -0,0 +1,140 @@
+/*
+ * 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.structure.util.star;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
+import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.apache.tinkerpop.gremlin.structure.T;
+import org.apache.tinkerpop.gremlin.structure.VertexProperty;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+
+public class StarGraphSerializer implements SerializerShim<StarGraph> {
+
+ private final Direction edgeDirectionToSerialize;
+ private GraphFilter graphFilter;
+
+ private final static byte VERSION_1 = Byte.MIN_VALUE;
+
+ public StarGraphSerializer(final Direction edgeDirectionToSerialize, final GraphFilter graphFilter) {
+ this.edgeDirectionToSerialize = edgeDirectionToSerialize;
+ this.graphFilter = graphFilter;
+ }
+
+ @Override
+ public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final StarGraph starGraph) {
+ output.writeByte(VERSION_1);
+ kryo.writeObjectOrNull(output, starGraph.edgeProperties, HashMap.class);
+ kryo.writeObjectOrNull(output, starGraph.metaProperties, HashMap.class);
+ kryo.writeClassAndObject(output, starGraph.starVertex.id);
+ kryo.writeObject(output, starGraph.starVertex.label);
+ writeEdges(kryo, output, starGraph, Direction.IN);
+ writeEdges(kryo, output, starGraph, Direction.OUT);
+ kryo.writeObject(output, null != starGraph.starVertex.vertexProperties);
+ if (null != starGraph.starVertex.vertexProperties) {
+ kryo.writeObject(output, starGraph.starVertex.vertexProperties.size());
+ for (final Map.Entry<String, List<VertexProperty>> vertexProperties : starGraph.starVertex.vertexProperties.entrySet()) {
+ kryo.writeObject(output, vertexProperties.getKey());
+ kryo.writeObject(output, vertexProperties.getValue().size());
+ for (final VertexProperty vertexProperty : vertexProperties.getValue()) {
+ kryo.writeClassAndObject(output, vertexProperty.id());
+ kryo.writeClassAndObject(output, vertexProperty.value());
+ }
+ }
+ }
+ }
+
+ /**
+ * If the returned {@link StarGraph} is null, that means that the {@link GraphFilter} filtered the vertex.
+ */
+ @Override
+ public <I extends InputShim> StarGraph read(KryoShim<I, ?> kryo, I input, Class<StarGraph> clazz) {
+ final StarGraph starGraph = StarGraph.open();
+ input.readByte(); // version field ignored for now - for future use with backward compatibility
+ starGraph.edgeProperties = kryo.readObjectOrNull(input, HashMap.class);
+ starGraph.metaProperties = kryo.readObjectOrNull(input, HashMap.class);
+ starGraph.addVertex(T.id, kryo.readClassAndObject(input), T.label, kryo.readObject(input, String.class));
+ readEdges(kryo, input, starGraph, Direction.IN);
+ readEdges(kryo, input, starGraph, Direction.OUT);
+ if (kryo.readObject(input, Boolean.class)) {
+ final int numberOfUniqueKeys = kryo.readObject(input, Integer.class);
+ for (int i = 0; i < numberOfUniqueKeys; i++) {
+ final String vertexPropertyKey = kryo.readObject(input, String.class);
+ final int numberOfVertexPropertiesWithKey = kryo.readObject(input, Integer.class);
+ for (int j = 0; j < numberOfVertexPropertiesWithKey; j++) {
+ final Object id = kryo.readClassAndObject(input);
+ final Object value = kryo.readClassAndObject(input);
+ starGraph.starVertex.property(VertexProperty.Cardinality.list, vertexPropertyKey, value, T.id, id);
+ }
+ }
+ }
+ return this.graphFilter.hasFilter() ? starGraph.applyGraphFilter(this.graphFilter).orElse(null) : starGraph;
+ }
+
+ private <O extends OutputShim> void writeEdges(final KryoShim<?, O> kryo, final O output, final StarGraph starGraph, final Direction direction) {
+ // only write edges if there are some AND if the user requested them to be serialized AND if they match
+ // the direction being serialized by the format
+ final Map<String, List<Edge>> starEdges = direction.equals(Direction.OUT) ? starGraph.starVertex.outEdges : starGraph.starVertex.inEdges;
+ final boolean writeEdges = null != starEdges && edgeDirectionToSerialize != null
+ && (edgeDirectionToSerialize == direction || edgeDirectionToSerialize == Direction.BOTH);
+ kryo.writeObject(output, writeEdges);
+ if (writeEdges) {
+ kryo.writeObject(output, starEdges.size());
+ for (final Map.Entry<String, List<Edge>> edges : starEdges.entrySet()) {
+ kryo.writeObject(output, edges.getKey());
+ kryo.writeObject(output, edges.getValue().size());
+ for (final Edge edge : edges.getValue()) {
+ kryo.writeClassAndObject(output, edge.id());
+ kryo.writeClassAndObject(output, direction.equals(Direction.OUT) ? edge.inVertex().id() : edge.outVertex().id());
+ }
+ }
+ }
+ }
+
+ private <I extends InputShim> void readEdges(final KryoShim<I, ?> kryo, final I input, final StarGraph starGraph, final Direction direction) {
+ if (kryo.readObject(input, Boolean.class)) {
+ final int numberOfUniqueLabels = kryo.readObject(input, Integer.class);
+ for (int i = 0; i < numberOfUniqueLabels; i++) {
+ final String edgeLabel = kryo.readObject(input, String.class);
+ final int numberOfEdgesWithLabel = kryo.readObject(input, Integer.class);
+ for (int j = 0; j < numberOfEdgesWithLabel; j++) {
+ final Object edgeId = kryo.readClassAndObject(input);
+ final Object adjacentVertexId = kryo.readClassAndObject(input);
+ if (this.graphFilter.checkEdgeLegality(direction, edgeLabel).positive()) {
+ if (direction.equals(Direction.OUT))
+ starGraph.starVertex.addOutEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId);
+ else
+ starGraph.starVertex.addInEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId);
+ } else if (null != starGraph.edgeProperties) {
+ starGraph.edgeProperties.remove(edgeId);
+ }
+ }
+ }
+ }
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
new file mode 100644
index 0000000..2053280
--- /dev/null
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
@@ -0,0 +1,69 @@
+/*
+ * 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.hadoop.structure.io;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService;
+import org.apache.tinkerpop.shaded.kryo.Kryo;
+import org.apache.tinkerpop.shaded.kryo.io.Input;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class HadoopPoolShimService implements KryoShimService {
+
+ public Object readClassAndObject(InputStream source) {
+
+ Kryo k = null;
+
+ try {
+ k = HadoopPools.getGryoPool().takeKryo();
+
+ return k.readClassAndObject(new Input(source));
+ } finally {
+ if (null != k) {
+ HadoopPools.getGryoPool().offerKryo(k);
+ }
+ }
+ }
+
+ public void writeClassAndObject(Object o, OutputStream sink) {
+
+ Kryo k = null;
+
+ try {
+ k = HadoopPools.getGryoPool().takeKryo();
+
+ Output output = new Output(sink);
+
+ k.writeClassAndObject(output, o);
+
+ output.flush();
+ } finally {
+ if (null != k) {
+ HadoopPools.getGryoPool().offerKryo(k);
+ }
+ }
+ }
+
+ @Override
+ public int getPriority() {
+ return 0;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPools.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPools.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPools.java
index f09f703..5074ad5 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPools.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPools.java
@@ -43,6 +43,7 @@ public final class HadoopPools {
GRYO_POOL = GryoPool.build().
poolSize(configuration.getInt(GryoPool.CONFIG_IO_GRYO_POOL_SIZE, 256)).
ioRegistries(configuration.getList(GryoPool.CONFIG_IO_REGISTRY, Collections.emptyList())).
+ initializeMapper(m -> m.registrationRequired(false)).
create();
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
index 9a07f75..e7a38a5 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
@@ -21,6 +21,8 @@ package org.apache.tinkerpop.gremlin.hadoop.structure.io;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimServiceLoader;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -63,29 +65,14 @@ public final class ObjectWritable<T> implements WritableComparable<ObjectWritabl
@Override
public void readFields(final DataInput input) throws IOException {
- this.t = HadoopPools.getGryoPool().doWithReader(gryoReader -> {
- try {
- // class argument is Object because gryo doesn't really care that we don't know the specific type.
- // the type is embedded in the stream so it can just read it from there and return it as needed.
- // presumably that will cast nicely to T
- return (T) gryoReader.readObject(new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input)), Object.class);
- } catch (final IOException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- });
+ ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
+ this.t = KryoShimServiceLoader.readClassAndObject(bais);
}
@Override
public void write(final DataOutput output) throws IOException {
- HadoopPools.getGryoPool().doWithWriter(gryoWriter -> {
- try {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- gryoWriter.writeObject(outputStream, this.t);
- WritableUtils.writeCompressedByteArray(output, outputStream.toByteArray());
- } catch (final IOException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- });
+ byte serialized[] = KryoShimServiceLoader.writeClassAndObjectToBytes(this.t);
+ WritableUtils.writeCompressedByteArray(output, serialized);
}
private void writeObject(final ObjectOutputStream outputStream) throws IOException {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
index ac360e9..7ac8e8c 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.hadoop.structure.io;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
@@ -60,42 +61,15 @@ public final class VertexWritable implements Writable, Serializable {
@Override
public void readFields(final DataInput input) throws IOException {
- try {
- this.vertex = null;
- this.vertex = HadoopPools.getGryoPool().doWithReader(gryoReader -> {
- try {
- final ByteArrayInputStream inputStream = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
- return gryoReader.readObject(inputStream, StarGraph.class).getStarVertex(); // read the star graph
- } catch (final IOException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- });
- } catch (final IllegalStateException e) {
- if (e.getCause() instanceof IOException)
- throw (IOException) e.getCause();
- else
- throw e;
- }
+ this.vertex = null;
+ ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input));
+ this.vertex = ((StarGraph)KryoShimServiceLoader.readClassAndObject(bais)).getStarVertex(); // read the star graph;
}
@Override
public void write(final DataOutput output) throws IOException {
- try {
- HadoopPools.getGryoPool().doWithWriter(gryoWriter -> {
- try {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- gryoWriter.writeObject(outputStream, this.vertex.graph()); // write the star graph
- WritableUtils.writeCompressedByteArray(output, outputStream.toByteArray());
- } catch (final IOException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- });
- } catch (final IllegalStateException e) {
- if (e.getCause() instanceof IOException)
- throw (IOException) e.getCause();
- else
- throw e;
- }
+ byte serialized[] = KryoShimServiceLoader.writeClassAndObjectToBytes(this.vertex.graph());
+ WritableUtils.writeCompressedByteArray(output, serialized);
}
private void writeObject(final ObjectOutputStream outputStream) throws IOException {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService b/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
new file mode 100644
index 0000000..0b27e72
--- /dev/null
+++ b/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
@@ -0,0 +1 @@
+org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPoolShimService # HadoopPools provides/caches instances of TinkerPop's shaded Kryo
[8/8] incubator-tinkerpop git commit: GryoRegistrar is the new name
for TinkerPopKryoRegistrar. Added GryoPoolKryoShimSerivce for use by
non-Hadoop projects. Updated VertexProgramHelper's serizlizer to use the
KryoShimService. Few issues there, need to t
Posted by ok...@apache.org.
GryoRegistrar is the new name for TinkerPopKryoRegistrar. Added GryoPoolKryoShimSerivce for use by non-Hadoop projects. Updated VertexProgramHelper's serizlizer to use the KryoShimService. Few issues there, need to talk to @dalaro. Only thing failing are some traversal interruption exception tests... they were working prior to GryoPoolKryoShimService usage.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/4a185a37
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/4a185a37
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/4a185a37
Branch: refs/heads/TINKERPOP-1321
Commit: 4a185a3765ed5c19e1d7d14413a33c11fc2e0d29
Parents: 55eb1e6 ebd2f85
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Mon Jun 6 10:31:01 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Mon Jun 6 10:31:01 2016 -0600
----------------------------------------------------------------------
.../process/computer/GiraphWorkerContext.java | 3 +-
.../computer/util/VertexProgramHelper.java | 30 +-
.../gremlin/structure/io/gryo/GryoMapper.java | 447 ++++++++++++-------
.../gremlin/structure/io/gryo/GryoPool.java | 1 +
.../structure/io/gryo/GryoSerializers.java | 40 +-
.../structure/io/gryo/JavaTimeSerializers.java | 125 ++----
.../structure/io/gryo/PairSerializer.java | 11 +-
.../structure/io/gryo/TypeRegistration.java | 82 ++++
.../structure/io/gryo/URISerializer.java | 23 +-
.../structure/io/gryo/UUIDSerializer.java | 24 +-
.../gryo/kryoshim/GryoPoolKryoShimService.java | 65 +++
.../structure/io/gryo/kryoshim/InputShim.java | 38 ++
.../structure/io/gryo/kryoshim/KryoShim.java | 41 ++
.../io/gryo/kryoshim/KryoShimService.java | 99 ++++
.../io/gryo/kryoshim/KryoShimServiceLoader.java | 214 +++++++++
.../structure/io/gryo/kryoshim/OutputShim.java | 40 ++
.../io/gryo/kryoshim/SerializerShim.java | 36 ++
.../io/gryo/kryoshim/package-info.java | 55 +++
.../kryoshim/shaded/ShadedInputAdapter.java | 66 +++
.../gryo/kryoshim/shaded/ShadedKryoAdapter.java | 67 +++
.../kryoshim/shaded/ShadedOutputAdapter.java | 72 +++
.../shaded/ShadedSerializerAdapter.java | 54 +++
.../io/gryo/kryoshim/shaded/package-info.java | 25 ++
.../util/star/StarGraphGryoSerializer.java | 130 +-----
.../util/star/StarGraphSerializer.java | 150 +++++++
...n.structure.io.gryo.kryoshim.KryoShimService | 1 +
.../hadoop/process/computer/HadoopCombine.java | 3 +-
.../hadoop/process/computer/HadoopMap.java | 3 +-
.../hadoop/process/computer/HadoopReduce.java | 3 +-
.../structure/io/HadoopPoolShimService.java | 76 ++++
.../hadoop/structure/io/HadoopPools.java | 1 +
.../structure/io/HadoopPoolsConfigurable.java | 4 +-
.../hadoop/structure/io/ObjectWritable.java | 25 +-
.../hadoop/structure/io/VertexWritable.java | 39 +-
.../structure/io/gryo/GryoRecordReader.java | 3 +-
.../structure/io/gryo/GryoRecordWriter.java | 4 +-
...n.structure.io.gryo.kryoshim.KryoShimService | 1 +
.../spark/process/computer/SparkExecutor.java | 3 +-
.../structure/io/gryo/GryoRegistrator.java | 194 ++++++++
.../spark/structure/io/gryo/GryoSerializer.java | 2 +-
.../io/gryo/IoRegistryAwareKryoSerializer.java | 116 +++++
.../io/gryo/ObjectWritableSerializer.java | 12 +-
.../io/gryo/VertexWritableSerializer.java | 12 +-
.../kryoshim/unshaded/UnshadedInputAdapter.java | 78 ++++
.../kryoshim/unshaded/UnshadedKryoAdapter.java | 74 +++
.../unshaded/UnshadedKryoShimService.java | 154 +++++++
.../unshaded/UnshadedOutputAdapter.java | 83 ++++
.../unshaded/UnshadedSerializerAdapter.java | 57 +++
...n.structure.io.gryo.kryoshim.KryoShimService | 1 +
.../spark/structure/io/ToyGraphInputRDD.java | 3 +-
50 files changed, 2403 insertions(+), 487 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4a185a37/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/util/VertexProgramHelper.java
----------------------------------------------------------------------
diff --cc gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/util/VertexProgramHelper.java
index bc67866,bc67866..74bfd2e
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/util/VertexProgramHelper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/computer/util/VertexProgramHelper.java
@@@ -25,8 -25,8 +25,14 @@@ import org.apache.tinkerpop.gremlin.pro
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.EdgeVertexStep;
import org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexStep;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.GryoPoolKryoShimService;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.util.Serializer;
++import java.io.ByteArrayInputStream;
++import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
@@@ -63,25 -63,25 +69,19 @@@ public final class VertexProgramHelper
public static void serialize(final Object object, final Configuration configuration, final String key) {
if (configuration instanceof AbstractConfiguration)
((AbstractConfiguration) configuration).setDelimiterParsingDisabled(true);
-- try {
-- final String byteString = Arrays.toString(Serializer.serializeObject(object));
-- configuration.setProperty(key, byteString.substring(1, byteString.length() - 1));
-- } catch (final IOException e) {
-- throw new IllegalArgumentException(e.getMessage(), e);
-- }
++ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
++ new GryoPoolKryoShimService().writeClassAndObject(object, outputStream);
++ final String byteString = Arrays.toString(outputStream.toByteArray());
++ configuration.setProperty(key, byteString.substring(1, byteString.length() - 1));
}
public static <T> T deserialize(final Configuration configuration, final String key) {
-- try {
-- final String[] stringBytes = configuration.getString(key).split(",");
-- byte[] bytes = new byte[stringBytes.length];
-- for (int i = 0; i < stringBytes.length; i++) {
-- bytes[i] = Byte.valueOf(stringBytes[i].trim());
-- }
-- return (T) Serializer.deserializeObject(bytes);
-- } catch (final IOException | ClassNotFoundException e) {
-- throw new IllegalArgumentException(e.getMessage(), e);
++ final String[] stringBytes = configuration.getString(key).split(",");
++ byte[] bytes = new byte[stringBytes.length];
++ for (int i = 0; i < stringBytes.length; i++) {
++ bytes[i] = Byte.valueOf(stringBytes[i].trim());
}
++ return (T) new GryoPoolKryoShimService().readClassAndObject(new ByteArrayInputStream(bytes));
}
public static <S, E> Traversal.Admin<S, E> reverse(final Traversal.Admin<S, E> traversal) {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4a185a37/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
----------------------------------------------------------------------
diff --cc gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
index 9cae845,41ca44d..8c5fdcd
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
@@@ -43,10 -45,10 +45,7 @@@ import org.apache.tinkerpop.gremlin.pro
import org.apache.tinkerpop.gremlin.process.traversal.traverser.O_OB_S_SE_SL_Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.O_Traverser;
import org.apache.tinkerpop.gremlin.process.traversal.traverser.util.TraverserSet;
--import org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversalMetrics;
--import org.apache.tinkerpop.gremlin.process.traversal.util.ImmutableMetrics;
--import org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics;
--import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation;
++import org.apache.tinkerpop.gremlin.process.traversal.util.*;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
@@@ -244,119 -243,119 +240,121 @@@ public final class GryoMapper implement
* Note that the following are pre-registered boolean, Boolean, byte, Byte, char, Character, double, Double,
* int, Integer, float, Float, long, Long, short, Short, String, void.
*/
- private final List<Triplet<Class, Function<Kryo, Serializer>, Integer>> serializationList = new ArrayList<Triplet<Class, Function<Kryo, Serializer>, Integer>>() {{
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(byte[].class, null, 25));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(char[].class, null, 26));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(short[].class, null, 27));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(int[].class, null, 28));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(long[].class, null, 29));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(float[].class, null, 30));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(double[].class, null, 31));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(String[].class, null, 32));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Object[].class, null, 33));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ArrayList.class, null, 10));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BigInteger.class, null, 34));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BigDecimal.class, null, 35));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Calendar.class, null, 39));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Class.class, null, 41));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collection.class, null, 37));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_LIST.getClass(), null, 51));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_MAP.getClass(), null, 52));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_SET.getClass(), null, 53));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singleton(null).getClass(), null, 54));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singletonList(null).getClass(), null, 24));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singletonMap(null, null).getClass(), null, 23));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Contains.class, null, 49));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Currency.class, null, 40));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Date.class, null, 38));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Direction.class, null, 12));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedEdge.class, null, 21));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedVertexProperty.class, null, 20));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedProperty.class, null, 18));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedVertex.class, null, 19));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedPath.class, null, 60));
+ private final List<TypeRegistration<?>> typeRegistrations = new ArrayList<TypeRegistration<?>>() {{
+ add(GryoTypeReg.of(byte[].class, 25));
+ add(GryoTypeReg.of(char[].class, 26));
+ add(GryoTypeReg.of(short[].class, 27));
+ add(GryoTypeReg.of(int[].class, 28));
+ add(GryoTypeReg.of(long[].class, 29));
+ add(GryoTypeReg.of(float[].class, 30));
+ add(GryoTypeReg.of(double[].class, 31));
+ add(GryoTypeReg.of(String[].class, 32));
+ add(GryoTypeReg.of(Object[].class, 33));
+ add(GryoTypeReg.of(ArrayList.class, 10));
+ add(GryoTypeReg.of(BigInteger.class, 34));
+ add(GryoTypeReg.of(BigDecimal.class, 35));
+ add(GryoTypeReg.of(Calendar.class, 39));
+ add(GryoTypeReg.of(Class.class, 41));
+ add(GryoTypeReg.of(Collection.class, 37));
+ add(GryoTypeReg.of(Collections.EMPTY_LIST.getClass(), 51));
+ add(GryoTypeReg.of(Collections.EMPTY_MAP.getClass(), 52));
+ add(GryoTypeReg.of(Collections.EMPTY_SET.getClass(), 53));
+ add(GryoTypeReg.of(Collections.singleton(null).getClass(), 54));
+ add(GryoTypeReg.of(Collections.singletonList(null).getClass(), 24));
+ add(GryoTypeReg.of(Collections.singletonMap(null, null).getClass(), 23));
+ add(GryoTypeReg.of(Contains.class, 49));
+ add(GryoTypeReg.of(Currency.class, 40));
+ add(GryoTypeReg.of(Date.class, 38));
+ add(GryoTypeReg.of(Direction.class, 12));
+ add(GryoTypeReg.of(DetachedEdge.class, 21));
+ add(GryoTypeReg.of(DetachedVertexProperty.class, 20));
+ add(GryoTypeReg.of(DetachedProperty.class, 18));
+ add(GryoTypeReg.of(DetachedVertex.class, 19));
+ add(GryoTypeReg.of(DetachedPath.class, 60));
// skip 14
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(EnumSet.class, null, 46));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashMap.class, null, 11));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashMap.Entry.class, null, 16));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HASH_MAP_NODE, null, 92));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(KryoSerializable.class, null, 36));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedHashMap.class, null, 47));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedHashSet.class, null, 71));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedList.class, null, 116));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LINKED_HASH_MAP_ENTRY_CLASS, null, 15));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Locale.class, null, 22));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StringBuffer.class, null, 43));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StringBuilder.class, null, 44));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(T.class, null, 48));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TimeZone.class, null, 42));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeMap.class, null, 45));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeSet.class, null, 50));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(UUID.class, kryo -> new UUIDSerializer(), 17));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(URI.class, kryo -> new URISerializer(), 72));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(VertexTerminator.class, null, 13));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceEdge.class, null, 81));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceVertexProperty.class, null, 82));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceProperty.class, null, 83));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceVertex.class, null, 84));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferencePath.class, null, 85));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StarGraph.class, kryo -> StarGraphGryoSerializer.with(Direction.BOTH), 86));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Edge.class, kryo -> new GryoSerializers.EdgeSerializer(), 65));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Vertex.class, kryo -> new GryoSerializers.VertexSerializer(), 66));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Property.class, kryo -> new GryoSerializers.PropertySerializer(), 67));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(VertexProperty.class, kryo -> new GryoSerializers.VertexPropertySerializer(), 68));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Path.class, kryo -> new GryoSerializers.PathSerializer(), 59));
+ add(GryoTypeReg.of(EnumSet.class, 46));
+ add(GryoTypeReg.of(HashMap.class, 11));
+ add(GryoTypeReg.of(HashMap.Entry.class, 16));
+ add(GryoTypeReg.of(HASH_MAP_NODE, 92));
+ add(GryoTypeReg.of(KryoSerializable.class, 36));
+ add(GryoTypeReg.of(LinkedHashMap.class, 47));
+ add(GryoTypeReg.of(LinkedHashSet.class, 71));
+ add(GryoTypeReg.of(LinkedList.class, 116));
+ add(GryoTypeReg.of(LINKED_HASH_MAP_ENTRY_CLASS, 15));
+ add(GryoTypeReg.of(Locale.class, 22));
+ add(GryoTypeReg.of(StringBuffer.class, 43));
+ add(GryoTypeReg.of(StringBuilder.class, 44));
+ add(GryoTypeReg.of(T.class, 48));
+ add(GryoTypeReg.of(TimeZone.class, 42));
+ add(GryoTypeReg.of(TreeMap.class, 45));
+ add(GryoTypeReg.of(TreeSet.class, 50));
+ add(GryoTypeReg.of(UUID.class, 17, new UUIDSerializer()));
+ add(GryoTypeReg.of(URI.class, 72, new URISerializer()));
+ add(GryoTypeReg.of(VertexTerminator.class, 13));
+
+ add(GryoTypeReg.of(ReferenceEdge.class, 81));
+ add(GryoTypeReg.of(ReferenceVertexProperty.class, 82));
+ add(GryoTypeReg.of(ReferenceProperty.class, 83));
+ add(GryoTypeReg.of(ReferenceVertex.class, 84));
+ add(GryoTypeReg.of(ReferencePath.class, 85));
+
+ add(GryoTypeReg.of(StarGraph.class, 86, new StarGraphSerializer(Direction.BOTH, new GraphFilter())));
+
+ add(GryoTypeReg.of(Edge.class, 65, new GryoSerializers.EdgeSerializer()));
+ add(GryoTypeReg.of(Vertex.class, 66, new GryoSerializers.VertexSerializer()));
+ add(GryoTypeReg.of(Property.class, 67, new GryoSerializers.PropertySerializer()));
+ add(GryoTypeReg.of(VertexProperty.class, 68, new GryoSerializers.VertexPropertySerializer()));
+ add(GryoTypeReg.of(Path.class, 59, new GryoSerializers.PathSerializer()));
// skip 55
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_O_Traverser.class, null, 75));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(O_Traverser.class, null, 76));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_LP_O_P_S_SE_SL_Traverser.class, null, 77));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_O_S_SE_SL_Traverser.class, null, 78));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_LP_O_S_SE_SL_Traverser.class, null, 87));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(O_OB_S_SE_SL_Traverser.class, null, 89));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LP_O_OB_S_SE_SL_Traverser.class, null, 90));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LP_O_OB_P_S_SE_SL_Traverser.class, null, 91));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TraverserSet.class, null, 58));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Tree.class, null, 61));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashSet.class, null, 62));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BulkSet.class, null, 64));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MutableMetrics.class, null, 69));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ImmutableMetrics.class, null, 115));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DefaultTraversalMetrics.class, null, 70));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MapMemory.class, null, 73));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MapReduce.NullObject.class, null, 74));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(AtomicLong.class, null, 79));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Pair.class, kryo -> new PairSerializer(), 88));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TraversalExplanation.class, kryo -> new JavaSerializer(), 106));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Duration.class, kryo -> new JavaTimeSerializers.DurationSerializer(), 93));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Instant.class, kryo -> new JavaTimeSerializers.InstantSerializer(), 94));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalDate.class, kryo -> new JavaTimeSerializers.LocalDateSerializer(), 95));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalDateTime.class, kryo -> new JavaTimeSerializers.LocalDateTimeSerializer(), 96));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalTime.class, kryo -> new JavaTimeSerializers.LocalTimeSerializer(), 97));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MonthDay.class, kryo -> new JavaTimeSerializers.MonthDaySerializer(), 98));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OffsetDateTime.class, kryo -> new JavaTimeSerializers.OffsetDateTimeSerializer(), 99));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OffsetTime.class, kryo -> new JavaTimeSerializers.OffsetTimeSerializer(), 100));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Period.class, kryo -> new JavaTimeSerializers.PeriodSerializer(), 101));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Year.class, kryo -> new JavaTimeSerializers.YearSerializer(), 102));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(YearMonth.class, kryo -> new JavaTimeSerializers.YearMonthSerializer(), 103));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ZonedDateTime.class, kryo -> new JavaTimeSerializers.ZonedDateTimeSerializer(), 104));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ZoneOffset.class, kryo -> new JavaTimeSerializers.ZoneOffsetSerializer(), 105));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Operator.class, null, 107));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(FoldStep.FoldBiOperator.class, null, 108));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupCountStep.GroupCountBiOperator.class, null, 109));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupStep.GroupBiOperator.class, kryo -> new JavaSerializer(), 117)); // because they contain traversals
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MeanGlobalStep.MeanGlobalBiOperator.class, null, 110));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MeanGlobalStep.MeanNumber.class, null, 111));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeStep.TreeBiOperator.class, null, 112));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupStepV3d0.GroupBiOperatorV3d0.class, null, 113));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(RangeGlobalStep.RangeBiOperator.class, null, 114));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OrderGlobalStep.OrderBiOperator.class, kryo -> new JavaSerializer(), 118)); // because they contain traversals
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ProfileStep.ProfileBiOperator.class, null, 119)); // ***LAST ID***
+ add(GryoTypeReg.of(B_O_Traverser.class, 75));
+ add(GryoTypeReg.of(O_Traverser.class, 76));
+ add(GryoTypeReg.of(B_LP_O_P_S_SE_SL_Traverser.class, 77));
+ add(GryoTypeReg.of(B_O_S_SE_SL_Traverser.class, 78));
+ add(GryoTypeReg.of(B_LP_O_S_SE_SL_Traverser.class, 87));
+ add(GryoTypeReg.of(O_OB_S_SE_SL_Traverser.class, 89));
+ add(GryoTypeReg.of(LP_O_OB_S_SE_SL_Traverser.class, 90));
+ add(GryoTypeReg.of(LP_O_OB_P_S_SE_SL_Traverser.class, 91));
+
+ add(GryoTypeReg.of(TraverserSet.class, 58));
+ add(GryoTypeReg.of(Tree.class, 61));
+ add(GryoTypeReg.of(HashSet.class, 62));
+ add(GryoTypeReg.of(BulkSet.class, 64));
+ add(GryoTypeReg.of(MutableMetrics.class, 69));
+ add(GryoTypeReg.of(ImmutableMetrics.class, 115));
+ add(GryoTypeReg.of(DefaultTraversalMetrics.class, 70));
+ add(GryoTypeReg.of(MapMemory.class, 73));
+ add(GryoTypeReg.of(MapReduce.NullObject.class, 74));
+ add(GryoTypeReg.of(AtomicLong.class, 79));
+ add(GryoTypeReg.of(Pair.class, 88, new PairSerializer()));
+ add(GryoTypeReg.of(TraversalExplanation.class, 106, new JavaSerializer()));
++ add(GryoTypeReg.of(PureTraversal.class, 120, new JavaSerializer()));
++ add(GryoTypeReg.of(GraphFilter.class, 121, new JavaSerializer())); // ***LAST ID***
+
+ add(GryoTypeReg.of(Duration.class, 93, new JavaTimeSerializers.DurationSerializer()));
+ add(GryoTypeReg.of(Instant.class, 94, new JavaTimeSerializers.InstantSerializer()));
+ add(GryoTypeReg.of(LocalDate.class, 95, new JavaTimeSerializers.LocalDateSerializer()));
+ add(GryoTypeReg.of(LocalDateTime.class, 96, new JavaTimeSerializers.LocalDateTimeSerializer()));
+ add(GryoTypeReg.of(LocalTime.class, 97, new JavaTimeSerializers.LocalTimeSerializer()));
+ add(GryoTypeReg.of(MonthDay.class, 98, new JavaTimeSerializers.MonthDaySerializer()));
+ add(GryoTypeReg.of(OffsetDateTime.class, 99, new JavaTimeSerializers.OffsetDateTimeSerializer()));
+ add(GryoTypeReg.of(OffsetTime.class, 100, new JavaTimeSerializers.OffsetTimeSerializer()));
+ add(GryoTypeReg.of(Period.class, 101, new JavaTimeSerializers.PeriodSerializer()));
+ add(GryoTypeReg.of(Year.class, 102, new JavaTimeSerializers.YearSerializer()));
+ add(GryoTypeReg.of(YearMonth.class, 103, new JavaTimeSerializers.YearMonthSerializer()));
+ add(GryoTypeReg.of(ZonedDateTime.class, 104, new JavaTimeSerializers.ZonedDateTimeSerializer()));
+ add(GryoTypeReg.of(ZoneOffset.class, 105, new JavaTimeSerializers.ZoneOffsetSerializer()));
+
+ add(GryoTypeReg.of(Operator.class, 107));
+ add(GryoTypeReg.of(FoldStep.FoldBiOperator.class, 108));
+ add(GryoTypeReg.of(GroupCountStep.GroupCountBiOperator.class, 109));
+ add(GryoTypeReg.of(GroupStep.GroupBiOperator.class, 117, new JavaSerializer())); // because they contain traversals
+ add(GryoTypeReg.of(MeanGlobalStep.MeanGlobalBiOperator.class, 110));
+ add(GryoTypeReg.of(MeanGlobalStep.MeanNumber.class, 111));
+ add(GryoTypeReg.of(TreeStep.TreeBiOperator.class, 112));
+ add(GryoTypeReg.of(GroupStepV3d0.GroupBiOperatorV3d0.class, 113));
+ add(GryoTypeReg.of(RangeGlobalStep.RangeBiOperator.class, 114));
+ add(GryoTypeReg.of(OrderGlobalStep.OrderBiOperator.class, 118, new JavaSerializer())); // because they contain traversals
- add(GryoTypeReg.of(ProfileStep.ProfileBiOperator.class, 119)); // ***LAST ID***
++ add(GryoTypeReg.of(ProfileStep.ProfileBiOperator.class, 119));
}};
private final List<IoRegistry> registries = new ArrayList<>();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4a185a37/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/GryoPoolKryoShimService.java
----------------------------------------------------------------------
diff --cc gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/GryoPoolKryoShimService.java
index 0000000,0000000..526b702
new file mode 100644
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/GryoPoolKryoShimService.java
@@@ -1,0 -1,0 +1,65 @@@
++/*
++ * 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.structure.io.gryo.kryoshim;
++
++import org.apache.commons.configuration.BaseConfiguration;
++import org.apache.commons.configuration.Configuration;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoPool;
++import org.apache.tinkerpop.shaded.kryo.io.Input;
++
++import java.io.InputStream;
++import java.io.OutputStream;
++import java.util.Collections;
++
++/**
++ * @author Marko A. Rodriguez (http://markorodriguez.com)
++ */
++public class GryoPoolKryoShimService implements KryoShimService {
++
++ private static GryoPool GRYO_POOL = null;
++
++ public Object readClassAndObject(final InputStream inputStream) {
++ applyConfiguration(new BaseConfiguration());
++ return GRYO_POOL.readWithKryo(reader -> reader.readClassAndObject(new Input(inputStream)));
++ }
++
++ @Override
++ public void writeClassAndObject(final Object object, final OutputStream outputStream) {
++ applyConfiguration(new BaseConfiguration());
++ GRYO_POOL.doWithWriter(writer -> writer.writeObject(outputStream, object));
++
++ }
++
++ @Override
++ public int getPriority() {
++ return 1;
++ }
++
++ @Override
++ public void applyConfiguration(final Configuration configuration) {
++ if (null == GRYO_POOL) {
++ GRYO_POOL = GryoPool.build().
++ poolSize(configuration.getInt(GryoPool.CONFIG_IO_GRYO_POOL_SIZE, 256)).
++ ioRegistries(configuration.getList(GryoPool.CONFIG_IO_REGISTRY, Collections.emptyList())).
++ initializeMapper(m -> m.registrationRequired(false)).
++ create();
++ }
++ }
++}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4a185a37/gremlin-core/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
----------------------------------------------------------------------
diff --cc gremlin-core/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
index 0000000,0000000..5045e05
new file mode 100644
--- /dev/null
+++ b/gremlin-core/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
@@@ -1,0 -1,0 +1,1 @@@
++org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.GryoPoolKryoShimService
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4a185a37/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoRegistrator.java
----------------------------------------------------------------------
diff --cc spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoRegistrator.java
index 0000000,0000000..1b98284
new file mode 100644
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoRegistrator.java
@@@ -1,0 -1,0 +1,194 @@@
++/*
++ * 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.spark.structure.io.gryo;
++
++import com.esotericsoftware.kryo.Kryo;
++import com.esotericsoftware.kryo.Serializer;
++import com.esotericsoftware.kryo.serializers.JavaSerializer;
++import org.apache.spark.serializer.KryoRegistrator;
++import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
++import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
++import org.apache.tinkerpop.gremlin.spark.process.computer.payload.MessagePayload;
++import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewIncomingPayload;
++import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewOutgoingPayload;
++import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewPayload;
++import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded.UnshadedSerializerAdapter;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.TypeRegistration;
++import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
++import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
++import org.slf4j.Logger;
++import org.slf4j.LoggerFactory;
++
++import java.util.*;
++
++/**
++ * A spark.kryo.registrator implementation that installs TinkerPop types.
++ * This is intended for use with spark.serializer=KryoSerializer, not GryoSerializer.
++ */
++public class GryoRegistrator implements KryoRegistrator {
++
++ private static final Logger log = LoggerFactory.getLogger(GryoRegistrator.class);
++
++ @Override
++ public void registerClasses(Kryo kryo) {
++ registerClasses(kryo, Collections.emptyMap(), Collections.emptySet());
++ }
++
++ /**
++ * Register TinkerPop's classes with the supplied {@link Kryo} instance
++ * while honoring optional overrides and optional class blacklist ("blackset"?).
++ *
++ * @param kryo the Kryo serializer instance with which to register types
++ * @param serializerOverrides serializer mappings that override this class's defaults
++ * @param blacklist classes which should not be registered at all, even if there is an override entry
++ * or if they would be registered by this class by default (does not affect Kryo's
++ * built-in registrations, e.g. String.class).
++ */
++ public void registerClasses(Kryo kryo, Map<Class<?>, Serializer<?>> serializerOverrides, Set<Class<?>> blacklist) {
++ // Apply TinkerPop type registrations copied from GyroSerializer's constructor
++ for (Map.Entry<Class<?>, Serializer<?>> ent : getExtraRegistrations().entrySet()) {
++ Class<?> targetClass = ent.getKey();
++ Serializer<?> ser = ent.getValue();
++
++ // Is this class blacklisted? Skip it. (takes precedence over serializerOverrides)
++ if (blacklist.contains(targetClass)) {
++ log.debug("Not registering serializer for {} (blacklisted)", targetClass);
++ continue;
++ }
++
++ if (checkForAndApplySerializerOverride(serializerOverrides, kryo, targetClass)) {
++ // do nothing but skip the remaining else(-if) clauses
++ } else if (null == ser) {
++ log.debug("Registering {} with default serializer", targetClass);
++ kryo.register(targetClass);
++ } else {
++ log.debug("Registering {} with serializer {}", targetClass, ser);
++ kryo.register(targetClass, ser);
++ }
++ }
++
++ Set<Class<?>> shimmedClassesFromGryoMapper = new HashSet<>();
++
++ // Apply GryoMapper's default registrations
++ for (TypeRegistration<?> tr : GryoMapper.build().create().getTypeRegistrations()) {
++ // Is this class blacklisted? Skip it. (takes precedence over serializerOverrides)
++ if (blacklist.contains(tr.getTargetClass())) {
++ log.debug("Not registering serializer for {} (blacklisted)", tr.getTargetClass());
++ continue;
++ }
++
++ final org.apache.tinkerpop.shaded.kryo.Serializer<?> shadedSerializer = tr.getShadedSerializer();
++ final SerializerShim<?> serializerShim = tr.getSerializerShim();
++ final java.util.function.Function<
++ org.apache.tinkerpop.shaded.kryo.Kryo,
++ org.apache.tinkerpop.shaded.kryo.Serializer> functionOfShadedKryo = tr.getFunctionOfShadedKryo();
++
++ // Apply overrides with the highest case-precedence
++ if (checkForAndApplySerializerOverride(serializerOverrides, kryo, tr.getTargetClass())) {
++ // do nothing but skip the remaining else(-if) clauses
++ } else if (null != shadedSerializer) {
++ if (shadedSerializer.getClass().equals(org.apache.tinkerpop.shaded.kryo.serializers.JavaSerializer.class)) {
++ // Convert GryoMapper's shaded JavaSerializer mappings to their unshaded equivalents
++ log.debug("Registering {} with JavaSerializer", tr.getTargetClass());
++ kryo.register(tr.getTargetClass(), new JavaSerializer());
++ } else {
++ // There's supposed to be a check in GryoMapper that prevents this from happening
++ log.error("GryoMapper's default serialization registration for {} is a {}. " +
++ "This is probably a bug in TinkerPop (this is not a valid default registration). " +
++ "I am configuring Spark to use Kryo's default serializer for this class, " +
++ "but this may cause serialization failures at runtime.",
++ tr.getTargetClass(),
++ org.apache.tinkerpop.shaded.kryo.Serializer.class.getCanonicalName());
++ kryo.register(tr.getTargetClass());
++ }
++ } else if (null != serializerShim) {
++ // Wrap shim serializers in an adapter for Spark's unshaded Kryo
++ log.debug("Registering {} to serializer shim {} (serializer shim {})",
++ tr.getTargetClass(), serializerShim, serializerShim.getClass());
++ kryo.register(tr.getTargetClass(), new UnshadedSerializerAdapter<>(serializerShim));
++ shimmedClassesFromGryoMapper.add(tr.getTargetClass());
++ } else if (null != functionOfShadedKryo) {
++ // As with shaded serializers, there's supposed to be a check in GryoMapper that prevents this from happening
++ log.error("GryoMapper's default serialization registration for {} is a Function<{},{}>. " +
++ "This is probably a bug in TinkerPop (this is not a valid default registration). " +
++ "I am configuring Spark to use Kryo's default serializer instead of this function, " +
++ "but this may cause serialization failures at runtime.",
++ tr.getTargetClass(),
++ org.apache.tinkerpop.shaded.kryo.Kryo.class.getCanonicalName(),
++ org.apache.tinkerpop.shaded.kryo.Serializer.class.getCanonicalName());
++ kryo.register(tr.getTargetClass());
++ } else {
++ // Register all other classes with the default behavior (FieldSerializer)
++ log.debug("Registering {} with default serializer", tr.getTargetClass());
++ kryo.register(tr.getTargetClass());
++ }
++ }
++
++ // StarGraph's shim serializer is especially important on Spark for efficiency reasons,
++ // so log a warning if we failed to register it somehow
++ if (!shimmedClassesFromGryoMapper.contains(StarGraph.class)) {
++ log.warn("No SerializerShim found for StarGraph");
++ }
++ }
++
++ private LinkedHashMap<Class<?>, Serializer<?>> getExtraRegistrations() {
++
++ /* The map returned by this method MUST have a fixed iteration order!
++ *
++ * The order itself is irrelevant, so long as it is completely stable at runtime.
++ *
++ * LinkedHashMap satisfies this requirement (its contract specifies
++ * iteration in key-insertion-order).
++ */
++
++ LinkedHashMap<Class<?>, Serializer<?>> m = new LinkedHashMap<>();
++ // The following entries were copied from GryoSerializer's constructor
++ // This could be turned into a static collection on GryoSerializer to avoid
++ // duplication, but it would be a bit cumbersome to do so without disturbing
++ // the ordering of the existing entries in that constructor, since not all
++ // of the entries are for TinkerPop (and the ordering is significant).
++ m.put(MessagePayload.class, null);
++ m.put(ViewIncomingPayload.class, null);
++ m.put(ViewOutgoingPayload.class, null);
++ m.put(ViewPayload.class, null);
++ m.put(VertexWritable.class, new UnshadedSerializerAdapter<>(new VertexWritableSerializer()));
++ m.put(ObjectWritable.class, new UnshadedSerializerAdapter<>(new ObjectWritableSerializer<>()));
++
++ return m;
++ }
++
++ private boolean checkForAndApplySerializerOverride(Map<Class<?>, Serializer<?>> serializerOverrides,
++ Kryo kryo, Class<?> targetClass) {
++ if (serializerOverrides.containsKey(targetClass)) {
++ Serializer<?> ser = serializerOverrides.get(targetClass);
++ if (null == ser) {
++ // null means use Kryo's default serializer
++ log.debug("Registering {} with default serializer per overrides", targetClass);
++ kryo.register(targetClass);
++ } else {
++ // nonnull means use that serializer
++ log.debug("Registering {} with serializer {} per overrides", targetClass, ser);
++ kryo.register(targetClass, ser);
++ }
++ return true;
++ }
++ return false;
++ }
++}
[3/8] incubator-tinkerpop git commit: Kryo shim refactoring and
documentation
Posted by ok...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
index d5ba90d..431e1eb 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphSerializer.java
@@ -27,11 +27,21 @@ import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+/**
+ * Kryo serializer for {@link StarGraph}. Implements an internal versioning capability for backward compatibility.
+ * The single byte at the front of the serialization stream denotes the version. That version can be used to choose
+ * the correct deserialization mechanism. The limitation is that this versioning won't help with backward
+ * compatibility for custom serializers from providers. Providers should be encouraged to write their serializers
+ * with backward compatibility in mind.
+ *
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
public class StarGraphSerializer implements SerializerShim<StarGraph> {
private final Direction edgeDirectionToSerialize;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
index 2053280..c19b914 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
@@ -18,7 +18,7 @@
*/
package org.apache.tinkerpop.gremlin.hadoop.structure.io;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.io.Input;
import org.apache.tinkerpop.shaded.kryo.io.Output;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
index e7a38a5..88f7ee1 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/ObjectWritable.java
@@ -21,11 +21,9 @@ package org.apache.tinkerpop.gremlin.hadoop.structure.io;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimServiceLoader;
-import org.apache.tinkerpop.shaded.kryo.io.Output;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
index 7ac8e8c..2252ded 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/VertexWritable.java
@@ -21,12 +21,11 @@ package org.apache.tinkerpop.gremlin.hadoop.structure.io;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableUtils;
import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimServiceLoader;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService b/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
new file mode 100644
index 0000000..0b27e72
--- /dev/null
+++ b/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
@@ -0,0 +1 @@
+org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPoolShimService # HadoopPools provides/caches instances of TinkerPop's shaded Kryo
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService b/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
deleted file mode 100644
index 0b27e72..0000000
--- a/hadoop-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShimService
+++ /dev/null
@@ -1 +0,0 @@
-org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPoolShimService # HadoopPools provides/caches instances of TinkerPop's shaded Kryo
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
new file mode 100644
index 0000000..4c99e70
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
@@ -0,0 +1,121 @@
+/*
+ * 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.spark.structure.io;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.serializers.JavaSerializer;
+import com.google.common.base.Preconditions;
+import org.apache.spark.serializer.KryoRegistrator;
+import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
+import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.GroupStep;
+import org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.MessagePayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewIncomingPayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewOutgoingPayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewPayload;
+import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.ObjectWritableSerializer;
+import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.VertexWritableSerializer;
+import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded.UnshadedSerializerAdapter;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.TypeRegistration;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A spark.kryo.registrator implementation that installs TinkerPop types.
+ * This is intended for use with spark.serializer=KryoSerializer, not GryoSerializer.
+ */
+public class TinkerPopKryoRegistrator implements KryoRegistrator {
+
+ private static final Logger log = LoggerFactory.getLogger(TinkerPopKryoRegistrator.class);
+
+ @Override
+ public void registerClasses(Kryo kryo) {
+ // TinkerPop type registrations copied from GyroSerializer's constructor
+ kryo.register(MessagePayload.class);
+ kryo.register(ViewIncomingPayload.class);
+ kryo.register(ViewOutgoingPayload.class);
+ kryo.register(ViewPayload.class);
+ kryo.register(VertexWritable.class, new UnshadedSerializerAdapter<>(new VertexWritableSerializer()));
+ kryo.register(ObjectWritable.class, new UnshadedSerializerAdapter<>(new ObjectWritableSerializer<>()));
+
+ Set<Class<?>> shimmedClasses = new HashSet<>();
+
+ Set<Class<?>> javaSerializationClasses = new HashSet<>();
+
+ // Copy GryoMapper's default registrations
+ for (TypeRegistration<?> tr : GryoMapper.build().create().getTypeRegistrations()) {
+ // Special case for JavaSerializer, which is generally implemented in terms of TinkerPop's
+ // problematic static GryoMapper/GryoSerializer pool (these are handled below the loop)
+ org.apache.tinkerpop.shaded.kryo.Serializer<?> shadedSerializer = tr.getShadedSerializer();
+ SerializerShim<?> serializerShim = tr.getSerializerShim();
+ if (null != shadedSerializer &&
+ shadedSerializer.getClass().equals(org.apache.tinkerpop.shaded.kryo.serializers.JavaSerializer.class)) {
+ javaSerializationClasses.add(tr.getTargetClass());
+ } else if (null != serializerShim) {
+ log.debug("Registering class {} to serializer shim {} (serializer shim class {})",
+ tr.getTargetClass(), serializerShim, serializerShim.getClass());
+ kryo.register(tr.getTargetClass(), new UnshadedSerializerAdapter<>(serializerShim));
+ shimmedClasses.add(tr.getTargetClass());
+ } else {
+ // Register with the default behavior (FieldSerializer)
+ log.debug("Registering class {} with default serializer", tr.getTargetClass());
+ kryo.register(tr.getTargetClass());
+ }
+ }
+
+ Map<Class<?>, Serializer<?>> javaSerializerReplacements = new HashMap<>();
+ javaSerializerReplacements.put(GroupStep.GroupBiOperator.class, new JavaSerializer());
+ javaSerializerReplacements.put(OrderGlobalStep.OrderBiOperator.class, null);
+ javaSerializerReplacements.put(TraversalExplanation.class, null);
+
+ for (Map.Entry<Class<?>, Serializer<?>> e : javaSerializerReplacements.entrySet()) {
+ Class<?> c = e.getKey();
+ Serializer<?> s = e.getValue();
+
+ if (javaSerializationClasses.remove(c)) {
+ if (null != s) {
+ log.debug("Registering class {} with serializer {}", c, s);
+ kryo.register(c, s);
+ } else {
+ log.debug("Registering class {} with default serializer", c);
+ kryo.register(c);
+ }
+ } else {
+ log.debug("Registering class {} with JavaSerializer", c);
+ kryo.register(c, new JavaSerializer());
+ }
+ }
+
+ // We really care about StarGraph's shim serializer, so make sure we registered it
+ if (!shimmedClasses.contains(StarGraph.class)) {
+ log.warn("No SerializerShim found for StarGraph");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/ObjectWritableSerializer.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/ObjectWritableSerializer.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/ObjectWritableSerializer.java
index 21cbc60..4ceb045 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/ObjectWritableSerializer.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/ObjectWritableSerializer.java
@@ -20,6 +20,10 @@
package org.apache.tinkerpop.gremlin.spark.structure.io.gryo;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
import org.apache.tinkerpop.shaded.kryo.io.Input;
@@ -28,16 +32,16 @@ import org.apache.tinkerpop.shaded.kryo.io.Output;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public final class ObjectWritableSerializer<T> extends Serializer<ObjectWritable<T>> {
+public final class ObjectWritableSerializer<T> implements SerializerShim<ObjectWritable<T>> {
@Override
- public void write(final Kryo kryo, final Output output, final ObjectWritable<T> objectWritable) {
- kryo.writeClassAndObject(output, objectWritable.get());
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, ObjectWritable<T> starGraph) {
+ kryo.writeClassAndObject(output, starGraph.get());
output.flush();
}
@Override
- public ObjectWritable<T> read(final Kryo kryo, final Input input, final Class<ObjectWritable<T>> clazz) {
+ public <I extends InputShim> ObjectWritable<T> read(KryoShim<I, ?> kryo, I input, Class<ObjectWritable<T>> clazz) {
return new ObjectWritable(kryo.readClassAndObject(input));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/VertexWritableSerializer.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/VertexWritableSerializer.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/VertexWritableSerializer.java
index 97891f3..f3c1b15 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/VertexWritableSerializer.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/VertexWritableSerializer.java
@@ -20,6 +20,10 @@
package org.apache.tinkerpop.gremlin.spark.structure.io.gryo;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
@@ -29,14 +33,16 @@ import org.apache.tinkerpop.shaded.kryo.io.Output;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-public final class VertexWritableSerializer extends Serializer<VertexWritable> {
+public final class VertexWritableSerializer implements SerializerShim<VertexWritable> {
+
@Override
- public void write(final Kryo kryo, final Output output, final VertexWritable vertexWritable) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, VertexWritable vertexWritable) {
kryo.writeObject(output, vertexWritable.get().graph());
+ output.flush();
}
@Override
- public VertexWritable read(final Kryo kryo, final Input input, final Class<VertexWritable> aClass) {
+ public <I extends InputShim> VertexWritable read(KryoShim<I, ?> kryo, I input, Class<VertexWritable> clazz) {
return new VertexWritable(kryo.readObject(input, StarGraph.class).getStarVertex());
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedInputAdapter.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedInputAdapter.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedInputAdapter.java
new file mode 100644
index 0000000..c533af7
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedInputAdapter.java
@@ -0,0 +1,78 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ *
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
+
+import com.esotericsoftware.kryo.io.Input;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+
+public class UnshadedInputAdapter implements InputShim
+{
+
+ private final Input unshadedInput;
+
+ public UnshadedInputAdapter(Input unshadedInput)
+ {
+ this.unshadedInput = unshadedInput;
+ }
+
+ Input getUnshadedInput()
+ {
+ return unshadedInput;
+ }
+
+ @Override
+ public byte readByte()
+ {
+ return unshadedInput.readByte();
+ }
+
+ @Override
+ public byte[] readBytes(int size) {
+ return unshadedInput.readBytes(size);
+ }
+
+ @Override
+ public String readString()
+ {
+ return unshadedInput.readString();
+ }
+
+ @Override
+ public long readLong()
+ {
+ return unshadedInput.readLong();
+ }
+
+ @Override
+ public int readInt() {
+ return unshadedInput.readInt();
+ }
+
+ @Override
+ public double readDouble()
+ {
+ return unshadedInput.readDouble();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoAdapter.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoAdapter.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoAdapter.java
new file mode 100644
index 0000000..b14abe0
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoAdapter.java
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ *
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
+
+import com.esotericsoftware.kryo.Kryo;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+
+public class UnshadedKryoAdapter implements KryoShim<UnshadedInputAdapter, UnshadedOutputAdapter>
+{
+ private final Kryo unshadedKryo;
+
+ public UnshadedKryoAdapter(Kryo unshadedKryo)
+ {
+ this.unshadedKryo = unshadedKryo;
+ }
+
+ @Override
+ public <T> T readObject(UnshadedInputAdapter input, Class<T> type)
+ {
+ return unshadedKryo.readObject(input.getUnshadedInput(), type);
+ }
+
+ @Override
+ public Object readClassAndObject(UnshadedInputAdapter input)
+ {
+ return unshadedKryo.readClassAndObject(input.getUnshadedInput());
+ }
+
+ @Override
+ public void writeObject(UnshadedOutputAdapter output, Object object)
+ {
+ unshadedKryo.writeObject(output.getUnshadedOutput(), object);
+ }
+
+ @Override
+ public void writeClassAndObject(UnshadedOutputAdapter output, Object object)
+ {
+ unshadedKryo.writeClassAndObject(output.getUnshadedOutput(), object);
+ }
+
+ @Override
+ public <T> T readObjectOrNull(UnshadedInputAdapter input, Class<T> type)
+ {
+ return unshadedKryo.readObjectOrNull(input.getUnshadedInput(), type);
+ }
+
+ @Override
+ public void writeObjectOrNull(UnshadedOutputAdapter output, Object object, Class type)
+ {
+ unshadedKryo.writeObjectOrNull(output.getUnshadedOutput(), object, type);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
new file mode 100644
index 0000000..d0411e8
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
@@ -0,0 +1,115 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ * <p>
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
+
+import com.twitter.chill.KryoInstantiator;
+import com.twitter.chill.KryoPool;
+import com.twitter.chill.SerDeState;
+import org.apache.spark.SparkConf;
+import org.apache.spark.serializer.KryoSerializer;
+import org.apache.tinkerpop.gremlin.spark.structure.io.TinkerPopKryoRegistrator;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class UnshadedKryoShimService implements KryoShimService {
+
+ public static final String SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY = "tinkerpop.kryo.poolsize";
+
+ private static final Logger log = LoggerFactory.getLogger(UnshadedKryoShimService.class);
+ private static final int SPARK_KRYO_POOL_SIZE_DEFAULT = 8;
+
+ private final KryoSerializer sparkKryoSerializer;
+ private final KryoPool kryoPool;
+
+ public UnshadedKryoShimService() {
+ this(TinkerPopKryoRegistrator.class.getCanonicalName(), getDefaultKryoPoolSize());
+ }
+
+ public UnshadedKryoShimService(String sparkKryoRegistratorClassname, int kryoPoolSize) {
+ SparkConf sparkConf = new SparkConf();
+ sparkConf.set("spark.serializer", KryoSerializer.class.getCanonicalName());
+ sparkConf.set("spark.kryo.registrator", sparkKryoRegistratorClassname);
+ sparkKryoSerializer = new KryoSerializer(sparkConf);
+ kryoPool = KryoPool.withByteArrayOutputStream(kryoPoolSize, new KryoInstantiator());
+ }
+
+ @Override
+ public Object readClassAndObject(InputStream source) {
+ SerDeState sds = null;
+ try {
+ sds = kryoPool.borrow();
+
+ sds.setInput(source);
+
+ return sds.readClassAndObject();
+ } finally {
+ kryoPool.release(sds);
+ }
+ }
+
+ @Override
+ public void writeClassAndObject(Object o, OutputStream sink) {
+ SerDeState sds = null;
+ try {
+ sds = kryoPool.borrow();
+
+ sds.writeClassAndObject(o); // this writes to an internal buffer
+
+ sds.writeOutputTo(sink); // this copies the internal buffer to sink
+
+ sink.flush();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ } finally {
+ kryoPool.release(sds);
+ }
+ }
+
+ @Override
+ public int getPriority() {
+ return 1024;
+ }
+
+ private static int getDefaultKryoPoolSize() {
+ String raw = System.getProperty(SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY);
+
+ int size = SPARK_KRYO_POOL_SIZE_DEFAULT;
+ try {
+ size = Integer.valueOf(raw);
+ log.info("Setting kryo pool size to {} according to system property {}", size,
+ SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY);
+ } catch (NumberFormatException e) {
+ log.error("System property {}={} could not be parsed as an integer, using default value {}",
+ SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY, raw, size, e);
+ }
+
+ return size;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedOutputAdapter.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedOutputAdapter.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedOutputAdapter.java
new file mode 100644
index 0000000..9cc59d4
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedOutputAdapter.java
@@ -0,0 +1,83 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ *
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
+
+import com.esotericsoftware.kryo.io.Output;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+
+public class UnshadedOutputAdapter implements OutputShim
+{
+ private final Output unshadedOutput;
+
+ public UnshadedOutputAdapter(Output unshadedOutput)
+ {
+ this.unshadedOutput = unshadedOutput;
+ }
+
+ Output getUnshadedOutput()
+ {
+ return unshadedOutput;
+ }
+
+ @Override
+ public void writeByte(byte b)
+ {
+ unshadedOutput.writeByte(b);
+ }
+
+ @Override
+ public void writeBytes(byte[] array, int offset, int count) {
+ unshadedOutput.writeBytes(array, offset, count);
+ }
+
+ @Override
+ public void writeString(String s)
+ {
+ unshadedOutput.writeString(s);
+ }
+
+ @Override
+ public void writeLong(long l)
+ {
+ unshadedOutput.writeLong(l);
+ }
+
+ @Override
+ public void writeInt(int i) {
+ unshadedOutput.writeInt(i);
+ }
+
+ @Override
+ public void writeDouble(double d)
+ {
+ unshadedOutput.writeDouble(d);
+ }
+
+ @Override
+ public void flush()
+ {
+ unshadedOutput.flush();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedSerializerAdapter.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedSerializerAdapter.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedSerializerAdapter.java
new file mode 100644
index 0000000..efc9a4f
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedSerializerAdapter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ *
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+
+public class UnshadedSerializerAdapter<T> extends Serializer<T>
+{
+
+ SerializerShim<T> serializer;
+
+ public UnshadedSerializerAdapter(SerializerShim<T> serializer) {
+ this.serializer = serializer;
+ setImmutable(this.serializer.isImmutable());
+ }
+
+ @Override
+ public void write(Kryo kryo, Output output, T t) {
+ UnshadedKryoAdapter shadedKryoAdapter = new UnshadedKryoAdapter(kryo);
+ UnshadedOutputAdapter shadedOutputAdapter = new UnshadedOutputAdapter(output);
+ serializer.write(shadedKryoAdapter, shadedOutputAdapter, t);
+ }
+
+ @Override
+ public T read(Kryo kryo, Input input, Class<T> aClass)
+ {
+ UnshadedKryoAdapter shadedKryoAdapter = new UnshadedKryoAdapter(kryo);
+ UnshadedInputAdapter shadedInputAdapter = new UnshadedInputAdapter(input);
+ return serializer.read(shadedKryoAdapter, shadedInputAdapter, aClass);
+ }
+}
[5/8] incubator-tinkerpop git commit: Kryo shim configuration tweaks
Posted by ok...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/test/java/org/apache/tinkerpop/gremlin/spark/structure/io/ToyGraphInputRDD.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/test/java/org/apache/tinkerpop/gremlin/spark/structure/io/ToyGraphInputRDD.java b/spark-gremlin/src/test/java/org/apache/tinkerpop/gremlin/spark/structure/io/ToyGraphInputRDD.java
index ea3636f..4cd8cea 100644
--- a/spark-gremlin/src/test/java/org/apache/tinkerpop/gremlin/spark/structure/io/ToyGraphInputRDD.java
+++ b/spark-gremlin/src/test/java/org/apache/tinkerpop/gremlin/spark/structure/io/ToyGraphInputRDD.java
@@ -30,6 +30,7 @@ import org.apache.tinkerpop.gremlin.structure.io.GraphReader;
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.GryoResourceAccess;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
@@ -46,7 +47,7 @@ public final class ToyGraphInputRDD implements InputRDD {
@Override
public JavaPairRDD<Object, VertexWritable> readGraphRDD(final Configuration configuration, final JavaSparkContext sparkContext) {
- HadoopPools.initialize(TinkerGraph.open().configuration());
+ KryoShimServiceLoader.applyConfiguration(TinkerGraph.open().configuration());
final List<VertexWritable> vertices;
if (configuration.getString(Constants.GREMLIN_HADOOP_INPUT_LOCATION).contains("modern"))
vertices = IteratorUtils.list(IteratorUtils.map(TinkerFactory.createModern().vertices(), VertexWritable::new));
[7/8] incubator-tinkerpop git commit: Fix MonthDay serializer
Posted by ok...@apache.org.
Fix MonthDay serializer
I carelessly broke the read leg of this serializer when porting it to
the shim. Ireplaced actual deserialization logic with autogenerated
"return null" when inserting a method template with the shim's
slightly different method signature. Trivial fix though.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ebd2f857
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ebd2f857
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ebd2f857
Branch: refs/heads/TINKERPOP-1321
Commit: ebd2f8576acdb472fa8696b58db128eac191f0ef
Parents: 9321a3e
Author: Dan LaRocque <da...@hopcount.org>
Authored: Mon Jun 6 04:35:34 2016 -0400
Committer: Dan LaRocque <da...@hopcount.org>
Committed: Mon Jun 6 04:35:34 2016 -0400
----------------------------------------------------------------------
.../tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ebd2f857/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
index 8b14345..24df032 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
@@ -139,7 +139,7 @@ final class JavaTimeSerializers {
@Override
public <I extends InputShim> MonthDay read(KryoShim<I, ?> kryo, I input, Class<MonthDay> clazz) {
- return null;
+ return MonthDay.of(input.readInt(), input.readInt());
}
}
[2/8] incubator-tinkerpop git commit: Introduce Kryo shim to support
serializer reuse
Posted by ok...@apache.org.
Introduce Kryo shim to support serializer reuse
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ef528697
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ef528697
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ef528697
Branch: refs/heads/TINKERPOP-1321
Commit: ef52869788ebf5b8b825f78ef21e1d38423d9aa0
Parents: aa673db
Author: Dan LaRocque <da...@hopcount.org>
Authored: Thu Jun 2 02:09:29 2016 -0500
Committer: Dan LaRocque <da...@hopcount.org>
Committed: Thu Jun 2 03:09:29 2016 -0400
----------------------------------------------------------------------
.../gremlin/structure/io/gryo/GryoMapper.java | 403 ++++++++++++-------
.../structure/io/gryo/TypeRegistration.java | 70 ++++
.../structure/io/gryo/URISerializer.java | 23 +-
.../structure/io/gryo/UUIDSerializer.java | 20 +-
.../structure/io/kryoshim/InputShim.java | 37 ++
.../gremlin/structure/io/kryoshim/KryoShim.java | 40 ++
.../structure/io/kryoshim/KryoShimService.java | 83 ++++
.../io/kryoshim/KryoShimServiceLoader.java | 137 +++++++
.../structure/io/kryoshim/OutputShim.java | 41 ++
.../structure/io/kryoshim/SerializerShim.java | 35 ++
.../structure/io/kryoshim/package-info.java | 54 +++
.../io/kryoshim/shaded/ShadedInputAdapter.java | 66 +++
.../io/kryoshim/shaded/ShadedKryoAdapter.java | 67 +++
.../io/kryoshim/shaded/ShadedOutputAdapter.java | 72 ++++
.../shaded/ShadedSerializerAdapter.java | 54 +++
.../io/kryoshim/shaded/package-info.java | 25 ++
.../util/star/StarGraphGryoSerializer.java | 123 +-----
.../util/star/StarGraphSerializer.java | 140 +++++++
.../structure/io/HadoopPoolShimService.java | 69 ++++
.../hadoop/structure/io/HadoopPools.java | 1 +
.../hadoop/structure/io/ObjectWritable.java | 25 +-
.../hadoop/structure/io/VertexWritable.java | 38 +-
...remlin.structure.io.kryoshim.KryoShimService | 1 +
23 files changed, 1293 insertions(+), 331 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
index 9cae845..5bc71da 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
@@ -18,6 +18,7 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
+import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
import org.apache.tinkerpop.gremlin.process.computer.util.MapMemory;
import org.apache.tinkerpop.gremlin.process.traversal.Contains;
@@ -56,6 +57,8 @@ import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
import org.apache.tinkerpop.gremlin.structure.io.Mapper;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded.ShadedSerializerAdapter;
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;
@@ -69,6 +72,7 @@ import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertexProp
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGryoSerializer;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer;
import org.apache.tinkerpop.shaded.kryo.ClassResolver;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.KryoSerializable;
@@ -77,7 +81,6 @@ import org.apache.tinkerpop.shaded.kryo.serializers.JavaSerializer;
import org.apache.tinkerpop.shaded.kryo.util.DefaultStreamFactory;
import org.apache.tinkerpop.shaded.kryo.util.MapReferenceResolver;
import org.javatuples.Pair;
-import org.javatuples.Triplet;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -156,13 +159,13 @@ import java.util.stream.Collectors;
public final class GryoMapper implements Mapper<Kryo> {
public static final byte[] GIO = "gio".getBytes();
public static final byte[] HEADER = Arrays.copyOf(GIO, 16);
- private final List<Triplet<Class, Function<Kryo, Serializer>, Integer>> serializationList;
+ private final List<TypeRegistration<?>> typeRegistrations;
private final boolean registrationRequired;
private final boolean referenceTracking;
private final Supplier<ClassResolver> classResolver;
private GryoMapper(final Builder builder) {
- this.serializationList = builder.serializationList;
+ this.typeRegistrations = builder.typeRegistrations;
validate();
this.registrationRequired = builder.registrationRequired;
@@ -176,19 +179,17 @@ public final class GryoMapper implements Mapper<Kryo> {
kryo.addDefaultSerializer(Map.Entry.class, new EntrySerializer());
kryo.setRegistrationRequired(registrationRequired);
kryo.setReferences(referenceTracking);
-
- serializationList.forEach(p -> {
- final Function<Kryo, Serializer> serializer = p.getValue1();
- if (null == serializer)
- kryo.register(p.getValue0(), kryo.getDefaultSerializer(p.getValue0()), p.getValue2());
- else
- kryo.register(p.getValue0(), serializer.apply(kryo), p.getValue2());
- });
+ for (TypeRegistration tr : typeRegistrations)
+ tr.registerWith(kryo);
return kryo;
}
public List<Class> getRegisteredClasses() {
- return this.serializationList.stream().map(Triplet::getValue0).collect(Collectors.toList());
+ return this.typeRegistrations.stream().map(TypeRegistration::getTargetClass).collect(Collectors.toList());
+ }
+
+ public List<TypeRegistration<?>> getTypeRegistrations() {
+ return typeRegistrations;
}
public static Builder build() {
@@ -199,11 +200,11 @@ public final class GryoMapper implements Mapper<Kryo> {
final Set<Integer> duplicates = new HashSet<>();
final Set<Integer> ids = new HashSet<>();
- serializationList.forEach(t -> {
- if (!ids.contains(t.getValue2()))
- ids.add(t.getValue2());
+ typeRegistrations.forEach(t -> {
+ if (!ids.contains(t.getId()))
+ ids.add(t.getId());
else
- duplicates.add(t.getValue2());
+ duplicates.add(t.getId());
});
if (duplicates.size() > 0)
@@ -244,119 +245,119 @@ public final class GryoMapper implements Mapper<Kryo> {
* Note that the following are pre-registered boolean, Boolean, byte, Byte, char, Character, double, Double,
* int, Integer, float, Float, long, Long, short, Short, String, void.
*/
- private final List<Triplet<Class, Function<Kryo, Serializer>, Integer>> serializationList = new ArrayList<Triplet<Class, Function<Kryo, Serializer>, Integer>>() {{
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(byte[].class, null, 25));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(char[].class, null, 26));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(short[].class, null, 27));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(int[].class, null, 28));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(long[].class, null, 29));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(float[].class, null, 30));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(double[].class, null, 31));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(String[].class, null, 32));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Object[].class, null, 33));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ArrayList.class, null, 10));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BigInteger.class, null, 34));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BigDecimal.class, null, 35));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Calendar.class, null, 39));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Class.class, null, 41));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collection.class, null, 37));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_LIST.getClass(), null, 51));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_MAP.getClass(), null, 52));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.EMPTY_SET.getClass(), null, 53));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singleton(null).getClass(), null, 54));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singletonList(null).getClass(), null, 24));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Collections.singletonMap(null, null).getClass(), null, 23));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Contains.class, null, 49));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Currency.class, null, 40));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Date.class, null, 38));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Direction.class, null, 12));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedEdge.class, null, 21));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedVertexProperty.class, null, 20));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedProperty.class, null, 18));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedVertex.class, null, 19));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DetachedPath.class, null, 60));
+ private final List<TypeRegistration<?>> typeRegistrations = new ArrayList<TypeRegistration<?>>() {{
+ add(GryoTypeReg.of(byte[].class, 25));
+ add(GryoTypeReg.of(char[].class, 26));
+ add(GryoTypeReg.of(short[].class, 27));
+ add(GryoTypeReg.of(int[].class, 28));
+ add(GryoTypeReg.of(long[].class, 29));
+ add(GryoTypeReg.of(float[].class, 30));
+ add(GryoTypeReg.of(double[].class, 31));
+ add(GryoTypeReg.of(String[].class, 32));
+ add(GryoTypeReg.of(Object[].class, 33));
+ add(GryoTypeReg.of(ArrayList.class, 10));
+ add(GryoTypeReg.of(BigInteger.class, 34));
+ add(GryoTypeReg.of(BigDecimal.class, 35));
+ add(GryoTypeReg.of(Calendar.class, 39));
+ add(GryoTypeReg.of(Class.class, 41));
+ add(GryoTypeReg.of(Collection.class, 37));
+ add(GryoTypeReg.of(Collections.EMPTY_LIST.getClass(), 51));
+ add(GryoTypeReg.of(Collections.EMPTY_MAP.getClass(), 52));
+ add(GryoTypeReg.of(Collections.EMPTY_SET.getClass(), 53));
+ add(GryoTypeReg.of(Collections.singleton(null).getClass(), 54));
+ add(GryoTypeReg.of(Collections.singletonList(null).getClass(), 24));
+ add(GryoTypeReg.of(Collections.singletonMap(null, null).getClass(), 23));
+ add(GryoTypeReg.of(Contains.class, 49));
+ add(GryoTypeReg.of(Currency.class, 40));
+ add(GryoTypeReg.of(Date.class, 38));
+ add(GryoTypeReg.of(Direction.class, 12));
+ add(GryoTypeReg.of(DetachedEdge.class, 21));
+ add(GryoTypeReg.of(DetachedVertexProperty.class, 20));
+ add(GryoTypeReg.of(DetachedProperty.class, 18));
+ add(GryoTypeReg.of(DetachedVertex.class, 19));
+ add(GryoTypeReg.of(DetachedPath.class, 60));
// skip 14
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(EnumSet.class, null, 46));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashMap.class, null, 11));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashMap.Entry.class, null, 16));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HASH_MAP_NODE, null, 92));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(KryoSerializable.class, null, 36));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedHashMap.class, null, 47));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedHashSet.class, null, 71));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LinkedList.class, null, 116));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LINKED_HASH_MAP_ENTRY_CLASS, null, 15));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Locale.class, null, 22));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StringBuffer.class, null, 43));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StringBuilder.class, null, 44));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(T.class, null, 48));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TimeZone.class, null, 42));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeMap.class, null, 45));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeSet.class, null, 50));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(UUID.class, kryo -> new UUIDSerializer(), 17));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(URI.class, kryo -> new URISerializer(), 72));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(VertexTerminator.class, null, 13));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceEdge.class, null, 81));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceVertexProperty.class, null, 82));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceProperty.class, null, 83));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferenceVertex.class, null, 84));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ReferencePath.class, null, 85));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(StarGraph.class, kryo -> StarGraphGryoSerializer.with(Direction.BOTH), 86));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Edge.class, kryo -> new GryoSerializers.EdgeSerializer(), 65));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Vertex.class, kryo -> new GryoSerializers.VertexSerializer(), 66));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Property.class, kryo -> new GryoSerializers.PropertySerializer(), 67));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(VertexProperty.class, kryo -> new GryoSerializers.VertexPropertySerializer(), 68));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Path.class, kryo -> new GryoSerializers.PathSerializer(), 59));
+ add(GryoTypeReg.of(EnumSet.class, 46));
+ add(GryoTypeReg.of(HashMap.class, 11));
+ add(GryoTypeReg.of(HashMap.Entry.class, 16));
+ add(GryoTypeReg.of(HASH_MAP_NODE, 92));
+ add(GryoTypeReg.of(KryoSerializable.class, 36));
+ add(GryoTypeReg.of(LinkedHashMap.class, 47));
+ add(GryoTypeReg.of(LinkedHashSet.class, 71));
+ add(GryoTypeReg.of(LinkedList.class, 116));
+ add(GryoTypeReg.of(LINKED_HASH_MAP_ENTRY_CLASS, 15));
+ add(GryoTypeReg.of(Locale.class, 22));
+ add(GryoTypeReg.of(StringBuffer.class, 43));
+ add(GryoTypeReg.of(StringBuilder.class, 44));
+ add(GryoTypeReg.of(T.class, 48));
+ add(GryoTypeReg.of(TimeZone.class, 42));
+ add(GryoTypeReg.of(TreeMap.class, 45));
+ add(GryoTypeReg.of(TreeSet.class, 50));
+ add(GryoTypeReg.of(UUID.class, 17, new UUIDSerializer()));
+ add(GryoTypeReg.of(URI.class, 72, new URISerializer()));
+ add(GryoTypeReg.of(VertexTerminator.class, 13));
+
+ add(GryoTypeReg.of(ReferenceEdge.class, 81));
+ add(GryoTypeReg.of(ReferenceVertexProperty.class, 82));
+ add(GryoTypeReg.of(ReferenceProperty.class, 83));
+ add(GryoTypeReg.of(ReferenceVertex.class, 84));
+ add(GryoTypeReg.of(ReferencePath.class, 85));
+
+ add(GryoTypeReg.of(StarGraph.class, 86, new StarGraphSerializer(Direction.BOTH, new GraphFilter())));
+
+ add(GryoTypeReg.of(Edge.class, 65, new GryoSerializers.EdgeSerializer()));
+ add(GryoTypeReg.of(Vertex.class, 66, new GryoSerializers.VertexSerializer()));
+ add(GryoTypeReg.of(Property.class, 67, new GryoSerializers.PropertySerializer()));
+ add(GryoTypeReg.of(VertexProperty.class, 68, new GryoSerializers.VertexPropertySerializer()));
+ add(GryoTypeReg.of(Path.class, 59, new GryoSerializers.PathSerializer()));
// skip 55
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_O_Traverser.class, null, 75));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(O_Traverser.class, null, 76));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_LP_O_P_S_SE_SL_Traverser.class, null, 77));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_O_S_SE_SL_Traverser.class, null, 78));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(B_LP_O_S_SE_SL_Traverser.class, null, 87));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(O_OB_S_SE_SL_Traverser.class, null, 89));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LP_O_OB_S_SE_SL_Traverser.class, null, 90));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LP_O_OB_P_S_SE_SL_Traverser.class, null, 91));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TraverserSet.class, null, 58));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Tree.class, null, 61));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(HashSet.class, null, 62));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(BulkSet.class, null, 64));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MutableMetrics.class, null, 69));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ImmutableMetrics.class, null, 115));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(DefaultTraversalMetrics.class, null, 70));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MapMemory.class, null, 73));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MapReduce.NullObject.class, null, 74));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(AtomicLong.class, null, 79));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Pair.class, kryo -> new PairSerializer(), 88));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TraversalExplanation.class, kryo -> new JavaSerializer(), 106));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Duration.class, kryo -> new JavaTimeSerializers.DurationSerializer(), 93));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Instant.class, kryo -> new JavaTimeSerializers.InstantSerializer(), 94));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalDate.class, kryo -> new JavaTimeSerializers.LocalDateSerializer(), 95));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalDateTime.class, kryo -> new JavaTimeSerializers.LocalDateTimeSerializer(), 96));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(LocalTime.class, kryo -> new JavaTimeSerializers.LocalTimeSerializer(), 97));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MonthDay.class, kryo -> new JavaTimeSerializers.MonthDaySerializer(), 98));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OffsetDateTime.class, kryo -> new JavaTimeSerializers.OffsetDateTimeSerializer(), 99));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OffsetTime.class, kryo -> new JavaTimeSerializers.OffsetTimeSerializer(), 100));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Period.class, kryo -> new JavaTimeSerializers.PeriodSerializer(), 101));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Year.class, kryo -> new JavaTimeSerializers.YearSerializer(), 102));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(YearMonth.class, kryo -> new JavaTimeSerializers.YearMonthSerializer(), 103));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ZonedDateTime.class, kryo -> new JavaTimeSerializers.ZonedDateTimeSerializer(), 104));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ZoneOffset.class, kryo -> new JavaTimeSerializers.ZoneOffsetSerializer(), 105));
-
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(Operator.class, null, 107));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(FoldStep.FoldBiOperator.class, null, 108));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupCountStep.GroupCountBiOperator.class, null, 109));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupStep.GroupBiOperator.class, kryo -> new JavaSerializer(), 117)); // because they contain traversals
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MeanGlobalStep.MeanGlobalBiOperator.class, null, 110));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(MeanGlobalStep.MeanNumber.class, null, 111));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(TreeStep.TreeBiOperator.class, null, 112));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(GroupStepV3d0.GroupBiOperatorV3d0.class, null, 113));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(RangeGlobalStep.RangeBiOperator.class, null, 114));
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(OrderGlobalStep.OrderBiOperator.class, kryo -> new JavaSerializer(), 118)); // because they contain traversals
- add(Triplet.<Class, Function<Kryo, Serializer>, Integer>with(ProfileStep.ProfileBiOperator.class, null, 119)); // ***LAST ID***
+ add(GryoTypeReg.of(B_O_Traverser.class, 75));
+ add(GryoTypeReg.of(O_Traverser.class, 76));
+ add(GryoTypeReg.of(B_LP_O_P_S_SE_SL_Traverser.class, 77));
+ add(GryoTypeReg.of(B_O_S_SE_SL_Traverser.class, 78));
+ add(GryoTypeReg.of(B_LP_O_S_SE_SL_Traverser.class, 87));
+ add(GryoTypeReg.of(O_OB_S_SE_SL_Traverser.class, 89));
+ add(GryoTypeReg.of(LP_O_OB_S_SE_SL_Traverser.class, 90));
+ add(GryoTypeReg.of(LP_O_OB_P_S_SE_SL_Traverser.class, 91));
+
+ add(GryoTypeReg.of(TraverserSet.class, 58));
+ add(GryoTypeReg.of(Tree.class, 61));
+ add(GryoTypeReg.of(HashSet.class, 62));
+ add(GryoTypeReg.of(BulkSet.class, 64));
+ add(GryoTypeReg.of(MutableMetrics.class, 69));
+ add(GryoTypeReg.of(ImmutableMetrics.class, 115));
+ add(GryoTypeReg.of(DefaultTraversalMetrics.class, 70));
+ add(GryoTypeReg.of(MapMemory.class, 73));
+ add(GryoTypeReg.of(MapReduce.NullObject.class, 74));
+ add(GryoTypeReg.of(AtomicLong.class, 79));
+ add(GryoTypeReg.of(Pair.class, 88, new PairSerializer()));
+ add(GryoTypeReg.of(TraversalExplanation.class, 106, new JavaSerializer()));
+
+ add(GryoTypeReg.of(Duration.class, 93, new JavaTimeSerializers.DurationSerializer()));
+ add(GryoTypeReg.of(Instant.class, 94, new JavaTimeSerializers.InstantSerializer()));
+ add(GryoTypeReg.of(LocalDate.class, 95, new JavaTimeSerializers.LocalDateSerializer()));
+ add(GryoTypeReg.of(LocalDateTime.class, 96, new JavaTimeSerializers.LocalDateTimeSerializer()));
+ add(GryoTypeReg.of(LocalTime.class, 97, new JavaTimeSerializers.LocalTimeSerializer()));
+ add(GryoTypeReg.of(MonthDay.class, 98, new JavaTimeSerializers.MonthDaySerializer()));
+ add(GryoTypeReg.of(OffsetDateTime.class, 99, new JavaTimeSerializers.OffsetDateTimeSerializer()));
+ add(GryoTypeReg.of(OffsetTime.class, 100, new JavaTimeSerializers.OffsetTimeSerializer()));
+ add(GryoTypeReg.of(Period.class, 101, new JavaTimeSerializers.PeriodSerializer()));
+ add(GryoTypeReg.of(Year.class, 102, new JavaTimeSerializers.YearSerializer()));
+ add(GryoTypeReg.of(YearMonth.class, 103, new JavaTimeSerializers.YearMonthSerializer()));
+ add(GryoTypeReg.of(ZonedDateTime.class, 104, new JavaTimeSerializers.ZonedDateTimeSerializer()));
+ add(GryoTypeReg.of(ZoneOffset.class, 105, new JavaTimeSerializers.ZoneOffsetSerializer()));
+
+ add(GryoTypeReg.of(Operator.class, 107));
+ add(GryoTypeReg.of(FoldStep.FoldBiOperator.class, 108));
+ add(GryoTypeReg.of(GroupCountStep.GroupCountBiOperator.class, 109));
+ add(GryoTypeReg.of(GroupStep.GroupBiOperator.class, 117, new JavaSerializer())); // because they contain traversals
+ add(GryoTypeReg.of(MeanGlobalStep.MeanGlobalBiOperator.class, 110));
+ add(GryoTypeReg.of(MeanGlobalStep.MeanNumber.class, 111));
+ add(GryoTypeReg.of(TreeStep.TreeBiOperator.class, 112));
+ add(GryoTypeReg.of(GroupStepV3d0.GroupBiOperatorV3d0.class, 113));
+ add(GryoTypeReg.of(RangeGlobalStep.RangeBiOperator.class, 114));
+ add(GryoTypeReg.of(OrderGlobalStep.OrderBiOperator.class, 118, new JavaSerializer())); // because they contain traversals
+ add(GryoTypeReg.of(ProfileStep.ProfileBiOperator.class, 119)); // ***LAST ID***
}};
private final List<IoRegistry> registries = new ArrayList<>();
@@ -404,8 +405,8 @@ public final class GryoMapper implements Mapper<Kryo> {
*/
public Builder addCustom(final Class... custom) {
if (custom != null && custom.length > 0) {
- for (Class clazz : custom) {
- addCustom(clazz, (Function<Kryo, Serializer>) null);
+ for (Class c : custom) {
+ addOrOverrideRegistration(GryoTypeReg.of(c, currentSerializationId.getAndIncrement()));
}
}
return this;
@@ -416,10 +417,15 @@ public final class GryoMapper implements Mapper<Kryo> {
* a class that is already registered will override that registration.
*/
public Builder addCustom(final Class clazz, final Serializer serializer) {
- if (null == serializer)
- addCustom(clazz);
- else
- addCustom(clazz, kryo -> serializer);
+ addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), serializer));
+ return this;
+ }
+
+ /**
+ * Register custom class to serialize with a custom serialization shim.
+ */
+ public Builder addCustom(final Class clazz, final SerializerShim serializer) {
+ addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), serializer));
return this;
}
@@ -427,15 +433,8 @@ public final class GryoMapper implements Mapper<Kryo> {
* Register a custom class to serialize with a custom serializer as returned from a {@link Function}. Note
* that calling this method for a class that is already registered will override that registration.
*/
- public Builder addCustom(final Class clazz, final Function<Kryo, Serializer> serializer) {
- final Optional<Triplet<Class, Function<Kryo, Serializer>, Integer>> found = findSerializer(clazz);
- if (found.isPresent()) {
- final Triplet<Class, Function<Kryo, Serializer>, Integer> t = found.get();
- serializationList.remove(t);
- serializationList.add(t.setAt1(serializer));
- } else
- serializationList.add(Triplet.with(clazz, serializer, currentSerializationId.getAndIncrement()));
-
+ public Builder addCustom(final Class clazz, final Function<Kryo, Serializer> functionOfKryo) {
+ addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), functionOfKryo));
return this;
}
@@ -481,23 +480,123 @@ public final class GryoMapper implements Mapper<Kryo> {
addCustom(p.getValue0(), (Function<Kryo, Serializer>) p.getValue1());
else
throw new IllegalStateException(String.format(
- "Unexpected value provided by the %s for %s - expects [null, %s implementation or Function<%s, %s>]",
- IoRegistry.class.getSimpleName(), p.getValue0().getClass().getSimpleName(),
+ "Unexpected value provided by %s for serializable class %s - expected a parameter in [null, %s implementation or Function<%s, %s>], but received %s",
+ registry.getClass().getSimpleName(), p.getValue0().getClass().getCanonicalName(),
Serializer.class.getName(), Kryo.class.getSimpleName(),
- Serializer.class.getSimpleName()));
+ Serializer.class.getSimpleName(), p.getValue1()));
});
});
return new GryoMapper(this);
}
- private Optional<Triplet<Class, Function<Kryo, Serializer>, Integer>> findSerializer(final Class clazz) {
- final Iterator<Triplet<Class, Function<Kryo, Serializer>, Integer>> itty = IteratorUtils.filter(
- serializationList, t -> t.getValue0().equals(clazz)).iterator();
- if (itty.hasNext())
- return Optional.of(itty.next());
- else
- return Optional.empty();
+ private <T> void addOrOverrideRegistration(TypeRegistration<T> newRegistration) {
+ Iterator<TypeRegistration<?>> iter = typeRegistrations.iterator();
+ while (iter.hasNext()) {
+ TypeRegistration<?> existingRegistration = iter.next();
+ if (existingRegistration.getTargetClass().equals(newRegistration.getTargetClass())) {
+ iter.remove();
+ break;
+ }
+ }
+ typeRegistrations.add(newRegistration);
+ }
+ }
+
+ private static class GryoTypeReg<T> implements TypeRegistration<T> {
+
+ private final Class<T> clazz;
+ private final Serializer<T> shadedSerializer;
+ private final SerializerShim<T> serializerShim;
+ private final Function<Kryo, Serializer> functionOfShadedKryo;
+ private final int id;
+
+ private GryoTypeReg(Class<T> clazz,
+ Serializer<T> shadedSerializer,
+ SerializerShim<T> serializerShim,
+ Function<Kryo, Serializer> functionOfShadedKryo,
+ int id) {
+ this.clazz = clazz;
+ this.shadedSerializer = shadedSerializer;
+ this.serializerShim = serializerShim;
+ this.functionOfShadedKryo = functionOfShadedKryo;
+ this.id = id;
+
+ int serializerCount = 0;
+ if (null != this.shadedSerializer)
+ serializerCount++;
+ if (null != this.serializerShim)
+ serializerCount++;
+ if (null != this.functionOfShadedKryo)
+ serializerCount++;
+
+ if (1 < serializerCount) {
+ String msg = String.format(
+ "GryoTypeReg accepts at most one kind of serializer, but multiple " +
+ "serializers were supplied for class %s (id %s). " +
+ "Shaded serializer: %s. Shim serializer: %s. Shaded serializer function: %s.",
+ this.clazz.getCanonicalName(), id,
+ this.shadedSerializer, this.serializerShim, this.functionOfShadedKryo);
+ throw new IllegalArgumentException(msg);
+ }
+ }
+
+ private static <T> GryoTypeReg<T> of(Class<T> clazz, int id) {
+ return new GryoTypeReg<>(clazz, null, null, null, id);
+ }
+
+ private static <T> GryoTypeReg<T> of(Class<T> clazz, int id, Serializer<T> shadedSerializer) {
+ return new GryoTypeReg<>(clazz, shadedSerializer, null, null, id);
+ }
+
+ private static <T> GryoTypeReg<T> of(Class<T> clazz, int id, SerializerShim<T> serializerShim) {
+ return new GryoTypeReg<>(clazz, null, serializerShim, null, id);
+ }
+
+ private static <T> GryoTypeReg<T> of(Class clazz, int id, Function<Kryo, Serializer> fct) {
+ return new GryoTypeReg<>(clazz, null, null, fct, id);
+ }
+
+ @Override
+ public Serializer<T> getShadedSerializer() {
+ return shadedSerializer;
+ }
+
+ @Override
+ public SerializerShim<T> getSerializerShim() {
+ return serializerShim;
+ }
+
+ @Override
+ public Function<Kryo, Serializer> getFunctionOfShadedKryo() {
+ return functionOfShadedKryo;
+ }
+
+ @Override
+ public Class<T> getTargetClass() {
+ return clazz;
+ }
+
+ @Override
+ public int getId() {
+ return id;
+ }
+
+ @Override
+ public Kryo registerWith(Kryo kryo) {
+ if (null != functionOfShadedKryo)
+ kryo.register(clazz, functionOfShadedKryo.apply(kryo), id);
+ else if (null != shadedSerializer)
+ kryo.register(clazz, shadedSerializer, id);
+ else if (null != serializerShim)
+ kryo.register(clazz, new ShadedSerializerAdapter<>(serializerShim), id);
+ else {
+ kryo.register(clazz, kryo.getDefaultSerializer(clazz), id);
+ // Suprisingly, the preceding call is not equivalent to
+ // kryo.register(clazz, id);
+ }
+
+ return kryo;
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
new file mode 100644
index 0000000..5ca3f31
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
@@ -0,0 +1,70 @@
+/*
+ * 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.structure.io.gryo;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+import org.apache.tinkerpop.shaded.kryo.Kryo;
+import org.apache.tinkerpop.shaded.kryo.Serializer;
+
+/**
+ * Represents a class serializable with Gryo.
+ * <p>
+ * At most one of the {@link #getShadedSerializer()}, {@link #getSerializerShim()},
+ * and {@link #getFunctionOfShadedKryo()} will return a non-null value. If all
+ * three methods return null, then there is no custom serialization logic associated
+ * with this class. Gryo/Kryo will use its default serializer.
+ *
+ * @param <T> the serializable type
+ */
+public interface TypeRegistration<T> {
+
+ /**
+ * @return the serializable class this instance describes
+ */
+ Class<T> getTargetClass();
+
+ /**
+ * @return numeric identifier used as a shorthand for this type in Gryo's serialized form
+ */
+ int getId();
+
+ /**
+ * @return the shaded-Kryo serializer that handles this type, if one is defined
+ */
+ Serializer<T> getShadedSerializer();
+
+ /**
+ * @return the shim-Kryo serializer that handles this type, if one is defined
+ */
+ SerializerShim<T> getSerializerShim();
+
+ /**
+ * @return a function that accepts a shaded-Kryo instance and returns a serializer, if such a function is defined
+ */
+ java.util.function.Function<Kryo, Serializer> getFunctionOfShadedKryo();
+
+ /**
+ * Registers this type on the supplied {@link Kryo} instance, using whatever custom serializer
+ * may be present, then returns the same {@linkplain Kryo} instance supplied as the parameter.
+ *
+ * @param kryo Kryo instance into which this type is registered
+ * @return the sole parameter
+ */
+ Kryo registerWith(Kryo kryo);
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
index 7b6cfec..de08061 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
@@ -18,29 +18,32 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
-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.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
import java.net.URI;
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-final class URISerializer extends Serializer<URI> {
+final class URISerializer implements SerializerShim<URI> {
- public URISerializer() {
- setImmutable(true);
- }
+ public URISerializer() { }
@Override
- public void write(final Kryo kryo, final Output output, final URI uri) {
+ public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final URI uri) {
output.writeString(uri.toString());
}
@Override
- public URI read(final Kryo kryo, final Input input, final Class<URI> uriClass) {
+ public <I extends InputShim> URI read(final KryoShim<I, ?> kryo, final I input, final Class<URI> uriClass) {
return URI.create(input.readString());
}
+
+ @Override
+ public boolean isImmutable() {
+ return true;
+ }
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
index e694e38..b86ddc2 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
@@ -18,6 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
import org.apache.tinkerpop.shaded.kryo.io.Input;
@@ -28,19 +32,23 @@ import java.util.UUID;
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-final class UUIDSerializer extends Serializer<UUID> {
- public UUIDSerializer() {
- setImmutable(true);
- }
+final class UUIDSerializer implements SerializerShim<UUID> {
+
+ public UUIDSerializer() { }
@Override
- public void write(final Kryo kryo, final Output output, final UUID uuid) {
+ public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final UUID uuid) {
output.writeLong(uuid.getMostSignificantBits());
output.writeLong(uuid.getLeastSignificantBits());
}
@Override
- public UUID read(final Kryo kryo, final Input input, final Class<UUID> uuidClass) {
+ public <I extends InputShim> UUID read(final KryoShim<I, ?> kryo, final I input, final Class<UUID> uuidClass) {
return new UUID(input.readLong(), input.readLong());
}
+
+ @Override
+ public boolean isImmutable() {
+ return true;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
new file mode 100644
index 0000000..6d00884
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
@@ -0,0 +1,37 @@
+/*
+ * 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.structure.io.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Input}-like abstraction.
+ */
+public interface InputShim {
+
+ byte readByte();
+
+ byte[] readBytes(int size);
+
+ String readString();
+
+ long readLong();
+
+ int readInt();
+
+ double readDouble();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
new file mode 100644
index 0000000..e2a95e6
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
@@ -0,0 +1,40 @@
+/*
+ * 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.structure.io.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.Kryo}-like abstraction.
+ *
+ * @param <I> this interface's complementary InputShim
+ * @param <O> this interface's complementary OutputShim
+ */
+public interface KryoShim<I extends InputShim, O extends OutputShim> {
+
+ <T> T readObject(I input, Class<T> type);
+
+ Object readClassAndObject(I input);
+
+ void writeObject(O output, Object object);
+
+ void writeClassAndObject(O output, Object object);
+
+ <T> T readObjectOrNull(I input, Class<T> type);
+
+ void writeObjectOrNull(O output, Object object, Class type);
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
new file mode 100644
index 0000000..024d40c
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
@@ -0,0 +1,83 @@
+/*
+ * 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.structure.io.kryoshim;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * This interface exists to decouple HadoopPools from TinkerPop's shaded Kryo.
+ * <p>
+ * VertexWritable and ObjectWritable formerly implemented Serializable by
+ * resorting to statically-pooled shaded Kryo instances maintained by the HadoopPools class.
+ * This is awkward because those shaded Kryo instances require class registration by default.
+ * <p>
+ * Consider what happens with custom property datatypes reachable from the reference graph rooted at an ObjectWritable
+ * or VertexWritable instance. It is not enough for these property classes to merely implement
+ * Serializable, though one think that from skimming ObjectWritable/VertexWritable. Those classes
+ * must also register with TinkerPop's internal, shaded Kryo instances as maintained by HadoopPools,
+ * or else configure those instances to accept unregistered classes.
+ * Otherwise, TinkerPop's shaded Kryo will refuse to serialize those properties (even though
+ * they implement Serializable, and even though the user might think they are only using
+ * Java's standard Serialization mechanism!).
+ * <p>
+ * By hiding the mechanics of serialization behind this interface instead of hardcoding it in
+ * HadoopPools, the user can decide how to implement serialization for ObjectWritable/VertexWritable
+ * (and whatever other classes in TinkerPop decide to implement Serializable but then delegate
+ * all of the implementation details, like ObjectWritable/VertexWritable do now).
+ */
+public interface KryoShimService {
+
+ /**
+ * Deserializes an object from an input stream.
+ *
+ * @param source the stream from which to read an object's serialized form
+ * @return the first deserialized object available from {@code source}
+ */
+ Object readClassAndObject(InputStream source);
+
+ /**
+ * Serializes an object to an output stream. This may flush the output stream.
+ *
+ * @param o the object to serialize
+ * @param sink the stream into which the serialized object is written
+ */
+ void writeClassAndObject(Object o, OutputStream sink);
+
+ /**
+ * Returns this service's relative priority number. Unless explicitly overridden through a
+ * system property ({@link KryoShimServiceLoader#SHIM_CLASS_SYSTEM_PROPERTY}),
+ * the service implementation with the numerically highest priority will be used
+ * and all others ignored. In other words, the highest priority wins (in the absence of a
+ * system property override).
+ * <p>
+ * TinkerPop's current default implementation uses priority value zero.
+ * <p>
+ * Third-party implementations of this interface should (but are not technically required)
+ * to use a priority value with absolute value greater than 100.
+ * <p>
+ * The implementation currently breaks priority ties by lexicographical comparison of
+ * fully-qualified package-and-classname, but this tie-breaking behavior should be
+ * considered undefined and subject to future change. Ties are ignored if the service
+ * is explicitly set through the system property mentioned above.
+ *
+ * @return this implementation's priority value
+ */
+ int getPriority();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
new file mode 100644
index 0000000..1d5413d
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
@@ -0,0 +1,137 @@
+/*
+ * 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.structure.io.kryoshim;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.ServiceLoader;
+
+public class KryoShimServiceLoader {
+
+ private static volatile KryoShimService CACHED_SHIM_SERVICE;
+
+ private static final Logger log = LoggerFactory.getLogger(KryoShimServiceLoader.class);
+
+ /**
+ * Set this system property to the fully-qualified name of a {@link KryoShimService}
+ * package-and-classname to force it into service. Setting this property causes the
+ * priority-selection mechanism ({@link KryoShimService#getPriority()}) to be ignored.
+ */
+ public static final String SHIM_CLASS_SYSTEM_PROPERTY = "tinkerpop.kryo.shim";
+
+ public static KryoShimService load(boolean forceReload) {
+
+ if (null != CACHED_SHIM_SERVICE && !forceReload) {
+ return CACHED_SHIM_SERVICE;
+ }
+
+ ArrayList<KryoShimService> services = new ArrayList<>();
+
+ ServiceLoader<KryoShimService> sl = ServiceLoader.load(KryoShimService.class);
+
+ KryoShimService result = null;
+
+ synchronized (KryoShimServiceLoader.class) {
+ if (forceReload) {
+ sl.reload();
+ }
+
+ for (KryoShimService kss : sl) {
+ services.add(kss);
+ }
+ }
+
+ String shimClass = System.getProperty(SHIM_CLASS_SYSTEM_PROPERTY);
+
+ if (null != shimClass) {
+ for (KryoShimService kss : services) {
+ if (kss.getClass().getCanonicalName().equals(shimClass)) {
+ log.info("Set {} provider to {} ({}) from system property {}={}",
+ KryoShimService.class.getSimpleName(), kss, kss.getClass(),
+ SHIM_CLASS_SYSTEM_PROPERTY, shimClass);
+ result = kss;
+ }
+ }
+ } else {
+ Collections.sort(services, KryoShimServiceComparator.INSTANCE);
+
+ for (KryoShimService kss : services) {
+ log.debug("Found Kryo shim service class {} (priority {})", kss.getClass(), kss.getPriority());
+ }
+
+ if (0 != services.size()) {
+ result = services.get(services.size() - 1);
+ }
+ }
+
+
+ if (null == result) {
+ throw new IllegalStateException("Unable to load KryoShimService");
+ }
+
+ log.info("Set {} provider to {} ({}) because its priority value ({}) is the highest available",
+ KryoShimService.class.getSimpleName(), result, result.getClass(), result.getPriority());
+
+ return CACHED_SHIM_SERVICE = result;
+ }
+
+ public static KryoShimService load() {
+ return load(false);
+ }
+
+ public static byte[] writeClassAndObjectToBytes(Object o) {
+ KryoShimService shimService = load();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ shimService.writeClassAndObject(o, baos);
+
+ return baos.toByteArray();
+ }
+
+ public static <T> T readClassAndObject(InputStream source) {
+ KryoShimService shimService = load();
+
+ return (T)shimService.readClassAndObject(source);
+ }
+
+ private enum KryoShimServiceComparator implements Comparator<KryoShimService> {
+ INSTANCE;
+
+ @Override
+ public int compare(KryoShimService a, KryoShimService b) {
+ int ap = a.getPriority();
+ int bp = b.getPriority();
+
+ if (ap < bp) {
+ return -1;
+ } else if (bp < ap) {
+ return 1;
+ } else {
+ return a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName());
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
new file mode 100644
index 0000000..4468434
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
@@ -0,0 +1,41 @@
+/*
+ * 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.structure.io.kryoshim;
+
+import java.io.IOException;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Output}-like abstraction.
+ */
+public interface OutputShim {
+
+ void writeByte(byte b);
+
+ void writeBytes(byte[] array, int offset, int count);
+
+ void writeString(String s);
+
+ void writeLong(long l);
+
+ void writeInt(int i);
+
+ void writeDouble(double d);
+
+ void flush();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
new file mode 100644
index 0000000..686350d
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
@@ -0,0 +1,35 @@
+/*
+ * 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.structure.io.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.Serializer}-like abstraction.
+ *
+ * @param <T> the class this serializer reads/writes from/to bytes.
+ */
+public interface SerializerShim<T> {
+
+ <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, T starGraph);
+
+ <I extends InputShim> T read(KryoShim<I, ?> kryo, I input, Class<T> clazz);
+
+ default boolean isImmutable() {
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
new file mode 100644
index 0000000..436f117
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+/**
+ * Abstracts a minimal subset of Kryo types and methods.
+ * <p>
+ * Kryo is often shaded. For instance, TinkerPop's Gryo
+ * serializer relies on a shaded Kryo package.
+ * TinkerPop serializers written against a particular shaded
+ * Kryo package (or the unshaded Kryo package) are compatible
+ * only with that package. In contrast, TinkerPop serializers written
+ * against this abstraction can be used with any shaded or
+ * unshaded Kryo package, so long as the signatures and behavior
+ * of the methods in this package remain stable.
+ * <p>
+ * To show how this is useful, consider
+ * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer}.
+ * This class has logic unique to TinkerPop that performs
+ * efficient and forward-compatible serialization of
+ * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraph}
+ * instances. It takes advantage of package-level visibility
+ * and the fact that it shares a package with its target,
+ * so it would be challenging to cleanly and naturally replicate
+ * (i.e. without package spoofing or runtime visibility overrides).
+ * By implementing
+ * {@link org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim}
+ * instead of, say, Gryo's shaded
+ * {@link org.apache.tinkerpop.shaded.kryo.Serializer},
+ * it can be used with anybody's Kryo package, regardless of whether
+ * that package is shaded or not. This lets third-parties reuse
+ * TinkerPop's efficient, internals-aware StarGraph serializer on
+ * their own serialization platform (and without altering
+ * TinkerPop's bytecode, let alone its source).
+ * <p>
+ * The number of types and methods in this
+ * package is deliberately small to reduce the likelihood of a
+ * new Kryo release introducing an incompatible change.
+ */
+package org.apache.tinkerpop.gremlin.structure.io.kryoshim;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
new file mode 100644
index 0000000..2872326
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
@@ -0,0 +1,66 @@
+/*
+ * 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.structure.io.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
+import org.apache.tinkerpop.shaded.kryo.io.Input;
+
+public class ShadedInputAdapter implements InputShim {
+
+ private final Input shadedInput;
+
+ public ShadedInputAdapter(Input shadedInput) {
+ this.shadedInput = shadedInput;
+ }
+
+ Input getShadedInput() {
+ return shadedInput;
+ }
+
+ @Override
+ public byte readByte()
+ {
+ return shadedInput.readByte();
+ }
+
+ @Override
+ public byte[] readBytes(int size) {
+ return shadedInput.readBytes(size);
+ }
+
+ @Override
+ public String readString() {
+ return shadedInput.readString();
+ }
+
+ @Override
+ public long readLong() {
+ return shadedInput.readLong();
+ }
+
+ @Override
+ public int readInt() {
+ return shadedInput.readInt();
+ }
+
+ @Override
+ public double readDouble() {
+ return shadedInput.readDouble();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
new file mode 100644
index 0000000..0e85f6f
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
@@ -0,0 +1,67 @@
+/*
+ * 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.structure.io.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
+import org.apache.tinkerpop.shaded.kryo.Kryo;
+
+public class ShadedKryoAdapter implements KryoShim<ShadedInputAdapter, ShadedOutputAdapter> {
+
+ private final Kryo shadedKryo;
+
+ public ShadedKryoAdapter(Kryo shadedKryo) {
+ this.shadedKryo = shadedKryo;
+ }
+
+ @Override
+ public <T> T readObject(ShadedInputAdapter input, Class<T> type)
+ {
+ return shadedKryo.readObject(input.getShadedInput(), type);
+ }
+
+ @Override
+ public Object readClassAndObject(ShadedInputAdapter input)
+ {
+ return shadedKryo.readClassAndObject(input.getShadedInput());
+ }
+
+ @Override
+ public void writeObject(ShadedOutputAdapter output, Object object)
+ {
+ shadedKryo.writeObject(output.getShadedOutput(), object);
+ }
+
+ @Override
+ public void writeClassAndObject(ShadedOutputAdapter output, Object object)
+ {
+ shadedKryo.writeClassAndObject(output.getShadedOutput(), object);
+ }
+
+ @Override
+ public <T> T readObjectOrNull(ShadedInputAdapter input, Class<T> type)
+ {
+ return shadedKryo.readObjectOrNull(input.getShadedInput(), type);
+ }
+
+ @Override
+ public void writeObjectOrNull(ShadedOutputAdapter output, Object object, Class type)
+ {
+ shadedKryo.writeObjectOrNull(output.getShadedOutput(), object, type);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
new file mode 100644
index 0000000..5ca0ecb
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
@@ -0,0 +1,72 @@
+/*
+ * 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.structure.io.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
+
+public class ShadedOutputAdapter implements OutputShim {
+
+ private final Output shadedOutput;
+
+ public ShadedOutputAdapter(Output shadedOutput) {
+ this.shadedOutput = shadedOutput;
+ }
+
+ @Override
+ public void writeByte(byte b)
+ {
+ shadedOutput.writeByte(b);
+ }
+
+ @Override
+ public void writeBytes(byte[] array, int offset, int count) {
+ shadedOutput.writeBytes(array, offset, count);
+ }
+
+ @Override
+ public void writeString(String s) {
+ shadedOutput.writeString(s);
+ }
+
+ @Override
+ public void writeLong(long l) {
+ shadedOutput.writeLong(l);
+ }
+
+ @Override
+ public void writeInt(int i) {
+ shadedOutput.writeInt(i);
+ }
+
+ @Override
+ public void writeDouble(double d) {
+ shadedOutput.writeDouble(d);
+ }
+
+ @Override
+ public void flush() {
+ shadedOutput.flush();
+ }
+
+ Output getShadedOutput()
+ {
+ return shadedOutput;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
new file mode 100644
index 0000000..ebfd0fb
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
@@ -0,0 +1,54 @@
+/*
+ * 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.structure.io.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+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;
+
+public class ShadedSerializerAdapter<T> extends Serializer<T> {
+
+ SerializerShim<T> serializer;
+
+ public ShadedSerializerAdapter(SerializerShim<T> serializer) {
+ this.serializer = serializer;
+ setImmutable(this.serializer.isImmutable());
+ }
+
+ @Override
+ public void write(Kryo kryo, Output output, T t) {
+ /* These adapters could be cached pretty efficiently in instance fields if it were guaranteed that this
+ * class was never subject to concurrent use. That's true of Kryo instances, but it is not clear that
+ * it is true of Serializer instances.
+ */
+ ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
+ ShadedOutputAdapter shadedOutputAdapter = new ShadedOutputAdapter(output);
+ serializer.write(shadedKryoAdapter, shadedOutputAdapter, t);
+ }
+
+ @Override
+ public T read(Kryo kryo, Input input, Class<T> aClass) {
+ // Same caching opportunity as in write(...)
+ ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
+ ShadedInputAdapter shadedInputAdapter = new ShadedInputAdapter(input);
+ return serializer.read(shadedKryoAdapter, shadedInputAdapter, aClass);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
new file mode 100644
index 0000000..ea70003
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementations of the interfaces in
+ * {@link org.apache.tinkerpop.gremlin.structure.io.kryoshim}
+ * using TinkerPop's shaded copy of Kryo.
+ */
+package org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ef528697/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
index 3805e2c..2acd770 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
@@ -18,20 +18,13 @@
*/
package org.apache.tinkerpop.gremlin.structure.util.star;
-import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
-import org.apache.tinkerpop.gremlin.structure.Direction;
-import org.apache.tinkerpop.gremlin.structure.Edge;
-import org.apache.tinkerpop.gremlin.structure.T;
-import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-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 java.util.HashMap;
-import java.util.List;
import java.util.Map;
+import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
+import org.apache.tinkerpop.gremlin.structure.Direction;
+import org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded.ShadedSerializerAdapter;
+
/**
* Kryo serializer for {@link StarGraph}. Implements an internal versioning capability for backward compatibility.
* The single byte at the front of the serialization stream denotes the version. That version can be used to choose
@@ -42,15 +35,10 @@ import java.util.Map;
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-public final class StarGraphGryoSerializer extends Serializer<StarGraph> {
+public final class StarGraphGryoSerializer extends ShadedSerializerAdapter<StarGraph> {
private static final Map<Direction, StarGraphGryoSerializer> CACHE = new HashMap<>();
- private final Direction edgeDirectionToSerialize;
- private GraphFilter graphFilter = new GraphFilter(); // will allow all vertices/edges
-
- private final static byte VERSION_1 = Byte.MIN_VALUE;
-
static {
CACHE.put(Direction.BOTH, new StarGraphGryoSerializer(Direction.BOTH));
CACHE.put(Direction.IN, new StarGraphGryoSerializer(Direction.IN));
@@ -58,8 +46,12 @@ public final class StarGraphGryoSerializer extends Serializer<StarGraph> {
CACHE.put(null, new StarGraphGryoSerializer(null));
}
+ private StarGraphGryoSerializer(final Direction edgeDirectionToSerialize, final GraphFilter graphFilter) {
+ super(new StarGraphSerializer(edgeDirectionToSerialize, graphFilter));
+ }
+
private StarGraphGryoSerializer(final Direction edgeDirectionToSerialize) {
- this.edgeDirectionToSerialize = edgeDirectionToSerialize;
+ this(edgeDirectionToSerialize, new GraphFilter());
}
/**
@@ -71,100 +63,7 @@ public final class StarGraphGryoSerializer extends Serializer<StarGraph> {
}
public static StarGraphGryoSerializer withGraphFilter(final GraphFilter graphFilter) {
- final StarGraphGryoSerializer serializer = new StarGraphGryoSerializer(Direction.BOTH);
- serializer.graphFilter = graphFilter.clone();
+ final StarGraphGryoSerializer serializer = new StarGraphGryoSerializer(Direction.BOTH, graphFilter.clone());
return serializer;
}
-
- @Override
- public void write(final Kryo kryo, final Output output, final StarGraph starGraph) {
- output.writeByte(VERSION_1);
- kryo.writeObjectOrNull(output, starGraph.edgeProperties, HashMap.class);
- kryo.writeObjectOrNull(output, starGraph.metaProperties, HashMap.class);
- kryo.writeClassAndObject(output, starGraph.starVertex.id);
- kryo.writeObject(output, starGraph.starVertex.label);
- writeEdges(kryo, output, starGraph, Direction.IN);
- writeEdges(kryo, output, starGraph, Direction.OUT);
- kryo.writeObject(output, null != starGraph.starVertex.vertexProperties);
- if (null != starGraph.starVertex.vertexProperties) {
- kryo.writeObject(output, starGraph.starVertex.vertexProperties.size());
- for (final Map.Entry<String, List<VertexProperty>> vertexProperties : starGraph.starVertex.vertexProperties.entrySet()) {
- kryo.writeObject(output, vertexProperties.getKey());
- kryo.writeObject(output, vertexProperties.getValue().size());
- for (final VertexProperty vertexProperty : vertexProperties.getValue()) {
- kryo.writeClassAndObject(output, vertexProperty.id());
- kryo.writeClassAndObject(output, vertexProperty.value());
- }
- }
- }
- }
-
- /**
- * If the returned {@link StarGraph} is null, that means that the {@link GraphFilter} filtered the vertex.
- */
- @Override
- public StarGraph read(final Kryo kryo, final Input input, final Class<StarGraph> aClass) {
- final StarGraph starGraph = StarGraph.open();
- input.readByte(); // version field ignored for now - for future use with backward compatibility
- starGraph.edgeProperties = kryo.readObjectOrNull(input, HashMap.class);
- starGraph.metaProperties = kryo.readObjectOrNull(input, HashMap.class);
- starGraph.addVertex(T.id, kryo.readClassAndObject(input), T.label, kryo.readObject(input, String.class));
- readEdges(kryo, input, starGraph, Direction.IN);
- readEdges(kryo, input, starGraph, Direction.OUT);
- if (kryo.readObject(input, Boolean.class)) {
- final int numberOfUniqueKeys = kryo.readObject(input, Integer.class);
- for (int i = 0; i < numberOfUniqueKeys; i++) {
- final String vertexPropertyKey = kryo.readObject(input, String.class);
- final int numberOfVertexPropertiesWithKey = kryo.readObject(input, Integer.class);
- for (int j = 0; j < numberOfVertexPropertiesWithKey; j++) {
- final Object id = kryo.readClassAndObject(input);
- final Object value = kryo.readClassAndObject(input);
- starGraph.starVertex.property(VertexProperty.Cardinality.list, vertexPropertyKey, value, T.id, id);
- }
- }
- }
- return this.graphFilter.hasFilter() ? starGraph.applyGraphFilter(this.graphFilter).orElse(null) : starGraph;
- }
-
- private void writeEdges(final Kryo kryo, final Output output, final StarGraph starGraph, final Direction direction) {
- // only write edges if there are some AND if the user requested them to be serialized AND if they match
- // the direction being serialized by the format
- final Map<String, List<Edge>> starEdges = direction.equals(Direction.OUT) ? starGraph.starVertex.outEdges : starGraph.starVertex.inEdges;
- final boolean writeEdges = null != starEdges && edgeDirectionToSerialize != null
- && (edgeDirectionToSerialize == direction || edgeDirectionToSerialize == Direction.BOTH);
- kryo.writeObject(output, writeEdges);
- if (writeEdges) {
- kryo.writeObject(output, starEdges.size());
- for (final Map.Entry<String, List<Edge>> edges : starEdges.entrySet()) {
- kryo.writeObject(output, edges.getKey());
- kryo.writeObject(output, edges.getValue().size());
- for (final Edge edge : edges.getValue()) {
- kryo.writeClassAndObject(output, edge.id());
- kryo.writeClassAndObject(output, direction.equals(Direction.OUT) ? edge.inVertex().id() : edge.outVertex().id());
- }
- }
- }
- }
-
- private void readEdges(final Kryo kryo, final Input input, final StarGraph starGraph, final Direction direction) {
- if (kryo.readObject(input, Boolean.class)) {
- final int numberOfUniqueLabels = kryo.readObject(input, Integer.class);
- for (int i = 0; i < numberOfUniqueLabels; i++) {
- final String edgeLabel = kryo.readObject(input, String.class);
- final int numberOfEdgesWithLabel = kryo.readObject(input, Integer.class);
- for (int j = 0; j < numberOfEdgesWithLabel; j++) {
- final Object edgeId = kryo.readClassAndObject(input);
- final Object adjacentVertexId = kryo.readClassAndObject(input);
- if (this.graphFilter.checkEdgeLegality(direction, edgeLabel).positive()) {
- if (direction.equals(Direction.OUT))
- starGraph.starVertex.addOutEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId);
- else
- starGraph.starVertex.addInEdge(edgeLabel, starGraph.addVertex(T.id, adjacentVertexId), T.id, edgeId);
- } else if (null != starGraph.edgeProperties) {
- starGraph.edgeProperties.remove(edgeId);
- }
- }
- }
- }
- }
}
[6/8] incubator-tinkerpop git commit: Kryo shim configuration tweaks
Posted by ok...@apache.org.
Kryo shim configuration tweaks
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/9321a3e1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/9321a3e1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/9321a3e1
Branch: refs/heads/TINKERPOP-1321
Commit: 9321a3e14eab4ed05f8ef5f4c77de481a4011b81
Parents: 218d790
Author: Dan LaRocque <da...@hopcount.org>
Authored: Mon Jun 6 02:24:12 2016 -0400
Committer: Dan LaRocque <da...@hopcount.org>
Committed: Mon Jun 6 03:10:03 2016 -0400
----------------------------------------------------------------------
.../process/computer/GiraphWorkerContext.java | 3 +-
.../gremlin/structure/io/gryo/GryoMapper.java | 30 ++-
.../gremlin/structure/io/gryo/GryoPool.java | 1 +
.../structure/io/gryo/GryoSerializers.java | 40 ++--
.../structure/io/gryo/JavaTimeSerializers.java | 127 +++++-------
.../structure/io/gryo/PairSerializer.java | 11 +-
.../structure/io/gryo/TypeRegistration.java | 12 ++
.../io/gryo/kryoshim/KryoShimService.java | 16 ++
.../io/gryo/kryoshim/KryoShimServiceLoader.java | 23 ++-
.../io/gryo/kryoshim/SerializerShim.java | 2 +-
.../hadoop/process/computer/HadoopCombine.java | 3 +-
.../hadoop/process/computer/HadoopMap.java | 3 +-
.../hadoop/process/computer/HadoopReduce.java | 3 +-
.../structure/io/HadoopPoolShimService.java | 7 +
.../structure/io/HadoopPoolsConfigurable.java | 4 +-
.../structure/io/gryo/GryoRecordReader.java | 3 +-
.../structure/io/gryo/GryoRecordWriter.java | 4 +-
.../spark/process/computer/SparkExecutor.java | 3 +-
.../structure/io/TinkerPopKryoRegistrator.java | 121 ------------
.../spark/structure/io/gryo/GryoSerializer.java | 2 +-
.../io/gryo/IoRegistryAwareKryoSerializer.java | 116 +++++++++++
.../io/gryo/TinkerPopKryoRegistrator.java | 194 +++++++++++++++++++
.../unshaded/UnshadedKryoShimService.java | 131 ++++++++-----
...n.structure.io.gryo.kryoshim.KryoShimService | 1 +
.../spark/structure/io/ToyGraphInputRDD.java | 3 +-
25 files changed, 572 insertions(+), 291 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/giraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/giraph/process/computer/GiraphWorkerContext.java
----------------------------------------------------------------------
diff --git a/giraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/giraph/process/computer/GiraphWorkerContext.java b/giraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/giraph/process/computer/GiraphWorkerContext.java
index 86b733c..0122ab4 100644
--- a/giraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/giraph/process/computer/GiraphWorkerContext.java
+++ b/giraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/giraph/process/computer/GiraphWorkerContext.java
@@ -28,6 +28,7 @@ import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
import org.apache.tinkerpop.gremlin.process.computer.VertexProgram;
import org.apache.tinkerpop.gremlin.process.computer.util.ImmutableMemory;
import org.apache.tinkerpop.gremlin.process.computer.util.VertexProgramPool;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import java.util.Iterator;
@@ -45,7 +46,7 @@ public final class GiraphWorkerContext extends WorkerContext {
public void preApplication() throws InstantiationException, IllegalAccessException {
final Configuration apacheConfiguration = ConfUtil.makeApacheConfiguration(this.getContext().getConfiguration());
- HadoopPools.initialize(apacheConfiguration);
+ KryoShimServiceLoader.applyConfiguration(apacheConfiguration);
final VertexProgram vertexProgram = VertexProgram.createVertexProgram(HadoopGraph.open(apacheConfiguration), apacheConfiguration);
this.vertexProgramPool = new VertexProgramPool(vertexProgram, this.getContext().getConfiguration().getInt(GiraphConstants.NUM_COMPUTE_THREADS.getKey(), 1));
this.memory = new GiraphMemory(this, vertexProgram);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
index 851b03c..41ca44d 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
@@ -18,6 +18,7 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
+import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
import org.apache.tinkerpop.gremlin.process.computer.util.MapMemory;
@@ -369,6 +370,20 @@ public final class GryoMapper implements Mapper<Kryo> {
private Supplier<ClassResolver> classResolver = GryoClassResolver::new;
private Builder() {
+ // Validate the default registrations
+ // For justification of these default registration rules, see TinkerPopKryoRegistrator
+ for (TypeRegistration<?> tr : typeRegistrations) {
+ if (tr.hasSerializer() /* no serializer is acceptable */ &&
+ null == tr.getSerializerShim() /* a shim serializer is acceptable */ &&
+ !(tr.getShadedSerializer() instanceof JavaSerializer) /* shaded JavaSerializer is acceptable */) {
+ // everything else is invalid
+ String msg = String.format("The default GryoMapper type registration %s is invalid. " +
+ "It must supply either an implementation of %s or %s, but supplies neither. " +
+ "This is probably a bug in GryoMapper's default serialization class registrations.", tr,
+ SerializerShim.class.getCanonicalName(), JavaSerializer.class.getCanonicalName());
+ throw new IllegalStateException(msg);
+ }
+ }
}
/**
@@ -538,8 +553,8 @@ public final class GryoMapper implements Mapper<Kryo> {
if (1 < serializerCount) {
String msg = String.format(
"GryoTypeReg accepts at most one kind of serializer, but multiple " +
- "serializers were supplied for class %s (id %s). " +
- "Shaded serializer: %s. Shim serializer: %s. Shaded serializer function: %s.",
+ "serializers were supplied for class %s (id %s). " +
+ "Shaded serializer: %s. Shim serializer: %s. Shaded serializer function: %s.",
this.clazz.getCanonicalName(), id,
this.shadedSerializer, this.serializerShim, this.functionOfShadedKryo);
throw new IllegalArgumentException(msg);
@@ -603,5 +618,16 @@ public final class GryoMapper implements Mapper<Kryo> {
return kryo;
}
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this)
+ .append("targetClass", clazz)
+ .append("id", id)
+ .append("shadedSerializer", shadedSerializer)
+ .append("serializerShim", serializerShim)
+ .append("functionOfShadedKryo", functionOfShadedKryo)
+ .toString();
+ }
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
index e7bf636..59f8a5d 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
@@ -40,6 +40,7 @@ import java.util.function.Function;
public final class GryoPool {
public static final String CONFIG_IO_REGISTRY = "gremlin.io.registry";
public static final String CONFIG_IO_GRYO_POOL_SIZE = "gremlin.io.gryo.poolSize";
+ public static final int CONFIG_IO_GRYO_POOL_SIZE_DEFAULT = 256;
public enum Type {READER, WRITER, READER_WRITER}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoSerializers.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoSerializers.java
index ae99ac6..16fbe85 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoSerializers.java
@@ -23,16 +23,16 @@ import org.apache.tinkerpop.gremlin.structure.Edge;
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.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory;
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.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;
/**
* Class used to serialize graph-based objects such as vertices, edges, properties, and paths. These objects are
@@ -42,19 +42,19 @@ import org.apache.tinkerpop.shaded.kryo.io.Output;
* @author Stephen Mallette (http://stephen.genoprime.com)
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
-final class GryoSerializers {
+public final class GryoSerializers {
/**
* Serializes any {@link Edge} implementation encountered to a {@link DetachedEdge}.
*/
- final static class EdgeSerializer extends Serializer<Edge> {
+ final static class EdgeSerializer implements SerializerShim<Edge> {
@Override
- public void write(final Kryo kryo, final Output output, final Edge edge) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Edge edge) {
kryo.writeClassAndObject(output, DetachedFactory.detach(edge, true));
}
@Override
- public Edge read(final Kryo kryo, final Input input, final Class<Edge> edgeClass) {
+ public <I extends InputShim> Edge read(KryoShim<I, ?> kryo, I input, Class<Edge> edgeClass) {
final Object o = kryo.readClassAndObject(input);
return (Edge) o;
}
@@ -63,14 +63,14 @@ final class GryoSerializers {
/**
* Serializes any {@link Vertex} implementation encountered to an {@link DetachedVertex}.
*/
- final static class VertexSerializer extends Serializer<Vertex> {
+ final static class VertexSerializer implements SerializerShim<Vertex> {
@Override
- public void write(final Kryo kryo, final Output output, final Vertex vertex) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Vertex vertex) {
kryo.writeClassAndObject(output, DetachedFactory.detach(vertex, true));
}
@Override
- public Vertex read(final Kryo kryo, final Input input, final Class<Vertex> vertexClass) {
+ public <I extends InputShim> Vertex read(KryoShim<I, ?> kryo, I input, Class<Vertex> vertexClass) {
return (Vertex) kryo.readClassAndObject(input);
}
}
@@ -78,14 +78,14 @@ final class GryoSerializers {
/**
* Serializes any {@link Property} implementation encountered to an {@link DetachedProperty}.
*/
- final static class PropertySerializer extends Serializer<Property> {
+ final static class PropertySerializer implements SerializerShim<Property> {
@Override
- public void write(final Kryo kryo, final Output output, final Property property) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Property property) {
kryo.writeClassAndObject(output, property instanceof VertexProperty ? DetachedFactory.detach((VertexProperty) property, true) : DetachedFactory.detach(property));
}
@Override
- public Property read(final Kryo kryo, final Input input, final Class<Property> propertyClass) {
+ public <I extends InputShim> Property read(KryoShim<I, ?> kryo, I input, Class<Property> propertyClass) {
return (Property) kryo.readClassAndObject(input);
}
}
@@ -93,14 +93,14 @@ final class GryoSerializers {
/**
* Serializes any {@link VertexProperty} implementation encountered to an {@link DetachedVertexProperty}.
*/
- final static class VertexPropertySerializer extends Serializer<VertexProperty> {
+ final static class VertexPropertySerializer implements SerializerShim<VertexProperty> {
@Override
- public void write(final Kryo kryo, final Output output, final VertexProperty vertexProperty) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, VertexProperty vertexProperty) {
kryo.writeClassAndObject(output, DetachedFactory.detach(vertexProperty, true));
}
@Override
- public VertexProperty read(final Kryo kryo, final Input input, final Class<VertexProperty> vertexPropertyClass) {
+ public <I extends InputShim> VertexProperty read(KryoShim<I, ?> kryo, I input, Class<VertexProperty> vertexPropertyClass) {
return (VertexProperty) kryo.readClassAndObject(input);
}
}
@@ -108,14 +108,14 @@ final class GryoSerializers {
/**
* Serializes any {@link Path} implementation encountered to an {@link DetachedPath}.
*/
- final static class PathSerializer extends Serializer<Path> {
+ public final static class PathSerializer implements SerializerShim<Path> {
@Override
- public void write(final Kryo kryo, final Output output, final Path path) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Path path) {
kryo.writeClassAndObject(output, DetachedFactory.detach(path, false));
}
@Override
- public Path read(final Kryo kryo, final Input input, final Class<Path> pathClass) {
+ public <I extends InputShim> Path read(KryoShim<I, ?> kryo, I input, Class<Path> pathClass) {
return (Path) kryo.readClassAndObject(input);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
index 1d4e236..8b14345 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/JavaTimeSerializers.java
@@ -18,10 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
-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.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import java.time.Duration;
import java.time.Instant;
@@ -48,17 +48,14 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link Duration} class.
*/
- final static class DurationSerializer extends Serializer<Duration>
- {
+ final static class DurationSerializer implements SerializerShim<Duration> {
@Override
- public void write(final Kryo kryo, final Output output, final Duration duration)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Duration duration) {
output.writeLong(duration.toNanos());
}
@Override
- public Duration read(final Kryo kryo, final Input input, final Class<Duration> durationClass)
- {
+ public <I extends InputShim> Duration read(KryoShim<I, ?> kryo, I input, Class<Duration> durationClass) {
return Duration.ofNanos(input.readLong());
}
}
@@ -66,18 +63,15 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link Instant} class.
*/
- final static class InstantSerializer extends Serializer<Instant>
- {
+ final static class InstantSerializer implements SerializerShim<Instant> {
@Override
- public void write(Kryo kryo, Output output, Instant instant)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Instant instant) {
output.writeLong(instant.getEpochSecond());
output.writeInt(instant.getNano());
}
@Override
- public Instant read(Kryo kryo, Input input, Class<Instant> aClass)
- {
+ public <I extends InputShim> Instant read(KryoShim<I, ?> kryo, I input, Class<Instant> aClass) {
return Instant.ofEpochSecond(input.readLong(), input.readInt());
}
}
@@ -85,17 +79,14 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link LocalDate} class.
*/
- final static class LocalDateSerializer extends Serializer<LocalDate>
- {
+ final static class LocalDateSerializer implements SerializerShim<LocalDate> {
@Override
- public void write(final Kryo kryo, final Output output, final LocalDate localDate)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, LocalDate localDate) {
output.writeLong(localDate.toEpochDay());
}
@Override
- public LocalDate read(final Kryo kryo, final Input input, final Class<LocalDate> clazz)
- {
+ public <I extends InputShim> LocalDate read(KryoShim<I, ?> kryo, I input, Class<LocalDate> clazz) {
return LocalDate.ofEpochDay(input.readLong());
}
}
@@ -103,11 +94,9 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link LocalDateTime} class.
*/
- final static class LocalDateTimeSerializer extends Serializer<LocalDateTime>
- {
+ final static class LocalDateTimeSerializer implements SerializerShim<LocalDateTime> {
@Override
- public void write(final Kryo kryo, final Output output, final LocalDateTime localDateTime)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, LocalDateTime localDateTime) {
output.writeInt(localDateTime.getYear());
output.writeInt(localDateTime.getMonthValue());
output.writeInt(localDateTime.getDayOfMonth());
@@ -118,8 +107,7 @@ final class JavaTimeSerializers {
}
@Override
- public LocalDateTime read(final Kryo kryo, final Input input, final Class<LocalDateTime> clazz)
- {
+ public <I extends InputShim> LocalDateTime read(KryoShim<I, ?> kryo, I input, Class<LocalDateTime> clazz) {
return LocalDateTime.of(input.readInt(), input.readInt(), input.readInt(), input.readInt(), input.readInt(), input.readInt(), input.readInt());
}
}
@@ -127,17 +115,14 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link LocalTime} class.
*/
- final static class LocalTimeSerializer extends Serializer<LocalTime>
- {
+ final static class LocalTimeSerializer implements SerializerShim<LocalTime> {
@Override
- public void write(final Kryo kryo, final Output output, final LocalTime localTime)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, LocalTime localTime) {
output.writeLong(localTime.toNanoOfDay());
}
@Override
- public LocalTime read(final Kryo kryo, final Input input, final Class<LocalTime> clazz)
- {
+ public <I extends InputShim> LocalTime read(KryoShim<I, ?> kryo, I input, Class<LocalTime> clazz) {
return LocalTime.ofNanoOfDay(input.readLong());
}
}
@@ -145,37 +130,31 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link MonthDay} class.
*/
- final static class MonthDaySerializer extends Serializer<MonthDay>
- {
+ final static class MonthDaySerializer implements SerializerShim<MonthDay> {
@Override
- public void write(final Kryo kryo, final Output output, final MonthDay monthDay)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, MonthDay monthDay) {
output.writeInt(monthDay.getMonthValue());
output.writeInt(monthDay.getDayOfMonth());
}
@Override
- public MonthDay read(final Kryo kryo, final Input input, final Class<MonthDay> clazz)
- {
- return MonthDay.of(input.readInt(), input.readInt());
+ public <I extends InputShim> MonthDay read(KryoShim<I, ?> kryo, I input, Class<MonthDay> clazz) {
+ return null;
}
}
/**
* Serializer for the {@link OffsetDateTime} class.
*/
- final static class OffsetDateTimeSerializer extends Serializer<OffsetDateTime>
- {
+ final static class OffsetDateTimeSerializer implements SerializerShim<OffsetDateTime> {
@Override
- public void write(final Kryo kryo, final Output output, final OffsetDateTime offsetDateTime)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, OffsetDateTime offsetDateTime) {
kryo.writeObject(output, offsetDateTime.toLocalDateTime());
kryo.writeObject(output, offsetDateTime.getOffset());
}
@Override
- public OffsetDateTime read(final Kryo kryo, final Input input, final Class<OffsetDateTime> clazz)
- {
+ public <I extends InputShim> OffsetDateTime read(KryoShim<I, ?> kryo, I input, Class<OffsetDateTime> clazz) {
return OffsetDateTime.of(kryo.readObject(input, LocalDateTime.class), kryo.readObject(input, ZoneOffset.class));
}
}
@@ -183,18 +162,15 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link OffsetTime} class.
*/
- final static class OffsetTimeSerializer extends Serializer<OffsetTime>
- {
+ final static class OffsetTimeSerializer implements SerializerShim<OffsetTime> {
@Override
- public void write(final Kryo kryo, final Output output, final OffsetTime offsetTime)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, OffsetTime offsetTime) {
kryo.writeObject(output, offsetTime.toLocalTime());
kryo.writeObject(output, offsetTime.getOffset());
}
@Override
- public OffsetTime read(final Kryo kryo, final Input input, final Class<OffsetTime> clazz)
- {
+ public <I extends InputShim> OffsetTime read(KryoShim<I, ?> kryo, I input, Class<OffsetTime> clazz) {
return OffsetTime.of(kryo.readObject(input, LocalTime.class), kryo.readObject(input, ZoneOffset.class));
}
}
@@ -202,19 +178,16 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link Period} class.
*/
- final static class PeriodSerializer extends Serializer<Period>
- {
+ final static class PeriodSerializer implements SerializerShim<Period> {
@Override
- public void write(final Kryo kryo, final Output output, final Period period)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Period period) {
output.writeInt(period.getYears());
output.writeInt(period.getMonths());
output.writeInt(period.getDays());
}
@Override
- public Period read(final Kryo kryo, final Input input, final Class<Period> clazz)
- {
+ public <I extends InputShim> Period read(KryoShim<I, ?> kryo, I input, Class<Period> clazz) {
return Period.of(input.readInt(), input.readInt(), input.readInt());
}
}
@@ -222,17 +195,14 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link Year} class.
*/
- final static class YearSerializer extends Serializer<Year>
- {
+ final static class YearSerializer implements SerializerShim<Year> {
@Override
- public void write(final Kryo kryo, final Output output, final Year year)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Year year) {
output.writeInt(year.getValue());
}
@Override
- public Year read(final Kryo kryo, final Input input, final Class<Year> clazz)
- {
+ public <I extends InputShim> Year read(KryoShim<I, ?> kryo, I input, Class<Year> clazz) {
return Year.of(input.readInt());
}
}
@@ -240,18 +210,15 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link YearMonth} class.
*/
- final static class YearMonthSerializer extends Serializer<YearMonth>
- {
+ final static class YearMonthSerializer implements SerializerShim<YearMonth> {
@Override
- public void write(final Kryo kryo, final Output output, final YearMonth monthDay)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, YearMonth monthDay) {
output.writeInt(monthDay.getYear());
output.writeInt(monthDay.getMonthValue());
}
@Override
- public YearMonth read(final Kryo kryo, final Input input, final Class<YearMonth> clazz)
- {
+ public <I extends InputShim> YearMonth read(KryoShim<I, ?> kryo, I input, Class<YearMonth> clazz) {
return YearMonth.of(input.readInt(), input.readInt());
}
}
@@ -259,11 +226,9 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link ZonedDateTime} class.
*/
- final static class ZonedDateTimeSerializer extends Serializer<ZonedDateTime>
- {
+ final static class ZonedDateTimeSerializer implements SerializerShim<ZonedDateTime> {
@Override
- public void write(final Kryo kryo, final Output output, final ZonedDateTime zonedDateTime)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, ZonedDateTime zonedDateTime) {
output.writeInt(zonedDateTime.getYear());
output.writeInt(zonedDateTime.getMonthValue());
output.writeInt(zonedDateTime.getDayOfMonth());
@@ -275,8 +240,7 @@ final class JavaTimeSerializers {
}
@Override
- public ZonedDateTime read(final Kryo kryo, final Input input, final Class<ZonedDateTime> clazz)
- {
+ public <I extends InputShim> ZonedDateTime read(KryoShim<I, ?> kryo, I input, Class<ZonedDateTime> clazz) {
return ZonedDateTime.of(input.readInt(), input.readInt(), input.readInt(),
input.readInt(), input.readInt(), input.readInt(), input.readInt(),
ZoneId.of(input.readString()));
@@ -286,17 +250,14 @@ final class JavaTimeSerializers {
/**
* Serializer for the {@link ZoneOffset} class.
*/
- final static class ZoneOffsetSerializer extends Serializer<ZoneOffset>
- {
+ final static class ZoneOffsetSerializer implements SerializerShim<ZoneOffset> {
@Override
- public void write(final Kryo kryo, final Output output, final ZoneOffset zoneOffset)
- {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, ZoneOffset zoneOffset) {
output.writeString(zoneOffset.getId());
}
@Override
- public ZoneOffset read(final Kryo kryo, final Input input, final Class<ZoneOffset> clazz)
- {
+ public <I extends InputShim> ZoneOffset read(KryoShim<I, ?> kryo, I input, Class<ZoneOffset> clazz) {
return ZoneOffset.of(input.readString());
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/PairSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/PairSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/PairSerializer.java
index e5e92e7..0464b22 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/PairSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/PairSerializer.java
@@ -18,6 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
import org.apache.tinkerpop.shaded.kryo.io.Input;
@@ -27,16 +31,15 @@ import org.javatuples.Pair;
/**
* @author Daniel Kuppitz (http://gremlin.guru)
*/
-final class PairSerializer extends Serializer<Pair> {
-
+final class PairSerializer implements SerializerShim<Pair> {
@Override
- public void write(final Kryo kryo, final Output output, final Pair pair) {
+ public <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, Pair pair) {
kryo.writeClassAndObject(output, pair.getValue0());
kryo.writeClassAndObject(output, pair.getValue1());
}
@Override
- public Pair read(final Kryo kryo, final Input input, final Class<Pair> pairClass) {
+ public <I extends InputShim> Pair read(KryoShim<I, ?> kryo, I input, Class<Pair> pairClass) {
return Pair.with(kryo.readClassAndObject(input), kryo.readClassAndObject(input));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
index ef105ce..1f41c0d 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
@@ -67,4 +67,16 @@ public interface TypeRegistration<T> {
* @return the sole parameter
*/
Kryo registerWith(Kryo kryo);
+
+ /**
+ * Returns true if at least one of {@link #getShadedSerializer()}, {@link #getSerializerShim()}, or
+ * {@link #getFunctionOfShadedKryo()} is non null. Returns false if all are null.
+ *
+ * @return whether a serializer is defined for this type registration
+ */
+ default boolean hasSerializer() {
+ return null != getFunctionOfShadedKryo() ||
+ null != getSerializerShim() ||
+ null != getShadedSerializer();
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
index 959605c..7783856 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
@@ -18,6 +18,8 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim;
+import org.apache.commons.configuration.Configuration;
+
import java.io.InputStream;
import java.io.OutputStream;
@@ -80,4 +82,18 @@ public interface KryoShimService {
* @return this implementation's priority value
*/
int getPriority();
+
+ /**
+ * Attempt to incorporate the supplied configuration in future read/write calls.
+ * <p>
+ * This method is a wart that exists essentially just to support the old
+ * {@link HadoopPools#initialize(Configuration)} use-case.
+ * <p>
+ * This method is not guaranteed to have any effect on an instance of this interface
+ * after {@link #writeClassAndObject(Object, OutputStream)} or {@link #readClassAndObject(InputStream)}
+ * has been invoked on that particular instance.
+ *
+ * @param conf the configuration to apply to this service's internal serializer
+ */
+ void applyConfiguration(Configuration conf);
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
index 9ccf2de..9184dd0 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
@@ -18,6 +18,7 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim;
+import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.shaded.kryo.io.Input;
import org.apache.tinkerpop.shaded.kryo.io.Output;
import org.slf4j.Logger;
@@ -35,7 +36,9 @@ import java.util.ServiceLoader;
*/
public class KryoShimServiceLoader {
- private static volatile KryoShimService CACHED_SHIM_SERVICE;
+ private static volatile KryoShimService cachedShimService;
+
+ private static volatile Configuration conf;
private static final Logger log = LoggerFactory.getLogger(KryoShimServiceLoader.class);
@@ -46,6 +49,10 @@ public class KryoShimServiceLoader {
*/
public static final String SHIM_CLASS_SYSTEM_PROPERTY = "tinkerpop.kryo.shim";
+ public static void applyConfiguration(Configuration conf) {
+ KryoShimServiceLoader.conf = conf;
+ }
+
/**
* Return a reference to the shim service. This method may return a cached shim service
* unless {@code forceReload} is true. Calls to this method need not be externally
@@ -58,8 +65,8 @@ public class KryoShimServiceLoader {
*/
public static KryoShimService load(boolean forceReload) {
- if (null != CACHED_SHIM_SERVICE && !forceReload) {
- return CACHED_SHIM_SERVICE;
+ if (null != cachedShimService && !forceReload) {
+ return cachedShimService;
}
ArrayList<KryoShimService> services = new ArrayList<>();
@@ -109,7 +116,15 @@ public class KryoShimServiceLoader {
log.info("Set {} provider to {} ({}) because its priority value ({}) is the highest available",
KryoShimService.class.getSimpleName(), result, result.getClass(), result.getPriority());
- return CACHED_SHIM_SERVICE = result;
+ Configuration userConf = conf;
+
+ if (null != userConf) {
+ log.info("Configuring {} provider {} with user-provided configuration",
+ KryoShimService.class.getSimpleName(), result);
+ result.applyConfiguration(userConf);
+ }
+
+ return cachedShimService = result;
}
/**
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
index 191cdd8..e5f9005 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
@@ -26,7 +26,7 @@ package org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim;
*/
public interface SerializerShim<T> {
- <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, T starGraph);
+ <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, T object);
<I extends InputShim> T read(KryoShim<I, ?> kryo, I input, Class<T> clazz);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopCombine.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopCombine.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopCombine.java
index de1e2f9..06778e6 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopCombine.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopCombine.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPools;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,7 +48,7 @@ public final class HadoopCombine extends Reducer<ObjectWritable, ObjectWritable,
@Override
public void setup(final Reducer<ObjectWritable, ObjectWritable, ObjectWritable, ObjectWritable>.Context context) {
final Configuration apacheConfiguration = ConfUtil.makeApacheConfiguration(context.getConfiguration());
- HadoopPools.initialize(apacheConfiguration);
+ KryoShimServiceLoader.applyConfiguration(apacheConfiguration);
this.mapReduce = MapReduce.createMapReduce(HadoopGraph.open(apacheConfiguration), apacheConfiguration);
this.mapReduce.workerStart(MapReduce.Stage.COMBINE);
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopMap.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopMap.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopMap.java
index 9e6fac3..5fc7026 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopMap.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopMap.java
@@ -28,6 +28,7 @@ import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
import org.apache.tinkerpop.gremlin.process.computer.util.ComputerGraph;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,7 +51,7 @@ public final class HadoopMap extends Mapper<NullWritable, VertexWritable, Object
@Override
public void setup(final Mapper<NullWritable, VertexWritable, ObjectWritable, ObjectWritable>.Context context) {
final Configuration apacheConfiguration = ConfUtil.makeApacheConfiguration(context.getConfiguration());
- HadoopPools.initialize(apacheConfiguration);
+ KryoShimServiceLoader.applyConfiguration(apacheConfiguration);
this.mapReduce = MapReduce.createMapReduce(HadoopGraph.open(apacheConfiguration), apacheConfiguration);
this.mapReduce.workerStart(MapReduce.Stage.MAP);
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopReduce.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopReduce.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopReduce.java
index 06dfba1..6ca7b8f 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopReduce.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/process/computer/HadoopReduce.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPools;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
import org.apache.tinkerpop.gremlin.process.computer.MapReduce;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,7 +48,7 @@ public final class HadoopReduce extends Reducer<ObjectWritable, ObjectWritable,
@Override
public void setup(final Reducer<ObjectWritable, ObjectWritable, ObjectWritable, ObjectWritable>.Context context) {
final Configuration apacheConfiguration = ConfUtil.makeApacheConfiguration(context.getConfiguration());
- HadoopPools.initialize(apacheConfiguration);
+ KryoShimServiceLoader.applyConfiguration(apacheConfiguration);
this.mapReduce = MapReduce.createMapReduce(HadoopGraph.open(apacheConfiguration), apacheConfiguration);
this.mapReduce.workerStart(MapReduce.Stage.REDUCE);
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
index c19b914..5753d90 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolShimService.java
@@ -18,7 +18,9 @@
*/
package org.apache.tinkerpop.gremlin.hadoop.structure.io;
+import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.io.Input;
import org.apache.tinkerpop.shaded.kryo.io.Output;
@@ -66,4 +68,9 @@ public class HadoopPoolShimService implements KryoShimService {
public int getPriority() {
return 0;
}
+
+ @Override
+ public void applyConfiguration(Configuration conf) {
+ KryoShimServiceLoader.applyConfiguration(conf);
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolsConfigurable.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolsConfigurable.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolsConfigurable.java
index f3a1bac..0e5f135 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolsConfigurable.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/HadoopPoolsConfigurable.java
@@ -20,6 +20,8 @@ package org.apache.tinkerpop.gremlin.hadoop.structure.io;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
+import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -28,7 +30,7 @@ public interface HadoopPoolsConfigurable extends Configurable {
@Override
public default void setConf(final Configuration configuration) {
- HadoopPools.initialize(configuration);
+ KryoShimServiceLoader.applyConfiguration(ConfUtil.makeApacheConfiguration(configuration));
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordReader.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordReader.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordReader.java
index d7ed46b..a1daddf 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordReader.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordReader.java
@@ -37,6 +37,7 @@ import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader;
import org.apache.tinkerpop.gremlin.structure.io.gryo.VertexTerminator;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -71,7 +72,7 @@ public final class GryoRecordReader extends RecordReader<NullWritable, VertexWri
final Configuration configuration = context.getConfiguration();
if (configuration.get(Constants.GREMLIN_HADOOP_GRAPH_FILTER, null) != null)
this.graphFilter = VertexProgramHelper.deserialize(ConfUtil.makeApacheConfiguration(configuration), Constants.GREMLIN_HADOOP_GRAPH_FILTER);
- HadoopPools.initialize(configuration);
+ KryoShimServiceLoader.applyConfiguration(ConfUtil.makeApacheConfiguration(configuration));
this.gryoReader = HadoopPools.getGryoPool().takeReader();
long start = split.getStart();
final Path file = split.getPath();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordWriter.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordWriter.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordWriter.java
index 67a8339..2ea3394 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordWriter.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/gryo/GryoRecordWriter.java
@@ -25,8 +25,10 @@ import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.tinkerpop.gremlin.hadoop.Constants;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.HadoopPools;
import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
+import org.apache.tinkerpop.gremlin.hadoop.structure.util.ConfUtil;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoWriter;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import java.io.DataOutputStream;
import java.io.IOException;
@@ -43,7 +45,7 @@ public final class GryoRecordWriter extends RecordWriter<NullWritable, VertexWri
public GryoRecordWriter(final DataOutputStream outputStream, final Configuration configuration) {
this.outputStream = outputStream;
this.hasEdges = configuration.getBoolean(Constants.GREMLIN_HADOOP_GRAPH_WRITER_HAS_EDGES, true);
- HadoopPools.initialize(configuration);
+ KryoShimServiceLoader.applyConfiguration(ConfUtil.makeApacheConfiguration(configuration));
this.gryoWriter = HadoopPools.getGryoPool().takeWriter();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/process/computer/SparkExecutor.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/process/computer/SparkExecutor.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/process/computer/SparkExecutor.java
index c2b85dd..9e5ac53 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/process/computer/SparkExecutor.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/process/computer/SparkExecutor.java
@@ -38,6 +38,7 @@ import org.apache.tinkerpop.gremlin.spark.process.computer.payload.Payload;
import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewIncomingPayload;
import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewOutgoingPayload;
import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewPayload;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimServiceLoader;
import org.apache.tinkerpop.gremlin.structure.util.Attachable;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty;
@@ -89,7 +90,7 @@ public final class SparkExecutor {
graphRDD.leftOuterJoin(viewIncomingRDD)) // every other iteration may have views and messages
// for each partition of vertices emit a view and their outgoing messages
.mapPartitionsToPair(partitionIterator -> {
- HadoopPools.initialize(apacheConfiguration);
+ KryoShimServiceLoader.applyConfiguration(apacheConfiguration);
final VertexProgram<M> workerVertexProgram = VertexProgram.<VertexProgram<M>>createVertexProgram(HadoopGraph.open(apacheConfiguration), apacheConfiguration); // each partition(Spark)/worker(TP3) has a local copy of the vertex program (a worker's task)
final String[] vertexComputeKeysArray = VertexProgramHelper.vertexComputeKeysAsArray(workerVertexProgram.getVertexComputeKeys()); // the compute keys as an array
final SparkMessenger<M> messenger = new SparkMessenger<>();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
deleted file mode 100644
index 4c99e70..0000000
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/TinkerPopKryoRegistrator.java
+++ /dev/null
@@ -1,121 +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.spark.structure.io;
-
-import com.esotericsoftware.kryo.Kryo;
-import com.esotericsoftware.kryo.Serializer;
-import com.esotericsoftware.kryo.serializers.JavaSerializer;
-import com.google.common.base.Preconditions;
-import org.apache.spark.serializer.KryoRegistrator;
-import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
-import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
-import org.apache.tinkerpop.gremlin.process.traversal.step.map.GroupStep;
-import org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep;
-import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation;
-import org.apache.tinkerpop.gremlin.spark.process.computer.payload.MessagePayload;
-import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewIncomingPayload;
-import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewOutgoingPayload;
-import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewPayload;
-import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.ObjectWritableSerializer;
-import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.VertexWritableSerializer;
-import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded.UnshadedSerializerAdapter;
-import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
-import org.apache.tinkerpop.gremlin.structure.io.gryo.TypeRegistration;
-import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
-import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * A spark.kryo.registrator implementation that installs TinkerPop types.
- * This is intended for use with spark.serializer=KryoSerializer, not GryoSerializer.
- */
-public class TinkerPopKryoRegistrator implements KryoRegistrator {
-
- private static final Logger log = LoggerFactory.getLogger(TinkerPopKryoRegistrator.class);
-
- @Override
- public void registerClasses(Kryo kryo) {
- // TinkerPop type registrations copied from GyroSerializer's constructor
- kryo.register(MessagePayload.class);
- kryo.register(ViewIncomingPayload.class);
- kryo.register(ViewOutgoingPayload.class);
- kryo.register(ViewPayload.class);
- kryo.register(VertexWritable.class, new UnshadedSerializerAdapter<>(new VertexWritableSerializer()));
- kryo.register(ObjectWritable.class, new UnshadedSerializerAdapter<>(new ObjectWritableSerializer<>()));
-
- Set<Class<?>> shimmedClasses = new HashSet<>();
-
- Set<Class<?>> javaSerializationClasses = new HashSet<>();
-
- // Copy GryoMapper's default registrations
- for (TypeRegistration<?> tr : GryoMapper.build().create().getTypeRegistrations()) {
- // Special case for JavaSerializer, which is generally implemented in terms of TinkerPop's
- // problematic static GryoMapper/GryoSerializer pool (these are handled below the loop)
- org.apache.tinkerpop.shaded.kryo.Serializer<?> shadedSerializer = tr.getShadedSerializer();
- SerializerShim<?> serializerShim = tr.getSerializerShim();
- if (null != shadedSerializer &&
- shadedSerializer.getClass().equals(org.apache.tinkerpop.shaded.kryo.serializers.JavaSerializer.class)) {
- javaSerializationClasses.add(tr.getTargetClass());
- } else if (null != serializerShim) {
- log.debug("Registering class {} to serializer shim {} (serializer shim class {})",
- tr.getTargetClass(), serializerShim, serializerShim.getClass());
- kryo.register(tr.getTargetClass(), new UnshadedSerializerAdapter<>(serializerShim));
- shimmedClasses.add(tr.getTargetClass());
- } else {
- // Register with the default behavior (FieldSerializer)
- log.debug("Registering class {} with default serializer", tr.getTargetClass());
- kryo.register(tr.getTargetClass());
- }
- }
-
- Map<Class<?>, Serializer<?>> javaSerializerReplacements = new HashMap<>();
- javaSerializerReplacements.put(GroupStep.GroupBiOperator.class, new JavaSerializer());
- javaSerializerReplacements.put(OrderGlobalStep.OrderBiOperator.class, null);
- javaSerializerReplacements.put(TraversalExplanation.class, null);
-
- for (Map.Entry<Class<?>, Serializer<?>> e : javaSerializerReplacements.entrySet()) {
- Class<?> c = e.getKey();
- Serializer<?> s = e.getValue();
-
- if (javaSerializationClasses.remove(c)) {
- if (null != s) {
- log.debug("Registering class {} with serializer {}", c, s);
- kryo.register(c, s);
- } else {
- log.debug("Registering class {} with default serializer", c);
- kryo.register(c);
- }
- } else {
- log.debug("Registering class {} with JavaSerializer", c);
- kryo.register(c, new JavaSerializer());
- }
- }
-
- // We really care about StarGraph's shim serializer, so make sure we registered it
- if (!shimmedClasses.contains(StarGraph.class)) {
- log.warn("No SerializerShim found for StarGraph");
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoSerializer.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoSerializer.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoSerializer.java
index 2c1dfa2..28a4d55 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoSerializer.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/GryoSerializer.java
@@ -78,7 +78,7 @@ public final class GryoSerializer extends Serializer {
}
}
this.gryoPool = GryoPool.build().
- poolSize(sparkConfiguration.getInt(GryoPool.CONFIG_IO_GRYO_POOL_SIZE, 256)).
+ poolSize(sparkConfiguration.getInt(GryoPool.CONFIG_IO_GRYO_POOL_SIZE, GryoPool.CONFIG_IO_GRYO_POOL_SIZE_DEFAULT)).
ioRegistries(makeApacheConfiguration(sparkConfiguration).getList(GryoPool.CONFIG_IO_REGISTRY, Collections.emptyList())).
initializeMapper(builder -> {
try {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/IoRegistryAwareKryoSerializer.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/IoRegistryAwareKryoSerializer.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/IoRegistryAwareKryoSerializer.java
new file mode 100644
index 0000000..8b21e21
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/IoRegistryAwareKryoSerializer.java
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+
+/**
+ * Copyright DataStax, Inc.
+ * <p>
+ * Please see the included license file for details.
+ */
+package org.apache.tinkerpop.gremlin.spark.structure.io.gryo;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import org.apache.spark.SparkConf;
+import org.apache.spark.serializer.KryoSerializer;
+import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoIo;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoPool;
+import org.javatuples.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * A {@link KryoSerializer} that attempts to honor {@link GryoPool#CONFIG_IO_REGISTRY}.
+ */
+public class IoRegistryAwareKryoSerializer extends KryoSerializer {
+
+ private final SparkConf conf;
+
+ private static final Logger log = LoggerFactory.getLogger(IoRegistryAwareKryoSerializer.class);
+
+ public IoRegistryAwareKryoSerializer(SparkConf conf) {
+ super(conf);
+ // store conf so that we can access its registry (if one is present) in newKryo()
+ this.conf = conf;
+ }
+
+ @Override
+ public Kryo newKryo() {
+ Kryo kryo = super.newKryo();
+
+ return applyIoRegistryIfPresent(kryo);
+ }
+
+ private Kryo applyIoRegistryIfPresent(Kryo kryo) {
+ if (!conf.contains(GryoPool.CONFIG_IO_REGISTRY)) {
+ log.info("SparkConf {} does not contain setting {}, skipping {} handling",
+ GryoPool.CONFIG_IO_REGISTRY, conf, IoRegistry.class.getCanonicalName());
+ return kryo;
+ }
+
+ String registryClassnames = conf.get(GryoPool.CONFIG_IO_REGISTRY);
+
+ for (String registryClassname : registryClassnames.split(",")) {
+ final IoRegistry registry;
+
+ try {
+ registry = (IoRegistry) Class.forName(registryClassname).newInstance();
+ log.info("Instantiated {}", registryClassname);
+ } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
+ log.error("Unable to reflectively instantiate the {} implementation named {}",
+ IoRegistry.class.getCanonicalName(), registryClassname, e);
+ return kryo;
+ }
+
+ // Left is the class targeted for serialization, right is a mess of potential types, including
+ // a shaded Serializer impl, unshaded Serializer impl, or Function<shaded.Kryo,shaded.Serializer>
+ final List<Pair<Class, Object>> serializers = registry.find(GryoIo.class);
+
+ if (null == serializers) {
+ log.info("Invoking find({}.class) returned null on registry {}; ignoring this registry",
+ GryoIo.class.getCanonicalName(), registry);
+ return kryo;
+ }
+
+ for (Pair<Class, Object> p : serializers) {
+ if (null == p.getValue1()) {
+ // null on the right is fine
+ log.info("Registering {} with default serializer", p.getValue0());
+ kryo.register(p.getValue0());
+ } else if (p.getValue1() instanceof Serializer) {
+ // unshaded serializer on the right is fine
+ log.info("Registering {} with serializer {}", p.getValue0(), p.getValue1());
+ kryo.register(p.getValue0(), (Serializer) p.getValue1());
+ } else {
+ // anything else on the right is unsupported with Spark
+ log.error("Serializer {} found in {} must implement {} " +
+ "(the shaded interface {} is not supported on Spark). This class will be registered with " +
+ "the default behavior of Spark's KryoSerializer.",
+ p.getValue1(), registryClassname, Serializer.class.getCanonicalName(),
+ org.apache.tinkerpop.shaded.kryo.Serializer.class.getCanonicalName());
+ kryo.register(p.getValue0());
+ }
+ }
+ }
+
+ return kryo;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/TinkerPopKryoRegistrator.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/TinkerPopKryoRegistrator.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/TinkerPopKryoRegistrator.java
new file mode 100644
index 0000000..bdb80fd
--- /dev/null
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/TinkerPopKryoRegistrator.java
@@ -0,0 +1,194 @@
+/*
+ * 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.spark.structure.io.gryo;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.serializers.JavaSerializer;
+import org.apache.spark.serializer.KryoRegistrator;
+import org.apache.tinkerpop.gremlin.hadoop.structure.io.ObjectWritable;
+import org.apache.tinkerpop.gremlin.hadoop.structure.io.VertexWritable;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.MessagePayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewIncomingPayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewOutgoingPayload;
+import org.apache.tinkerpop.gremlin.spark.process.computer.payload.ViewPayload;
+import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded.UnshadedSerializerAdapter;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.TypeRegistration;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * A spark.kryo.registrator implementation that installs TinkerPop types.
+ * This is intended for use with spark.serializer=KryoSerializer, not GryoSerializer.
+ */
+public class TinkerPopKryoRegistrator implements KryoRegistrator {
+
+ private static final Logger log = LoggerFactory.getLogger(TinkerPopKryoRegistrator.class);
+
+ @Override
+ public void registerClasses(Kryo kryo) {
+ registerClasses(kryo, Collections.emptyMap(), Collections.emptySet());
+ }
+
+ /**
+ * Register TinkerPop's classes with the supplied {@link Kryo} instance
+ * while honoring optional overrides and optional class blacklist ("blackset"?).
+ *
+ * @param kryo the Kryo serializer instance with which to register types
+ * @param serializerOverrides serializer mappings that override this class's defaults
+ * @param blacklist classes which should not be registered at all, even if there is an override entry
+ * or if they would be registered by this class by default (does not affect Kryo's
+ * built-in registrations, e.g. String.class).
+ */
+ public void registerClasses(Kryo kryo, Map<Class<?>, Serializer<?>> serializerOverrides, Set<Class<?>> blacklist) {
+ // Apply TinkerPop type registrations copied from GyroSerializer's constructor
+ for (Map.Entry<Class<?>, Serializer<?>> ent : getExtraRegistrations().entrySet()) {
+ Class<?> targetClass = ent.getKey();
+ Serializer<?> ser = ent.getValue();
+
+ // Is this class blacklisted? Skip it. (takes precedence over serializerOverrides)
+ if (blacklist.contains(targetClass)) {
+ log.debug("Not registering serializer for {} (blacklisted)", targetClass);
+ continue;
+ }
+
+ if (checkForAndApplySerializerOverride(serializerOverrides, kryo, targetClass)) {
+ // do nothing but skip the remaining else(-if) clauses
+ } else if (null == ser) {
+ log.debug("Registering {} with default serializer", targetClass);
+ kryo.register(targetClass);
+ } else {
+ log.debug("Registering {} with serializer {}", targetClass, ser);
+ kryo.register(targetClass, ser);
+ }
+ }
+
+ Set<Class<?>> shimmedClassesFromGryoMapper = new HashSet<>();
+
+ // Apply GryoMapper's default registrations
+ for (TypeRegistration<?> tr : GryoMapper.build().create().getTypeRegistrations()) {
+ // Is this class blacklisted? Skip it. (takes precedence over serializerOverrides)
+ if (blacklist.contains(tr.getTargetClass())) {
+ log.debug("Not registering serializer for {} (blacklisted)", tr.getTargetClass());
+ continue;
+ }
+
+ final org.apache.tinkerpop.shaded.kryo.Serializer<?> shadedSerializer = tr.getShadedSerializer();
+ final SerializerShim<?> serializerShim = tr.getSerializerShim();
+ final java.util.function.Function<
+ org.apache.tinkerpop.shaded.kryo.Kryo,
+ org.apache.tinkerpop.shaded.kryo.Serializer> functionOfShadedKryo = tr.getFunctionOfShadedKryo();
+
+ // Apply overrides with the highest case-precedence
+ if (checkForAndApplySerializerOverride(serializerOverrides, kryo, tr.getTargetClass())) {
+ // do nothing but skip the remaining else(-if) clauses
+ } else if (null != shadedSerializer) {
+ if (shadedSerializer.getClass().equals(org.apache.tinkerpop.shaded.kryo.serializers.JavaSerializer.class)) {
+ // Convert GryoMapper's shaded JavaSerializer mappings to their unshaded equivalents
+ log.debug("Registering {} with JavaSerializer", tr.getTargetClass());
+ kryo.register(tr.getTargetClass(), new JavaSerializer());
+ } else {
+ // There's supposed to be a check in GryoMapper that prevents this from happening
+ log.error("GryoMapper's default serialization registration for {} is a {}. " +
+ "This is probably a bug in TinkerPop (this is not a valid default registration). " +
+ "I am configuring Spark to use Kryo's default serializer for this class, " +
+ "but this may cause serialization failures at runtime.",
+ tr.getTargetClass(),
+ org.apache.tinkerpop.shaded.kryo.Serializer.class.getCanonicalName());
+ kryo.register(tr.getTargetClass());
+ }
+ } else if (null != serializerShim) {
+ // Wrap shim serializers in an adapter for Spark's unshaded Kryo
+ log.debug("Registering {} to serializer shim {} (serializer shim {})",
+ tr.getTargetClass(), serializerShim, serializerShim.getClass());
+ kryo.register(tr.getTargetClass(), new UnshadedSerializerAdapter<>(serializerShim));
+ shimmedClassesFromGryoMapper.add(tr.getTargetClass());
+ } else if (null != functionOfShadedKryo) {
+ // As with shaded serializers, there's supposed to be a check in GryoMapper that prevents this from happening
+ log.error("GryoMapper's default serialization registration for {} is a Function<{},{}>. " +
+ "This is probably a bug in TinkerPop (this is not a valid default registration). " +
+ "I am configuring Spark to use Kryo's default serializer instead of this function, " +
+ "but this may cause serialization failures at runtime.",
+ tr.getTargetClass(),
+ org.apache.tinkerpop.shaded.kryo.Kryo.class.getCanonicalName(),
+ org.apache.tinkerpop.shaded.kryo.Serializer.class.getCanonicalName());
+ kryo.register(tr.getTargetClass());
+ } else {
+ // Register all other classes with the default behavior (FieldSerializer)
+ log.debug("Registering {} with default serializer", tr.getTargetClass());
+ kryo.register(tr.getTargetClass());
+ }
+ }
+
+ // StarGraph's shim serializer is especially important on Spark for efficiency reasons,
+ // so log a warning if we failed to register it somehow
+ if (!shimmedClassesFromGryoMapper.contains(StarGraph.class)) {
+ log.warn("No SerializerShim found for StarGraph");
+ }
+ }
+
+ private LinkedHashMap<Class<?>, Serializer<?>> getExtraRegistrations() {
+
+ /* The map returned by this method MUST have a fixed iteration order!
+ *
+ * The order itself is irrelevant, so long as it is completely stable at runtime.
+ *
+ * LinkedHashMap satisfies this requirement (its contract specifies
+ * iteration in key-insertion-order).
+ */
+
+ LinkedHashMap<Class<?>, Serializer<?>> m = new LinkedHashMap<>();
+ // The following entries were copied from GryoSerializer's constructor
+ // This could be turned into a static collection on GryoSerializer to avoid
+ // duplication, but it would be a bit cumbersome to do so without disturbing
+ // the ordering of the existing entries in that constructor, since not all
+ // of the entries are for TinkerPop (and the ordering is significant).
+ m.put(MessagePayload.class, null);
+ m.put(ViewIncomingPayload.class, null);
+ m.put(ViewOutgoingPayload.class, null);
+ m.put(ViewPayload.class, null);
+ m.put(VertexWritable.class, new UnshadedSerializerAdapter<>(new VertexWritableSerializer()));
+ m.put(ObjectWritable.class, new UnshadedSerializerAdapter<>(new ObjectWritableSerializer<>()));
+
+ return m;
+ }
+
+ private boolean checkForAndApplySerializerOverride(Map<Class<?>, Serializer<?>> serializerOverrides,
+ Kryo kryo, Class<?> targetClass) {
+ if (serializerOverrides.containsKey(targetClass)) {
+ Serializer<?> ser = serializerOverrides.get(targetClass);
+ if (null == ser) {
+ // null means use Kryo's default serializer
+ log.debug("Registering {} with default serializer per overrides", targetClass);
+ kryo.register(targetClass);
+ } else {
+ // nonnull means use that serializer
+ log.debug("Registering {} with serializer {} per overrides", targetClass, ser);
+ kryo.register(targetClass, ser);
+ }
+ return true;
+ }
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
index d0411e8..a524a97 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/structure/io/gryo/kryoshim/unshaded/UnshadedKryoShimService.java
@@ -24,92 +24,131 @@
*/
package org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded;
-import com.twitter.chill.KryoInstantiator;
-import com.twitter.chill.KryoPool;
-import com.twitter.chill.SerDeState;
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
import org.apache.spark.SparkConf;
-import org.apache.spark.serializer.KryoSerializer;
-import org.apache.tinkerpop.gremlin.spark.structure.io.TinkerPopKryoRegistrator;
+import org.apache.tinkerpop.gremlin.spark.structure.io.gryo.IoRegistryAwareKryoSerializer;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoPool;
import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.concurrent.LinkedBlockingQueue;
public class UnshadedKryoShimService implements KryoShimService {
- public static final String SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY = "tinkerpop.kryo.poolsize";
-
private static final Logger log = LoggerFactory.getLogger(UnshadedKryoShimService.class);
- private static final int SPARK_KRYO_POOL_SIZE_DEFAULT = 8;
- private final KryoSerializer sparkKryoSerializer;
- private final KryoPool kryoPool;
+ private static final LinkedBlockingQueue<Kryo> KRYOS = new LinkedBlockingQueue<>();
- public UnshadedKryoShimService() {
- this(TinkerPopKryoRegistrator.class.getCanonicalName(), getDefaultKryoPoolSize());
- }
+ private static volatile boolean initialized;
- public UnshadedKryoShimService(String sparkKryoRegistratorClassname, int kryoPoolSize) {
- SparkConf sparkConf = new SparkConf();
- sparkConf.set("spark.serializer", KryoSerializer.class.getCanonicalName());
- sparkConf.set("spark.kryo.registrator", sparkKryoRegistratorClassname);
- sparkKryoSerializer = new KryoSerializer(sparkConf);
- kryoPool = KryoPool.withByteArrayOutputStream(kryoPoolSize, new KryoInstantiator());
- }
+ public UnshadedKryoShimService() { }
@Override
public Object readClassAndObject(InputStream source) {
- SerDeState sds = null;
- try {
- sds = kryoPool.borrow();
- sds.setInput(source);
+ LinkedBlockingQueue<Kryo> kryos = initialize();
- return sds.readClassAndObject();
+ Kryo k = null;
+ try {
+ k = kryos.take();
+
+ return k.readClassAndObject(new Input(source));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
} finally {
- kryoPool.release(sds);
+ try {
+ kryos.put(k);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
}
}
@Override
public void writeClassAndObject(Object o, OutputStream sink) {
- SerDeState sds = null;
+
+ LinkedBlockingQueue<Kryo> kryos = initialize();
+
+ Kryo k = null;
try {
- sds = kryoPool.borrow();
+ k = kryos.take();
- sds.writeClassAndObject(o); // this writes to an internal buffer
+ Output kryoOutput = new Output(sink);
- sds.writeOutputTo(sink); // this copies the internal buffer to sink
+ k.writeClassAndObject(kryoOutput, o);
- sink.flush();
- } catch (IOException e) {
+ kryoOutput.flush();
+ } catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
- kryoPool.release(sds);
+ try {
+ kryos.put(k);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
}
}
@Override
public int getPriority() {
- return 1024;
+ return 50;
}
- private static int getDefaultKryoPoolSize() {
- String raw = System.getProperty(SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY);
+ @Override
+ public void applyConfiguration(Configuration conf) {
+ initialize(conf);
+ }
- int size = SPARK_KRYO_POOL_SIZE_DEFAULT;
- try {
- size = Integer.valueOf(raw);
- log.info("Setting kryo pool size to {} according to system property {}", size,
- SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY);
- } catch (NumberFormatException e) {
- log.error("System property {}={} could not be parsed as an integer, using default value {}",
- SPARK_KRYO_POOL_SIZE_SYSTEM_PROPERTY, raw, size, e);
+ private LinkedBlockingQueue<Kryo> initialize() {
+ return initialize(new BaseConfiguration());
+ }
+
+ private LinkedBlockingQueue<Kryo> initialize(Configuration conf) {
+ // DCL is safe in this case due to volatility
+ if (!initialized) {
+ synchronized (UnshadedKryoShimService.class) {
+ if (!initialized) {
+ SparkConf sparkConf = new SparkConf();
+
+ // Copy the user's IoRegistry from the param conf to the SparkConf we just created
+ String regStr = conf.getString(GryoPool.CONFIG_IO_REGISTRY);
+ if (null != regStr) { // SparkConf rejects null values with NPE, so this has to be checked before set(...)
+ sparkConf.set(GryoPool.CONFIG_IO_REGISTRY, regStr);
+ }
+ // Setting spark.serializer here almost certainly isn't necessary, but it doesn't hurt
+ sparkConf.set("spark.serializer", IoRegistryAwareKryoSerializer.class.getCanonicalName());
+
+ String registrator = conf.getString("spark.kryo.registrator");
+ if (null != registrator) {
+ sparkConf.set("spark.kryo.registrator", registrator);
+ log.info("Copied spark.kryo.registrator: {}", registrator);
+ } else {
+ log.info("Not copying spark.kryo.registrator");
+ }
+
+ // Reuse Gryo poolsize for Kryo poolsize (no need to copy this to SparkConf)
+ int poolSize = conf.getInt(GryoPool.CONFIG_IO_GRYO_POOL_SIZE,
+ GryoPool.CONFIG_IO_GRYO_POOL_SIZE_DEFAULT);
+ // Instantiate the spark.serializer
+ final IoRegistryAwareKryoSerializer ioReg = new IoRegistryAwareKryoSerializer(sparkConf);
+ // Setup a pool backed by our spark.serializer instance
+
+ for (int i = 0; i < poolSize; i++) {
+ KRYOS.add(ioReg.newKryo());
+ }
+
+ initialized = true;
+ }
+ }
}
- return size;
+ return KRYOS;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9321a3e1/spark-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService b/spark-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
new file mode 100644
index 0000000..68712a6
--- /dev/null
+++ b/spark-gremlin/src/main/resources/META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService
@@ -0,0 +1 @@
+org.apache.tinkerpop.gremlin.spark.structure.io.gryo.kryoshim.unshaded.UnshadedKryoShimService # Supports Spark
[4/8] incubator-tinkerpop git commit: Kryo shim refactoring and
documentation
Posted by ok...@apache.org.
Kryo shim refactoring and documentation
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/218d7909
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/218d7909
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/218d7909
Branch: refs/heads/TINKERPOP-1321
Commit: 218d7909b9c35a4488102f53c444c92be737972c
Parents: ef52869
Author: Dan LaRocque <da...@hopcount.org>
Authored: Fri Jun 3 07:41:50 2016 -0400
Committer: Dan LaRocque <da...@hopcount.org>
Committed: Fri Jun 3 07:41:50 2016 -0400
----------------------------------------------------------------------
.../gremlin/structure/io/gryo/GryoMapper.java | 29 +--
.../structure/io/gryo/TypeRegistration.java | 2 +-
.../structure/io/gryo/URISerializer.java | 8 +-
.../structure/io/gryo/UUIDSerializer.java | 12 +-
.../structure/io/gryo/kryoshim/InputShim.java | 38 ++++
.../structure/io/gryo/kryoshim/KryoShim.java | 41 ++++
.../io/gryo/kryoshim/KryoShimService.java | 83 ++++++++
.../io/gryo/kryoshim/KryoShimServiceLoader.java | 199 +++++++++++++++++++
.../structure/io/gryo/kryoshim/OutputShim.java | 40 ++++
.../io/gryo/kryoshim/SerializerShim.java | 36 ++++
.../io/gryo/kryoshim/package-info.java | 55 +++++
.../kryoshim/shaded/ShadedInputAdapter.java | 66 ++++++
.../gryo/kryoshim/shaded/ShadedKryoAdapter.java | 67 +++++++
.../kryoshim/shaded/ShadedOutputAdapter.java | 72 +++++++
.../shaded/ShadedSerializerAdapter.java | 54 +++++
.../io/gryo/kryoshim/shaded/package-info.java | 25 +++
.../structure/io/kryoshim/InputShim.java | 37 ----
.../gremlin/structure/io/kryoshim/KryoShim.java | 40 ----
.../structure/io/kryoshim/KryoShimService.java | 83 --------
.../io/kryoshim/KryoShimServiceLoader.java | 137 -------------
.../structure/io/kryoshim/OutputShim.java | 41 ----
.../structure/io/kryoshim/SerializerShim.java | 35 ----
.../structure/io/kryoshim/package-info.java | 54 -----
.../io/kryoshim/shaded/ShadedInputAdapter.java | 66 ------
.../io/kryoshim/shaded/ShadedKryoAdapter.java | 67 -------
.../io/kryoshim/shaded/ShadedOutputAdapter.java | 72 -------
.../shaded/ShadedSerializerAdapter.java | 54 -----
.../io/kryoshim/shaded/package-info.java | 25 ---
.../util/star/StarGraphGryoSerializer.java | 9 +-
.../util/star/StarGraphSerializer.java | 18 +-
.../structure/io/HadoopPoolShimService.java | 2 +-
.../hadoop/structure/io/ObjectWritable.java | 4 +-
.../hadoop/structure/io/VertexWritable.java | 3 +-
...n.structure.io.gryo.kryoshim.KryoShimService | 1 +
...remlin.structure.io.kryoshim.KryoShimService | 1 -
.../structure/io/TinkerPopKryoRegistrator.java | 121 +++++++++++
.../io/gryo/ObjectWritableSerializer.java | 12 +-
.../io/gryo/VertexWritableSerializer.java | 12 +-
.../kryoshim/unshaded/UnshadedInputAdapter.java | 78 ++++++++
.../kryoshim/unshaded/UnshadedKryoAdapter.java | 74 +++++++
.../unshaded/UnshadedKryoShimService.java | 115 +++++++++++
.../unshaded/UnshadedOutputAdapter.java | 83 ++++++++
.../unshaded/UnshadedSerializerAdapter.java | 57 ++++++
43 files changed, 1368 insertions(+), 760 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
index 5bc71da..851b03c 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoMapper.java
@@ -57,8 +57,8 @@ import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
import org.apache.tinkerpop.gremlin.structure.io.Mapper;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded.ShadedSerializerAdapter;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.shaded.ShadedSerializerAdapter;
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;
@@ -70,8 +70,6 @@ import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceProperty;
import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex;
import org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertexProperty;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraph;
-import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGryoSerializer;
-import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer;
import org.apache.tinkerpop.shaded.kryo.ClassResolver;
import org.apache.tinkerpop.shaded.kryo.Kryo;
@@ -115,7 +113,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
@@ -406,7 +403,7 @@ public final class GryoMapper implements Mapper<Kryo> {
public Builder addCustom(final Class... custom) {
if (custom != null && custom.length > 0) {
for (Class c : custom) {
- addOrOverrideRegistration(GryoTypeReg.of(c, currentSerializationId.getAndIncrement()));
+ addOrOverrideRegistration(c, id -> GryoTypeReg.of(c, id));
}
}
return this;
@@ -417,7 +414,7 @@ public final class GryoMapper implements Mapper<Kryo> {
* a class that is already registered will override that registration.
*/
public Builder addCustom(final Class clazz, final Serializer serializer) {
- addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), serializer));
+ addOrOverrideRegistration(clazz, id -> GryoTypeReg.of(clazz, id, serializer));
return this;
}
@@ -425,7 +422,7 @@ public final class GryoMapper implements Mapper<Kryo> {
* Register custom class to serialize with a custom serialization shim.
*/
public Builder addCustom(final Class clazz, final SerializerShim serializer) {
- addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), serializer));
+ addOrOverrideRegistration(clazz, id -> GryoTypeReg.of(clazz, id, serializer));
return this;
}
@@ -434,7 +431,7 @@ public final class GryoMapper implements Mapper<Kryo> {
* that calling this method for a class that is already registered will override that registration.
*/
public Builder addCustom(final Class clazz, final Function<Kryo, Serializer> functionOfKryo) {
- addOrOverrideRegistration(GryoTypeReg.of(clazz, currentSerializationId.getAndIncrement(), functionOfKryo));
+ addOrOverrideRegistration(clazz, id -> GryoTypeReg.of(clazz, id, functionOfKryo));
return this;
}
@@ -490,16 +487,24 @@ public final class GryoMapper implements Mapper<Kryo> {
return new GryoMapper(this);
}
- private <T> void addOrOverrideRegistration(TypeRegistration<T> newRegistration) {
+ private <T> void addOrOverrideRegistration(Class<?> clazz, Function<Integer, TypeRegistration<T>> newRegistrationBuilder) {
Iterator<TypeRegistration<?>> iter = typeRegistrations.iterator();
+ Integer registrationId = null;
while (iter.hasNext()) {
TypeRegistration<?> existingRegistration = iter.next();
- if (existingRegistration.getTargetClass().equals(newRegistration.getTargetClass())) {
+ if (existingRegistration.getTargetClass().equals(clazz)) {
+ // when overridding a registration, use the old id
+ registrationId = existingRegistration.getId();
+ // remove the old registration (we install its override below)
iter.remove();
break;
}
}
- typeRegistrations.add(newRegistration);
+ if (null == registrationId) {
+ // when not overridding a registration, get an id from the counter
+ registrationId = currentSerializationId.getAndIncrement();
+ }
+ typeRegistrations.add(newRegistrationBuilder.apply(registrationId));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
index 5ca3f31..ef105ce 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/TypeRegistration.java
@@ -18,7 +18,7 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import org.apache.tinkerpop.shaded.kryo.Kryo;
import org.apache.tinkerpop.shaded.kryo.Serializer;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
index de08061..a65bcea 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/URISerializer.java
@@ -18,10 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import java.net.URI;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
index b86ddc2..d1d59d3 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/UUIDSerializer.java
@@ -18,14 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.structure.io.gryo;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
-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.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
import java.util.UUID;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/InputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/InputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/InputShim.java
new file mode 100644
index 0000000..23bec16
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/InputShim.java
@@ -0,0 +1,38 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Input}-like abstraction.
+ * See that class for method documentation.
+ */
+public interface InputShim {
+
+ byte readByte();
+
+ byte[] readBytes(int size);
+
+ String readString();
+
+ long readLong();
+
+ int readInt();
+
+ double readDouble();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShim.java
new file mode 100644
index 0000000..a0f3f87
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShim.java
@@ -0,0 +1,41 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.Kryo}-like abstraction.
+ * See that class for method documentation.
+ *
+ * @param <I> this interface's complementary InputShim
+ * @param <O> this interface's complementary OutputShim
+ */
+public interface KryoShim<I extends InputShim, O extends OutputShim> {
+
+ <T> T readObject(I input, Class<T> type);
+
+ Object readClassAndObject(I input);
+
+ void writeObject(O output, Object object);
+
+ void writeClassAndObject(O output, Object object);
+
+ <T> T readObjectOrNull(I input, Class<T> type);
+
+ void writeObjectOrNull(O output, Object object, Class type);
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
new file mode 100644
index 0000000..959605c
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimService.java
@@ -0,0 +1,83 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * This interface exists to decouple HadoopPools from TinkerPop's shaded Kryo.
+ * <p>
+ * VertexWritable and ObjectWritable formerly implemented Serializable by
+ * resorting to statically-pooled shaded Kryo instances maintained by the HadoopPools class.
+ * This is awkward because those shaded Kryo instances require class registration by default.
+ * <p>
+ * Consider what happens with custom property datatypes reachable from the reference graph rooted at an ObjectWritable
+ * or VertexWritable instance. It is not enough for these property classes to merely implement
+ * Serializable, though one think that from skimming ObjectWritable/VertexWritable. Those classes
+ * must also register with TinkerPop's internal, shaded Kryo instances as maintained by HadoopPools,
+ * or else configure those instances to accept unregistered classes.
+ * Otherwise, TinkerPop's shaded Kryo will refuse to serialize those properties (even though
+ * they implement Serializable, and even though the user might think they are only using
+ * Java's standard Serialization mechanism!).
+ * <p>
+ * By hiding the mechanics of serialization behind this interface instead of hardcoding it in
+ * HadoopPools, the user can decide how to implement serialization for ObjectWritable/VertexWritable
+ * (and whatever other classes in TinkerPop decide to implement Serializable but then delegate
+ * all of the implementation details, like ObjectWritable/VertexWritable do now).
+ */
+public interface KryoShimService {
+
+ /**
+ * Deserializes an object from an input stream.
+ *
+ * @param source the stream from which to read an object's serialized form
+ * @return the first deserialized object available from {@code source}
+ */
+ Object readClassAndObject(InputStream source);
+
+ /**
+ * Serializes an object to an output stream. This may flush the output stream.
+ *
+ * @param o the object to serialize
+ * @param sink the stream into which the serialized object is written
+ */
+ void writeClassAndObject(Object o, OutputStream sink);
+
+ /**
+ * Returns this service's relative priority number. Unless explicitly overridden through a
+ * system property ({@link KryoShimServiceLoader#SHIM_CLASS_SYSTEM_PROPERTY}),
+ * the service implementation with the numerically highest priority will be used
+ * and all others ignored. In other words, the highest priority wins (in the absence of a
+ * system property override).
+ * <p>
+ * TinkerPop's current default implementation uses priority value zero.
+ * <p>
+ * Third-party implementations of this interface should (but are not technically required)
+ * to use a priority value with absolute value greater than 100.
+ * <p>
+ * The implementation currently breaks priority ties by lexicographical comparison of
+ * fully-qualified package-and-classname, but this tie-breaking behavior should be
+ * considered undefined and subject to future change. Ties are ignored if the service
+ * is explicitly set through the system property mentioned above.
+ *
+ * @return this implementation's priority value
+ */
+ int getPriority();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
new file mode 100644
index 0000000..9ccf2de
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/KryoShimServiceLoader.java
@@ -0,0 +1,199 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+import org.apache.tinkerpop.shaded.kryo.io.Input;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.ServiceLoader;
+
+/**
+ * Loads the highest-priority or user-selected {@link KryoShimService}.
+ */
+public class KryoShimServiceLoader {
+
+ private static volatile KryoShimService CACHED_SHIM_SERVICE;
+
+ private static final Logger log = LoggerFactory.getLogger(KryoShimServiceLoader.class);
+
+ /**
+ * Set this system property to the fully-qualified name of a {@link KryoShimService}
+ * package-and-classname to force it into service. Setting this property causes the
+ * priority-selection mechanism ({@link KryoShimService#getPriority()}) to be ignored.
+ */
+ public static final String SHIM_CLASS_SYSTEM_PROPERTY = "tinkerpop.kryo.shim";
+
+ /**
+ * Return a reference to the shim service. This method may return a cached shim service
+ * unless {@code forceReload} is true. Calls to this method need not be externally
+ * synchonized.
+ *
+ * @param forceReload if false, this method may use its internal service cache; if true,
+ * this method must ignore cache, and it must invoke {@link ServiceLoader#reload()}
+ * before selecting a new service to return
+ * @return the shim service
+ */
+ public static KryoShimService load(boolean forceReload) {
+
+ if (null != CACHED_SHIM_SERVICE && !forceReload) {
+ return CACHED_SHIM_SERVICE;
+ }
+
+ ArrayList<KryoShimService> services = new ArrayList<>();
+
+ ServiceLoader<KryoShimService> sl = ServiceLoader.load(KryoShimService.class);
+
+ KryoShimService result = null;
+
+ synchronized (KryoShimServiceLoader.class) {
+ if (forceReload) {
+ sl.reload();
+ }
+
+ for (KryoShimService kss : sl) {
+ services.add(kss);
+ }
+ }
+
+ String shimClass = System.getProperty(SHIM_CLASS_SYSTEM_PROPERTY);
+
+ if (null != shimClass) {
+ for (KryoShimService kss : services) {
+ if (kss.getClass().getCanonicalName().equals(shimClass)) {
+ log.info("Set {} provider to {} ({}) from system property {}={}",
+ KryoShimService.class.getSimpleName(), kss, kss.getClass(),
+ SHIM_CLASS_SYSTEM_PROPERTY, shimClass);
+ result = kss;
+ }
+ }
+ } else {
+ Collections.sort(services, KryoShimServiceComparator.INSTANCE);
+
+ for (KryoShimService kss : services) {
+ log.debug("Found Kryo shim service class {} (priority {})", kss.getClass(), kss.getPriority());
+ }
+
+ if (0 != services.size()) {
+ result = services.get(services.size() - 1);
+ }
+ }
+
+
+ if (null == result) {
+ throw new IllegalStateException("Unable to load KryoShimService");
+ }
+
+ log.info("Set {} provider to {} ({}) because its priority value ({}) is the highest available",
+ KryoShimService.class.getSimpleName(), result, result.getClass(), result.getPriority());
+
+ return CACHED_SHIM_SERVICE = result;
+ }
+
+ /**
+ * Equivalent to {@link #load(boolean)} with the parameter {@code true}.
+ *
+ * @return the (possibly cached) shim service
+ */
+ public static KryoShimService load() {
+ return load(false);
+ }
+
+ /**
+ * A loose abstraction of {@link org.apache.tinkerpop.shaded.kryo.Kryo#writeClassAndObject(Output, Object)},
+ * where the {@code output} parameter is an internally-created {@link ByteArrayOutputStream}. Returns
+ * the byte array underlying that stream.
+ *
+ * @param o an object for which the instance and class are serialized
+ * @return the serialized form
+ */
+ public static byte[] writeClassAndObjectToBytes(Object o) {
+ KryoShimService shimService = load();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ shimService.writeClassAndObject(o, baos);
+
+ return baos.toByteArray();
+ }
+
+ /**
+ * A loose abstraction of {@link org.apache.tinkerpop.shaded.kryo.Kryo#readClassAndObject(Input)},
+ * where the {@code input} parameter is {@code source}. Returns the deserialized object.
+ *
+ * @param source an input stream containing data for a serialized object class and instance
+ * @param <T> the type to which the deserialized object is cast as it is returned
+ * @return the deserialized object
+ */
+ public static <T> T readClassAndObject(InputStream source) {
+ KryoShimService shimService = load();
+
+ return (T)shimService.readClassAndObject(source);
+ }
+
+ /**
+ * Selects the service with greatest {@link KryoShimService#getPriority()}
+ * (not absolute value).
+ *
+ * Breaks ties with lexicographical comparison of classnames where the
+ * name that sorts last is considered to have highest priority. Ideally
+ * nothing should rely on that tiebreaking behavior, but it beats random
+ * selection in case a user ever gets into that situation by accident and
+ * tries to figure out what's going on.
+ */
+ private enum KryoShimServiceComparator implements Comparator<KryoShimService> {
+ INSTANCE;
+
+ @Override
+ public int compare(KryoShimService a, KryoShimService b) {
+ int ap = a.getPriority();
+ int bp = b.getPriority();
+
+ if (ap < bp) {
+ return -1;
+ } else if (bp < ap) {
+ return 1;
+ } else {
+ int result = a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName());
+
+ if (0 == result) {
+ log.warn("Found two {} implementations with the same canonical classname: {}. " +
+ "This may indicate a problem with the classpath/classloader such as " +
+ "duplicate or conflicting copies of the file " +
+ "META-INF/services/org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShimService.",
+ a.getClass().getCanonicalName());
+ } else {
+ String winner = 0 < result ? a.getClass().getCanonicalName() : b.getClass().getCanonicalName();
+ log.warn("{} implementations {} and {} are tied with priority value {}. " +
+ "Preferring {} to the other because it has a lexicographically greater classname. " +
+ "Consider setting the system property \"{}\" instead of relying on priority tie-breaking.",
+ KryoShimService.class.getSimpleName(), a, b, ap, winner, SHIM_CLASS_SYSTEM_PROPERTY);
+ }
+
+ return result;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/OutputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/OutputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/OutputShim.java
new file mode 100644
index 0000000..e4ca3d5
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/OutputShim.java
@@ -0,0 +1,40 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Output}-like abstraction.
+ * See that class for method documentation.
+ */
+public interface OutputShim {
+
+ void writeByte(byte b);
+
+ void writeBytes(byte[] array, int offset, int count);
+
+ void writeString(String s);
+
+ void writeLong(long l);
+
+ void writeInt(int i);
+
+ void writeDouble(double d);
+
+ void flush();
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
new file mode 100644
index 0000000..191cdd8
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/SerializerShim.java
@@ -0,0 +1,36 @@
+/*
+ * 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.structure.io.gryo.kryoshim;
+
+/**
+ * A minimal {@link org.apache.tinkerpop.shaded.kryo.Serializer}-like abstraction.
+ * See that class for method documentation.
+ *
+ * @param <T> the class this serializer reads/writes from/to bytes.
+ */
+public interface SerializerShim<T> {
+
+ <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, T starGraph);
+
+ <I extends InputShim> T read(KryoShim<I, ?> kryo, I input, Class<T> clazz);
+
+ default boolean isImmutable() {
+ return false;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/package-info.java
new file mode 100644
index 0000000..70675da
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/package-info.java
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+/**
+ * Abstracts a minimal subset of Kryo types and methods.
+ * <p>
+ * Kryo is often shaded. For instance, TinkerPop's Gryo
+ * serializer relies on a shaded Kryo package.
+ * TinkerPop serializers written against a particular shaded
+ * Kryo package (or an unshaded Kryo package) are compatible
+ * only with that package. In contrast, TinkerPop serializers written
+ * against this abstraction can be used with any shaded or
+ * unshaded Kryo package, so long as the signatures and behavior
+ * of the methods in this package remain stable.
+ * <p>
+ * To show how this is useful, consider
+ * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer}.
+ * This class has logic unique to TinkerPop that performs
+ * efficient and forward-compatible serialization of
+ * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraph}
+ * instances. It takes advantage of package-level visibility
+ * and the fact that it shares a package with its target,
+ * so it would be challenging to cleanly and naturally replicate
+ * (i.e. without package spoofing or runtime visibility overrides).
+ * By implementing
+ * {@link org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim}
+ * instead of, say, Gryo's shaded
+ * {@link org.apache.tinkerpop.shaded.kryo.Serializer},
+ * such a serializer can be used with anybody's Kryo package,
+ * regardless of whether
+ * that package is shaded or not. This lets third-parties reuse
+ * TinkerPop's efficient, internals-aware StarGraph serializer on
+ * their own serialization platform (and without altering
+ * TinkerPop's bytecode, let alone its source).
+ * <p>
+ * The number of types and methods in this
+ * package is deliberately small to reduce the likelihood of a
+ * new Kryo release introducing an incompatible change.
+ */
+package org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedInputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedInputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedInputAdapter.java
new file mode 100644
index 0000000..d11c1c4
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedInputAdapter.java
@@ -0,0 +1,66 @@
+/*
+ * 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.structure.io.gryo.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.InputShim;
+import org.apache.tinkerpop.shaded.kryo.io.Input;
+
+public class ShadedInputAdapter implements InputShim {
+
+ private final Input shadedInput;
+
+ public ShadedInputAdapter(Input shadedInput) {
+ this.shadedInput = shadedInput;
+ }
+
+ Input getShadedInput() {
+ return shadedInput;
+ }
+
+ @Override
+ public byte readByte()
+ {
+ return shadedInput.readByte();
+ }
+
+ @Override
+ public byte[] readBytes(int size) {
+ return shadedInput.readBytes(size);
+ }
+
+ @Override
+ public String readString() {
+ return shadedInput.readString();
+ }
+
+ @Override
+ public long readLong() {
+ return shadedInput.readLong();
+ }
+
+ @Override
+ public int readInt() {
+ return shadedInput.readInt();
+ }
+
+ @Override
+ public double readDouble() {
+ return shadedInput.readDouble();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedKryoAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedKryoAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedKryoAdapter.java
new file mode 100644
index 0000000..4283298
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedKryoAdapter.java
@@ -0,0 +1,67 @@
+/*
+ * 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.structure.io.gryo.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.KryoShim;
+import org.apache.tinkerpop.shaded.kryo.Kryo;
+
+public class ShadedKryoAdapter implements KryoShim<ShadedInputAdapter, ShadedOutputAdapter> {
+
+ private final Kryo shadedKryo;
+
+ public ShadedKryoAdapter(Kryo shadedKryo) {
+ this.shadedKryo = shadedKryo;
+ }
+
+ @Override
+ public <T> T readObject(ShadedInputAdapter input, Class<T> type)
+ {
+ return shadedKryo.readObject(input.getShadedInput(), type);
+ }
+
+ @Override
+ public Object readClassAndObject(ShadedInputAdapter input)
+ {
+ return shadedKryo.readClassAndObject(input.getShadedInput());
+ }
+
+ @Override
+ public void writeObject(ShadedOutputAdapter output, Object object)
+ {
+ shadedKryo.writeObject(output.getShadedOutput(), object);
+ }
+
+ @Override
+ public void writeClassAndObject(ShadedOutputAdapter output, Object object)
+ {
+ shadedKryo.writeClassAndObject(output.getShadedOutput(), object);
+ }
+
+ @Override
+ public <T> T readObjectOrNull(ShadedInputAdapter input, Class<T> type)
+ {
+ return shadedKryo.readObjectOrNull(input.getShadedInput(), type);
+ }
+
+ @Override
+ public void writeObjectOrNull(ShadedOutputAdapter output, Object object, Class type)
+ {
+ shadedKryo.writeObjectOrNull(output.getShadedOutput(), object, type);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedOutputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedOutputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedOutputAdapter.java
new file mode 100644
index 0000000..7547466
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedOutputAdapter.java
@@ -0,0 +1,72 @@
+/*
+ * 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.structure.io.gryo.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.OutputShim;
+import org.apache.tinkerpop.shaded.kryo.io.Output;
+
+public class ShadedOutputAdapter implements OutputShim {
+
+ private final Output shadedOutput;
+
+ public ShadedOutputAdapter(Output shadedOutput) {
+ this.shadedOutput = shadedOutput;
+ }
+
+ @Override
+ public void writeByte(byte b)
+ {
+ shadedOutput.writeByte(b);
+ }
+
+ @Override
+ public void writeBytes(byte[] array, int offset, int count) {
+ shadedOutput.writeBytes(array, offset, count);
+ }
+
+ @Override
+ public void writeString(String s) {
+ shadedOutput.writeString(s);
+ }
+
+ @Override
+ public void writeLong(long l) {
+ shadedOutput.writeLong(l);
+ }
+
+ @Override
+ public void writeInt(int i) {
+ shadedOutput.writeInt(i);
+ }
+
+ @Override
+ public void writeDouble(double d) {
+ shadedOutput.writeDouble(d);
+ }
+
+ @Override
+ public void flush() {
+ shadedOutput.flush();
+ }
+
+ Output getShadedOutput()
+ {
+ return shadedOutput;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedSerializerAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedSerializerAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedSerializerAdapter.java
new file mode 100644
index 0000000..4ce27b1
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/ShadedSerializerAdapter.java
@@ -0,0 +1,54 @@
+/*
+ * 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.structure.io.gryo.kryoshim.shaded;
+
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.SerializerShim;
+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;
+
+public class ShadedSerializerAdapter<T> extends Serializer<T> {
+
+ SerializerShim<T> serializer;
+
+ public ShadedSerializerAdapter(SerializerShim<T> serializer) {
+ this.serializer = serializer;
+ setImmutable(this.serializer.isImmutable());
+ }
+
+ @Override
+ public void write(Kryo kryo, Output output, T t) {
+ /* These adapters could be cached pretty efficiently in instance fields if it were guaranteed that this
+ * class was never subject to concurrent use. That's true of Kryo instances, but it is not clear that
+ * it is true of Serializer instances.
+ */
+ ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
+ ShadedOutputAdapter shadedOutputAdapter = new ShadedOutputAdapter(output);
+ serializer.write(shadedKryoAdapter, shadedOutputAdapter, t);
+ }
+
+ @Override
+ public T read(Kryo kryo, Input input, Class<T> aClass) {
+ // Same caching opportunity as in write(...)
+ ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
+ ShadedInputAdapter shadedInputAdapter = new ShadedInputAdapter(input);
+ return serializer.read(shadedKryoAdapter, shadedInputAdapter, aClass);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/package-info.java
new file mode 100644
index 0000000..d91a3f1
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/kryoshim/shaded/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementations of the interfaces in
+ * {@link org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim}
+ * using TinkerPop's shaded copy of Kryo.
+ */
+package org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.shaded;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
deleted file mode 100644
index 6d00884..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/InputShim.java
+++ /dev/null
@@ -1,37 +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.structure.io.kryoshim;
-
-/**
- * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Input}-like abstraction.
- */
-public interface InputShim {
-
- byte readByte();
-
- byte[] readBytes(int size);
-
- String readString();
-
- long readLong();
-
- int readInt();
-
- double readDouble();
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
deleted file mode 100644
index e2a95e6..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShim.java
+++ /dev/null
@@ -1,40 +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.structure.io.kryoshim;
-
-/**
- * A minimal {@link org.apache.tinkerpop.shaded.kryo.Kryo}-like abstraction.
- *
- * @param <I> this interface's complementary InputShim
- * @param <O> this interface's complementary OutputShim
- */
-public interface KryoShim<I extends InputShim, O extends OutputShim> {
-
- <T> T readObject(I input, Class<T> type);
-
- Object readClassAndObject(I input);
-
- void writeObject(O output, Object object);
-
- void writeClassAndObject(O output, Object object);
-
- <T> T readObjectOrNull(I input, Class<T> type);
-
- void writeObjectOrNull(O output, Object object, Class type);
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
deleted file mode 100644
index 024d40c..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimService.java
+++ /dev/null
@@ -1,83 +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.structure.io.kryoshim;
-
-import java.io.InputStream;
-import java.io.OutputStream;
-
-/**
- * This interface exists to decouple HadoopPools from TinkerPop's shaded Kryo.
- * <p>
- * VertexWritable and ObjectWritable formerly implemented Serializable by
- * resorting to statically-pooled shaded Kryo instances maintained by the HadoopPools class.
- * This is awkward because those shaded Kryo instances require class registration by default.
- * <p>
- * Consider what happens with custom property datatypes reachable from the reference graph rooted at an ObjectWritable
- * or VertexWritable instance. It is not enough for these property classes to merely implement
- * Serializable, though one think that from skimming ObjectWritable/VertexWritable. Those classes
- * must also register with TinkerPop's internal, shaded Kryo instances as maintained by HadoopPools,
- * or else configure those instances to accept unregistered classes.
- * Otherwise, TinkerPop's shaded Kryo will refuse to serialize those properties (even though
- * they implement Serializable, and even though the user might think they are only using
- * Java's standard Serialization mechanism!).
- * <p>
- * By hiding the mechanics of serialization behind this interface instead of hardcoding it in
- * HadoopPools, the user can decide how to implement serialization for ObjectWritable/VertexWritable
- * (and whatever other classes in TinkerPop decide to implement Serializable but then delegate
- * all of the implementation details, like ObjectWritable/VertexWritable do now).
- */
-public interface KryoShimService {
-
- /**
- * Deserializes an object from an input stream.
- *
- * @param source the stream from which to read an object's serialized form
- * @return the first deserialized object available from {@code source}
- */
- Object readClassAndObject(InputStream source);
-
- /**
- * Serializes an object to an output stream. This may flush the output stream.
- *
- * @param o the object to serialize
- * @param sink the stream into which the serialized object is written
- */
- void writeClassAndObject(Object o, OutputStream sink);
-
- /**
- * Returns this service's relative priority number. Unless explicitly overridden through a
- * system property ({@link KryoShimServiceLoader#SHIM_CLASS_SYSTEM_PROPERTY}),
- * the service implementation with the numerically highest priority will be used
- * and all others ignored. In other words, the highest priority wins (in the absence of a
- * system property override).
- * <p>
- * TinkerPop's current default implementation uses priority value zero.
- * <p>
- * Third-party implementations of this interface should (but are not technically required)
- * to use a priority value with absolute value greater than 100.
- * <p>
- * The implementation currently breaks priority ties by lexicographical comparison of
- * fully-qualified package-and-classname, but this tie-breaking behavior should be
- * considered undefined and subject to future change. Ties are ignored if the service
- * is explicitly set through the system property mentioned above.
- *
- * @return this implementation's priority value
- */
- int getPriority();
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
deleted file mode 100644
index 1d5413d..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/KryoShimServiceLoader.java
+++ /dev/null
@@ -1,137 +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.structure.io.kryoshim;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.ServiceLoader;
-
-public class KryoShimServiceLoader {
-
- private static volatile KryoShimService CACHED_SHIM_SERVICE;
-
- private static final Logger log = LoggerFactory.getLogger(KryoShimServiceLoader.class);
-
- /**
- * Set this system property to the fully-qualified name of a {@link KryoShimService}
- * package-and-classname to force it into service. Setting this property causes the
- * priority-selection mechanism ({@link KryoShimService#getPriority()}) to be ignored.
- */
- public static final String SHIM_CLASS_SYSTEM_PROPERTY = "tinkerpop.kryo.shim";
-
- public static KryoShimService load(boolean forceReload) {
-
- if (null != CACHED_SHIM_SERVICE && !forceReload) {
- return CACHED_SHIM_SERVICE;
- }
-
- ArrayList<KryoShimService> services = new ArrayList<>();
-
- ServiceLoader<KryoShimService> sl = ServiceLoader.load(KryoShimService.class);
-
- KryoShimService result = null;
-
- synchronized (KryoShimServiceLoader.class) {
- if (forceReload) {
- sl.reload();
- }
-
- for (KryoShimService kss : sl) {
- services.add(kss);
- }
- }
-
- String shimClass = System.getProperty(SHIM_CLASS_SYSTEM_PROPERTY);
-
- if (null != shimClass) {
- for (KryoShimService kss : services) {
- if (kss.getClass().getCanonicalName().equals(shimClass)) {
- log.info("Set {} provider to {} ({}) from system property {}={}",
- KryoShimService.class.getSimpleName(), kss, kss.getClass(),
- SHIM_CLASS_SYSTEM_PROPERTY, shimClass);
- result = kss;
- }
- }
- } else {
- Collections.sort(services, KryoShimServiceComparator.INSTANCE);
-
- for (KryoShimService kss : services) {
- log.debug("Found Kryo shim service class {} (priority {})", kss.getClass(), kss.getPriority());
- }
-
- if (0 != services.size()) {
- result = services.get(services.size() - 1);
- }
- }
-
-
- if (null == result) {
- throw new IllegalStateException("Unable to load KryoShimService");
- }
-
- log.info("Set {} provider to {} ({}) because its priority value ({}) is the highest available",
- KryoShimService.class.getSimpleName(), result, result.getClass(), result.getPriority());
-
- return CACHED_SHIM_SERVICE = result;
- }
-
- public static KryoShimService load() {
- return load(false);
- }
-
- public static byte[] writeClassAndObjectToBytes(Object o) {
- KryoShimService shimService = load();
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- shimService.writeClassAndObject(o, baos);
-
- return baos.toByteArray();
- }
-
- public static <T> T readClassAndObject(InputStream source) {
- KryoShimService shimService = load();
-
- return (T)shimService.readClassAndObject(source);
- }
-
- private enum KryoShimServiceComparator implements Comparator<KryoShimService> {
- INSTANCE;
-
- @Override
- public int compare(KryoShimService a, KryoShimService b) {
- int ap = a.getPriority();
- int bp = b.getPriority();
-
- if (ap < bp) {
- return -1;
- } else if (bp < ap) {
- return 1;
- } else {
- return a.getClass().getCanonicalName().compareTo(b.getClass().getCanonicalName());
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
deleted file mode 100644
index 4468434..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/OutputShim.java
+++ /dev/null
@@ -1,41 +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.structure.io.kryoshim;
-
-import java.io.IOException;
-
-/**
- * A minimal {@link org.apache.tinkerpop.shaded.kryo.io.Output}-like abstraction.
- */
-public interface OutputShim {
-
- void writeByte(byte b);
-
- void writeBytes(byte[] array, int offset, int count);
-
- void writeString(String s);
-
- void writeLong(long l);
-
- void writeInt(int i);
-
- void writeDouble(double d);
-
- void flush();
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
deleted file mode 100644
index 686350d..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/SerializerShim.java
+++ /dev/null
@@ -1,35 +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.structure.io.kryoshim;
-
-/**
- * A minimal {@link org.apache.tinkerpop.shaded.kryo.Serializer}-like abstraction.
- *
- * @param <T> the class this serializer reads/writes from/to bytes.
- */
-public interface SerializerShim<T> {
-
- <O extends OutputShim> void write(KryoShim<?, O> kryo, O output, T starGraph);
-
- <I extends InputShim> T read(KryoShim<I, ?> kryo, I input, Class<T> clazz);
-
- default boolean isImmutable() {
- return false;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
deleted file mode 100644
index 436f117..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/package-info.java
+++ /dev/null
@@ -1,54 +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.
- */
-/**
- * Abstracts a minimal subset of Kryo types and methods.
- * <p>
- * Kryo is often shaded. For instance, TinkerPop's Gryo
- * serializer relies on a shaded Kryo package.
- * TinkerPop serializers written against a particular shaded
- * Kryo package (or the unshaded Kryo package) are compatible
- * only with that package. In contrast, TinkerPop serializers written
- * against this abstraction can be used with any shaded or
- * unshaded Kryo package, so long as the signatures and behavior
- * of the methods in this package remain stable.
- * <p>
- * To show how this is useful, consider
- * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraphSerializer}.
- * This class has logic unique to TinkerPop that performs
- * efficient and forward-compatible serialization of
- * {@link org.apache.tinkerpop.gremlin.structure.util.star.StarGraph}
- * instances. It takes advantage of package-level visibility
- * and the fact that it shares a package with its target,
- * so it would be challenging to cleanly and naturally replicate
- * (i.e. without package spoofing or runtime visibility overrides).
- * By implementing
- * {@link org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim}
- * instead of, say, Gryo's shaded
- * {@link org.apache.tinkerpop.shaded.kryo.Serializer},
- * it can be used with anybody's Kryo package, regardless of whether
- * that package is shaded or not. This lets third-parties reuse
- * TinkerPop's efficient, internals-aware StarGraph serializer on
- * their own serialization platform (and without altering
- * TinkerPop's bytecode, let alone its source).
- * <p>
- * The number of types and methods in this
- * package is deliberately small to reduce the likelihood of a
- * new Kryo release introducing an incompatible change.
- */
-package org.apache.tinkerpop.gremlin.structure.io.kryoshim;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
deleted file mode 100644
index 2872326..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedInputAdapter.java
+++ /dev/null
@@ -1,66 +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.structure.io.kryoshim.shaded;
-
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.InputShim;
-import org.apache.tinkerpop.shaded.kryo.io.Input;
-
-public class ShadedInputAdapter implements InputShim {
-
- private final Input shadedInput;
-
- public ShadedInputAdapter(Input shadedInput) {
- this.shadedInput = shadedInput;
- }
-
- Input getShadedInput() {
- return shadedInput;
- }
-
- @Override
- public byte readByte()
- {
- return shadedInput.readByte();
- }
-
- @Override
- public byte[] readBytes(int size) {
- return shadedInput.readBytes(size);
- }
-
- @Override
- public String readString() {
- return shadedInput.readString();
- }
-
- @Override
- public long readLong() {
- return shadedInput.readLong();
- }
-
- @Override
- public int readInt() {
- return shadedInput.readInt();
- }
-
- @Override
- public double readDouble() {
- return shadedInput.readDouble();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
deleted file mode 100644
index 0e85f6f..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedKryoAdapter.java
+++ /dev/null
@@ -1,67 +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.structure.io.kryoshim.shaded;
-
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.KryoShim;
-import org.apache.tinkerpop.shaded.kryo.Kryo;
-
-public class ShadedKryoAdapter implements KryoShim<ShadedInputAdapter, ShadedOutputAdapter> {
-
- private final Kryo shadedKryo;
-
- public ShadedKryoAdapter(Kryo shadedKryo) {
- this.shadedKryo = shadedKryo;
- }
-
- @Override
- public <T> T readObject(ShadedInputAdapter input, Class<T> type)
- {
- return shadedKryo.readObject(input.getShadedInput(), type);
- }
-
- @Override
- public Object readClassAndObject(ShadedInputAdapter input)
- {
- return shadedKryo.readClassAndObject(input.getShadedInput());
- }
-
- @Override
- public void writeObject(ShadedOutputAdapter output, Object object)
- {
- shadedKryo.writeObject(output.getShadedOutput(), object);
- }
-
- @Override
- public void writeClassAndObject(ShadedOutputAdapter output, Object object)
- {
- shadedKryo.writeClassAndObject(output.getShadedOutput(), object);
- }
-
- @Override
- public <T> T readObjectOrNull(ShadedInputAdapter input, Class<T> type)
- {
- return shadedKryo.readObjectOrNull(input.getShadedInput(), type);
- }
-
- @Override
- public void writeObjectOrNull(ShadedOutputAdapter output, Object object, Class type)
- {
- shadedKryo.writeObjectOrNull(output.getShadedOutput(), object, type);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
deleted file mode 100644
index 5ca0ecb..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedOutputAdapter.java
+++ /dev/null
@@ -1,72 +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.structure.io.kryoshim.shaded;
-
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.OutputShim;
-import org.apache.tinkerpop.shaded.kryo.io.Output;
-
-public class ShadedOutputAdapter implements OutputShim {
-
- private final Output shadedOutput;
-
- public ShadedOutputAdapter(Output shadedOutput) {
- this.shadedOutput = shadedOutput;
- }
-
- @Override
- public void writeByte(byte b)
- {
- shadedOutput.writeByte(b);
- }
-
- @Override
- public void writeBytes(byte[] array, int offset, int count) {
- shadedOutput.writeBytes(array, offset, count);
- }
-
- @Override
- public void writeString(String s) {
- shadedOutput.writeString(s);
- }
-
- @Override
- public void writeLong(long l) {
- shadedOutput.writeLong(l);
- }
-
- @Override
- public void writeInt(int i) {
- shadedOutput.writeInt(i);
- }
-
- @Override
- public void writeDouble(double d) {
- shadedOutput.writeDouble(d);
- }
-
- @Override
- public void flush() {
- shadedOutput.flush();
- }
-
- Output getShadedOutput()
- {
- return shadedOutput;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
deleted file mode 100644
index ebfd0fb..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/ShadedSerializerAdapter.java
+++ /dev/null
@@ -1,54 +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.structure.io.kryoshim.shaded;
-
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.SerializerShim;
-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;
-
-public class ShadedSerializerAdapter<T> extends Serializer<T> {
-
- SerializerShim<T> serializer;
-
- public ShadedSerializerAdapter(SerializerShim<T> serializer) {
- this.serializer = serializer;
- setImmutable(this.serializer.isImmutable());
- }
-
- @Override
- public void write(Kryo kryo, Output output, T t) {
- /* These adapters could be cached pretty efficiently in instance fields if it were guaranteed that this
- * class was never subject to concurrent use. That's true of Kryo instances, but it is not clear that
- * it is true of Serializer instances.
- */
- ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
- ShadedOutputAdapter shadedOutputAdapter = new ShadedOutputAdapter(output);
- serializer.write(shadedKryoAdapter, shadedOutputAdapter, t);
- }
-
- @Override
- public T read(Kryo kryo, Input input, Class<T> aClass) {
- // Same caching opportunity as in write(...)
- ShadedKryoAdapter shadedKryoAdapter = new ShadedKryoAdapter(kryo);
- ShadedInputAdapter shadedInputAdapter = new ShadedInputAdapter(input);
- return serializer.read(shadedKryoAdapter, shadedInputAdapter, aClass);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
deleted file mode 100644
index ea70003..0000000
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/kryoshim/shaded/package-info.java
+++ /dev/null
@@ -1,25 +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.
- */
-
-/**
- * Implementations of the interfaces in
- * {@link org.apache.tinkerpop.gremlin.structure.io.kryoshim}
- * using TinkerPop's shaded copy of Kryo.
- */
-package org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/218d7909/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
index 2acd770..b2379ce 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphGryoSerializer.java
@@ -23,14 +23,11 @@ import java.util.Map;
import org.apache.tinkerpop.gremlin.process.computer.GraphFilter;
import org.apache.tinkerpop.gremlin.structure.Direction;
-import org.apache.tinkerpop.gremlin.structure.io.kryoshim.shaded.ShadedSerializerAdapter;
+import org.apache.tinkerpop.gremlin.structure.io.gryo.kryoshim.shaded.ShadedSerializerAdapter;
/**
- * Kryo serializer for {@link StarGraph}. Implements an internal versioning capability for backward compatibility.
- * The single byte at the front of the serialization stream denotes the version. That version can be used to choose
- * the correct deserialization mechanism. The limitation is that this versioning won't help with backward
- * compatibility for custom serializers from providers. Providers should be encouraged to write their serializers
- * with backward compatibility in mind.
+ * A wrapper for {@link StarGraphSerializer} that makes it compatible with TinkerPop's
+ * shaded Kryo.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Stephen Mallette (http://stephen.genoprime.com)