tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ok...@apache.org
Subject [03/30] tinkerpop git commit: Convert to wildcard imports for the console
Date Tue, 28 Feb 2017 19:05:49 GMT
Convert to wildcard imports for the console

The console, under the revised plugin system and CoreImports, took a massive performance hit
by not using wildcard imports. Imports are appended to each line executed in the console and
that seems to have some significant expense in execution. It wouldn't be so bad as it was
(maybe), but the doc generation process was very expensive since every line of the docs is
passed through the console when there is source code to be processed there and it slowed doc
generation down to 7x the previous speed. I imagine other automation use cases around the
console would have been affected as well. CTR


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/82147d67
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/82147d67
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/82147d67

Branch: refs/heads/TINKERPOP-1640
Commit: 82147d670c28a67dd318285db7fee1d633296147
Parents: e06dc21
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Fri Feb 24 09:21:21 2017 -0500
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Fri Feb 24 09:21:21 2017 -0500

----------------------------------------------------------------------
 .../tinkerpop/gremlin/console/Console.groovy    | 12 ++++----
 .../tinkerpop/gremlin/console/Mediator.groovy   |  3 ++
 .../gremlin/console/plugin/PluggedIn.groovy     |  7 +++--
 .../groovy/plugin/GremlinPluginAdapterTest.java | 11 +++----
 .../gremlin/jsr223/ImportCustomizer.java        | 31 ++++++++++++++++++++
 .../jsr223/DefaultImportCustomizerTest.java     |  7 +++++
 6 files changed, 54 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
----------------------------------------------------------------------
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 0788f42..be71cb0 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
@@ -57,8 +57,6 @@ class Console {
         Colorizer.installAnsi()
     }
 
-    private static final String IMPORT_SPACE = "import "
-    private static final String IMPORT_STATIC_SPACE = "import static "
     private static final String ELLIPSIS = "..."
 
     private Iterator tempIterator = Collections.emptyIterator()
@@ -107,15 +105,15 @@ class Console {
 
         if (Mediator.useV3d3) {
             def imports = (ImportCustomizer) CoreGremlinPlugin.instance().getCustomizers("gremlin-groovy").get()[0]
-            imports.classImports.collect { IMPORT_SPACE + it.canonicalName }.each { groovy.execute(it)
}
-            imports.methodImports.collect { IMPORT_STATIC_SPACE + it.getDeclaringClass().getCanonicalName()
+ "." + it.name}.each{ groovy.execute(it) }
-            imports.enumImports.collect { IMPORT_STATIC_SPACE + it.getDeclaringClass().getCanonicalName()
+ "." + it.name()}.each{ groovy.execute(it) }
+            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) }
         } else {
             // add the default imports
             new ConsoleImportCustomizerProvider().getCombinedImports().stream()
-                    .collect { IMPORT_SPACE + it }.each { groovy.execute(it) }
+                    .collect { Mediator.IMPORT_SPACE + it }.each { groovy.execute(it) }
             new ConsoleImportCustomizerProvider().getCombinedStaticImports().stream()
-                    .collect { IMPORT_STATIC_SPACE + it }.each { groovy.execute(it) }
+                    .collect { Mediator.IMPORT_STATIC_SPACE + it }.each { groovy.execute(it)
}
         }
 
         final InteractiveShellRunner runner = new InteractiveShellRunner(groovy, handlePrompt)

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
index 18e8d58..74f956d 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Mediator.groovy
@@ -34,6 +34,9 @@ class Mediator {
 
     private static String LINE_SEP = System.getProperty("line.separator")
 
+    public static final String IMPORT_SPACE = "import "
+    public static final String IMPORT_STATIC_SPACE = "import static "
+    public static final String IMPORT_WILDCARD = ".*"
     public static final boolean useV3d3 = System.getProperty("plugins", "v3d2") == "v3d3"
 
     public Mediator(final Console console) {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/plugin/PluggedIn.groovy
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/plugin/PluggedIn.groovy
b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/plugin/PluggedIn.groovy
index 364e6ef..3012caf 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/plugin/PluggedIn.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/plugin/PluggedIn.groovy
@@ -18,6 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.console.plugin
 
+import org.apache.tinkerpop.gremlin.console.Mediator
 import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin
 import org.apache.tinkerpop.gremlin.groovy.plugin.IllegalEnvironmentException
 import org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor
@@ -88,9 +89,9 @@ class PluggedIn {
             corePlugin.getCustomizers("gremlin-groovy").get().each {
                 if (it instanceof ImportCustomizer) {
                     def imports = [] as Set
-                    it.classImports.each { imports.add("import " + it.canonicalName )}
-                    it.methodImports.each { imports.add("import static " + it.declaringClass.canonicalName
+ "." + it.name) }
-                    it.enumImports.each { imports.add("import static " + it.declaringClass.canonicalName
+ "." + it.name()) }
+                    it.getClassPackages().collect {Mediator.IMPORT_SPACE + it.getName() +
Mediator.IMPORT_WILDCARD }.each { imports.add(it) }
+                    it.getMethodClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName()
+ Mediator.IMPORT_WILDCARD}.each {imports.add(it)}
+                    it.getEnumClasses().collect {Mediator.IMPORT_STATIC_SPACE + it.getCanonicalName()
+ Mediator.IMPORT_WILDCARD}.each {imports.add(it)}
                     pluginAcceptor.addImports(imports)
                 } else if (it instanceof ScriptCustomizer) {
                     it.getScripts().collect { it.join(LINE_SEPARATOR) }.each { pluginAcceptor.eval(it)
}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/GremlinPluginAdapterTest.java
----------------------------------------------------------------------
diff --git a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/GremlinPluginAdapterTest.java
b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/GremlinPluginAdapterTest.java
index cb6fa60..58b1d39 100644
--- a/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/GremlinPluginAdapterTest.java
+++ b/gremlin-console/src/test/java/org/apache/tinkerpop/gremlin/console/groovy/plugin/GremlinPluginAdapterTest.java
@@ -65,13 +65,10 @@ public class GremlinPluginAdapterTest {
         adapter.pluginTo(spy);
 
         final Set<String> imports = spy.getImports();
-        assertEquals(6, imports.size());
-        assertThat(imports, hasItems("import " + java.awt.Color.class.getCanonicalName()));
-        assertThat(imports, hasItems("import " + java.sql.CallableStatement.class.getCanonicalName()));
-        assertThat(imports, hasItems("import static " + DayOfWeek.class.getCanonicalName()
+ "." + DayOfWeek.SATURDAY.name()));
-        assertThat(imports, hasItems("import static " + DayOfWeek.class.getCanonicalName()
+ "." + DayOfWeek.SUNDAY.name()));
-        assertThat(imports, hasItems("import static " + DayOfWeek.class.getCanonicalName()
+ ".from"));
-        assertThat(imports, hasItems("import static " + DayOfWeek.class.getCanonicalName()
+ ".values"));
+        assertEquals(3, imports.size());
+        assertThat(imports, hasItems("import " + java.awt.Color.class.getPackage().getName()
+ ".*",
+                                     "import " + java.sql.CallableStatement.class.getPackage().getName()
+ ".*",
+                                     "import static " + DayOfWeek.class.getCanonicalName()
+ ".*"));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ImportCustomizer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ImportCustomizer.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ImportCustomizer.java
index 54c5d0c..d332df8 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ImportCustomizer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ImportCustomizer.java
@@ -20,6 +20,7 @@ package org.apache.tinkerpop.gremlin.jsr223;
 
 import java.lang.reflect.Method;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Provides the list of imports to apply to a {@link GremlinScriptEngine} instance.
@@ -36,9 +37,39 @@ public interface ImportCustomizer extends Customizer {
             .addEnumImports(CoreImports.getEnumImports())
             .addMethodImports(CoreImports.getMethodImports()).create();
 
+    /**
+     * Gets the set of classes to be imported to the {@link GremlinScriptEngine}.
+     */
     public Set<Class> getClassImports();
 
+    /**
+     * Gets the set of static methods to be imported to the {@link GremlinScriptEngine}.
+     */
     public Set<Method> getMethodImports();
 
+    /**
+     * Gets the set of enums to be imported to the {@link GremlinScriptEngine}.
+     */
     public Set<Enum> getEnumImports();
+
+    /**
+     * Gets the set of packages from the {@link #getClassImports()}.
+     */
+    public default Set<Package> getClassPackages() {
+        return getClassImports().stream().map(Class::getPackage).collect(Collectors.toSet());
+    }
+
+    /**
+     * Gets the set of classes from the {@link #getMethodImports()}.
+     */
+    public default Set<Class> getMethodClasses() {
+        return getMethodImports().stream().map(Method::getDeclaringClass).collect(Collectors.toSet());
+    }
+
+    /**
+     * Gets the set of classes from the {@link #getEnumImports()}.
+     */
+    public default Set<Class> getEnumClasses() {
+        return getEnumImports().stream().map(Enum::getDeclaringClass).collect(Collectors.toSet());
+    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/82147d67/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
index c010aa5..e480b7d 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/jsr223/DefaultImportCustomizerTest.java
@@ -25,6 +25,7 @@ import java.lang.reflect.Method;
 import java.time.DayOfWeek;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.stream.Collectors;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.core.IsCollectionContaining.hasItems;
@@ -46,12 +47,18 @@ public class DefaultImportCustomizerTest {
 
         assertEquals(2, imports.getClassImports().size());
         assertThat(imports.getClassImports(), hasItems(java.awt.Color.class, java.awt.AlphaComposite.class));
+        assertEquals(1, imports.getClassPackages().size());
+        assertThat(imports.getClassPackages(), hasItems(java.awt.Color.class.getPackage()));
 
         assertEquals(1, imports.getMethodImports().size());
         assertThat(imports.getMethodImports(), hasItems(abs));
+        assertEquals(1, imports.getMethodClasses().size());
+        assertThat(imports.getMethodClasses(), hasItems(Math.class));
 
         assertEquals(2, imports.getEnumImports().size());
         assertThat(imports.getEnumImports(), hasItems(dayOfWeekEnum, tEnum));
+        assertEquals(2, imports.getEnumClasses().size());
+        assertThat(imports.getEnumClasses(), hasItems(T.class, DayOfWeek.class));
     }
 
     @Test


Mime
View raw message