You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ke...@apache.org on 2023/03/02 22:20:37 UTC

[tinkerpop] branch 3.5-dev updated: TINKERPOP-2526 Add imports to the Console via an ImportCustomizer.

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

kenhuuu pushed a commit to branch 3.5-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/3.5-dev by this push:
     new 18173ec63f TINKERPOP-2526 Add imports to the Console via an ImportCustomizer.
     new 0b4a9d170d Merge pull request #1983 from Bit-Quill/ken/groovy
18173ec63f is described below

commit 18173ec63fd5c201eceb643349047a7f82cd9d04
Author: Ken Hu <10...@users.noreply.github.com>
AuthorDate: Thu Feb 23 11:40:15 2023 -0800

    TINKERPOP-2526 Add imports to the Console via an ImportCustomizer.
    
    Adding imports by providing the CompilerConfiguration with an
    ImportCustomizer greatly improves performance because the imports
    won't be resolved again for each complete line in a script.
---
 CHANGELOG.asciidoc                                         |  1 +
 .../org/apache/tinkerpop/gremlin/console/Console.groovy    | 10 ++++++----
 .../tinkerpop/gremlin/console/GremlinGroovysh.groovy       |  2 ++
 .../org/apache/tinkerpop/gremlin/console/PluggedIn.groovy  | 14 +++++++++++---
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 9f3b3f044d..64c1470f49 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -30,6 +30,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 * Added `SubmitWithOptions()` methods to `Client` and `DriverRemoteConnection` in Go GLV to pass `RequestOptions` to the server.
 * Fixed bug in which `gremlin-server` would not respond to clients if an `Error` was thrown during bytecode traversals.
 * Changed `with()` configuration for `ARGS_BATCH_SIZE` and `ARGS_EVAL_TIMEOUT` to be more forgiving on the type of `Number` used for the value.
+* Changed `gremlin-console` to add imports via an ImportCustomizer to reduce time spent resolving imports.
 
 [[release-3-5-5]]
 === TinkerPop 3.5.5 (Release Date: January 16, 2023)
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
index c47bbf3eb6..7e997e1af6 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
@@ -113,11 +113,13 @@ class Console {
         // hide output temporarily while imports execute
         showShellEvaluationOutput(false)
 
+        org.codehaus.groovy.control.customizers.ImportCustomizer ic = new org.codehaus.groovy.control.customizers.ImportCustomizer()
         def imports = (ImportCustomizer) CoreGremlinPlugin.instance().getCustomizers("gremlin-groovy").get()[0]
-        imports.getClassPackages().collect { Mediator.IMPORT_SPACE + it.getName() + Mediator.IMPORT_WILDCARD }.each { groovy.execute(it) }
-        imports.getMethodClasses().collect { Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each{ groovy.execute(it) }
-        imports.getEnumClasses().collect { Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each{ groovy.execute(it) }
-        imports.getFieldClasses().collect { Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each{ groovy.execute(it) }
+        ic.addStarImports(imports.getClassPackages().collect() { it.getName() }.toArray(new String[0]))
+        ic.addStaticStars(imports.getMethodClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
+        ic.addStaticStars(imports.getEnumClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
+        ic.addStaticStars(imports.getFieldClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
+        groovy.getCompilerConfiguration().addCompilationCustomizers(ic)
 
         final InteractiveShellRunner runner = new InteractiveShellRunner(groovy, handlePrompt)
         runner.reader.setHandleUserInterrupt(false)
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
index 96015db5f6..978223766b 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/GremlinGroovysh.groovy
@@ -46,6 +46,8 @@ class GremlinGroovysh extends Groovysh {
         this.mediator = mediator
     }
 
+    public CompilerConfiguration getCompilerConfiguration() { return compilerConfig }
+
     protected List parseLine(final String line) {
         assert line != null
         return line.trim().tokenize()
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/PluggedIn.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/PluggedIn.groovy
index fb87f791bf..5fc7d9ab70 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/PluggedIn.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/PluggedIn.groovy
@@ -58,9 +58,17 @@ class PluggedIn {
     void activate() {
         plugin.getCustomizers("gremlin-groovy").get().each {
             if (it instanceof ImportCustomizer) {
-                it.getClassPackages().collect {Mediator.IMPORT_SPACE + it.getName() + Mediator.IMPORT_WILDCARD }.each { shell.execute(it) }
-                it.getMethodClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each {shell.execute(it)}
-                it.getEnumClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each {shell.execute(it)}
+                if (shell instanceof GremlinGroovysh) {
+                    org.codehaus.groovy.control.customizers.ImportCustomizer ic = new org.codehaus.groovy.control.customizers.ImportCustomizer()
+                    ic.addStarImports(it.getClassPackages().collect() { it.getName() }.toArray(new String[0]))
+                    ic.addStaticStars(it.getMethodClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
+                    ic.addStaticStars(it.getEnumClasses().collect() { it.getCanonicalName() }.toArray(new String[0]))
+                    ((GremlinGroovysh) shell).getCompilerConfiguration().addCompilationCustomizers(ic)
+                } else {
+                    it.getClassPackages().collect {Mediator.IMPORT_SPACE + it.getName() + Mediator.IMPORT_WILDCARD }.each { shell.execute(it) }
+                    it.getMethodClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each {shell.execute(it)}
+                    it.getEnumClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName() + Mediator.IMPORT_WILDCARD}.each {shell.execute(it)}
+                }
             } else if (it instanceof ScriptCustomizer) {
                 it.getScripts().collect { it.join(LINE_SEPARATOR) }.each { shell.execute(it) }
             } else if (it instanceof BindingsCustomizer) {