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 2019/03/19 20:36:59 UTC
[tinkerpop] branch tp4 updated: a better explain() model using
DecorationStrategy.
This is an automated email from the ASF dual-hosted git repository.
okram pushed a commit to branch tp4
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/tp4 by this push:
new 33d48d2 a better explain() model using DecorationStrategy.
33d48d2 is described below
commit 33d48d21d82d5b772a96fd00fc3ab7e596d01c7f
Author: Marko A. Rodriguez <ok...@gmail.com>
AuthorDate: Tue Mar 19 14:36:49 2019 -0600
a better explain() model using DecorationStrategy.
---
.../tinkerpop/language/gremlin/Traversal.java | 2 +
.../tinkerpop/machine/bytecode/Compilation.java | 20 ++---
.../machine/processor/ExplainProcessor.java | 82 -------------------
.../strategy/decoration/ExplainStrategy.java | 92 ++++++++++++++++++++++
.../machine/processor/pipes/AbstractStep.java | 2 +-
5 files changed, 100 insertions(+), 98 deletions(-)
diff --git a/java/language/gremlin/src/main/java/org/apache/tinkerpop/language/gremlin/Traversal.java b/java/language/gremlin/src/main/java/org/apache/tinkerpop/language/gremlin/Traversal.java
index af444ec..6b1dd97 100644
--- a/java/language/gremlin/src/main/java/org/apache/tinkerpop/language/gremlin/Traversal.java
+++ b/java/language/gremlin/src/main/java/org/apache/tinkerpop/language/gremlin/Traversal.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.machine.bytecode.CoreCompiler.Symbols;
import org.apache.tinkerpop.machine.bytecode.Pred;
import org.apache.tinkerpop.machine.coefficient.Coefficient;
import org.apache.tinkerpop.machine.coefficient.LongCoefficient;
+import org.apache.tinkerpop.machine.strategy.decoration.ExplainStrategy;
import org.apache.tinkerpop.machine.traverser.Traverser;
import org.apache.tinkerpop.machine.traverser.path.Path;
@@ -116,6 +117,7 @@ public class Traversal<C, S, E> implements Iterator<E> {
}
public Traversal<C, S, String> explain() {
+ this.bytecode.addSourceInstruction(Symbols.WITH_STRATEGY, ExplainStrategy.class);
this.bytecode.addInstruction(this.currentCoefficient, Symbols.EXPLAIN);
return (Traversal) this;
}
diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Compilation.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Compilation.java
index 2e9b8df..397c53a 100644
--- a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Compilation.java
+++ b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/bytecode/Compilation.java
@@ -19,14 +19,12 @@
package org.apache.tinkerpop.machine.bytecode;
import org.apache.tinkerpop.machine.function.CFunction;
-import org.apache.tinkerpop.machine.processor.ExplainProcessor;
import org.apache.tinkerpop.machine.processor.FilterProcessor;
import org.apache.tinkerpop.machine.processor.LoopsProcessor;
import org.apache.tinkerpop.machine.processor.Processor;
import org.apache.tinkerpop.machine.processor.ProcessorFactory;
import org.apache.tinkerpop.machine.structure.EmptyStructure;
import org.apache.tinkerpop.machine.structure.StructureFactory;
-import org.apache.tinkerpop.machine.traverser.COTraverserFactory;
import org.apache.tinkerpop.machine.traverser.Traverser;
import org.apache.tinkerpop.machine.traverser.TraverserFactory;
@@ -50,19 +48,11 @@ public final class Compilation<C, S, E> implements Serializable {
private transient Processor<C, S, E> processor;
public Compilation(final Bytecode<C> bytecode) {
- if (bytecode.lastInstruction().op().equals(CoreCompiler.Symbols.EXPLAIN)) {
- this.processorFactory = new ExplainProcessor(bytecode);
- this.traverserFactory = null;
- this.functions = Collections.emptyList();
- this.structureFactory = null;
-
- } else {
- BytecodeUtil.strategize(bytecode);
- this.structureFactory = BytecodeUtil.getStructureFactory(bytecode).orElse(EmptyStructure.instance());
- this.processorFactory = BytecodeUtil.getProcessorFactory(bytecode).get();
- this.traverserFactory = BytecodeUtil.getTraverserFactory(bytecode).get();
- this.functions = CompositeCompiler.compile(bytecode, Arrays.asList(CoreCompiler.instance(), this.structureFactory.getCompiler().orElse(new CoreCompiler())));
- }
+ BytecodeUtil.strategize(bytecode);
+ this.structureFactory = BytecodeUtil.getStructureFactory(bytecode).orElse(EmptyStructure.instance());
+ this.processorFactory = BytecodeUtil.getProcessorFactory(bytecode).get();
+ this.traverserFactory = BytecodeUtil.getTraverserFactory(bytecode).get();
+ this.functions = CompositeCompiler.compile(bytecode, Arrays.asList(CoreCompiler.instance(), this.structureFactory.getCompiler().orElse(new CoreCompiler())));
}
public Compilation(final ProcessorFactory processorFactory) {
diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/processor/ExplainProcessor.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/processor/ExplainProcessor.java
deleted file mode 100644
index 23262d3..0000000
--- a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/processor/ExplainProcessor.java
+++ /dev/null
@@ -1,82 +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.machine.processor;
-
-import org.apache.tinkerpop.machine.bytecode.Bytecode;
-import org.apache.tinkerpop.machine.bytecode.BytecodeUtil;
-import org.apache.tinkerpop.machine.bytecode.CompositeCompiler;
-import org.apache.tinkerpop.machine.bytecode.CoreCompiler;
-import org.apache.tinkerpop.machine.coefficient.LongCoefficient;
-import org.apache.tinkerpop.machine.strategy.Strategy;
-import org.apache.tinkerpop.machine.structure.EmptyStructure;
-import org.apache.tinkerpop.machine.traverser.ShellTraverser;
-import org.apache.tinkerpop.machine.traverser.Traverser;
-
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * @author Marko A. Rodriguez (http://markorodriguez.com)
- */
-public final class ExplainProcessor extends SimpleProcessor<Long, String, String> {
-
- public ExplainProcessor(final Bytecode bytecode) {
- bytecode.getInstructions().remove(bytecode.getInstructions().size() - 1);
- super.traverser = new ShellTraverser<>(LongCoefficient.create(), ExplainProcessor.processBytecode(bytecode));
- }
-
- @Override
- public void addStart(final Traverser<Long, String> traverser) {
- throw new IllegalStateException("This shouldn't occur"); // TODO: exception handling system
- }
-
- private static String processBytecode(final Bytecode<Long> bytecode) {
- final Map<String, String> explain = new LinkedHashMap<>();
- explain.put("Original", bytecode.toString());
- for (final Strategy strategy : BytecodeUtil.getStrategies(bytecode)) {
- BytecodeUtil.strategize(bytecode, strategy);
- explain.put(strategy.toString(), bytecode.toString());
- }
- int maxLength = 0;
- for (final String key : explain.keySet()) {
- if (maxLength < key.length())
- maxLength = key.length();
- }
- final StringBuilder builder = new StringBuilder();
- for (final Map.Entry<String, String> entry : explain.entrySet()) {
- final int spaces = maxLength - entry.getKey().length();
- builder.append(entry.getKey());
- for (int i = 0; i < spaces; i++) {
- builder.append(" ");
- }
- builder.append("\t\t").append(entry.getValue()).append("\n");
- }
- final String functions = CompositeCompiler.compile(bytecode, Arrays.asList(CoreCompiler.instance(),
- BytecodeUtil.getStructureFactory(bytecode).orElse(EmptyStructure.instance()).getCompiler().orElse(new CoreCompiler()))).toString();
-
- builder.append("Final");
- for (int i = 0; i < maxLength - "Final".length(); i++) {
- builder.append(" ");
- }
- builder.append("\t\t").append(functions);
- return builder.toString();
-
- }
-}
diff --git a/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/strategy/decoration/ExplainStrategy.java b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/strategy/decoration/ExplainStrategy.java
new file mode 100644
index 0000000..44d7ded
--- /dev/null
+++ b/java/machine/machine-core/src/main/java/org/apache/tinkerpop/machine/strategy/decoration/ExplainStrategy.java
@@ -0,0 +1,92 @@
+/*
+ * 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.machine.strategy.decoration;
+
+import org.apache.tinkerpop.machine.bytecode.Bytecode;
+import org.apache.tinkerpop.machine.bytecode.BytecodeUtil;
+import org.apache.tinkerpop.machine.bytecode.Compilation;
+import org.apache.tinkerpop.machine.bytecode.CoreCompiler.Symbols;
+import org.apache.tinkerpop.machine.strategy.AbstractStrategy;
+import org.apache.tinkerpop.machine.strategy.Strategy;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @author Marko A. Rodriguez (http://markorodriguez.com)
+ */
+public final class ExplainStrategy extends AbstractStrategy<Strategy.DecorationStrategy> implements Strategy.DecorationStrategy {
+
+ private static final String ORIGINAL = "Original";
+ private static final String COMPILATION = "Compilation";
+ private static final String EXECUTION_PLAN = "Execution Plan";
+
+ @Override
+ public <C> void apply(final Bytecode<C> bytecode) {
+ if (bytecode.lastInstruction().op().equals(Symbols.EXPLAIN)) {
+ bytecode.getInstructions().remove(bytecode.lastInstruction());
+ final Bytecode<C> clone = bytecode.clone();
+ bytecode.getInstructions().clear();
+ bytecode.addInstruction(
+ BytecodeUtil.getCoefficient(clone).get(),
+ Symbols.INJECT,
+ ExplainStrategy.processBytecode(clone));
+ }
+ }
+
+ private static <C> String processBytecode(final Bytecode<C> bytecode) {
+ final Map<String, String> explain = new LinkedHashMap<>();
+ explain.put(ORIGINAL, bytecode.toString());
+ for (final Strategy strategy : BytecodeUtil.getStrategies(bytecode)) {
+ BytecodeUtil.strategize(bytecode, strategy);
+ explain.put(strategy.toString(), bytecode.toString());
+ }
+ final Compilation<C, ?, ?> compilation = Compilation.compile(bytecode);
+ explain.put(COMPILATION, compilation.getFunctions().toString());
+ explain.put(EXECUTION_PLAN + " [" + BytecodeUtil.getProcessorFactory(bytecode).get().getClass().getSimpleName() + "]", compilation.getProcessor().toString());
+
+ int maxLength = 0;
+ for (final String key : explain.keySet()) {
+ if (maxLength < key.length())
+ maxLength = key.length();
+ }
+ final StringBuilder builder = new StringBuilder();
+ for (final Map.Entry<String, String> entry : explain.entrySet()) {
+ if (entry.getKey().equals(COMPILATION)) {
+ for (int i = 0; i < maxLength; i++) {
+ builder.append("-");
+ }
+ builder.append("\n");
+ }
+ builder.append(entry.getKey());
+ ExplainStrategy.addSpaces(builder, entry.getKey(), maxLength);
+ builder.append("\t\t").append(entry.getValue()).append("\n");
+ }
+ builder.replace(builder.length() - 1, builder.length(), "");
+ return builder.toString();
+
+ }
+
+ private static void addSpaces(final StringBuilder builder, final String current, final int maxLength) {
+ final int spaces = maxLength - current.length();
+ for (int i = 0; i < spaces; i++) {
+ builder.append(" ");
+ }
+ }
+}
diff --git a/java/machine/processor/pipes/src/main/java/org/apache/tinkerpop/machine/processor/pipes/AbstractStep.java b/java/machine/processor/pipes/src/main/java/org/apache/tinkerpop/machine/processor/pipes/AbstractStep.java
index f1243f2..1967d2e 100644
--- a/java/machine/processor/pipes/src/main/java/org/apache/tinkerpop/machine/processor/pipes/AbstractStep.java
+++ b/java/machine/processor/pipes/src/main/java/org/apache/tinkerpop/machine/processor/pipes/AbstractStep.java
@@ -35,6 +35,6 @@ abstract class AbstractStep<C, S, E> implements Step<C, S, E> {
@Override
public String toString() {
- return this.function.toString();
+ return this.getClass().getSimpleName() + "[" + this.function.toString() + "]";
}
}