You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/03/30 20:27:19 UTC
incubator-tinkerpop git commit: Added support for :set max-iteration
in Gremlin Console as per TINKERPOP3-556
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/master 2806e787d -> ce5a16990
Added support for :set max-iteration in Gremlin Console as per TINKERPOP3-556
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ce5a1699
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ce5a1699
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ce5a1699
Branch: refs/heads/master
Commit: ce5a1699086f97fd2f656d4f94f8854053f2870c
Parents: 2806e78
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Mar 30 14:26:26 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Mar 30 14:26:26 2015 -0400
----------------------------------------------------------------------
CHANGELOG.asciidoc | 1 +
docs/src/gremlin-applications.asciidoc | 29 ++++++++
.../tinkerpop/gremlin/console/Console.groovy | 24 ++++++-
.../console/commands/GremlinSetCommand.groovy | 70 ++++++++++++++++++++
4 files changed, 121 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce5a1699/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 75f45a3..b55f219 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,6 +25,7 @@ image::http://www.tinkerpop.com/docs/current/images/gremlin-hindu.png[width=225]
TinkerPop 3.0.0.M8 (Release Date: NOT OFFICIALLY RELEASED YET)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+* Gremlin Console accepts a `max-iteration` configuration via the standard `:set` command to limit result iteration.
* `Vertex.property()` default behavior is now `Cardinality.single`.
* Added `ElementIdStrategy` as a `TraversalStrategy`.
* Introduce `AbstractTransaction` to simplify implementation of standard transactional features for vendors.
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce5a1699/docs/src/gremlin-applications.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/gremlin-applications.asciidoc b/docs/src/gremlin-applications.asciidoc
index 59baf8d..8cfb982 100644
--- a/docs/src/gremlin-applications.asciidoc
+++ b/docs/src/gremlin-applications.asciidoc
@@ -119,6 +119,35 @@ In addition to the standard commands of the link:http://groovy.codehaus.org/Groo
|:submit |:> |Submit Gremlin to the currently active context defined by `:remote`.
|=========================================================
+Gremlin Console adds a special `max-iteration` preference that can be configured with the standard `:set` command from the Groovy Shell. Use this setting to control the maximum number of results that the Console will display. Consider the following usage:
+
+[source, groovy]
+----
+gremlin> :set max-iteration 10
+gremlin> (0..200)
+==>0
+==>1
+==>2
+==>3
+==>4
+==>5
+==>6
+==>7
+==>8
+==>9
+...
+gremlin> :set max-iteration 5
+gremlin> (0..200)
+==>0
+==>1
+==>2
+==>3
+==>4
+...
+----
+
+If this setting is not present, the console will default the maximum to 100 results.
+
Dependencies and Plugin Usage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce5a1699/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 aa8fb63..b101a9a 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
@@ -28,9 +28,13 @@ import org.codehaus.groovy.tools.shell.ExitNotification
import org.codehaus.groovy.tools.shell.Groovysh
import org.codehaus.groovy.tools.shell.IO
import org.codehaus.groovy.tools.shell.InteractiveShellRunner
+import org.codehaus.groovy.tools.shell.commands.SetCommand
+import org.codehaus.groovy.tools.shell.util.Preferences
import java.nio.charset.Charset
import java.util.concurrent.TimeUnit
+import java.util.prefs.PreferenceChangeEvent
+import java.util.prefs.PreferenceChangeListener
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
@@ -41,7 +45,9 @@ class Console {
System.setProperty("java.awt.headless", "true")
}
- private static final int ITERATION_MAX = 100; // TODO: make this configurable using the :set command
+ public static final String PREFERENCE_ITERATION_MAX = "max-iteration"
+ private static final int DEFAULT_ITERATION_MAX = 100
+ private static int maxIteration = DEFAULT_ITERATION_MAX
private static final String HISTORY_FILE = ".gremlin_groovy_history"
private static final String STANDARD_INPUT_PROMPT = "gremlin> "
@@ -49,7 +55,7 @@ class Console {
private static final String IMPORT_SPACE = "import "
private static final String IMPORT_STATIC_SPACE = "import static "
private static final String NULL = "null"
- private static final String ELLIPSIS = "...";
+ private static final String ELLIPSIS = "..."
private Iterator tempIterator = Collections.emptyIterator()
@@ -62,7 +68,19 @@ class Console {
io.out.println(" (o o)")
io.out.println("-----oOOo-(3)-oOOo-----")
+ maxIteration = Integer.parseInt(Preferences.get(PREFERENCE_ITERATION_MAX, Integer.toString(DEFAULT_ITERATION_MAX)))
+ Preferences.addChangeListener(new PreferenceChangeListener() {
+ @Override
+ void preferenceChange(PreferenceChangeEvent evt) {
+ if (evt.key == PREFERENCE_ITERATION_MAX)
+ maxIteration = Integer.parseInt(evt.newValue)
+ }
+ })
+
final Mediator mediator = new Mediator(this)
+ def commandsToRemove = groovy.getRegistry().commands().findAll{it instanceof SetCommand}
+ commandsToRemove.each {groovy.getRegistry().remove(it)}
+ groovy.register(new GremlinSetCommand(groovy))
groovy.register(new UninstallCommand(groovy, mediator))
groovy.register(new InstallCommand(groovy, mediator))
groovy.register(new PluginCommand(groovy, mediator))
@@ -151,7 +169,7 @@ class Console {
while (true) {
if (this.tempIterator.hasNext()) {
int counter = 0;
- while (this.tempIterator.hasNext() && (ITERATION_MAX == -1 || counter < ITERATION_MAX)) {
+ while (this.tempIterator.hasNext() && (maxIteration == -1 || counter < maxIteration)) {
final Object object = this.tempIterator.next()
io.out.println(buildResultPrompt() + ((null == object) ? NULL : object.toString()))
counter++;
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce5a1699/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/commands/GremlinSetCommand.groovy
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/commands/GremlinSetCommand.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/commands/GremlinSetCommand.groovy
new file mode 100644
index 0000000..492fe4d
--- /dev/null
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/commands/GremlinSetCommand.groovy
@@ -0,0 +1,70 @@
+/*
+ * 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.console.commands
+
+import org.apache.tinkerpop.gremlin.console.Console
+import jline.console.completer.Completer
+import org.codehaus.groovy.tools.shell.Groovysh
+import org.codehaus.groovy.tools.shell.commands.SetCommand
+import org.codehaus.groovy.tools.shell.util.PackageHelper
+import org.codehaus.groovy.tools.shell.util.Preferences
+import org.codehaus.groovy.tools.shell.util.SimpleCompletor
+
+/**
+ * A Gremlin-specific implementation of the {@code SetCommand} provided by Groovy. Didn't see another way to
+ * get auto-complete features for Gremlin-specific things to be added to preferences so a subclass with an override
+ * seemed to be the only way.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class GremlinSetCommand extends SetCommand {
+
+ GremlinSetCommand(final Groovysh shell) {
+ super(shell)
+ }
+
+ @Override
+ protected List<Completer> createCompleters() {
+ def loader = {
+ final Set<String> set = [] as Set<String>
+
+ final String[] keys = Preferences.keys()
+
+ keys.each { String key -> set.add(key) }
+
+ set << Preferences.VERBOSITY_KEY
+ set << Preferences.EDITOR_KEY
+ set << Preferences.PARSER_FLAVOR_KEY
+ set << Preferences.SANITIZE_STACK_TRACE_KEY
+ set << Preferences.SHOW_LAST_RESULT_KEY
+ set << Groovysh.INTERPRETER_MODE_PREFERENCE_KEY
+ set << Groovysh.AUTOINDENT_PREFERENCE_KEY
+ set << Groovysh.COLORS_PREFERENCE_KEY
+ set << Groovysh.METACLASS_COMPLETION_PREFIX_LENGTH_PREFERENCE_KEY
+ set << PackageHelper.IMPORT_COMPLETION_PREFERENCE_KEY
+
+ // add Gremlin Console specific preferences here
+ set << Console.PREFERENCE_ITERATION_MAX
+
+ return set.toList()
+ }
+
+ return [new SimpleCompletor(loader),null]
+ }
+}