This is an automated email from the ASF dual-hosted git repository.

valentyn pushed a commit to branch valentyn/bytecode-poc
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 94945faf4f1a4109f99e5b4b9a647660b4d7db2c
Author: Valentyn Kahamlyk <valentyn.kaham...@improving.com>
AuthorDate: Fri May 24 14:05:43 2024 -0700

    draft
---
 .../gremlin/process/traversal/Bytecode.java        | 34 ++++++++++++++++++++
 .../gremlin/process/traversal/BytecodeV4test.java  | 36 ++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
index 5b88cea5fc..37535765df 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Bytecode.java
@@ -54,10 +54,40 @@ public class Bytecode implements Cloneable, Serializable {
     private List<Instruction> sourceInstructions = new ArrayList<>();
     private List<Instruction> stepInstructions = new ArrayList<>();
 
+    private final StringBuilder gremlin = new StringBuilder();
+
     public Bytecode() {}
 
     public Bytecode(final String sourceName, final Object... arguments) {
         this.sourceInstructions.add(new Instruction(sourceName, 
flattenArguments(arguments)));
+        addToGremlin(sourceName, arguments);
+    }
+
+    private void addToGremlin(final String name, final Object... arguments) {
+        gremlin.append(".").append(name).append('(');
+
+        final Object[] flattenedArguments = flattenArguments(arguments);
+        for (int i = 0; i < flattenedArguments.length; i++) {
+            if (i != 0) {
+                gremlin.append(',');
+            }
+            gremlin.append(argAsString(flattenedArguments[i]));
+        }
+
+        gremlin.append(')');
+    }
+
+    private String argAsString(final Object arg) {
+        if (arg instanceof String)
+            return "\"" + arg + "\"";
+        if (arg instanceof Integer)
+            return arg.toString();
+
+        return null;
+    }
+
+    public String getGremlin(final String g) {
+        return g + gremlin;
     }
 
     /**
@@ -82,6 +112,8 @@ public class Bytecode implements Cloneable, Serializable {
         } else
             this.sourceInstructions.add(new Instruction(sourceName, 
flattenArguments(arguments)));
         Bindings.clear();
+
+        addToGremlin(sourceName, arguments);
     }
 
     /**
@@ -92,6 +124,8 @@ public class Bytecode implements Cloneable, Serializable {
      */
     public void addStep(final String stepName, final Object... arguments) {
         this.stepInstructions.add(new Instruction(stepName, 
flattenArguments(arguments)));
+
+        addToGremlin(stepName, arguments);
     }
 
     /**
diff --git 
a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeV4test.java
 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeV4test.java
new file mode 100644
index 0000000000..dbc66b8a7d
--- /dev/null
+++ 
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/BytecodeV4test.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.process.traversal;
+
+import 
org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class BytecodeV4test {
+
+    @Test
+    public void shouldWork() {
+        assertEquals("g.V().count()",
+                
EmptyGraph.instance().traversal().V().count().asAdmin().getBytecode().getGremlin("g"));
+        assertEquals("g.addV(\"test\")",
+                
EmptyGraph.instance().traversal().addV("test").asAdmin().getBytecode().getGremlin("g"));
+    }
+}

Reply via email to