forked from thinkaurelius/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Groovysh
set
builtin for result peek sizes
When a gremlin.sh command evaluates to an iterable, the number of initial elements in the iterable printed to the console is now controlled by a pair of Groovysh settings: * `set tp-olap-result-peek <int>` (default 50) * `set tp-oltp-result-peek <int>` (default 500) The OLAP setting controls the number of initial lines read out of result files produced by executing a HadoopPipeline (i.e. a titan-hadoop/Faunus job). The OLTP setting controls the number of initial elements read out of any other iterable result. Negative ints are treated as if Integer.MAX_VALUE were specified. Zero has slightly special interpretation; it completely disables iterator peeking. Zero means that the shell will not even call hasNext to decide whether to print "...". Positive ints are used exactly as specified. Settings are persistent across gremlin.sh sessions. They're stored in the Java user Preferences system under the node "/org/codehaus/groovy/tools/shell". On *NIX systems, this is usually in ~/.java/.userPrefs/org/codehaus/groovy/tools/shell/prefs.xml. You may notice that the implementation in this commit does not actually call Preferences.flush anywhere to guarantee persistence. It relies instead on this guarantee from the Preferences javadoc: "Normal termination of the Java Virtual Machine will not result in the loss of pending updates -- an explicit flush invocation is not required upon termination to ensure that pending updates are made persistent." For thinkaurelius#924
- Loading branch information
Showing
3 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ava/com/thinkaurelius/titan/hadoop/tinkerpop/gremlin/ConsolePreferenceChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.thinkaurelius.titan.hadoop.tinkerpop.gremlin; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Preconditions; | ||
import org.codehaus.groovy.tools.shell.util.Preferences; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.prefs.PreferenceChangeEvent; | ||
import java.util.prefs.PreferenceChangeListener; | ||
|
||
public class ConsolePreferenceChangeListener implements PreferenceChangeListener { | ||
|
||
static final String PREF_TINKERPOP_PREFIX = "tp-"; | ||
|
||
private static final Logger log = | ||
LoggerFactory.getLogger(ConsolePreferenceChangeListener.class); | ||
|
||
private final ConcurrentHashMap<String, Function<PreferenceChangeEvent, ?>> prefChangeConsumers = | ||
new ConcurrentHashMap<String, Function<PreferenceChangeEvent, ?>>(); | ||
|
||
/** | ||
* Add a new console preference consumer, and, if the supplied key maps to | ||
* a non-null console preference value, immediately fire a change event | ||
* | ||
* | ||
* @param triggerPrefKey | ||
* @param consumer | ||
*/ | ||
public void setConsumer(String triggerPrefKey, Function<PreferenceChangeEvent, ?> consumer) { | ||
|
||
Preconditions.checkNotNull(triggerPrefKey); | ||
// Preferences javadoc mandates that no path contain successive slashes | ||
Preconditions.checkArgument(!triggerPrefKey.startsWith("/")); | ||
|
||
String k = PREF_TINKERPOP_PREFIX + triggerPrefKey; | ||
|
||
Function<?, ?> oldConsumer = prefChangeConsumers.putIfAbsent(k, consumer); | ||
|
||
if (null == oldConsumer) { | ||
log.debug("Installing new preference consumer for key {}", k); | ||
} else { | ||
log.debug("Replacing existing preference consumer for key {} (old consumer: {})", | ||
k, oldConsumer); | ||
} | ||
|
||
String currentValue = Preferences.get(k); | ||
if (null != currentValue) { | ||
log.debug("Resetting stored value to trigger consumer: {}={}", k, currentValue); | ||
Preferences.put(k, currentValue); | ||
} else { | ||
log.debug("Read null for {}", k); | ||
} | ||
} | ||
|
||
@Override | ||
public void preferenceChange(PreferenceChangeEvent evt) { | ||
// This is probably never null, but why not check | ||
if (null == evt || null == evt.getKey()) | ||
return; | ||
|
||
String k = evt.getKey(); | ||
|
||
Function<PreferenceChangeEvent, ?> consumer = prefChangeConsumers.get(k); | ||
|
||
if (null == consumer) { | ||
log.debug("Ignoring preference key {} (no consumer registered)", k); | ||
return; | ||
} | ||
|
||
log.debug("Invoking consumer {} for key {}", consumer, k); | ||
|
||
consumer.apply(evt); // TODO uncaught exception handling? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters