This is an automated email from the ASF dual-hosted git repository.
jorgebg pushed a commit to branch TINKERPOP-1942
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/TINKERPOP-1942 by this push:
new 7f9f734 Add serializer for TraversalExplanation
7f9f734 is described below
commit 7f9f734f481186e4bee723edb40d45aa8c180fda
Author: Jorge Bay Gondra <[email protected]>
AuthorDate: Fri Dec 21 16:59:12 2018 +0100
Add serializer for TraversalExplanation
---
.../driver/ser/binary/TypeSerializerRegistry.java | 4 +-
.../types/TraversalExplanationSerializer.java | 85 ++++++++++++++++++++++
2 files changed, 88 insertions(+), 1 deletion(-)
diff --git
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistry.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistry.java
index 2f0b95c..c270a41 100644
---
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistry.java
+++
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/TypeSerializerRegistry.java
@@ -37,6 +37,7 @@ import
org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.process.traversal.util.AndP;
import org.apache.tinkerpop.gremlin.process.traversal.util.Metrics;
import org.apache.tinkerpop.gremlin.process.traversal.util.OrP;
+import
org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation;
import org.apache.tinkerpop.gremlin.structure.Column;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
@@ -135,8 +136,9 @@ public class TypeSerializerRegistry {
new RegistryEntry<>(Tree.class, new TreeSerializer()),
new RegistryEntry<>(Metrics.class, new MetricsSerializer()),
- // MapEntrySerializer is a TransformSerializer implementation
+ // TransformSerializer implementations
new RegistryEntry<>(Map.Entry.class, new MapEntrySerializer()),
+ new RegistryEntry<>(TraversalExplanation.class, new
TraversalExplanationSerializer()),
new RegistryEntry<>(Character.class, new CharSerializer()),
new RegistryEntry<>(Duration.class, new DurationSerializer()),
diff --git
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/TraversalExplanationSerializer.java
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/TraversalExplanationSerializer.java
new file mode 100644
index 0000000..dded5b6
--- /dev/null
+++
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/TraversalExplanationSerializer.java
@@ -0,0 +1,85 @@
+/*
+ * 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.driver.ser.binary.types;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import org.apache.tinkerpop.gremlin.driver.ser.SerializationException;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.GraphBinaryReader;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.GraphBinaryWriter;
+import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
+import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategy;
+import
org.apache.tinkerpop.gremlin.process.traversal.util.TraversalExplanation;
+import org.javatuples.Pair;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class TraversalExplanationSerializer extends
SimpleTypeSerializer<TraversalExplanation> implements
TransformSerializer<TraversalExplanation> {
+ private static final String ORIGINAL = "original";
+ private static final String FINAL = "final";
+ private static final String INTERMEDIATE = "intermediate";
+ private static final String CATEGORY = "category";
+ private static final String TRAVERSAL = "traversal";
+ private static final String STRATEGY = "strategy";
+
+ public TraversalExplanationSerializer() {
+ super(null);
+ }
+
+ @Override
+ TraversalExplanation readValue(ByteBuf buffer, GraphBinaryReader context)
throws SerializationException {
+ throw new SerializationException("A TraversalExplanation should not be
read individually");
+ }
+
+ @Override
+ public ByteBuf writeValue(TraversalExplanation value, ByteBufAllocator
allocator, GraphBinaryWriter context) throws SerializationException {
+ throw new SerializationException("A TraversalExplanation should not be
written individually");
+ }
+
+ /**
+ * Creates a Map containing "original", "intermediate" and "final" keys.
+ */
+ @Override
+ public Object transform(TraversalExplanation value) {
+ final Map<String, Object> result = new HashMap<>();
+ result.put(ORIGINAL, getTraversalSteps(value.getOriginalTraversal()));
+
+ final List<Pair<TraversalStrategy, Traversal.Admin<?, ?>>>
strategyTraversals = value.getStrategyTraversals();
+
+ result.put(INTERMEDIATE,
+ strategyTraversals.stream().map(pair -> {
+ final Map<String, Object> item = new HashMap<>();
+ item.put(STRATEGY, pair.getValue0().toString());
+ item.put(CATEGORY,
pair.getValue0().getTraversalCategory().getSimpleName());
+ item.put(TRAVERSAL, getTraversalSteps(pair.getValue1()));
+ return item;
+ }).collect(Collectors.toList()));
+
+ result.put(FINAL, getTraversalSteps(strategyTraversals.isEmpty()
+ ? value.getOriginalTraversal() :
strategyTraversals.get(strategyTraversals.size() - 1).getValue1()));
+ return result;
+ }
+
+ private static List<String> getTraversalSteps(final Traversal.Admin<?, ?>
t) {
+ return
t.getSteps().stream().map(Object::toString).collect(Collectors.toList());
+ }
+}